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. 仿path菜单button的实现

    path刚出来时.其菜单button也算是让大多数人感到了惊艳,如今看来事实上是非常easy的就是动画的结合. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...

  2. Visual Studio Package 插件开发

    背景 这段时间公司新做了一个支付系统,里面有N个后台服务,每次有更新修改,拷贝打包发布包"不亦乐乎"...于是我想要不要自己定制个打包插件. 部分朋友可能会认为,有现成的可以去找一 ...

  3. iOS开发-javaScript交互

    前言 当前混合开发模式迎来了前所未有的发展,跨平台开发.热更新等优点决定了这种模式的重要地位.虽然前端界面在交互.动效等多方面距离原生应用还有差距,但毫无疑问混合开发只会被越来越多的公司接受.在iOS ...

  4. select 中使用 case when 和 replace

    在SELECT中,用CASE   例如:     select   a.Cname   as   Tcomname,b.Cname   as   TGoodname,D.nQuanty,c.cNote ...

  5. CentOS 6.7安装配置Cacti监控系统

    一.安装配置LAMP环境 yum -y install httpd php php-mysql php-snmp php-xml php-gd mysql mysql-server 启动http和my ...

  6. SQL大量数据查询的优化 及 非用like不可时的处理方案

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  7. oracle的学习 第二节:创建数据表

    学习内容: A.创建数据库和表空间 B.创建用户和分配权限 C.创建数据表 一.创建数据库和表空间 (一)SQL语言的基本概念 1.概念 高级的结构化查询语言:沟通数据库服务器和客户的重要桥梁. PL ...

  8. orainstRoot.sh到底执行了哪些操作

    1 #!/bin/sh      1 #!/bin/sh      2 AWK=/bin/awk      3 CHMOD=/bin/chmod      4 CHGRP=/bin/chgrp     ...

  9. .NET 互操作

    首先推荐一本书<精通.NET 互操作> ,这本书是目前中文资料里讲 互操作最详尽的书了. 做系统集成项目的同学应该都和设备打过交道(如视频设备:海康.大华等),在大多数情况下这些设备厂商会 ...

  10. 原生JS实现幻灯片轮播效果

    在以往的认知中,一直以为用原生JS写轮播是件很难得事情,今天上班仿照网上的写了一个小demo.小试牛刀. 大致效果: html结构很简单,两个列表,一个代表图片列表,一个是右下角序号列表. <d ...