题目链接:codeforces 1405A. Permutation Forgery
题意:
给一个数组,将数组中所有元素归为0,可以同时给 a[i] - 1并且给a[j] +1,如果i < j, 花费为0,否则花费为1
解题思路:
从左往右遍历一遍,求前缀和中最小的负数
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int a[maxn];
long long ans;
int main(){
int t;
cin >> t;
while(t--) {
int n;
long long sum = 0;
cin >> n;
ans = 1e9+5;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
for(int i = 1; i <= n; i++) {
sum = sum + a[i];
if(sum < 0) {
ans = min(ans, sum);
}
}
if(ans == 1e9+5){
ans = 0;
}
cout << -ans << endl;
}
return 0;
}