Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
- ll dp[1000099][4];
- ll ans[1000099],a[1000099];
- int main(){
- fast;
- ll n,c;cin>>n>>c;
- for(ll i=1;i<=n;++i){
- cin>>a[i];
- }
- if(n==1){
- cout<<0;return 0;
- }
- for(int i=2;i<=n;++i){
- for(int j=1;j<=3 && i - j >=1;++j){
- if (j == 1){
- dp[i][i-j] = max(0ll,a[i]-a[i-1]-c);
- ans[i] = max(ans[i],dp[i][i-j]+ans[i-j-1]);
- }
- if(j==2){
- dp[i][i-j] = max(0ll,max(a[i-1]-a[i-2]-c, max(a[i]-a[i-1]-c,a[i]-a[i-2]-c)));
- ans[i] = max(ans[i],dp[i][i-j]+ans[i-j-1]);
- }
- if(j==3){//i-3 i-2 i-1 i
- dp[i][i-j] = max(0ll, a[i-2] - a[i-3] - c);
- dp[i][i-j] = max(dp[i][i-j], max(a[i-1]-a[i-3]-c, a[i]-a[i-3]-c));
- dp[i][i-j] = max(dp[i][i-j], max(a[i-1]-a[i-2]-c, a[i]-a[i-2]-c));
- dp[i][i-j] = max(dp[i][i-j], max(a[i]+a[i-2]-a[i-3]-a[i-1]-c , a[i]-a[i-1]-c));
- ans[i] = max(ans[i],dp[i][i-j]+ans[i-j-1]);
- }
- }
- }
- cout<<ans[n];
- return 0;
- }
Add Comment
Please, Sign In to add comment