aj30

Untitled

Oct 4th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4.  
  5. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  6.  
  7. ll dp[1000099][4];
  8. ll ans[1000099],a[1000099];
  9. int main(){
  10. fast;
  11. ll n,c;cin>>n>>c;
  12.  
  13. for(ll i=1;i<=n;++i){
  14. cin>>a[i];
  15. }
  16. if(n==1){
  17. cout<<0;return 0;
  18. }
  19. for(int i=2;i<=n;++i){
  20. for(int j=1;j<=3 && i - j >=1;++j){
  21. if (j == 1){
  22. dp[i][i-j] = max(0ll,a[i]-a[i-1]-c);
  23. ans[i] = max(ans[i],dp[i][i-j]+ans[i-j-1]);
  24. }
  25. if(j==2){
  26. 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)));
  27. ans[i] = max(ans[i],dp[i][i-j]+ans[i-j-1]);
  28. }
  29. if(j==3){//i-3 i-2 i-1 i
  30. dp[i][i-j] = max(0ll, a[i-2] - a[i-3] - c);
  31. dp[i][i-j] = max(dp[i][i-j], max(a[i-1]-a[i-3]-c, a[i]-a[i-3]-c));
  32.  
  33. dp[i][i-j] = max(dp[i][i-j], max(a[i-1]-a[i-2]-c, a[i]-a[i-2]-c));
  34.  
  35. 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));
  36.  
  37. ans[i] = max(ans[i],dp[i][i-j]+ans[i-j-1]);
  38. }
  39. }
  40. }
  41. cout<<ans[n];
  42.  
  43.  
  44. return 0;
  45. }
Add Comment
Please, Sign In to add comment