第八周训练 h题

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

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.

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
long long e[100010];
int main()
{
	long long n,m,sum=0,i,sum1=0;
	scanf("%lld %lld",&n,&m);
	for(i=1;i<=n;i++)
	scanf("%lld",&e[i]);
	sort(e+1,e+n+1);
	for(i=1;i<=n/2;i++)
	{
		sum=(e[i+1]-e[i]+e[n-i+1]-e[n-i])*i;
		if(m>=sum)
		m=m-sum;
		else
		{
			sum1=e[n+1-i]-e[i]-m/i;
			break;
		}
	}
	printf("%lld",sum1);
	return 0;
}

题意 

     有n个数,可执行k次操作,每次操作只能对一个数+1或者-1,求执行操作变化后的最大值和最小值的差值(使差值尽量               小)

思路       对数组进行 从小到大排序 , 将最小值和最大值改变就 需要将所有相同大小的都改变,左相同的数量为a, 右为b ,  但小心的是操作的次数不可大于k

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值