[CodeForces 859C] Pie Rules

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

思路:

首先想清楚一个问题,这个令牌在谁手里没有影响,关键就在于拿到令牌的这个人会给自己带来最大的收益,我们用dp[i] 表示 分到第i个饼的时候的拿令牌的这个人的最大价值,这样逆推回去,因为bob是第一个,所以dp[1]就是Bob的答案,总数减去dp[1] 就是Alice的答案。 

AC代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<cmath>
#define INF 0x3f3f3f3f
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fori(x) for(int i=0;i<x;i++)
#define forj(x) for(int j=0;j<x;j++)
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(y))
#define sca(x) scanf("%d", &x)
#define scas(x) scanf("%s",x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define pris(x) printf("%s\n",x)
#define prl(x) printf("%lld\n",x)
//#include <bits/stdc++.h>

typedef long long ll;
const int maxn=1e6+7;
const int mod=1e9+7;
const double eps=1e-8;
//const double pi = acos(-1);

using namespace std;

int a[maxn];
int sum[maxn];
int dp[maxn];

int main()
{
    int n;
    sca(n);
    rep(i,0,n)
    {
      sca(a[i]);
    }
    per(i,0,n)
    {
      sum[i] = sum[i+1] + a[i];
    }
    per(i,0,n)
    {
      dp[i] = max(dp[i+1],sum[i]-dp[i+1]);
    }
    printf("%d %d\n",sum[0] - dp[0],dp[0]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Prince_NYing/article/details/89005366
pie