编程介的小学生 2017-08-24 10:34 采纳率: 20.5%
浏览 807
已采纳

Maximize Game Time

Jack has got a new game of Super Mario recently. There are n castles in the game, and Princess Peach has been kidnapped by the King Koopa and enjailed in castle n - 1 (0-based). The King Koopa has Son Koopas, and the Son Koopas has Grandson Koopas, and the Grandson Koopas has Great-Grandson Koopas... The whole Koopa family is taking possession of these n castles, and there is exactly one Koopa in each castle and the King Koopa is in castle n - 1. Super Mario's task is to conquer these castles and save Princess Peach.

Each time, Super Mario can freely choose an unconquered castle and go in to fight with the Koopa. Super Mario has time Ti to conquer castle i and beat the Koopa in it. If Super Mario fails to conquer the castle or fails to do that within the time limit, the game is over. Otherwise, the defeated Koopa will go to seek the Father Koopa for help. If any Koopa has two or more Son Koopas beaten by Super Mario, he will get angry. Super Mario has to go to the castle of the angry Koopa and beat him immediately; otherwise the Princess Peach will be killed. If castle n - 1 is conquered and King Koopa is defeated, the game will also end, but with triumph and Princess Peach saved.

Jack loves this game a lot, but he has to prepare for the upcoming ACM/ICPC. So he guarantees to himself that he only plays the game once a month. However, Jack really loves playing game and hates programming, he wants to maximize the time he can spend on the game without breaking his promise.

Input
The input consists of multiple test cases.

Each test case starts with a number N (0 < N <= 1000) in a single line, the number of castles and Koopas. There are two lines following and each contains N numbers. The first line is N numbers, Ti (0 < Ti <= 100) for Super Mario to conquer castle i. The second line is N numbers of Pi (-1 <= Pi < N), the castle (0-based) in which the Parent of each Koopa resides. -1 means that there is no parent for him, and the last number will always be -1, since King Koopa has no Parent Koopa.

The input ends with 0(zero), which should not be processed.

Output
Output the maximal time Jack can play in a single line for each test case.

Sample Input
5
1 2 3 4 5
4 4 4 4 -1
5
2 2 2 2 2
1 2 3 4 -1
9
1 1 1 1 1 1 1 1 1
6 6 6 7 7 7 8 8 -1
0

Sample Output
12
10
8

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-08-27 16:42
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效