-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL.cpp
More file actions
132 lines (113 loc) · 3.29 KB
/
L.cpp
File metadata and controls
132 lines (113 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Jai Mata Di..
//
// @ author : Sanjit Anand (sanjit_15)
//
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>
using order_set = tree<T, null_type, less<T>, rb_tree_tag , tree_order_statistics_node_update>;
template<typename T>
using order_multiset = tree<T,null_type,less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define PI 3.1415926535897932384626
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define rep(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define pf pop_front
#define ppb pop_back
#define ppf pop_front
#define F first
#define S second
#define trav(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
#define ctzll __builtin_ctzll
#define clzll __builtin_clzll
#define all(x) x.begin(),x.end()
#define sz(x) x.size()
template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef set<int> si;
typedef list<int> li;
typedef tuple<int> tpi;
const long long INF = 1e18;
const long long MOD = 1000000007;
const long long MM = 998244353;
int mpow(int base, int exp, int m);
int gcd(int a, int b);
const int N = 3005;
// dp[i][j][turn]
int dp[N][N][2];
int helper(int ar[], int i, int j, int turn){
if(i > j){
return 0;
}
if(dp[i][j][turn] != -1){
return dp[i][j][turn];
}
if(turn == 1){
dp[i][j][turn] = max(ar[i] + helper(ar, i+1, j, 0), ar[j] + helper(ar, i, j-1, 0));
}
else{
dp[i][j][turn] = min(helper(ar, i+1, j, 1), helper(ar, i, j-1, 1));
}
return dp[i][j][turn];
}
void solve() {
int i, j, n, k;
cin >> n;
int ar[n+1];
int sum = 0;
for(int i=1; i<=n; i++){
cin >> ar[i];
sum += ar[i];
}
memset(dp, -1, sizeof(dp));
int X = helper(ar, 1, n, 1);
int Y = sum - X;
cout << (X - Y) << "\n";
return;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#ifdef NCR
pmain();
#endif
#ifdef SIEVE
sieve();
#endif
int t = 1;
// cin >> t;
while(t--) {
solve();
}
return 0;
}
int gcd(int a, int b){
return b==0 ? a : gcd(b,a%b);
}
int mpow(int base, int exp, int m=MOD) {
base %= m;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((long long)result * base) % m;
base = ((long long)base * base) % m;
exp >>= 1;
}
return result;
}