Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- long long A[2000009];
- long long dp1[2000009],dp2[2000009],cnt1[2000009],cnt2[2000009];
- using namespace std;
- int main()
- {
- long long N,M,K,j,k,low,high,mid,from,to;
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cin >> N >> K;
- for(register int i = 1;i <= N;i++)
- {
- cin >> A[i];
- }
- low = -1;
- high = 50000;
- while(low + 1 < high)
- {
- mid = (low + high)/2;
- /*for(register int i = 0;i <= N;i++)
- {
- dp1[i] = 0;
- dp2[i] = -1000000000000000000;
- cnt1[i] = 0;
- cnt2[i] = 0;
- }*/
- memset(dp1,0,N+1);
- memset(dp2,-2000000000,N+1);
- memset(cnt1,0,N+1);
- memset(cnt2,0,N+1);
- for(register int i = 1;i <= N;i++)
- {
- dp1[i] = dp1[i-1];
- cnt1[i] = cnt1[i-1];
- if(dp2[i-1] + A[i] - mid > dp1[i])
- {
- dp1[i] = dp2[i-1] + A[i] - mid;
- cnt1[i] = cnt2[i-1] + 1;
- }
- dp2[i] = dp2[i-1];
- cnt2[i] = cnt2[i-1];
- if(dp1[i-1] - A[i] > dp2[i])
- {
- dp2[i] = dp1[i-1] - A[i];
- cnt2[i] = cnt1[i-1];
- }
- }
- if(cnt1[N] > K)
- {
- low = mid;
- }
- else
- {
- high = mid;
- }
- }
- //cout << "high = " << high <<endl;
- //cout << "cnt1[N] = " << cnt1[N] <<endl;
- mid = high;
- for(register int i = 0;i <= N;i++)
- {
- dp1[i] = 0;
- dp2[i] = -1000000000000000000;
- cnt1[i] = 0;
- cnt2[i] = 0;
- }
- for(register int i = 1;i <= N;i++)
- {
- dp1[i] = dp1[i-1];
- cnt1[i] = cnt1[i-1];
- if(dp2[i-1] + A[i] - mid > dp1[i])
- {
- dp1[i] = dp2[i-1] + A[i] - mid;
- cnt1[i] = cnt2[i-1] + 1;
- }
- dp2[i] = dp2[i-1];
- cnt2[i] = cnt2[i-1];
- if(dp1[i-1] - A[i] > dp2[i])
- {
- dp2[i] = dp1[i-1] - A[i];
- cnt2[i] = cnt1[i-1];
- }
- }/*
- for(i = 1;i <= N;i++)
- {
- cout << dp1[i] <<" ";
- }
- cout <<endl;
- for(i = 1;i <= N;i++)
- {
- cout << dp2[i] <<" ";
- }
- cout <<endl;
- for(i = 1;i <= N;i++)
- {
- cout << cnt1[i] <<" ";
- }
- cout <<endl;
- for(i = 1;i <= N;i++)
- {
- cout << cnt2[i] <<" ";
- }
- cout <<endl;*/
- //assert(cnt1[N] <= K);
- cout << max(0LL,dp1[N]+K*mid) <<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement