-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARC072A.cpp
42 lines (38 loc) · 845 Bytes
/
ARC072A.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
using namespace std;
int improveSeq(vector<int> &a, bool positive) {
int res = 0;
int sum = 0;
for (int i = 0; i < a.size(); i++) {
int nextSum = sum + a.at(i);
if (positive && nextSum >= 0) {
a.at(i) -= (nextSum + 1);
res += nextSum + 1;
}
if (!positive && nextSum <= 0) {
a.at(i) += (-nextSum + 1);
res += -nextSum + 1;
}
sum += a.at(i);
positive = !positive;
}
return res;
}
int main() {
int n;
cin >> n;
vector<int> a1 = vector<int>(n);
vector<int> a2 = vector<int>(n);
for (int i = 0; i < n; i++) {
int a;
cin >> a;
a1.at(i) = a;
a2.at(i) = a;
}
int count1 = improveSeq(a1, true);
int count2 = improveSeq(a2, false);
int count = count1 < count2 ? count1 : count2;
cout << count << endl;
return 0;
}