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. java连接oracle的简单实例

    连接oracle的时候,要导入oracle驱动的jar包. 连接的时候,有statement和preparedstatement两种,从代码中可以看出不同. example: package com. ...

  2. MYSQL 系统命令 源码定位

    sql_cmd.h enum enum_sql_command { SQLCOM_SELECT, SQLCOM_CREATE_TABLE, SQLCOM_CREATE_INDEX, SQLCOM_AL ...

  3. JDK1.5中线程池,定时器知识

    package cn.it.pool; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executo ...

  4. AOP 的利器:ASM 3.0 介绍

    引言 什么是 ASM ? ASM 是一个 Java 字节码操控框架.它能被用来动态生成类或者增强既有类的功能.ASM 可以直接产生二进制 class 文件,也可以在类被加载入 Java 虚拟机之前动态 ...

  5. atlassian-jira-confluence-bitbucket破解

    ==================================================================================================== ...

  6. 关于Clone 的方法使用

    package cn.hncu.day7.clone.v1;//克隆的套路:// 第1步:重写User类的clone()方法,以供外面调用.因为外面的类无法直接调用User类父类中的clone()方法 ...

  7. jquery easyui动态校验,easyui动态验证

    >>>>>>>>>>>>>>>>>>>>>>>>> ...

  8. vs2015中ctrl+shift+F进行“在文件中查找”,有时候无效?

    搜狗拼音的默认简繁切换快捷键是ctrl+shift+F,改掉以后vs就可以收到这个按键消息了

  9. 对于EditText的详细用法

    EditText这个控件对于每一个Android开发者来说都是再熟悉不过了,但是,为什么有的人的EditText可以表现的那么好看,而刚入学Android的程序员来讲却丑到爆.这就充分的说明对于Edi ...

  10. C#缓存处理

    第一种方式: 在ASP.NET中页面缓存的使用方法非常的简单,只需要在aspx页的顶部加这样一句声明即可: <%@ OutputCache Duration="60" Var ...