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. PHP直接查看换取的图片

    <!doctype html><head> <title></title> <meta http-equiv="Content-Type ...

  2. java web中的异常处理

    1.集中处理 参考:https://blog.csdn.net/weililansehudiefei/article/details/73691294

  3. JDBC创建链接的几种方式

    首先,使用java程序访问数据库的前提 数据库的主机地址(ip地址) 端口 数据库用户名 数据库用户密码 连接的数据库 代码: private static String url = "jd ...

  4. Android 实现手写板技术

    Android手写板和涂鸦的功能,代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andro ...

  5. 转---JS 一定要放在 Body 的最底部么?聊聊浏览器的渲染机制

    作者:德来 segmentfault.com/a/1190000004292479 如有好文章投稿,请点击 → 这里了解详情 一.从一个面试题说起 面试前端的时候我喜欢问一些看上去是常识的问题.比如: ...

  6. javascript Location对象属性和方法

    Location对象 Location对象包含有关当前URL的信息 https://blog.csdn.net/u010953692/article/details/80901035#3 https( ...

  7. asp.net mvc 安全测试漏洞 "跨站点请求伪造" 问题解决

    IBM Security Appscan漏洞筛查-跨站请求伪造,该漏洞的产生,有多种情况: 1.WebApi的跨站请求伪造,需要对WebApi的请求头部做限制(此文不做详细介绍): 2.MVC Act ...

  8. 从一个集合中查找最大最小的N个元素——Python heapq 堆数据结构

    Top N问题在搜索引擎.推荐系统领域应用很广, 如果用我们较为常见的语言,如C.C++.Java等,代码量至少也得五行,但是用Python的话,只用一个函数就能搞定,只需引入heapq(堆队列)这个 ...

  9. ASP.NET Web API实现微信公众平台开发(一)服务器验证

    最近朋友的微信公众号准备做活动,靠固定的微信公众平台模版搞定不了,于是请我代为开发微信后台.鉴于我也是第一次尝试开发微信后台,所以也踩了不少坑,此系列博客将会描述微信公众号各项功能的实现. 先决条件 ...

  10. AB(ApacheBench)工具 -- 压力测试

    服务器负载太大而影响程序效率也是很常见的,Apache服务器自带有一个叫AB(ApacheBench)的工具,可以对服务器进行负载测试 同时美多商城的秒杀功能也会被高负载影响,从而导致超卖现象 安装x ...