DuongNhi99

BONUS

Dec 10th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4.  
  5. const int N = 304;
  6.  
  7. int n, m;
  8. ll a[N];
  9. ll F[N][N][N/2];
  10.  
  11. ll dp(int l, int r, int k){
  12.     if(k == 0) return 0;
  13.     if(r - l + 1 < 2 * k) return -1e9;
  14.  
  15.     if(F[l][r][k] != -1)
  16.         return F[l][r][k];
  17.  
  18.     ll ans = -1e9;
  19.     ans = max({
  20.         dp(l+2, r, k-1) + abs(a[l] - a[l+1]),
  21.         dp(l, r-2, k-1) + abs(a[r] - a[r-1]),
  22.         dp(l+1, r-1, k-1) + abs(a[l] - a[r]),
  23.         dp(l+1, r, k),
  24.         dp(l, r-1, k)
  25.     });
  26.     return F[l][r][k] = ans;
  27. }
  28.  
  29. int main() {
  30.     //freopen("in.txt", "r", stdin);
  31.     freopen("BONUS.inp", "r", stdin);
  32.     freopen("BONUS.out", "w", stdout);
  33.     ios_base::sync_with_stdio(false);
  34.     cin.tie(NULL); cout.tie(NULL);
  35.  
  36.     cin >> n >> m;
  37.     for(int i = 1; i <= n; ++i)
  38.         cin >> a[i];
  39.  
  40.     memset(F, -1, sizeof(F));
  41.     cout << dp(1, n, m);
  42.     return 0;
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment