The end of the school year is near and Ms. Manana, the teacher, will soon have to say goodbye to a yet another class. She decided to prepare a goodbye present for her n students and give each of them a jigsaw puzzle (which, as wikipedia states, is a tiling puzzle that requires the assembly of numerous small, often oddly shaped, interlocking and tessellating pieces).

The shop assistant told the teacher that there are m puzzles in the shop, but they might differ in difficulty and size. Specifically, the first jigsaw puzzle consists of f1 pieces, the second one consists of f2 pieces and so on.

Ms. Manana doesn't want to upset the children, so she decided that the difference between the numbers of pieces in her presents must be as small as possible. Let A be the number of pieces in the largest puzzle that the teacher buys and B be the number of pieces in the smallest such puzzle. She wants to choose such n puzzles that A - B is minimum possible. Help the teacher and find the least possible value of A - B.

Input

The first line contains space-separated integers n and m (2 ≤ n ≤ m ≤ 50). The second line contains m space-separated integers f1, f2, ..., f**m (4 ≤ f**i ≤ 1000) — the quantities of pieces in the puzzles sold in the shop.

Output

Print a single integer — the least possible difference the teacher can obtain.

Examples

input

4 6
10 12 10 7 5 22

output

5

Note

Sample 1. The class has 4 students. The shop sells 6 puzzles. If Ms. Manana buys the first four puzzles consisting of 10, 12, 10 and 7 pieces correspondingly, then the difference between the sizes of the largest and the smallest puzzle will be equal to 5. It is impossible to obtain a smaller difference. Note that the teacher can also buy puzzles 1, 3, 4 and 5 to obtain the difference 5.

题意:

m个元素,要求选择n个,使得其中最大值减最小值最小

解题思路:

排序。肯定就是在连续n个元素中才会出现最大值减最小值的情况,而这个值就是第n个减第1个,整个区间往右移找出最小的就可以了

#include<bits/stdc++.h>
using namespace std;
int main() {
int n, a, c = 10001;
cin >> n >> a;
int b[a];
for (int i = 0; i < a; i++) {
cin >> b[i];
}
sort(b, b + a);
for (int j = 0; j <= a - n; j++) {
c = min(c, abs(b[j + n - 1] - b[j]));
}
cout << c;
}

CodeForces -337A:Puzzles(水)的更多相关文章

  1. CodeForces 337A Puzzles

    Puzzles Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origina ...

  2. codeforces 377A. Puzzles 水题

    A. Puzzles Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/33 ...

  3. Codeforces Round #196 (Div. 2) A. Puzzles 水题

    A. Puzzles Time Limit: 2 Sec  Memory Limit: 60 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...

  4. codeforces A. Puzzles 解题报告

    题目链接:http://codeforces.com/problemset/problem/337/A 题意:有n个学生,m块puzzles,选出n块puzzles,但是需要满足这n块puzzles里 ...

  5. Codeforces数据结构(水题)小结

    最近在使用codeblock,所以就先刷一些水题上上手 使用codeblock遇到的问题 1.无法进行编译-------从setting中的编译器设置中配置编译器 2.建立cpp后无法调试------ ...

  6. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  7. CodeForces 705B (训练水题)

    题目链接:http://codeforces.com/problemset/problem/705/B 题意略解: 两个人玩游戏,解数字,一个数字可以被分成两个不同或相同的数字 (3可以解成 1 2) ...

  8. codeforces hungry sequence 水题

    题目链接:http://codeforces.com/problemset/problem/327/B 这道题目虽然超级简单,但是当初我还真的没有想出来做法,囧,看完别人的代码恍然大悟. #inclu ...

  9. CodeForces 705A Hulk (水题)

    题意:输入一个 n,让你输出一行字符串. 析:很水题,只要判定奇偶性,输出就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,10240 ...

  10. 【数学相关、规律】Codeforces 696B Puzzles

    题目链接: http://codeforces.com/problemset/problem/696/B 题目大意: 给一棵树,从根节点开始递归,time=1,每次递归等概率随机访问这个节点的子节点, ...

随机推荐

  1. iOS信号量造成线程优先级反转

    在并发队列使用信号量会可能会造成线程优先级反转 一.在iOS16 & XCode14上遇到 - 使用信号量造成线程优先级反转问题 提醒 经过查询资料,发现是在XCode14上增加了工具,比如 ...

  2. 一次显著的性能提升,从8s到0.7s

    前言 最近我在公司优化了一些慢查询SQL,积累了一些SQL调优的实战经验. 我之前写过一些SQL优化相关的文章<聊聊SQL优化的15个小技巧>和<explain | 索引优化的这把绝 ...

  3. 不会这5个Excel函数,别说你会做数据分析?

    当涉及数据分析时,Excel是一个非常有用的工具,而掌握一些核心函数将大大提高你在数据处理和分析方面的能力.以下是我对五个重要的Excel函数的详细介绍: 1. VLOOKUP 函数 VLOOKUP ...

  4. 【漏洞分析】Reflection Token 反射型代币攻击事件通用分析思路

    在本篇文章中,我将通过一个攻击事件引出 Reflection Token 攻击事件的一个通用分析思路. 关于 Reflection Token 的其他案例分析,可以参考BEVO代币攻击事件分析及复现一 ...

  5. IP交付标准总结。

    RTL顶层代码,IP内部需要IP自己完成连接并保证正确,CM/PLL/MCU/SRAM/TX/RX内部模块不接受外部进行拼接,DFT内部自己处理.IP用到的宏,名称功能文档要说明清楚.优先使用硬核IP ...

  6. [GDOIpj221D] 小学生计数题

    第四题 小学生计数题 提交文件: counting.cpp 输入文件: counting.in 输出文件: counting.out 时间空间限制: 1 秒, 256 MB 作为 GDOI 的组题人, ...

  7. C# 常量 结构体 委托

    常量 const double PI = 3.1415926; 常量名命名一般使用大写字母 枚举类型 开发一个游戏,游戏角色有法师(Mage).射手(Archer).刺客(Assassin).坦克(T ...

  8. Ubuntu搭建邮件服务器

    转载:原文链接 前言 关于邮件服务器的工作原理我就不再赘述了.Postfix是优秀的MTA,而Dovecot则是优秀的MDA.前者负责发信.收信,提供smtp服务:后者负责邮件保存到邮箱,提供pop3 ...

  9. 工作中常用的一些Linux指令,简单易记还实用(三)

    成功路上最大的困难就是坚持,每天坚持看一道算法,每周坚持看一本好书! 工作中,离不开Linux系统,很多刚步入职场的小白,往往对于Linux操作系统的使用都显得生疏,最主要的就是对一些常用的指令记忆不 ...

  10. 主控FC1179 U盘量产修复

    当我们的U盘出现如下情况的话,可以做为参考修复 第一步:可以用Chip Genius工具,查看U盘主控(可得知主控厂商:一芯 ,主控型号:FC1179). 第二步:下载主控相对应的量产工具(笔者已经上 ...