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

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

After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have 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 two integers n and k (1 ≤ n ≤ 500 000, 0 ≤ k ≤ 109) — 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 is 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.

Example

Input
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 will remain the same for each person.

先排个序、算个平均数,然后左右二分,看看左右能到达的最接近平均的位置在哪

蒟蒻写的比较挫,还要分总和能不能被n整除讨论

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,k,ave,mx;
int a[];
LL s[];
int lim1,lim2,lim3;
inline bool jud(int d,int op)
{
LL sum=;
if (op==)
{
for (int i=;i<=lim1;i++)if (a[i]<d)sum+=d-a[i];else break;
return sum<=k;
}else
{
for (int i=n;i>=lim2;i--)if (a[i]>d)sum+=a[i]-d;else break;
return sum<=k;
}
}
int main()
{
while (~scanf("%d%d",&n,&k))
{
mx=-;
for (int i=;i<=n;i++)a[i]=read(),s[i]=s[i-]+a[i],mx=max(mx,a[i]);
sort(a+,a+n+);
ave=s[n]/n;
lim1=;lim2=n;
if ((LL)ave*n==s[n])
{
for (int i=;i<=n;i++)
if (a[i]<ave)lim1=i;else break;
for (int i=n;i>=;i--)
if (a[i]>ave)lim2=i;else break;
}else
{
for (int i=;i<=n;i++)
if (a[i]<=ave)lim1=i;else break;
for (int i=n;i>=;i--)
if (a[i]>=ave+)lim2=i;else break;
}
int L=ave,R=ave;
int l=,r=ave;
while (l<=r)
{
int mid=(l+r)>>;
if (jud(mid,))L=mid,l=mid+;
else r=mid-;
}
l=((LL)ave*n==s[n])?ave:ave+;r=mx;
while (l<=r)
{
int mid=(l+r)>>;
if (jud(mid,))R=mid,r=mid-;
else l=mid+;
}
printf("%d\n",R-L);
}
}

cf671B

cf671B Robin Hood的更多相关文章

  1. Codeforces Round #352 (Div. 2) D. Robin Hood 二分

    D. Robin Hood   We all know the impressive story of Robin Hood. Robin Hood uses his archery skills a ...

  2. Curious Robin Hood(树状数组+线段树)

    1112 - Curious Robin Hood    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 ...

  3. CF 672D Robin Hood(二分答案)

    D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. Codeforces Round #352 (Div. 1) B. Robin Hood 二分

    B. Robin Hood 题目连接: http://www.codeforces.com/contest/671/problem/B Description We all know the impr ...

  5. 【CodeForces】671 B. Robin Hood

    [题目]B. Robin Hood [题意]给定n个数字的序列和k次操作,每次将序列中最大的数-1,然后将序列中最小的数+1,求最终序列极差.n<=5*10^5,0<=k<=10^9 ...

  6. Codeforces 671B/Round #352(div.2) D.Robin Hood 二分

    D. Robin Hood We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and ...

  7. Codeforces Round #352 (Div. 1) B. Robin Hood (二分)

    B. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. Codeforces 672D Robin Hood(二分好题)

    D. Robin Hood time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  9. Codeforces Round #352 (Div. 1) B. Robin Hood

    B. Robin Hood 讲道理:这种题我是绝对不去(敢)碰的.比赛时被这个题坑了一把,对于我这种不A不罢休的人来说就算看题解也要得到一个Accepted. 这题网上有很多题解,我自己是很难做出来的 ...

随机推荐

  1. 苹果市值破万亿,iPhone 会涨价吗?

    今日导读 苹果教父乔布斯曾经说过:“活着就是为了改变世界.”虽然他在 56 岁时就遗憾离世,但他极具创新和变革的精神早已深埋进苹果公司的企业文化里,影响着一代又一代的人.就在最近,这家一直努力“改变世 ...

  2. Java动画 重力弹球 如鹏游戏引擎 精灵 设计一个小球加速落地又减速弹起并反复直到停止的Java程序

    package com.swift; import com.rupeng.game.GameCore; public class BouncingBall implements Runnable { ...

  3. iOS项目工程及目录结构

    做过一些iOS的项目,不同项目的沉淀没有积累到一起,目录的管理都在后期随着人员的增加越来越混乱,因此在这里做一些梳理,希望达到两个目的. 一套相对通用的目录结构,作为后续项目的模版. 积累相应的基础库 ...

  4. C#经典面试题——递归运算

    今天开始写递归,然而始终不得甚解.借鉴别人的理解:假设我们现在都不知道什么是递归,我们自然想到打开浏览器,输入到谷歌的网页,我们点击搜索递归,然后我们在为维基百科中了解到了递归的基本定义,在了解到了递 ...

  5. python入门:输出1-10的所有数(自写)

    #!/usr/bin/env python # -*- coding:utf-8 -*- #输出1-10的所有数(自写) """ 导入time库,给kaishi赋值为数字 ...

  6. Laravel核心解读--Console内核

    Console内核 上一篇文章我们介绍了Laravel的HTTP内核,详细概述了网络请求从进入应用到应用处理完请求返回HTTP响应整个生命周期中HTTP内核是如何调动Laravel各个核心组件来完成任 ...

  7. 【linux】【进程】stand alone 与 super daemon 区别

    本文引用自  鸟哥的linux私房菜如果依据 daemon 的启动与管理方式来区分,基本上,可以将 daemon 分为可独立启动的 stand alone , 与透过一支 super daemon 来 ...

  8. OOP中常用到的函数

    学习地址: http://www.jikexueyuan.com/course/2420.html 判断类是否存在 class_exists() 得到类或者对象中的成员方法组成的数组 get_clas ...

  9. 小谈python里 列表 的几种常用用法

    在python中列表的常用方法主要包括增加,删除,查看和修改.下面以举例子的方法具体说明,首先我们创建两个列表,列表是用[ ]表示的,里面的元素用逗号隔开. a=[‘hello’,78,15.6,‘你 ...

  10. 使用fio测试磁盘I/O性能

    简介: fio是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, libaio, posixaio, SG v3, splice, nu ...