Advertisement
Saleh127

CSES 1097 / DP

Aug 9th, 2022
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. /***
  2.  created: 2022-08-09-21.49.04
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
  11. #define ll long long
  12. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  13. #define get_lost_idiot return 0
  14. #define nl '\n'
  15.  
  16.  
  17. ll dp[5005][5005];
  18. ll a[5005],n;
  19.  
  20. ll solve(ll i,ll j)
  21. {
  22.  
  23.     if(i==j) return a[i];
  24.  
  25.     if(dp[i][j]!=-1) return dp[i][j];
  26.  
  27.     ll ans=-1e17;
  28.  
  29.     ans=a[i]-solve(i+1,j);
  30.     ans=max(ans,a[j]-solve(i,j-1));
  31.  
  32.     return dp[i][j]=ans;
  33.  
  34. }
  35.  
  36.  
  37. int main()
  38. {
  39.     ios_base::sync_with_stdio(0);
  40.     cin.tie(0);
  41.     cout.tie(0);
  42.  
  43.     cin>>n;
  44.  
  45.     ll tot=0;
  46.  
  47.     for(ll i=0; i<n; i++)
  48.     {
  49.         cin>>a[i];
  50.         tot+=a[i];
  51.     }
  52.  
  53.     memset(dp,-1,sizeof dp);
  54.  
  55.     ll ans=solve(0,n-1);
  56.  
  57.     ans=(ans+tot)/2;
  58.  
  59.     cout<<ans<<nl;
  60.  
  61.     get_lost_idiot;
  62. }
  63.  
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement