E. Minimizing Difference

链接:https://codeforces.com/contest/1244/problem/E

You are given a sequence a1,a2,…,ana1,a2,…,an consisting of nn integers.

You may perform the following operation on this sequence: choose any element and either increase or decrease it by one.

Calculate the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than kk times.

Input

The first line contains two integers nn and kk (2≤n≤105,1≤k≤1014)(2≤n≤105,1≤k≤1014) — the number of elements in the sequence and the maximum number of times you can perform the operation, respectively.

The second line contains a sequence of integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109).

Output

Print the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than kk times.

Examples

input

Copy

4 5
3 1 7 5

output

Copy

2

input

Copy

3 10
100 100 100

output

Copy

0

input

Copy

10 9
4 5 5 7 5 4 5 2 4 3

output

Copy

1

Note

In the first example you can increase the first element twice and decrease the third element twice, so the sequence becomes [3,3,5,5][3,3,5,5], and the difference between maximum and minimum is 22. You still can perform one operation after that, but it's useless since you can't make the answer less than 22.

In the second example all elements are already equal, so you may get 00 as the answer even without applying any operations.

题解:

#include<bits/stdc++.h>
using namespace std;
long long n,m,ans,l,r;
long long a[100001];
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	cin>>a[i];
	sort(a+1,a+1+n);
	for(int i=1;i<=n/2;i++)
	{
        int j=n-i+1;
        r=(a[i+1]-a[i]+a[j]-a[j-1])*i;
        if(r<=m) 
		m-=r;
        else
		{
            ans=a[j]-a[i]-m/i;
            break;
        }
    }
    cout<<ans;
}

 

全部评论

相关推荐

专业嗎喽:个人信息名字太大,合到电话邮箱那一栏就行,有党员写过党,剩下其他全删,站空太大了 把实习经历丰富,放最前面,然后是个人评价,技能之类的,然后是学校信息。项目经历最后面,可以就选一个自己擅长的。 现在是学校不是92就扣分的,没必要放前面。 然后现在看重实习经历>竞赛经历(校园经历)>课程项目经历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务