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. win7安装vs2017时闪退

    最近用公司的笔记本电脑,装win10发现太卡,无奈最终选择安装win7系统,本以为系统安装成功了,接下来只要安装下开发环境:vs2017 sqlserver等就好,结果在安装vs2017的时候,一直出 ...

  2. Windows核心编程:第1章 错误处理

    Github https://github.com/gongluck/Windows-Core-Program.git //第1章 错误处理.cpp: 定义应用程序的入口点. // #include ...

  3. 2019-4-22 linux学习

    linux 一.linux的目录结构 /          挂载目录:为所有目录的根目录 home  家目录:    用户的根目录 存放普通用户的文件 例如:创建一个jack用户,就会产生一个Jack ...

  4. cad.net 更改高版本填充交互方式为低版本样子

    /// <summary> /// 修改cui,双击填充 /// </summary> /// https://blog.csdn.net/hfmwu/article/deta ...

  5. Android热修复——Tinker的集成

    前言 做前端开发的都知道,当我们项目做完了以后,都会把应用上传到应用市场上供用户下载使用,比如上传到应用宝啊,应用汇啊,360啊,小米,华为,魅族啊,等等但是,有时候我们会经常遇到一些很扯淡的事情,刚 ...

  6. 三种方法在当前目录下打开cmd命令窗口

    概述 运行npm的时候,每次都要cd到目录,很麻烦,所以总结了三种在当前目录下直接打开cmd窗口的方法,供以后开发时参考,相信对其他人也有用. 方法一 在当前目录按住shift再右键. 会看到右键菜单 ...

  7. 使用docker redis-cluster集群搭建

    参考https://www.cnblogs.com/cxbhakim/p/9151720.html此文 主要搭建过程参考上文,此处讲下主要过程和遇到的坑 首先是镜像的基础搭建,我不知道是否是作者编写时 ...

  8. c++编程之内存的分配

    当我们在进行编程时,特别是使用c++语言进行编程时,需要知道内存有几个内存区可供我们使用,因为c++可以直接操作内存.接下让我们来看看内存中的几大内存区. 1.栈区 栈区(stack)是速度最快的一个 ...

  9. Mysql数据库操作命令行小结

    -- 创建数据库 create database python_test_1 charset=utf8; -- 使用数据库 use python_test_1; -- students表 create ...

  10. [Leetcode]双项队列解决滑动窗口最大值难题

    这道题是从优先队列的难题里面找到的一个题目.可是解法并不是优先队列,而是双项队列deque 其实只要知道思路,这一道题直接写没有太大的问题.我们看看题 给定一个数组 nums,有一个大小为 k 的滑动 ...