j33vansh

Question 6 Game of Chefs

Apr 30th, 2022 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define s 5005
  4. using namespace std;
  5. ll dp[s][s],a[s];
  6. ll fun(ll i,ll j)
  7. {
  8.     //Base case
  9.     if(i > j)
  10.     return 0;
  11.     //Lookup case
  12.     if(dp[i][j] != -1)
  13.     return dp[i][j];
  14.     //Recursion case
  15.     ll op1 = a[i] + min(fun(i+2,j), fun(i+1,j-1) );
  16.     ll op2 = a[j] + min(fun(i+1,j-1), fun(i,j-2) );
  17.     return dp[i][j] = max(op1,op2);
  18. }
  19. int main()
  20. {
  21.     ios_base::sync_with_stdio(false);
  22.     cin.tie(NULL);
  23.     cout.tie(NULL);
  24.     int t;
  25.     cin>>t;
  26.     while(t--){
  27.         ll n;
  28.         cin>>n;
  29.         for(ll i=0;i<n;i++)
  30.         cin>>a[i];
  31.         memset(dp,-1,sizeof(dp));
  32.         cout<<fun(0,n-1)<<endl;
  33.     }
  34.     return 0;
  35. }
Add Comment
Please, Sign In to add comment