D. Robin Hood

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

We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.

There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in k days. He decided to spend these last days with helping poor people.

After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too.

Your task is to find the difference between richest and poorest persons wealth after k days. Note that the choosing at random among richest and poorest doesn't affect the answer.

Input

The first line of the input contains two integers n and k (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109) — the number of citizens in Kekoland and the number of days left till Robin Hood's retirement.

The second line contains n integers, the i-th of them is ci (1 ≤ ci ≤ 109) — initial wealth of the i-th person.

Output

Print a single line containing the difference between richest and poorest peoples wealth.

Examples

input

4 1
1 1 4 2

output

2

input

3 1
2 2 2

output

0

Note

Lets look at how wealth changes through day in the first sample.

  1. [1, 1, 4, 2]
  2. [2, 1, 3, 2] or [1, 2, 3, 2]

So the answer is 3 - 1 = 2

In second sample wealth will remain the same for each person.

题意: 有n个人, 每个人都有一定的财富值, 每天有最多财富的人会把自己的一元钱给最少财富的人,求k天之后最富有的人跟最少财富的人的差值是多少。

思路:二分最后一天时的最大值和最小值。avg为数组平均值,最大值在avg到MAX之间,最小值在0到avg之间。check条件为能否用k使得小于mid的数都变为mid。

 //2017-10-15
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
int arr[N]; int main()
{
//freopen("inputI.txt", "r", stdin);
std::ios::sync_with_stdio(false);
cin.tie();
int n, k;
while(cin>>n>>k){
long long sum = ;
int MAX = ;
for(int i = ; i < n; i++){
cin>>arr[i];
MAX = max(MAX, arr[i]);
sum += arr[i];
}
sort(arr, arr+n);
int L = sum/n, R = (sum+n-)/n;
int l = , r = L, mininum = ;
while(l <= r){
int mid = (l+r)>>;
long long tmp = ;
for(int i = ; i < n; i++)
if(arr[i] < mid)
tmp += mid-arr[i];
else break;
if(tmp <= k){
mininum = mid;
l = mid+;
}else r = mid-;
}
l = R, r = MAX;
int maxinum = ;
while(l <= r){
int mid = (l+r)>>;
long long tmp = ;
int pos = lower_bound(arr, arr+n, mid)-arr;
for(int i = pos; i < n; i++)
if(arr[i] > mid)
tmp += arr[i]-mid;
if(tmp <= k){
maxinum = mid;
r = mid-;
}else l = mid+;
}
cout<<maxinum-mininum<<endl;
} return ;
}

Codeforces672D(SummerTrainingDay01-I)的更多相关文章

  1. 二分算法题目训练(四)——Robin Hood详解

    codeforces672D——Robin Hood详解 Robin Hood 问题描述(google翻译) 我们都知道罗宾汉令人印象深刻的故事.罗宾汉利用他的射箭技巧和他的智慧从富人那里偷钱,然后把 ...

随机推荐

  1. appendChild()方法遇到的问题

    在使用appendChild()方法中遇到了一个问题: 下面的代码可以正常插入多个新元素 <input type="button" value="在后面插入新元素& ...

  2. shell 命令 if elif else fi 用法

    #! /bin/bash if Iam; then echo "it worked two" else ls echo "I am in the else" f ...

  3. R语言-编写自定义函数 ZZ

    一.函数构造器 每一个R函数都包括三个部分:函数名,程序主体以及参数集合,在编写自定义R函数时,需要将三个部分各自储存在一个R对象中.这里需要使用function函数,形如: my_function& ...

  4. 接之前的文章,VS2017中使用Spring.NET配置以及使用方法(framework4.6.1超详细)

    众所周知,Spring在java中是很常见的框架,Spring.Net虽然体积比较大,但是功能相对齐全,本文介绍在VS2017 .Net FrameWork 4.6.1环境下,如何快速使用Spring ...

  5. 【转】再有人问你Http协议是什么,把这篇文章发给他

    一.HTTP简介 1.HTTP协议,即超文本传输协议(Hypertext transfer protocol).是一种详细规定了浏览器和万维网(WWW = World Wide Web)服务器之间互相 ...

  6. input可以自动换行吗???

    某天,在项目开发的时候,后台java问我input可以换行吗,当时我也是有点懵逼,思考了几秒钟说应该可以,然后就开始尝试各种方法.然后,然后就打脸了.... 最后发现,原来input没有自动换行功能, ...

  7. JS应用实例3:定时弹出广告

    在观看视频时候总会发现有广告弹出 这里就做一个类似这样的定时弹出广告的实例: 前面的JS代码和HTML写在同一个文件,实际开发中总是分开来写 用的时候引入即可 HTML代码: <!DOCTYPE ...

  8. Typescript 学习笔记五:类

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  9. 一个隐蔽的C语言问题反思

    今天在编译一个C代码的时候,从别的编译ok的头文件中拷贝了一份在上面做修改,没想到修改好之后一直 无法调用这个头文件中的函数和变量.看了好久,才在预编译宏中找到了问题的根源.代码 如下所示: 头文件A ...

  10. java中微信统一下单采坑(app微信支付)

    app支付前java后台统一下单文档:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1 微信支付接口签名校验工具:https ...