Skip to content

Commit 04fdb18

Browse files
committed
atcoder/abc059C
1 parent 09a5dac commit 04fdb18

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

atcoder/abc059/C/main.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
using ll = long long;
5+
6+
int N;
7+
vector<int> A;
8+
9+
ll solve(int sign) {
10+
assert(sign == 1 || sign == -1);
11+
ll ans = 0;
12+
ll sum = 0;
13+
for (int i = 0; i < N; ++i) {
14+
sum += A[i];
15+
int c1 = (sign == 1 && sum <= 0) ? -sum + 1 : 0;
16+
int c2 = (sign == -1 && sum >= 0) ? sum + 1 : 0;
17+
assert(c1 >= 0 && c2 >= 0);
18+
sum += c1 + -c2;
19+
sign *= -1;
20+
ans += c1 + c2;
21+
}
22+
return ans;
23+
}
24+
25+
int main() {
26+
ios_base::sync_with_stdio(false);
27+
cin.tie(0); cout.tie(0);
28+
29+
cin >> N;
30+
A.assign(N, 0);
31+
for (int i = 0; i < N; ++i) {
32+
cin >> A[i];
33+
}
34+
35+
cout << min(solve(1), solve(-1)) << endl;
36+
37+
return 0;
38+
}

atcoder/abc059/C/sample1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2
2+
0 2

atcoder/abc059/C/sample2.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2
2+
0 -2

atcoder/abc059/C/sample3.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
3
2+
0 2 -2

0 commit comments

Comments
 (0)