Robin Hood

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 c i 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 c i (1 ≤ c i ≤ 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个公民,每个人都有c i硬币。罗宾汉每天都会从城里最富有的人那里准确地拿走一枚硬币,然后把它给最穷的人(最穷的人在拿走最富有的一枚硬币后马上把它给他)。如果选择不是唯一的,他会随机选择其中一个。可悲的是,罗宾汉已经老了,想在k天后退休。他决定在最后几天帮助穷人。

拿了钱被罗宾汉抢走后,最富有的人也可能变成最穷的人,甚至有可能罗宾汉会把钱还给他。例如,如果所有人都有相同数量的硬币,那么第二天他们也会有相同数量的硬币。

你的任务是在k天后找出最富有和最贫穷的人的财富的差别。请注意,在最富有和最贫穷的人群中随机选择并不影响答案。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
using namespace std;
typedef __int64 ll;
const int idata=1e6+5;

ll n,m,t,_;
ll i,j,k;
vector<ll>v;

bool judge(ll aim)
{
    ll num=0;
    for(i=0;i<v.size();i++){
        if(aim>v[i])
            num+=aim-v[i];
    }
    return num<=k;//给穷人的太少
}
bool judge_(ll aim)
{
    ll num=0;
    for(i=0;i<v.size();i++){
        if(aim<v[i])
            num+=v[i]-aim;
    }
    return num<=k;//富人手里的太少
}
int main()
{
    cin.tie(0);
    iostream::sync_with_stdio(false);
    while(cin>>n>>k)
    {
        v.clear();
        ll sum=0,l,r,poor,rich,mid;
        for(i=1;i<=n;i++){
            cin>>_;
            v.push_back(_);
            sum+=_;
        }
        sort(v.begin(),v.end());
        l=0,r=sum/n;
        while(l<=r){
            mid=(l+r)/2;
            if(judge(mid)){
                poor=mid;
                l=mid+1;
            }
            else{
                r=mid-1;
            }
        }
        //cout<<poor<<endl;

        if(sum%n) l=sum/n+1;
        else l=sum/n;
        r=1e9;
        while(r>=l){
            mid=(l+r)/2;
            if(judge_(mid)){
                rich=mid;
                r=mid-1;
            }
            else{
                l=mid+1;
            }
        }
        //cout<<rich<<endl;
        cout<<rich-poor<<endl;
    }
    return 0;
}
原创文章 410 获赞 16 访问量 3万+

猜你喜欢

转载自blog.csdn.net/C_Dreamy/article/details/105931218