QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#6162. 卡牌游戏

統計

轩轩某天想到了一个卡牌游戏,游戏规则如下:

  1. 初始时轩轩的手中有自左向右排成一排的 $n$ 张卡牌,每张卡牌上有一个整数分值。
  2. 接下来,轩轩每次可以选取卡牌序列最左边的连续若干张卡牌(至少 $2$ 张),将它们替换为一张新卡牌。新卡牌将插入到序列的最左端,它的分值为本次操作中被替换掉的卡牌的分值之和。
  3. 初始时轩轩总分为 $0$,每执行一次卡牌替换操作,新卡牌的分值将加到总分中。当序列长度为 $1$ 时游戏结束,轩轩也可以在任意时刻结束游戏。

现在给出序列中各个卡牌的分值,请你来帮助轩轩计算他能够获得的最高总分是多少?

输入格式

第一行一个正整数 $n$,代表卡牌的数目。

接下来一行 $n$ 个以空格分隔的整数,第 $i$ 个数字 $a_i$ 代表自左向右第 $i$ 张卡牌的分值。

输出格式

仅一行一个整数表示答案。

样例数据

样例 1 输入

3
2 -1 2

样例 1 输出

4

样例 1 解释

最优策略为,首先选择最左侧的两张卡牌,总分增加 $2 + (-1) = 1$。此时轩轩选择的两张卡牌被替换为一张分值为 $1$ 的卡牌,且被放入序列最左侧,此时自左向右卡牌的分值为 $1$ 和 $2$。

接下来选择当前序列中所有卡牌,总分增加 $1 + 2 = 3$,总分为 $4$。此时轩轩选择的两张卡牌被替换为一张分值为 $3$ 的卡牌,且被放入序列最左侧,此时序列中只有一张分值为 $3$ 的卡牌,游戏结束。

样例 2 输入

7
-4 3 0 7 -3 -5 -3

样例 2 输出

9

样例 2 解释

最优策略为,首先选择最左侧的四张卡牌,总分增加 $(-4) + 3 + 0 + 7 = 6$。此时轩轩选择的四张卡牌被替换为一张分值为6 的卡牌,且被放入序列最左侧,此时自左向右卡牌的分值为 $6, -3, -5, -3$。

再选择最左侧的两张卡牌,总分增加 $6 + (-3) = 3$,总分为 $9$。此时轩轩选择的两张卡牌被替换为一张分值为 $3$ 的卡牌,且被放入序列最左侧,此时自左向右卡牌的分值为 $3, -5, -3$。

此时无论如何操作均无法使总分继续增大,轩轩选择结束游戏。

子任务

测试点 $1\sim6$ 满足:$1\le n\le 16, |a_i| \le 100$。

测试点 $7\sim 12$ 满足:$1\le n\le 10^3, |a_i| \le 100$。

测试点 $13\sim 20$ 满足:$1\le n\le 10^5, |a_i| \le 10^5$。

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.