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.
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.
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.
2 8 4 4
3 3
1 3 5
4
2 3 2 3
0 1
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 (A是n 个骰子已经显示出来的点数之和),并且告诉你每个骰子最大显示的点数(也就是有几
面),问每个骰子不可能展示出来的,总共有几个点!(描述的有点乱 ,在仔细读读吧,不难理解)
思路:
枚举每一个骰子,为了让这个骰子表示的点数尽可能大,你要让其他n-1个骰子都表示为1点,这样 当前这个骰子表示
的点数为x = A - n + 1,如果他比a[i]大的 话,那么这部分答案就是 a[i] - x;
如果让这个骰子表示的点数尽可能小,那么就让其他n-1个骰子都最大,也就是说当前这个骰子点数为
y = A - (sum - a[i]);如果y比 1 大,那么1 到 y-1 是都不可能的 答案就是 y-1
两部分加起来即可!
详细见代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 10;
typedef long long ll;
ll a[maxn];
int main(){
ll n,A;
scanf("%I64d %I64d",&n,&A);
ll sum = 0;
for (int i = 0; i < n; ++i){
scanf("%I64d",&a[i]);
sum += a[i];
}
for (int i = 0; i < n ; ++i){
ll ans = 0ll;
ll x = A - n + 1;
if (x < a[i])ans += a[i]-x;
x = A - (sum - a[i]);
if (x > 1)ans += x-1;
printf("%I64d ",ans);
}
puts("");
return 0;
}