Naxocist

TOI18_Sausage

Jun 18th, 2022
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 5005;
  5. int ar[N], dp[N][N], res[N];
  6.  
  7. int main() {
  8.     ios_base::sync_with_stdio(false); cout.tie(nullptr);
  9.  
  10.     int n; scanf("%d", &n);
  11.  
  12.     for(int i=1; i<=n; ++i) scanf("%d", &ar[i]);
  13.  
  14.     for(int l=n; l>=1; --l) {
  15.         for(int r=1; r<=n; ++r) {
  16.             if(l > r) continue ;
  17.             if(l == r) {
  18.                 dp[l][r] = ar[l];
  19.                 continue ;
  20.             }
  21.  
  22.             dp[l][r] = max(ar[l] + dp[l+1][r], ar[r] + dp[l][r-1]) + abs(ar[l] - ar[r]);
  23.         }
  24.     }
  25.  
  26.     for(int i=1; i<=n; ++i) {
  27.         res[i] = INT_MIN;
  28.         for(int l=0; l<i; ++l) {
  29.             res[i] = max(res[i], res[l] + dp[l+1][i]);
  30.         }
  31.     }
  32.     cout << res[n];
  33.  
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment