fork download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define MAX(A, B) A > B ? A : B
  3.  
  4. #include <iostream>
  5. #include <memory.h>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. int N, M;
  11. int A[3001];
  12. int B[101];
  13. int cache[5][4][4][2];
  14.  
  15. int snack(int a, int b1, int b2, bool skip) {
  16.  
  17. int& rt = cache[a][b1][b2][skip];
  18. if (rt != -1) return rt;
  19.  
  20. cout << a << ' ' << b1 << ' ' << b2 << ' ' << skip << '\n';
  21.  
  22. rt = 0;
  23. if (skip) {
  24. if (a < N) rt = snack(a + 1, b1, b2, 0);
  25. if (b1 <= b2) {
  26. rt = MAX(rt, snack(a, b1 + 1, b2, 0));
  27. rt = MAX(rt, snack(a, b1, b2 - 1, 0));
  28. }
  29. }
  30.  
  31. else {
  32. if (a < N) rt = snack(a + 1, b1, b2, 1) + A[a];
  33. if (b1 <= b2) {
  34. rt = MAX(rt, snack(a, b1 + 1, b2, 1)) + B[b1];
  35. rt = MAX(rt, snack(a, b1, b2 - 1, 1)) + B[b2];
  36. }
  37. }
  38. return rt;
  39. }
  40.  
  41. int main(int argc, char** argv) {
  42.  
  43. cin.tie(NULL);
  44. cout.tie(NULL);
  45. ios_base::sync_with_stdio(false);
  46.  
  47. int test_case;
  48. int T;
  49.  
  50. cin >> T;
  51.  
  52. for (test_case = 1; test_case <= T; ++test_case) {
  53.  
  54. A[0] = B[0] = 0;
  55.  
  56. cin >> N;
  57. for (int i = 1; i <= N; ++i)
  58. cin >> A[i];
  59.  
  60. cin >> M;
  61. for (int i = 1; i <= M; ++i)
  62. cin >> B[i];
  63.  
  64. sort(&B[1], &B[M]);
  65.  
  66. memset(cache, -1, sizeof(cache));
  67.  
  68. int a = MAX(snack(1, 1, M, 0), snack(1, 1, M, 1));
  69.  
  70. cout << '#' << test_case << ' ' << a << '\n';
  71. }
  72.  
  73. return 0;
  74. }
Success #stdin #stdout 0.01s 5352KB
stdin
1
5
10
12
6
14
7
3
8
1
2
stdout
1 1 3 0
2 1 3 1
3 1 3 0
4 1 3 1
4 2 3 0
4 3 3 1
4 3 2 0
4 2 2 1
4 2 1 0
4 1 2 0
4 1 1 1
4 1 0 0
3 2 3 1
3 3 3 0
3 4 3 1
3 3 2 1
3 2 2 0
3 2 1 1
3 1 2 1
3 1 1 0
3 1 0 1
2 2 3 0
2 3 3 1
2 4 3 0
2 3 2 0
2 2 2 1
2 2 1 0
2 1 2 0
2 1 1 1
2 1 0 0
1 2 3 1
1 3 3 0
1 4 3 1
1 3 2 1
1 2 2 0
1 2 1 1
1 1 2 1
1 1 1 0
1 1 0 1
1 1 3 1
2 1 3 0
3 1 3 1
4 1 3 0
4 2 3 1
4 3 3 0
4 3 2 1
4 2 2 0
4 2 1 1
4 1 2 1
4 1 1 0
4 1 0 1
3 2 3 0
3 3 3 1
3 4 3 0
3 3 2 0
3 2 2 1
3 2 1 0
3 1 2 0
3 1 1 1
3 1 0 0
2 2 3 1
2 3 3 0
2 4 3 1
2 3 2 1
2 2 2 0
2 2 1 1
2 1 2 1
2 1 1 0
2 1 0 1
1 2 3 0
1 3 3 1
1 4 3 0
1 3 2 0
1 2 2 1
1 2 1 0
1 1 2 0
1 1 1 1
1 1 0 0
#1 44