534C. Polycarpus' Dice(Codeforces Round #298(div2))

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. The i-th dice shows numbers from 1 to di. Polycarp rolled all the dice and the sum of numbers they showed is A. Agrippina didn't see which dice showed what number, she knows only the sum A 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 is A = 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 is A.

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 the i-th dice can show.

Output

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

Sample test(s)
input
2 8
4 4
output
3 3 
input
1 3
5
output
4 
input
2 3
2 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个骰子,每个骰子的最大值为di,其显示的总和值为A,求每个骰子不能取值的个数

题解:

    对每个骰子,

    1.其最大取值时,其他骰子取值最小,也就是其他骰子都取1,此时其他骰子总和为n-1,假如此时s-(n-1)<a[i],(s-(n-1)是第i个可以取得的最大值),此时不能取得个数为a[i]-(s-(n-1))。

    2.其取值最小时,其他骰子取值最大,也就是sum-a[i],假如此时s-(sum-a[i])>0,此时不能取得个数为(s-sum+a[i]-1)。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=300000+100;
int a[maxn];
int main()
{
    int n;
    long long s,sum;
    while(cin>>n>>s)
    {
        sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        for(int i=1;i<=n;i++)
        {
            int ans=0;
            if(s>(sum-a[i]))
            {
                ans+=(s-(sum-a[i]))-1;
            }
            if(s-(n-1)<a[i])
            {
                ans+=(a[i]-s+n-1);
            }
            if(i==1)
            printf("%d",ans);
            else
            printf(" %d",ans);
        }
        printf("\n");
    }
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值