Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <utility>
- #include <algorithm>
- #define fi first
- #define se second
- constexpr int MAX = 100010;
- int n;
- std::pair<int, int> arr[MAX];
- int memo[MAX];
- int solve(int i) {
- if (memo[i]) {
- return memo[i];
- }
- if (i >= n) {
- return 0;
- } else {
- int res1 = solve(i + 1);
- int res2 = 0, res3 = 0;
- if (arr[i].fi > 0) {
- res2 = arr[i].fi + solve(i + arr[i].fi);
- }
- if (arr[i].se > 0) {
- res3 = arr[i].se + solve(i + arr[i].se);
- }
- return memo[i] = std::max({ res1, res2, res3 });
- }
- }
- int main( ) {
- std::ios_base::sync_with_stdio(0);
- std::cin.tie(0);
- std::cout.tie(0);
- std::cin >> n;
- for (int i = 0; i < n; ++i) {
- std::cin >> arr[i].fi >> arr[i].se;
- }
- std::cout << solve(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement