Advertisement
Saleh127

UVA 10891 / DP

Apr 12th, 2022
1,568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. /***
  2.  created: 2021-11-11-20.55.17
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define ll long long
  8. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  9. #define get_lost_idiot return 0
  10. #define nl '\n'
  11.  
  12. ll dp[205][205];
  13. ll a[205],n;
  14.  
  15. ll solve(ll l,ll r)
  16. {
  17.      if(l>r) return 0;
  18.  
  19.      if(dp[l][r]!=INT_MIN) return dp[l][r];
  20.  
  21.      ll ans=INT_MIN,sum=0;
  22.  
  23.      for(ll i=l;i<=r;i++)
  24.      {
  25.           sum+=a[i];
  26.           ans=max(ans,sum-solve(i+1,r));
  27.      }
  28.      sum=0;
  29.      for(ll i=r;i>=l;i--)
  30.      {
  31.           sum+=a[i];
  32.           ans=max(ans,sum-solve(l,i-1));
  33.      }
  34.  
  35.      dp[l][r]=ans;
  36.  
  37.      return ans;
  38. }
  39.  
  40. int main()
  41. {
  42.    ios_base::sync_with_stdio(0);
  43.    cin.tie(0);cout.tie(0);
  44.  
  45.  
  46.    while(cin>>n && n)
  47.    {
  48.         ll i,j,k,l;
  49.  
  50.         for(i=0;i<n;i++) cin>>a[i];
  51.  
  52.         for(i=0;i<201;i++)
  53.         {
  54.              for(j=0;j<201;j++)
  55.              {
  56.                   dp[i][j]=INT_MIN;
  57.              }
  58.         }
  59.  
  60.         cout<<solve(0,n-1)<<nl;
  61.  
  62.    }
  63.  
  64.  
  65.    get_lost_idiot;
  66. }
  67.  
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement