codeforces-672d Robin Hood (two points) good question

Source: Internet
Author: User
Tags money back
codeforces-672d Robin Hood
Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %i64d &%i64u

Submitstatus

Description

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 perso N (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-integers n and K (1≤n≤500 000, 0≤k≤109)-the number of citizens in Kekol And 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≤ci≤109)-initial Wealth of the i-th person.

Output

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

Sample Input Input

4 1
1 1 4 2
Output
2
Input
3 1
2) 2 2
Output
0

Sample Output

Hint

Source codeforces Round #352 (Div. 2)//test instructions: There are n people in a city, they all have a certain number of coins a[i], and now you take a coin from the richest man every day to the poorest people, so do the K-day, Ask K days the richest man has a few more coins than the poorest. Idea: First of all, it must be clear that the initial number of coins less than the average person will eventually increase the K-coins, more than the average will reduce the K-coins, so according to the first two points to find the final minimum number of coins, and then the most use of two to find the final maximum number of coins, and then subtract on the line

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream >
#define INF 0x3f3f3f3f
#define LL Long long
#define N 500010
using namespace std;
ll A[n];
int main ()
{
	ll n,k;
	int i,j;
	while (scanf ("%lld%lld", &n,&k)!=eof)
	{
		ll sum=0;
		for (i=0;i<n;i++)
		{
			scanf ("%lld", &a[i]);
			Sum+=a[i];
		}
		Sort (a,a+n);
		ll la=sum/n;
		ll ra= (sum+n-1)/n;
		ll L=0,r=la,ans1=0;
		while (l<=r)
		{
			ll mid= (l+r) >>1;
			ll Kk=0;
			for (i=0;i<n;i++)
			{
				if (Mid>=a[i])
					kk+=mid-a[i];
			}
			if (kk<=k)
				ans1=mid,l=mid+1;
			else
				r=mid-1;
		}
		L=ra;r=inf;
		ll Ans2=0;
		while (l<=r)
		{
			ll mid= (l+r) >>1;
			ll Kk=0;
			for (i=0;i<n;i++)
			{
				if (Mid<=a[i])
					kk+=a[i]-mid;
			}
			if (kk<=k)
				ans2=mid,r=mid-1;
			else
				l=mid+1;
		}
		printf ("%lld\n", ans2-ans1);
	}
	return 0;


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.