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 43 44 45 46 47 48 49
| #include<bits/stdc++.h> using namespace std;
int n; int a[25], b[25];
int sum; int dp[25][25][25][25];
void Input() { scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); sum += a[i]; } for(int i = 1; i <= n; i++) { scanf("%d", &b[i]); sum += b[i]; } }
int Dfs(int l1, int r1, int l2, int r2, int tot) { if(l1 > r1 && l2 > r2) return 0; if(dp[l1][r1][l2][r2] != -1) return dp[l1][r1][l2][r2]; int ans = 0; if(l1 <= r1) { ans = max(ans, tot - min(Dfs(l1 + 1, r1, l2, r2, tot - a[l1]), Dfs(l1, r1 - 1, l2, r2, tot - a[r1]))); } if(l2 <= r2) { ans = max(ans, tot - min(Dfs(l1, r1, l2 + 1, r2, tot - b[l2]), Dfs(l1, r1, l2, r2 - 1, tot - b[r2]))); } return dp[l1][r1][l2][r2] = ans; }
void Work() { memset(dp, -1, sizeof(dp)); printf("%d", Dfs(1, n, 1, n, sum)); }
int main() { Input(); Work(); return 0; }
|