Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- typedef long long ll;
- int n;
- int niza[5001];
- ll dp[2][5001][5001];
- ll rec(int turn, int start, int end){
- if(start==end){
- if(turn == 0) {
- return niza[start];
- }
- return 0;
- }
- if(dp[turn][start][end] != -1){
- return dp[turn][start][end];
- }
- ll result;
- if(turn==0){
- result = -1e18;
- result=max(result, rec(1, start+1, end)+niza[start]);
- result=max(result, rec(1, start, end-1)+niza[end]);
- }
- else {
- result = 1e18;
- result=min(result, rec(0, start+1, end));
- result=min(result, rec(0, start, end-1));
- }
- dp[turn][start][end]=result;
- return result;
- }
- int main()
- {
- cin>>n;
- for(int i=0; i<n; i++){
- cin>>niza[i];
- }
- for(int i = 0; i < 2; i++) {
- for(int j= 0; j <n; j++) {
- for(int k= 0; k < n; k++) {
- dp[i][j][k] = -1;
- }
- }
- }
- memset(dp, -1, sizeof dp);
- cout<<rec(0, 0, n-1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement