time limit per test 2 seconds

memory limit per test 256 megabytes

input standard input

output standard output

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个不同美味值的派,Alice和Bob从1开始吃到n。对于第i个派,拥有令牌的人可以做出抉择:①吃这个派获得美味值,但是到下一个派,令牌就给另一个人。②让对方吃,自己保留令牌。已知Bob初始持有令牌,并且两人都采取最优策略,请分别输出Alice和Bob最后得到的美味值。

题解:

     ①从题目发现当前的抉择只会依据有令牌的那个人。

     ②使用DP:f[i]表示当前该吃第i个,拥有令牌的人吃到的最大美味值。

     ③由于只知道i==1时Bob拥有令牌,因此倒推。

     ④转移方程式:f[i]=max(f[i+1],sum(i+1~n)-f[i+1]+val[i]) (表示不吃与吃)

#include<stdio.h>
#include<algorithm>
#define go(i,a,b) for(int i=a;i<=b;i++)
#define ro(i,a,b) for(int i=a;i>=b;i--)
int n,f[55],a[55],sum;
int main()
{
scanf("%d",&n);
go(i,1,n)scanf("%d",a+i);
ro(i,n,1)f[i]=std::max(f[i+1],sum-f[i+1]+a[i]),sum+=a[i];
printf("%d %d\n",sum-f[1],f[1]);return 0;
}//Paul_Guderian

   

【CF MEMSQL 3.0 C. Pie Rules】的更多相关文章

  1. 【CF MEMSQL 3.0 A. Declined Finalists】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  2. 【CF MEMSQL 3.0 E. Desk Disorder】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  3. 【CF MEMSQL 3.0 D. Third Month Insanity】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. 【CF MEMSQL 3.0 B. Lazy Security Guard】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. 【CF edu 27 G. Shortest Path Problem?】

    time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standa ...

  6. 【CF Round 439 E. The Untended Antiquity】

    time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standa ...

  7. 【CF Round 439 C. The Intriguing Obsession】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  8. 【CF Round 439 B. The Eternal Immortality】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  9. 【Magicodes.IE 2.0.0-beta1版本发布】已支持数据表格、列筛选器和Sheet拆分

    为了更好的完善Magicodes.IE,春节期间我们会进行一次大的重构.由于精力有限,急缺文档和翻译(将文档翻译为英文文档)支持,诚邀各位加入.同时在功能方便也做了相关规划,有兴趣的朋友可以参与提交P ...

随机推荐

  1. 用C#实现WEB代理服务器

    用C#实现Web代理服务器 代理服务程序是一种广泛使用的网络应用程序.代理程序的种类非常多,根据协议不同可以分成HTTP代理服务程序.FTP代理服务程序等,而运行代理服务程序的服务器也就相应称为HTT ...

  2. Mult

    public class MULT { public static void main(String args[]) { int k; for(int x=1;x<=9;x++) { for(i ...

  3. ABAP CDS ON HANA-(7)CDSビューでの集約

    Aggregate expression in CDS View An aggregate expression calculates a single value from an operand o ...

  4. 前端面试题目汇总摘录(JS 基础篇 —— 2018.11.02更新)

    温故而知新,保持空杯心态 JS 基础 JavaScript 的 typeof 返回那些数据类型 object number function boolean undefined string type ...

  5. APIO2018 游记

    day \(-\infty\) \(\sim\) day0 5 月 5 号左右的时候去了趟中北大学,山西省大学生程序设计竞赛.不是太满意,现场 rk3.拿到了充电宝(冲着这个去的,虽然抵不过车费),抽 ...

  6. C#文件重命名的代码

    C#中没有重命名的方法,自己写了一个方法,来处理文件的重命名. /// <summary> /// 重命名文件夹内的所有子文件夹 /// </summary> /// < ...

  7. log报错: Caused by: java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.

    报错: 解决方式: 1.登录数据库查看错误原因 结果发现账号无法正常登录出现账号被锁定的错误. 2.如何账号解锁? 用sys系统管理员账号登录数据库 SQL> alter user 用户名 ac ...

  8. COJ 2192: Wells弹键盘 (dp)

    2192: Wells弹键盘 Description Wells十分羡慕和佩服那些会弹钢琴的人比如子浩君,然而Wells只会弹键盘…… Wells的键盘只有10个键,从1,2,3,……,9,0,如下图 ...

  9. 输出不重复的质因数(C++)

    [问题描述] 从键盘上输入一个大于 1 的正整数,输出它所有不等的质因数.(什么是质因数?既是质数,又是因数) [代码展示] # include<iostream>using namesp ...

  10. font and face, 浅探Emacs字体选择机制及部分记录

    缘起 最近因为仰慕org-mode,从vim迁移到了Emacs.偶然发现org-mode中调出的calendar第一行居然没有对齐,排查一下发现是字体的问题.刚好也想改改Emacs的字体,于是我就开始 ...