题干:

You may have heard of the pie rule before. It states that if two people wish to fairly share a slice of pie, one person should cut the slice in half, and the other person should choose who gets which slice. Alice and Bob have many slices of pie, and rather than cutting the slices in half, each individual slice will be eaten by just one person.

The way Alice and Bob decide who eats each slice is as follows. First, the order in which the pies are to be handed out is decided. There is a special token called the "decider" token, initially held by Bob. Until all the pie is handed out, whoever has the decider token will give the next slice of pie to one of the participants, and the decider token to the other participant. They continue until no slices of pie are left.

All of the slices are of excellent quality, so each participant obviously wants to maximize the total amount of pie they get to eat. Assuming both players make their decisions optimally, how much pie will each participant receive?

Input

Input will begin with an integer N (1 ≤ N ≤ 50), the number of slices of pie.

Following this is a line with N integers indicating the sizes of the slices (each between 1 and 100000, inclusive), in the order in which they must be handed out.

Output

Print two integers. First, the sum of the sizes of slices eaten by Alice, then the sum of the sizes of the slices eaten by Bob, assuming both players make their decisions optimally.

Examples

Input

3
141 592 653

Output

653 733

Input

5
10 21 10 21 10

Output

31 41

Note

In the first example, Bob takes the size 141 slice for himself and gives the decider token to Alice. Then Alice gives the size 592 slice to Bob and keeps the decider token for herself, so that she can then give the size 653 slice to herself.

题目大意:

有一个长度为N的权值数组,两个人A,B。定义一种权力X为:决定当前这个数组元素分给谁。下次权利X授予没有分得前一个元素的人。如,现在A有权力,将当前数组元素分给自己,那么下一次权力X将被B获得。如果A将数组元素分给B,那么A继续拥有这个权利。一开始权利在A手中,每个人都会采取对自己最有利的方式分配。问最后两个人各自的权值总和。

解题报告:

考虑dp求解,要明确dp的状态只与是否有令牌有关,而与令牌在谁手里无关。因为不论令牌在谁手里,那个人都会尽可能的获得最大的物品价值。

因此我们用dp[i]表示第i个物品开始分配,当前有令牌的人能获得的最大物品价值,然而因为我们只知道初始有令牌的是Bob,因此我们要逆推: dp[i] = max(dp[i + 1], sum[i + 1] - dp[i + 1] + val[i]) // max(不要当前物品, 要)

其实相当于是你如果要从前往后推的话,需要关注令牌是否是同一个人拿着的,所以麻烦很多,而逆推就简单很多,因为令牌是往后传的,我们当前是有令牌的,所以下一个令牌给谁,是我们决定的,就比较好写。

最后dp[1]就是Bob获得的价值了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll dp[MAX],a[MAX],sum[MAX];
int main()
{
int n;
cin>>n;
for(int i = 1; i<=n; i++) scanf("%lld",a+i);
for(int i = n; i>=1; i--) sum[i] = sum[i+1] + a[i];
for(int i = n; i>=1; i--) {
dp[i] = max(dp[i+1],a[i] + (sum[i+1] - dp[i+1]));//不选,选
}
printf("%lld %lld\n",sum[1] - dp[1],dp[1]);
return 0 ;
}

错误代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll a[MAX],sum[MAX];
ll dp[MAX][2][2];//dp[i][j][k]代表第i个数并且第i个选没选0/1,令牌在 0/1(Bob)手里的最大值 ,Bob获得的最大值
int main()
{
int n;
cin>>n;
for(int i = 1; i<=n; i++) scanf("%lld",a+i);
for(int i = 1; i<=n; i++) sum[i] = sum[i-1] + a[i];
dp[1][1][1] = a[1];dp[1][0][1] = 0;
for(int i = 2; i<=n; i++) {
dp[i][0][0] = a[i] + max(dp[i-1][1][1] , dp[i-1][0][0]);//sum[i-1] - min(dp[i-1][1][1] , dp[i-1][0][0]);
dp[i][0][1] = max(dp[i-1][1][0] , dp[i-1][0][1]);
dp[i][1][0] = dp[i][0][0];
dp[i][1][1] = dp[i][0][1] + a[i];
}
ll maxx = 0;
maxx = max(maxx,dp[n][0][0]);
maxx = max(maxx,dp[n][0][1]);
maxx = max(maxx,dp[n][1][0]);
maxx = max(maxx,dp[n][1][1]);
printf("%lld %lld\n",sum[n] - maxx,maxx);
return 0 ;
}
//适用于:可以选择要或者不要,但是不要的话不会把分数给对方  的方法。 
// for(int i = 2; i<=n; i++) {
// dp[i][0][0] = max(dp[i-1][1][1] , dp[i-1][0][0]);//sum[i-1] - min(dp[i-1][1][1] , dp[i-1][0][0]);
//
// dp[i][0][1] = max(dp[i-1][1][0] , dp[i-1][0][1]);
// dp[i][1][0] = dp[i][0][0];
// dp[i][1][1] = dp[i][0][1] + a[i];
// }

想到这个状态感觉可以就开始写了, 但是有个问题啊,,这是个博弈dp,也就是说你不能只顾着Bob,所以这样dp是错误的,因为对方也是用的最优决策你要知道这一点,所以就不太对。所以对于这种博弈dp,我们需要关注的是令牌,因为谁拿着令牌,谁就希望得到最优解。所以我们应该直接设定状态为dp[i]为拿着令牌的人第i轮选择到最后可以得到的最优解。

为什么倒着呢?因为这是求最优解问题,就跟数塔一样,先考虑最后一步决策,然后倒着推。如果是计数,那就一般正着写。

(其实貌似也是可以写的?就是比较麻烦,你考虑令牌不在Bob手里的时候求的是Bob拿到的最小值 就行了吧?)

改了改但还是放弃了、、、

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll a[MAX],sum[MAX];
ll dp[MAX][2][2];//dp[i][j][k]代表第i个数并且第i个选没选0/1,令牌在 0/1(Bob)手里的最大值 ,Bob获得的最大值
int main()
{
int n;
cin>>n;
for(int i = 1; i<=n; i++) scanf("%lld",a+i);
for(int i = 1; i<=n; i++) sum[i] = sum[i-1] + a[i];
dp[1][1][1] = a[1];dp[1][0][1] = 0;
for(int i = 2; i<=n; i++) {
dp[i][0][0] = a[i] + sum[i-1]/*sum[i]*/ - max(sum[i-1] - dp[i-1][1][1] , sum[i-1] - dp[i-1][0][0]);//sum[i-1] - min(dp[i-1][1][1] , dp[i-1][0][0]);
dp[i][0][1] = max(dp[i-1][1][0] , dp[i-1][0][1]);
dp[i][1][0] = dp[i][0][0] - a[i];
dp[i][1][1] = dp[i][0][1] + a[i];
}
ll maxx = 0;
maxx = max(maxx,dp[n][0][0]);
maxx = max(maxx,dp[n][0][1]);
maxx = max(maxx,dp[n][1][0]);
maxx = max(maxx,dp[n][1][1]);
printf("%lld %lld\n",sum[n] - maxx,maxx);
return 0 ;
}
/*
3
10 20 40
30 40
*/