Need help getting wrong answer in this dp problem

Revision en1, by mainnahihoo, 2025-09-29 14:22:26

Problem is Link

what my approach is -> i had written a brute force recurion simply trying all possible results i can make and storing the results in pair at the end i can see what the max score achieved by 1st person and the 2nd person

problem is getting wrong answer in large test case i am not able to debug after this .

please helppppppppppppppp

pair<int,int> mpl[2500][2500][2];
int x=0;
pair<int,int> rec(int l,int r,int flag,vector<int>&vec){
    if(l>r) {
        // cout<<"!!!!!!!!!!!!!"<<endl;
        return {0,0};
    }
    if(mpl[l][r][flag].first!=-1 && mpl[l][r][flag].second!=-1) return mpl[l][r][flag];
    x++;
    // cout<<"call for "<<l<<" "<<r<<" "<<flag<<endl;
    pair<int,int> ans={0,0};
    pair<int,int>left,right;
     if(flag){
        pair<int,int> left=rec(l+1,r,!flag,vec);
        pair<int,int> right=rec(l,r-1,!flag,vec);
        left.first+=vec[l];
        right.first+=vec[r];
        ans = (left.first > right.first) ? left : right;
    }
    else{ 
        pair<int,int> left=rec(l+1,r,!flag,vec);
        pair<int,int> right=rec(l,r-1,!flag,vec);
        left.second+=vec[l];
        right.second+=vec[r];
        ans = (left.second > right.second) ? left : right;
    }
    mpl[l][r][flag]=ans;
    return mpl[l][r][flag];
}
void solve() {
    // executing code from here 
    int t=1;
    while (t--) {
        int n;
        cin >> n;
        vi vec(n);
        for (int i = 0; i < n; i++) cin >> vec[i];
        // cout<<a.first<<" "<<a.second<<endl;
        // dbg(x);
        for(int i=0;i<2500;i++){
            for(int j=0;j<2500;j++){
                for(int k=0;k<2;k++){
                    mpl[i][j][k]={-1,-1};
                }
            }
        }
        pair<int,int>a=rec(0,n-1,1,vec);
        cout<<a.first<<endl;
    }
}

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English mainnahihoo 2025-09-29 14:22:26 1958 Initial revision (published)