Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- using namespace std;
- const int N = 304;
- int n, m;
- ll a[N];
- ll F[N][N][N/2];
- ll dp(int l, int r, int k){
- if(k == 0) return 0;
- if(r - l + 1 < 2 * k) return -1e9;
- if(F[l][r][k] != -1)
- return F[l][r][k];
- ll ans = -1e9;
- ans = max({
- dp(l+2, r, k-1) + abs(a[l] - a[l+1]),
- dp(l, r-2, k-1) + abs(a[r] - a[r-1]),
- dp(l+1, r-1, k-1) + abs(a[l] - a[r]),
- dp(l+1, r, k),
- dp(l, r-1, k)
- });
- return F[l][r][k] = ans;
- }
- int main() {
- //freopen("in.txt", "r", stdin);
- freopen("BONUS.inp", "r", stdin);
- freopen("BONUS.out", "w", stdout);
- ios_base::sync_with_stdio(false);
- cin.tie(NULL); cout.tie(NULL);
- cin >> n >> m;
- for(int i = 1; i <= n; ++i)
- cin >> a[i];
- memset(F, -1, sizeof(F));
- cout << dp(1, n, m);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment