Advertisement
Saleh127

UVA 10559 / MCM - DP

Jul 22nd, 2022
1,079
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. /***
  2.  created: 2022-07-22-21.52.17
  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. ll a[1000],n;
  17.  
  18. ll dp[202][202][205];
  19.  
  20. ll solve(ll s,ll e,ll cnt)
  21. {
  22.     if(s>e) return 0;
  23.  
  24.     if(dp[s][e][cnt]!=-1) return dp[s][e][cnt];
  25.  
  26.     ll ans = cnt*cnt + solve(s+1,e,1);
  27.  
  28.     for(ll i=s+1;i<=e;i++)
  29.     {
  30.         if(a[s]==a[i])
  31.         {
  32.             ans=max(ans, solve(s+1,i-1,1) +  solve(i,e,cnt+1));
  33.         }
  34.     }
  35.  
  36.     return dp[s][e][cnt]=ans;
  37.  
  38. }
  39.  
  40.  
  41. int main()
  42. {
  43.     ios_base::sync_with_stdio(0);
  44.     cin.tie(0);
  45.     cout.tie(0);
  46.  
  47.  
  48.     test
  49.     {
  50.         ll i,j,k,l;
  51.  
  52.         cin>>n;
  53.  
  54.         for(i=0;i<n;i++) cin>>a[i];
  55.  
  56.         memset(dp,-1,sizeof dp);
  57.  
  58.         cout<<"Case "<<cs<<": "<<solve(0,n-1,1)<<nl;
  59.     }
  60.  
  61.  
  62.     get_lost_idiot;
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement