알고리즘/DP
BOJ)9465 스티커(다시보기★)
광그로
2017. 6. 22. 12:56
https://www.acmicpc.net/problem/9465
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 | #include <iostream> #include <algorithm> using namespace std; long long sticker[100001][2]; long long dp[100001][3]; //[열][] 0 : 뜯지 않음 1 : 1열 뜯음 2 : 2열 뜯음 int main() { int test; int n; cin >> test; long long input; while (test--) { cin >> n; for (int i = 1; i <= n; i++) { cin >> sticker[i][0]; } for (int i = 1; i <= n; i++) { cin >> sticker[i][1]; } dp[0][0] = dp[0][1] = dp[0][2] = 0; for (int i = 1; i <= n; i++) { dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][1], dp[i - 1][2])); dp[i][1] = max(dp[i - 1][0], dp[i - 1][2]) + sticker[i][0]; dp[i][2] = max(dp[i - 1][0], dp[i - 1][1]) + sticker[i][1]; } long long ans = 0; for (int i = 1; i <= n; i++) { ans = max(max(ans, dp[i][0]), max(dp[i][1], dp[i][2])); } cout << ans<<endl; } } | cs |