(CodeForces

来源:互联网 发布:网站源码修改工具 编辑:程序博客网 时间:2024/06/06 11:00

(CodeForces - 337A)Puzzles

time limit per test1 :second
memory limit per test:256 megabytes
input:standard input
output:standard output

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, …, fm (4 ≤ fi ≤ 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的区间,选择落差最小的即可。

#include<cstdio>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;const int maxn=55;int a[maxn];int main(){    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {        for(int i=0;i<m;i++) scanf("%d",a+i);        sort(a,a+m);        int ans=INF;        for(int i=0;i<=m-n;i++)            ans=min(ans,a[i+n-1]-a[i]);        printf("%d\n",ans);     }    return 0;}
原创粉丝点击