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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <algorithm> #include <iostream> #include <numeric> #include <cstring> #include <cstdio> #include <string> #include <vector>
#define DEBUG(x) std::cerr << #x << " = " << x << std::endl; #define forall(G,u) for (int i_ = 0, __for_siz__ = (int) G[u].size(); i_ < __for_siz__; ++i_)
using std::cin; using std::cout; using std::endl;
inline int read() { int s = 0, x = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') x = -x; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return s * x; }
const int MAXN = 20 + 5;
int n, aa[MAXN], bb[MAXN], sum; int dp[MAXN][MAXN][MAXN][MAXN];
int search(int i, int j, int k, int l, int sum) { if (dp[i][j][k][l]) return dp[i][j][k][l]; int maxd = 0; if (i + j < n) { maxd = std::max(maxd, sum - search(i + 1, j, k, l, sum - aa[i + 1])); maxd = std::max(maxd, sum - search(i, j + 1, k, l, sum - aa[n - j])); } if (k + l < n) { maxd = std::max(maxd, sum - search(i, j, k + 1, l, sum - bb[k + 1])); maxd = std::max(maxd, sum - search(i, j, k, l + 1, sum - bb[n - l])); } dp[i][j][k][l] = maxd; return dp[i][j][k][l]; }
int main() { int T = read(); while (T --> 0) { n = read(); for (int i = 1; i <= n; ++i) aa[i] = read(); for (int i = 1; i <= n; ++i) bb[i] = read(); memset(dp, 0, sizeof dp); sum = std::accumulate(aa + 1, aa + 1 + n, 0) + std::accumulate(bb + 1, bb + 1 + n, 0); printf("%d\n", search(0, 0, 0, 0, sum)); } return 0; }
|