Advertisement
Josif_tepe

Untitled

Feb 9th, 2022
1,067
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. typedef long long ll;
  5. int n;
  6. int niza[5001];
  7. ll dp[2][5001][5001];
  8. ll rec(int turn, int start, int end){
  9.     if(start==end){
  10.         if(turn == 0) {
  11.             return niza[start];
  12.         }
  13.         return 0;
  14.     }
  15.  
  16.     if(dp[turn][start][end] != -1){
  17.         return dp[turn][start][end];
  18.     }
  19.  
  20.     ll result;
  21.  
  22.     if(turn==0){
  23.         result = -1e18;
  24.         result=max(result, rec(1, start+1, end)+niza[start]);
  25.         result=max(result, rec(1, start, end-1)+niza[end]);
  26.     }
  27.  
  28.      else {
  29.          result = 1e18;
  30.         result=min(result, rec(0, start+1, end));
  31.         result=min(result, rec(0, start, end-1));
  32.     }
  33.  
  34.     dp[turn][start][end]=result;
  35.     return result;
  36. }
  37. int main()
  38. {
  39. cin>>n;
  40.     for(int i=0; i<n; i++){
  41.     cin>>niza[i];
  42.     }
  43.     for(int i = 0; i < 2; i++) {
  44.         for(int j= 0; j <n; j++) {
  45.             for(int k= 0; k < n; k++) {
  46.                 dp[i][j][k] = -1;
  47.             }
  48.         }
  49.     }
  50.     memset(dp, -1, sizeof dp);
  51.     cout<<rec(0, 0, n-1);
  52.  
  53.  
  54.     return 0;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement