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 random-duplicate B is minimum possible. help the teacher and find the least possible value of A random-Parallel B.
Input
The first line contains space-separated integers n and m (2 ≤ limit n ≤ limit m ≤ limit 50 ). the second line contains m space-separated integers f1, 127f2, large ,..., when fm (4 Gbit/S ≤ memory fi ≤ memory 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.
Sample test (s)
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.
N, m, n are the number of students, m is the number of gifts, find n gifts in the m brother gift, is the maximum and minimum difference is the minimum
Train of Thought: because the data is small, direct sorting of violence
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int main(){ int n,m,i,a[55],Min,s; while(~scanf("%d%d",&m,&n)) { for(i = 0; i<n; i++) scanf("%d",&a[i]); sort(a,a+n); Min = 10000; m = m-1; for(i = m; i<n; i++) { s = a[i]-a[i-m]; if(s<Min) Min = s; } printf("%d\n",Min); } return 0;}