코딩테스트/동적계획법
[DP(Top-Down)] 백준 10835 카드게임 C++
유(YOO)
2023. 9. 4. 16:26
#include <iostream>
#include <vector>
#define N 2001
using namespace std;
int n;
vector<int> l(N, 0);
vector<int> r(N, 0);
int dp[N][N];
int func(int s, int e) {
int& ret = dp[s][e];
if (ret != -1) return ret;
if ((s >= n) || (e >= n)) return 0;
ret = 0;
if (l[s] > r[e]) ret = max(max(func(s+1, e), func(s+1, e+1)), func(s, e+1) + r[e]);
else ret = max(func(s+1, e), func(s+1, e+1));
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n;
for(int i=0; i<n; i++) cin >> l[i];
for(int i=0; i<n; i++) cin >> r[i];
fill(&dp[0][0], &dp[N-1][N], -1);
cout << func(0, 0);
return 0;
}