#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,m;
cin >> n >> m;
vector<ll> cost1(n+m-1),cost2(n+m-1);
for(int i = 0; i < n+m-1; ++i) cin >> cost1[i];
for(int i = 0; i < n+m-1; ++i) cin >> cost2[i];
ll p1 = 0,p2 = 0,i1 = 0,i2 = 0;
int x = 0,y = m-1;
for(int i = 0; i < n+m-1; ++i){
if((x+y)&1) i1 += cost1[i];
else p1 += cost1[i];
if(y > 0) --y;
else ++x;
}
x = 0,y = 0;
for(int i = 0; i < n+m-1; ++i){
if((x+y)&1) i2 += cost2[i];
else p2 += cost2[i];
if(x < n-1) ++x;
else ++y;
}
ll ans = 0;
if(p1 <= p2){
ans += p1;
if(m == n){
if((n%2 == 0) and cost1[0] + cost1[n+m-2] > cost2[n-1]){
ans += cost2[n-1] - cost1[0] - cost1[n+m-2];
}
}
else{
if((m%2 == 0) and cost1[0] > cost2[m-1]){
ans += cost2[m-1] - cost1[0];
}
if((n%2 == 0) and cost1[n+m-2] > cost2[n-1]){
ans += cost2[n-1] - cost1[n+m-2];
}
}
}
else{
ans += p2;
if(m == n){
if(cost2[0] + cost2[n+m-2] > cost1[n-1]){
ans += cost1[n-1] - cost2[0] - cost2[n+m-1];
}
}
else{
if(cost2[0] > cost1[m-1]){
ans += cost1[m-1] - cost2[0];
}
if(((n+m)%2 == 0) and cost2[n+m-2] > cost1[n-1]){
ans += cost1[n-1] - cost2[n+m-2];
}
}
}
if(i1 <= i2){
ans += i1;
if(m == n){
if((n%2 == 1) and cost1[0] + cost1[n+m-2] > cost2[n-1]){
ans += cost2[n-1] - cost1[0] - cost1[n+m-2];
}
}
else{
if((m%2 == 1) and cost1[0] > cost2[m-1]){
ans += cost2[m-1] - cost1[0];
}
if((n%2 == 1) and cost1[n+m-2] > cost2[n-1]){
ans += cost2[n-1] - cost1[n+m-2];
}
}
}
else{
ans += i2;
if(m != n){
if(((n+m)%2 == 1) and cost2[n+m-2] > cost1[n-1]){
ans += cost1[n-1] - cost2[n+m-2];
}
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
70 ms |
4652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
134 ms |
7672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |