Codeforces 534 C. Polycarpus' Dice【思维】

来源:互联网 发布:gtp吉他谱软件ios 编辑:程序博客网 时间:2024/05/17 21:44

C. Polycarpus' Dice
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp has n dice d1, d2, ..., dn. Thei-th dice shows numbers from 1 to di. Polycarp rolled all the dice and the sum of numbers they showed isA. Agrippina didn't see which dice showed what number, she knows only the sumA and the values d1, d2, ..., dn. However, she finds it enough to make a series of statements of the following type: dice i couldn't show number r. For example, if Polycarp had two six-faced dice and the total sum isA = 11, then Agrippina can state that each of the two dice couldn't show a value less than five (otherwise, the remaining dice must have a value of at least seven, which is impossible).

For each dice find the number of values for which it can be guaranteed that the dice couldn't show these values if the sum of the shown values isA.

Input

The first line contains two integers n, A (1 ≤ n ≤ 2·105, n ≤ A ≤ s) — the number of dice and the sum of shown values where s = d1 + d2 + ... + dn.

The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 106), where di is the maximum value that thei-th dice can show.

Output

Print n integers b1, b2, ..., bn, wherebi is the number of values for which it is guaranteed that thei-th dice couldn't show them.

Examples
Input
2 84 4
Output
3 3 
Input
1 35
Output
4 
Input
2 32 3
Output
0 1 
Note

In the first sample from the statement A equal to 8 could be obtained in the only case when both the first and the second dice show 4. Correspondingly, both dice couldn't show values 1, 2 or 3.

In the second sample from the statement A equal to 3 could be obtained when the single dice shows 3. Correspondingly, it couldn't show 1, 2, 4 or 5.

In the third sample from the statement A equal to 3 could be obtained when one dice shows 1 and the other dice shows 2. That's why the first dice doesn't have any values it couldn't show and the second dice couldn't show 3.


题目大意:

有n个色子,需要每个色子累加和为A,接下来一行n个数,表示这n个色子的最大点数(也就是说能扔出从1-di这么些个值),问每个色子有多少个点数扔出来之后其他色子无论如何扔都满足不了累加和为A的条件。


思路:


1、首先我们累加所有色子的最大点数:d1+d2+d3+d4+...............,记做maxn,另外我们再累加所有色子的最小点数:n。


2、那么我们枚举每一个色子的时候,maxn-=d【i】,minn-=1;就是除了这个色子之外,所有色子的最大、最小累加和的值,那么我们有这样两个区间:

【1,d[i]】,表示当前色子的能够扔出来的值。

【minn,maxn】表示其他色子的最大、最小累加和。

那么不难理解:

【A-maxn,A-minn】就是对应这个色子可以扔出来的值的区间,如果扔出来的值不在这个区间内,就是不可行的解。


3、那么问题就转化到:

【1,d[i]】,作为区间1,【minn,maxn】作为区间2,其中重叠部分就是可行解的部分,那么其余的就是不可行的部分咯。


Ac代码:


#include<stdio.h>#include<string.h>#include<iostream>using namespace std;#define ll __int64ll a[200500];int main(){    ll n,k,maxn,minn;    while(~scanf("%I64d%I64d",&n,&k))    {        memset(a,0,sizeof(a));        maxn=0;        for(int i=1;i<=n;i++)        {            scanf("%I64d",&a[i]);            maxn+=a[i];        }        minn=n-1;        for(int i=1;i<=n;i++)        {            maxn-=a[i];            ll r=k-minn;            ll l=k-maxn;            ll l2=1;            ll r2=a[i];            ll zuo=max(l,l2);            ll you=min(r,r2);            ll tmpp=a[i]-(you-zuo+1);            printf("%I64d ",tmpp);            maxn+=a[i];        }        printf("\n");    }}


0 0