Advertisement
Guest User

Untitled

a guest
Jan 12th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long int
  4. #define pb push_back
  5. #define mp make_pair
  6. #define deb(x) cout<< #x << " " << x << "\n";
  7. #define MAX 9223372036854775807
  8. #define MIN -9223372036854775807
  9. #define PI 3.141592653589
  10. #define setbits(n) __builtin_popcountll(n)
  11. #define mkunique(a) a.resize(unique(a.begin(),a.end())-a.begin());
  12. const ll mod=1e9+7;
  13.  
  14.  
  15. const int N=1e5+10;
  16. ll dp[N][5],n;
  17. vector<ll> a(N);
  18.  
  19. ll go(ll pos, ll s){
  20. if(pos==n){
  21. if(s<4) return -mod*mod;
  22. return 0;
  23. }
  24. if(dp[pos][s]!=-1)
  25. return dp[pos][s];
  26. ll ans;
  27. if(s==0)
  28. ans=max(go(pos+1,0),go(pos+1,1));
  29. else if(s==1)
  30. ans=max({a[pos]+go(pos+1,1),a[pos]+go(pos+1,2),a[pos]+go(pos+1,3)});
  31. else if(s==2)
  32. ans=max(go(pos+1,2),go(pos+1,3));
  33. else if(s==3)
  34. ans=max(a[pos]+go(pos+1,3),a[pos]+go(pos+1,4));
  35. else
  36. ans=go(pos+1,4);
  37. return dp[pos][s]=ans;
  38. }
  39.  
  40. int main() {
  41.  
  42. ios_base::sync_with_stdio(false);
  43. cin.tie(NULL);
  44. cout.tie(NULL);
  45. ll T=clock();
  46.  
  47. cin>>n;
  48. for(ll i=0;i<n;i++)
  49. cin>>a[i];
  50. memset(dp,-1,sizeof(dp));
  51. cout<<max(go(0,0),go(0,1));
  52.  
  53.  
  54. cerr<<"\n\nTIME: "<<(double)(clock()-T)/CLOCKS_PER_SEC<<" sec\n";
  55. T = clock();
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement