Codeforces 671b/round #352 (Div.2) D.robin Hood two points

Source: Internet
Author: User
Tags money back

D. Robin Hood

We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and he wits to steal the money from rich, and return it to the poor.

There is n citizens in Kekoland, with each person having Ci coins. Each day, Robin Hood'll take exactly 1 coin from the richest person in the city and he would give it to the poorest p Erson (poorest person right after taking richest ' s 1 coin). In case the choice are not unique, he would select one among them at random. Sadly, Robin Hood is-old and want-retire in K days. He decided to spend these last days with helping poor people.

After taking he money is taken by Robin Hood richest person could become poorest person as well, and it might even happen That Robin Hood would give his money back. For example if all people has same number of coins, then next day they would has 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 the integers n and k (1≤ n ≤500 000, 0≤ k ≤10 9)-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 are Ci (1≤< C9>ci ≤109)-initial Wealth of the i-th person.

Output

Print a single line containing the difference between richest and poorest peoples wealth.

Examplesinput
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 would remain the same for each person.

Test Instructions: give you the number of n, each time you can move from the largest number one to the smallest number, ask K operations, the maximum and minimum number of the difference.

Ideas: The title can be simplified to find the maximum and minimum values in the final state, then divide the maximum and minimum values separately, and pay attention to the difference between the maximum and the hour of the two points.

1#include <stdio.h>2#include <iostream>3#include <algorithm>4#include <string.h>5 using namespacestd;6 7 Const intMAX =500010;8 Const intINF =0x3f3f3f3f;9 intA[max], N;Ten  One intFindmax (intXintk) A { -      for(inti =0; I < n; i++) -     { the         if(A[i] >x) -K-=a[i]-x; -         if(K <0) -             return  0; +     } -     return 1; + } A intFindmin (intXintk) at { -      for(inti =0; I < n; i++) -     { -         if(A[i] <x) -K-=x-A[i]; -         if(K <0) in             return  0; -     } to     return 1; + } -  the intMain () * { $     intK;Panax Notoginseng__int64 sum =0; -     intT1, T2; theCIN >> N >>K; +T1 =0; At2 =INF; the      for(inti =0; I < n; i++) +     { -scanf"%d", A +i); $Sum + =A[i]; $T1 =max (t1, A[i]); -t2 =min (T2, a[i]); -     } the     intMX = sum/n + (sum%n>0?1:0); -     intmi = SUM/N;Wuyi  the     intL, R, Maxx, Minn; -     /////mi WuL = t2, r =mi; -      while(L <= R)//Note equals About     { $         intMid = (L + r) >>1; -         if(Findmin (Mid, k)) -         { -L = mid +1; AMinn =mid; +         } the         ElseR = Mid-1; -     } $     ////ma theL = mx, r =T1; the      while(L <R) the     { the         intMid = (L + r) >>1; -         if(Findmax (Mid, k)) in         { theR =mid; theMaxx =Max (Mid, Maxx); About         } the         ElseL = mid +1; the     } theMaxx = (L + r) >>1;//end state when fetching +printf"%d\n", Maxx-Minn); -}
View Code

Codeforces 671b/round #352 (Div.2) D.robin Hood two points

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.