CodeForces - 672D Robin Hood

Posted 小小超plus

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces - 672D 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.

技术分享图片
 1 #include <iostream>
 2 using namespace std;
 3 #include<string.h>
 4 #include<set>
 5 #include<stdio.h>
 6 #include<math.h>
 7 #include<queue>
 8 #include<map>
 9 #include<algorithm>
10 #include<cstdio>
11 #include<cmath>
12 #include<cstring>
13 #include <cstdio>
14 #include <cstdlib>
15 #include<stack>
16 #include<vector>
17 long long   n,m;
18 long long  a[510000];
19 long long  minfind(long long  zhi)
20 {
21     long long sum=0;
22     for(int i=1;i<=n;i++)
23     {
24         if(a[i]<=zhi)
25             sum+=zhi-a[i];
26     }
27     return sum;
28 }
29 long long maxfind(long long zhi)
30 {
31     long long sum=0;
32     for(int i=n;i>=1;i--)
33     {
34         if(a[i]>zhi)
35             sum+=a[i]-zhi;
36     }
37     return sum;
38 }
39 int main()
40 {
41     scanf("%d %d",&n,&m);
42 
43     long long sum=0;
44     for(int i=1;i<=n;i++)
45     {
46         scanf("%d",&a[i]);
47         sum+=a[i];
48     }
49     sort(a+1,a+1+n);
50     long long t;
51     long long kaishi=0;
52     long long jieshu=sum/n;
53     long long mid;
54     while(kaishi<=jieshu)
55     {
56         mid=(kaishi+jieshu)>>1;
57         if(m>=minfind(mid))
58         {
59             t=mid;
60             kaishi=mid+1;
61         }
62         else
63             jieshu=mid-1;
64     }
65     long long min1=t;
66     kaishi=(sum+n-1)/n;
67     jieshu=1e9;
68     while(kaishi<=jieshu)
69     {
70         //cout<<kaishi<<"_"<<jieshu<<endl;
71         mid=(kaishi+jieshu)>>1;
72         if(m>=maxfind(mid))
73         {
74             t=mid;
75             jieshu=mid-1;
76         }
77         else
78             kaishi=mid+1;
79 
80     }
81     //cout<<min1<<"_"<<t<<endl;
82     cout<<t-min1<<endl;
83     return 0;
84 }
View Code

 



以上是关于CodeForces - 672D Robin Hood的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 672D

Codeforces 671B/Round #352(div.2) D.Robin Hood 二分

Codeforces Round #352 (Div. 2) D. Robin Hood

Robin-Miller测素数

round robin schedule(循环日程表)

Codeforces-808D Array Division (multiset 折半???)