QOJ.ac

QOJ

时间限制: 1 s 内存限制: 128 MB 总分: 100

#23211. 卡牌游戏

统计

지훈 最近喜欢上了一种单人卡牌游戏。

游戏中的每张牌上都写有一个正整数,可以有多张牌写着相同的数。

开始游戏时,先将偶数张牌随机洗牌,再分成数量相等的两堆,一堆放在左边,另一堆放在右边。

另外准备一个空桶。

接下来,比较两堆最上面的牌来进行游戏。

游戏规则如下。从现在起,将左堆最上面的牌称为左牌,右堆最上面的牌称为右牌。

(1)任何时候都可以只将左牌扔进桶里,也可以将左牌和右牌一起扔进桶里。这两种操作都不得分。

(2)如果右牌上的数小于左牌上的数,也可以只将右牌扔进桶里。

只丢弃右牌时,可以获得与右牌上的数相等的分数。

(3)按照规则(1)和(2)进行游戏,当任意一堆的牌用完时,游戏结束,

此前获得的分数之和即为最终得分。

下面的例子中,开始时两堆各有三张牌。

problem_23211_589cbced26dab65280d2d7c91626ee13.png

此时,最初右牌上的 2 小于左牌上的 3,

因此,可以按照规则(1)只丢弃左牌,或者同时丢弃左牌和右牌,

也可以按照规则(2)只丢弃右牌。

如果选择只丢弃右牌,就得到 2 分,并丢弃右牌 2。

此时右堆最上面的牌变为 4,大于左牌 3,

因此,可以按照规则(1)只丢弃左牌,或者同时丢弃左牌和右牌。

如果选择同时丢弃两张牌,则左牌变为 2,右牌变为 1。

此时,又可以根据规则(1)和(2)从三种操作中选择一种,

若选择只丢弃左牌,则左牌变为 5,右牌仍为 1。

此时,同样可以根据规则(1)和(2)从三种操作中选择一种,

若选择只丢弃右牌,就得到 1 分,并丢弃右牌 1。

现在右堆已经没有牌,因此根据规则(3),游戏结束,最终得分为 $2+1=3$。

给定两堆牌,请编写程序,输出通过游戏能够获得的最大最终得分。

上例中的最大最终得分为 7。

输入格式

标准输入的第一行给出一个正整数 $N$($1\le N\le2{,}000$),表示一堆牌的数量。

下一行按牌的顺序给出左堆牌上的 $N$ 个整数 $A$($1\le A\le2{,}000$)。

再下一行按牌的顺序给出右堆牌上的 $N$ 个整数 $B$($1\le B\le2{,}000$)。

每堆中可以有两张或更多张牌写着相同的数。

输出格式

向标准输出输出能够获得的最大最终得分。

样例

输入格式 1

3
3 2 5
2 4 1

输出格式 1

7

输入格式 2

4
1 2 3 4
4 1 2 3

输出格式 2

6

子任务

31 分:$N\le10$。

33 分:$N\le25$。

36 分:没有额外限制。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.