CF 337A - Puzzles
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
Copy
4 6
10 12 10 7 5 22
output
Copy
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.
-------------------------------
學(xué)年即將結(jié)束,老師馬納娜女士很快就要告別另一個班級了。 她決定為她的 n 個學(xué)生準(zhǔn)備一份告別禮物,并給他們每個人一個拼圖游戲(正如維基百科所述,這是一種平鋪拼圖,需要組裝許多小的、通常形狀奇怪的、互鎖和鑲嵌的碎片)。
店員告訴老師,店里有m個拼圖,但難度和大小可能有所不同。 具體來說,第一個拼圖游戲由 f1 塊組成,第二個拼圖由 f2 塊組成,依此類推。
馬納納女士不想讓孩子們不高興,所以她決定禮物的件數(shù)差異必須盡可能小。 設(shè) A 為老師購買的最大拼圖的片數(shù),B 為最小的拼圖的片數(shù)。 她想選擇這樣的 n 個謎題,使得 A?-?B 的可能性最小。 幫助老師找出A?-?B的最小可能值。
輸入
第一行包含空格分隔的整數(shù) n 和 m (2?≤?n?≤?m?≤?50)。 第二行包含 m 個空格分隔的整數(shù) f1,?f2,?...,?fm (4?≤?fi?≤?1000) — 商店中出售的拼圖的塊數(shù)。
輸出
打印一個整數(shù)——教師可以獲得的最小可能差異。
-------------------------------------
排序,然后求區(qū)間的最大值即可。下面是代碼: