Advertisement
AlejandroGY

Mermelada

Mar 31st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <utility>
  3. #include <algorithm>
  4.  
  5. #define fi first
  6. #define se second
  7.  
  8. constexpr int MAX = 100010;
  9.  
  10. int n;
  11. std::pair<int, int> arr[MAX];
  12. int memo[MAX];
  13.  
  14. int solve(int i) {
  15.    if (memo[i]) {
  16.       return memo[i];
  17.    }
  18.    if (i >= n) {
  19.       return 0;
  20.    } else {
  21.       int res1 = solve(i + 1);
  22.       int res2 = 0, res3 = 0;
  23.       if (arr[i].fi > 0) {
  24.          res2 = arr[i].fi + solve(i + arr[i].fi);
  25.       }
  26.       if (arr[i].se > 0) {
  27.          res3 = arr[i].se + solve(i + arr[i].se);
  28.       }
  29.       return memo[i] = std::max({ res1, res2, res3 });
  30.    }
  31. }
  32.  
  33. int main( ) {
  34.    std::ios_base::sync_with_stdio(0);
  35.    std::cin.tie(0);
  36.    std::cout.tie(0);
  37.  
  38.    std::cin >> n;
  39.    for (int i = 0; i < n; ++i) {
  40.       std::cin >> arr[i].fi >> arr[i].se;
  41.    }
  42.    std::cout << solve(0);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement