cf671B 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 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.

Example

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整除讨论

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<deque>
 9 #include<set>
10 #include<map>
11 #include<ctime>
12 #define LL long long
13 #define inf 0x7ffffff
14 #define pa pair<int,int>
15 #define mkp(a,b) make_pair(a,b)
16 #define pi 3.1415926535897932384626433832795028841971
17 using namespace std;
18 inline LL read()
19 {
20     LL x=0,f=1;char ch=getchar();
21     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
22     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
23     return x*f;
24 }
25 int n,k,ave,mx;
26 int a[500010];
27 LL s[500010];
28 int lim1,lim2,lim3;
29 inline bool jud(int d,int op)
30 {
31     LL sum=0;
32     if (op==1)
33     {
34         for (int i=1;i<=lim1;i++)if (a[i]<d)sum+=d-a[i];else break;
35         return sum<=k;
36     }else
37     {
38         for (int i=n;i>=lim2;i--)if (a[i]>d)sum+=a[i]-d;else break;
39         return sum<=k;
40     }
41 }
42 int main()
43 {
44     while (~scanf("%d%d",&n,&k))
45     {
46         mx=-1;
47         for (int i=1;i<=n;i++)a[i]=read(),s[i]=s[i-1]+a[i],mx=max(mx,a[i]);
48         sort(a+1,a+n+1);
49         ave=s[n]/n;
50         lim1=1;lim2=n;
51         if ((LL)ave*n==s[n])
52         {
53             for (int i=1;i<=n;i++)
54                 if (a[i]<ave)lim1=i;else break;
55             for (int i=n;i>=1;i--)
56                 if (a[i]>ave)lim2=i;else break;
57         }else
58         {
59             for (int i=1;i<=n;i++)
60                 if (a[i]<=ave)lim1=i;else break;
61             for (int i=n;i>=1;i--)
62                 if (a[i]>=ave+1)lim2=i;else break;
63         }
64         int L=ave,R=ave;
65         int l=0,r=ave;
66         while (l<=r)
67         {
68             int mid=(l+r)>>1;
69             if (jud(mid,1))L=mid,l=mid+1;
70             else r=mid-1;
71         }
72         l=((LL)ave*n==s[n])?ave:ave+1;r=mx;
73         while (l<=r)
74         {
75             int mid=(l+r)>>1;
76             if (jud(mid,2))R=mid,r=mid-1;
77             else l=mid+1;
78         }
79         printf("%d
",R-L);
80     }
81 }
cf671B
原文地址:https://www.cnblogs.com/zhber/p/7284505.html