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. 义无反顾马督工,Bert-vits2V210复刻马督工实践(Python3.10)

    Bert-vits2更新了版本V210,修正了日/英的bert对齐问题,效果进一步优化:对底模使用的数据进行优化和加量,减少finetune失败以及电音的可能性:日语bert更换了模型,完善了多语言推 ...

  2. STM32外设:专用定时器 IWDG、WWDG、RTC

    主要外设: IWDG:Independent Watch DoG 独立看门狗 WWDG:Window Watch DoG 窗口看门狗 RTC: Real-Time Clock 实时时钟 IWDG 主要 ...

  3. NestJs系列之使用Vite搭建项目

    介绍 在使用nest创建项目时,默认使用webpack进行打包,有时候启动项目需要1-2分钟.所以希望采用vite进行快速启动项目进行开发. 本文主要使用NestJs.Vite和swc进行配置.文章实 ...

  4. idea用不了 idea.bat文件闪退

    由于idea的智能,在破解之后会留下一些问题,根据网上搜出来的解决办法. 1.C:\Users\dell\AppData\Roaming\JetBrains\IntelliJIdea2022.2 在这 ...

  5. 实验1:UML与面向对象程序设计原则

    本次实验属于模仿型实验,通过本次实验学生将掌握以下内容: 1.掌握面向对象程序设计中类与类之间的关系以及对应的UML类图: 2.理解面向对象程序设计原则.   [实验任务一]:UML复习 阅读教材第一 ...

  6. 新报: 根据IP获取物理地址不需要调用接口啦

    一. 复制data文件夹到自己的项目当中 可以进行加载到这个文件即可 博主放入在项目的根目录当中 请移步Gitee pull 该文件 https://gitee.com/yangbuyi/ip_fil ...

  7. 春秋云镜 - CVE-2022-28060

    Victor CMS v1.0 /includes/login.php 存在sql注入 找到页面的登录框,看介绍应该是post类型的表单注入. 上sqlmap用原本的梭发现ctf的那个表是空的,换用- ...

  8. DVWA Command Injection(命令注入)全等级

    Command Injection(命令注入) 目录: Command Injection(命令注入) 1. Low 利用 1.nc反弹shell 2.msf上马 2.Medium 3. High 4 ...

  9. zabbix-server 报错记录

    数据库磁盘满导至zabbix挂了 登陆zabbix页面提示错误信息,查看系统磁盘: /目录已满,导致mariadb数据库无法正常运行,如果有介质将会一直收取: 解决方法:扩容/目录,达到理想值,重启m ...

  10. P7112 【模板】行列式求值

    学<高等代数>第二章的时候过来搜了搜模板,结果真搜到了.于是水一篇题解. 本文部分内容来自<高等代数>. 行列式定义 对于一个 \(n\) 阶行列式 \[A_{n \times ...