Description

Once upon a time Petya and Gena gathered after another programming competition and decided to play some game. As they consider most modern games to be boring, they always try to invent their own games. They have only stickers and markers, but that won't stop them. The game they came up with has the following rules. Initially, there are \(n\) stickers on the wall arranged in a row. Each sticker has some number written on it. Now they alternate turn, Petya moves first.

One move happens as follows. Lets say there are \(m \le 2\) stickers on the wall. The player, who makes the current move, picks some integer \(k\) from \(2\) to \(m\) and takes \(k\) leftmost stickers(removes them from the wall). After that he makes the new sticker, puts it to the left end of the row, and writes on it the new integer, equal to the sum of all stickers he took on this move. Game ends when there is only one sticker left on the wall. The score of the player is equal to the sum of integers written on all stickers he took during all his moves. The goal of each player is to maximize the difference between his score and the score of his opponent. Given the integer \(n\) and the initial sequence of stickers on the wall, define the result of the

game, i.e. the difference between the Petya's and Gena's score if both players play optimally.

Input

The first line of input contains a single integer \(n\) \(( 2 \le n \le 200 000 )\) — the number of stickers,initially located on the wall.

The second line contains \(n\) integers \(a_1 , a_2 , \dots , a_n\) \(( ­ 10 000 \le a_i \le10 000 )\) — the numbers on stickers in order from left to right.

Output

Print one integer — the difference between the Petya's score and Gena's score at the end of the game if both players play optimally.

Sample Input

3

2 4 8

Sample Output

4

1 -7 -2 3

比赛时想了个dp,过了pretest,但是fst了。算法的确有很大的bug(居然还能过pretest,还没人hack。。。)

考虑最优解,它一定时\(a_{i_{1}}-a_{i_{2}}+\cdots+(-1)^{t-1}a_{i_{t}}+(i-1)^ta_n\)。

但是我们无法确定直接确定\(a_{i_1}\)的值,但是最后一个肯定是\(a_n\),所以我们可以从后边往前边推。由于两个人其实是等价的,对于已经确定好的一段后缀,很明显我们可以通过贪心的方法来使答案尽可能地优。设当前的\([i+1,n]\)这段后缀的答案为\(ans\),我们枚举第\(i\)个,则有$$ans = max(ans,a_i+(-ans))$$

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std; #define maxn (200010)
#define inf (1<<30)
int pre[maxn],N,mx; int main()
{
freopen("E.in","r",stdin);
freopen("E.out","w",stdout);
scanf("%d",&N);
for (int i = 1;i <= N;++i) scanf("%d",pre+i),pre[i] += pre[i-1];
mx = pre[N];
for (int i = N-1;i > 1;--i) mx = max(mx,pre[i]-mx);
printf("%d",mx);
fclose(stdin); fclose(stdout);
return 0;
}

Codeforces 731D Funny Game的更多相关文章

  1. 【codeforces 731D】80-th Level Archeology

    [题目链接]:http://codeforces.com/contest/731/problem/D [题意] 给你n个象形文; 每个象形文由l[i]个数字组成; 你可以把所有的组成象形文的数字同时增 ...

  2. CodeForces 731D 80-th Level Archeology

    区间并. 对于上下两个数字,如果不一样,那么可以计算出哪一段范围内可以保证字典序,并且后面所有位置都无需再考虑.对所有范围求交集就是答案了. 求交集写起来有点烦,直接对不可取的范围求并即可. #pra ...

  3. CodeForces 731D (差分+线段扫描)

    Description Archeologists have found a secret pass in the dungeon of one of the pyramids of Cyclelan ...

  4. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  7. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  8. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  9. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

随机推荐

  1. SQL Server与Oracle对比学习:权限管理(一)

    http://blog.csdn.net/weiwenhp/article/details/8093661 我们发现我们现在的生活中到处是涉及到密码,你要记各种各样的密码.比如银行卡,邮件,QQ,微博 ...

  2. Windows XP下安装WinCE6.0开发环境

    Windows下怎样编译WinCE6.0及开发应用程序.以下介绍(安装之前必须保证C盘有足够的空间!20g左右!主要是由于在安装程序在安装过程中要解压): 在Visual Studio 2005之前, ...

  3. go 初使用

    hello.go package main import "fmt" func main(){ fmt.Println("hello world") 直接运行 ...

  4. careercup-栈与队列 3.3

    3.3 栈就像叠盘子,当盘子叠得太高时,就会倾斜倒下.因此,在真实的世界中,当一叠盘子 (栈)超过了一定的高度时,我们就会另起一堆,再从头叠起.实现数据结构SetOfStacks 来模拟这种情况.Se ...

  5. Nginx vs Apache--reference

    May 14th, 2014 - By Walker Rowe https://anturis.com/blog/nginx-vs-apache/ What is the Nginx web and ...

  6. Java Interface是存放常量的最好地方吗?(转)

    虽然Inteface中默认常量就是static final 的.因此很多人就认为其实存放常量的最佳地方,effective java中就提过,不建议使用常量接口.其有一个原因就是:代码编译问题 好,我 ...

  7. 异步DNS解析的实现

    在高性能爬虫为什么使用定制DNS客户端一文中阐述了DNS解析是网络爬虫的瓶颈. 目前主要有两种方法来提高DNS解析效率: 1. 基于多线程的DNS 解析 2. 基于NIO的DNS解析 dnsjava中 ...

  8. CXF整合Spring开发WebService

    刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开 ...

  9. 使用JAXB来实现Java合xml之间的转换

    使用jaxb操作Java与xml之间的转换非常简单,看个例子就明白了. //javaBean-->xml @Test public void test1() { try { JAXBContex ...

  10. 我的 ubuntu 12.04.2修复Grub

    网上有很多…… 首先我做了U盘启动,然后进入LiveCD模式. 输入grub,提示说要重新安装,好了,那就安装吧,连好网,sudo apt-get install grub 安装成功后,sudo -i ...