Codeforces 337A Puzzles

Source: Internet
Author: User

Puzzlestime limit:1000msmemory limit:262144kbthis problem'll be judged onCodeforces. Original id:337a
64-bit integer IO format: %i64d Java class name: (any) The end of the school year was near and Ms Manana, the teacher, would soon has to say goodbye to a yet another class. She decided to prepare a goodbye present n students and give each of them a jigsaw puzzle (which, as Wikipedia states, was a tiling puzzle that require s the assembly of numerous small, often oddly shaped, interlocking and tessellating pieces).

The shop assistant told the teacher a there are  m  puzzles in the They might differ in difficulty and size. Specifically, the first jigsaw puzzle consists Of  F 1  pieces, the second one consists Of  F 2  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 pre Sents must be as small as possible. Let A is the number of pieces in the largest puzzle so the teacher buys and B be the number of Pieces in the smallest such puzzle. She wants to choose such n puzzles, 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≤ nm ≤50). The second line contains m space-separated integers f1, F2, ..., f m (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.

Sample InputInput
4 6
10 12 10 7 5 22
Output
5
Hint

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

SourceCodeforces Round #196 (Div. 2) Problem solving: a silly problem.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN = -;4 intd[maxn],n,m;5 intMain () {6scanf"%d%d",&n,&m);7      for(inti =0; I < m; ++i)8scanf"%d", d+i);9Sort (d,d+m);Ten     intRET =Int_max; One      for(inti =0+ N-1; I < m; ++i) Aret = min (ret,d[i]-d[i-n+1]); -printf"%d\n", ret); -     return 0; the}
View Code

Codeforces 337A Puzzles

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.