YEZAELP

o18_oct_rock

Jan 13th, 2022 (edited)
60
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int N = 50 + 50 + 10;
  5. int ar[N], dp[N][N];
  6. int n;
  7.  
  8. int f(int i, int j){
  9.     if(i == j) return ar[i];
  10.     if(i > j) return 0;
  11.     if(dp[i][j] != 0) return dp[i][j];
  12.     int l = 0, r = 0;
  13.     /// choose left hand
  14.     l = ar[i] + max(f(i + 1, j - 1), f(i + 2, j));
  15.     /// choose right hand
  16.     r = ar[j] + max(f(i + 1, j - 1), f(i, j - 2));
  17.     return dp[i][j] = max(l, r);
  18. }
  19.  
  20. int main(){
  21.  
  22.     scanf("%d", &n);
  23.  
  24.     for(int i=1;i<=2*n;i++){
  25.         scanf("%d", &ar[i]);
  26.     }
  27.  
  28.     printf("%d", f(1, 2*n));
  29.  
  30.     return 0;
  31. }
RAW Paste Data Copied