-
Notifications
You must be signed in to change notification settings - Fork 3
/
CCC15S5.cpp
46 lines (45 loc) · 1.1 KB
/
CCC15S5.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/**
* CCC '15 S5 - Greedy For Pies
* Question URL: Dynamic Programming
* 15/15 on DMOJ
* Question URL: https://dmoj.ca/problem/ccc15s5
* @author Tommy Pang
*/
int n, m;
ll dp[3005][105][105][2];
int A[3005], B[3005];
ll fun(int i, int L, int R, int can) {
if (dp[i][L][R][can]!=-1) return dp[i][L][R][can];
ll ret = 0;
if (i<=n) {
// take current one
if (can==1) ret = max(ret, fun(i+1, L, R, 0) + A[i]);
// skip current one
ret = max(ret, fun(i+1, L, R, 1));
}
if (L<=R) {
// insert and waste current one
ret = max(ret, fun(i, L+1, R, 1));
// insert and take current one
if (can==1) ret = max(ret, fun(i, L, R-1, 0) + B[R]);
}
dp[i][L][R][can] = ret;
return ret;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin>>n;
memset(dp, -1, sizeof (dp));
for (int i = 1; i <= n; ++i) {
cin>>A[i];
}
cin>>m;
for (int i = 1; i <= m; ++i) {
cin>>B[i];
}
sort(B, B+m+1);
cout<<fun(1, 1, m, 1);
}