-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
10891.cpp
39 lines (38 loc) · 828 Bytes
/
10891.cpp
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
#include <iostream>
#include <string.h>
using namespace std;
#define inf 999999
#define max(a,b) a > b ? a : b
int n ;
int dp[105][105];
int a[105],sum[105];
int get_sum(int i , int j){
return sum[j]-sum[i-1];
}
int solve(int l, int r)
{
if(l > r) return 0;
if(l == r) return a[l];
int &ret = dp[l][r];
if(ret != -1) return ret;
ret = -inf ;
for(int i = l; i <= r; i++)
{
ret = max(ret, get_sum(l,i) - solve(i+1, r));
ret = max(ret, get_sum(i,r) - solve(l, i-1));
}
return ret;
}
int main()
{
while(cin >> n && n){
memset(sum,0,sizeof sum);
for(int i = 1 ; i <= n ; i++ ) {
cin >> a[i];
sum[ i ] = sum[ i-1 ] + a[i] ;
}
memset(dp,-1,sizeof dp);
int ret = solve(1,n);
cout << ret << endl;
}
}