Minimizing Difference ------- CodeForces - 1244E

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

4 5
3 1 7 5

Output

2

Input

3 10
100 100 100

Output

0

Input

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

Output

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.

 

题意:给定一个序列n和一个数k,对序列进行不超过k次变换,每次变换可以对一个数加一或者减一,使序列的极差最小。

题解:显然该题是 一道贪心题。所以可以对于所给的一组数进行小到大排序。要想使得极差最小,即每次对于最大值或 最小值进行 改变即可。每次对于最大值,最小值 的个数进行一个比较,对于个数较小的优先进行操作。注意!注意!注意!!!

由于所排列的数不一定是 等差数列,所以要看一下对于最大值,最小值进行操作后,值是否能够到达所排完序列的与其临近的数值。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
long long a[100010];
int main()
{
    int n;
    long long k;
    while(~scanf("%d%lld",&n,&k))
    {
        for(int i=0;i<n;i++)
            scanf("%lld",&a[i]);
        sort(a,a+n);
        long long minn=a[0],maxx=a[n-1],l=1,r=n-2;
        while(1)
        {
            if(maxx==minn)
                break;
            while(minn==a[l]) l++;
            while(maxx==a[r]) r--;
            long long sum1=l;
            long long sum2=n-1-r;
            if(sum1<sum2)//最小值个数较少
            {
             long long ans=min(a[l]-minn,k/sum1);
                if(ans==0)
                    break;
                k=k-ans*sum1;
                minn=minn+ans;
            }
            else if(sum2<=sum1)//最大的值个数较少或两者相等
            {
                long long res=min(maxx-a[r],k/sum2);
                if(res==0)
                    break;
                k=k-res*sum2;
                maxx=maxx-res;
            }
        }
        printf("%lld\n",maxx-minn);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值