forked from Jonathan-Uy/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoin Arrangement.cpp
More file actions
39 lines (33 loc) · 789 Bytes
/
Coin Arrangement.cpp
File metadata and controls
39 lines (33 loc) · 789 Bytes
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 1e5;
int N, a[2][maxN];
int main(){
scanf("%d", &N);
for(int r = 0; r < 2; r++){
for(int i = 0; i < N; i++){
scanf("%d", &a[r][i]);
a[r][i]--;
}
}
ll ans = 0;
int top = 0, bot = 0;
for(int i = 0; i < N; i++){
top += a[0][i];
bot += a[1][i];
if((top < 0 && bot > 0) || (top > 0 && bot < 0)){
if(abs(top) < abs(bot)){
ans += abs(top);
bot += top;
top = 0;
} else {
ans += abs(bot);
top += bot;
bot = 0;
}
}
ans += abs(top + bot);
}
printf("%lld\n", ans);
}