CF1388D.Captain Flint and Treasure

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...

There are two arrays aa and bb of length nn. Initially, an ansans is equal to 00 and the following operation is defined:

  1. Choose position ii (1≤i≤n1 \le i \le n);
  2. Add aia_i to ansans;
  3. If bi≠−1b_i \neq -1 then add aia_i to abia_{b_i}.

What is the maximum ansans you can get by performing the operation on each ii (1≤i≤n1 \le i \le n) exactly once?

Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.

船长芬特(Captain Fint)又参与了一场寻宝行动,但目前只发现了一个奇怪的问题。该问题可能与宝藏的位置有关,也可能无关。因此,船长弗林特(Flint)决定将这个问题的求解工作交给他的船员,并开出了一个荒谬的高额奖励:一天假期。问题本身如下所述……

给定两个长度为 nn 的数组 aa 和 bb。初始时,ansans 的值为 00,并定义如下操作:

  1. 选择一个位置 ii(1≤i≤n1 \le i \le n);
  2. 将 aia_i 加到 ansans 上;
  3. 若 bi≠−1b_i \neq -1,则将 aia_i 加到 abia_{b_i} 上。

通过恰好对每个位置 ii(1≤i≤n1 \le i \le n)执行一次该操作,你能获得的最大 ansans 值是多少?

博格丹大叔(Uncle Bogdan)非常渴望赢得这份奖励,因此他请求你帮忙找出执行操作的最优位置顺序。

输入格式

The first line contains the integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the length of arrays aa and bb.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (−106≤ai≤106−10^6 \le a_i \le 10^6).

The third line contains nn integers b1,b2,…,bnb_1, b_2, \ldots, b_n (1≤bi≤n1 \le b_i \le n or bi=−1b_i = -1).

Additional constraint: it's guaranteed that for any ii (1≤i≤n1 \le i \le n) the sequence bi,bbi,bbbi,…b_i, b_{b_i}, b_{b_{b_i}}, \ldots is not cyclic, in other words it will always end with −1-1.

第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 数组 aa 和 bb 的长度。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(−106≤ai≤106−10^6 \le a_i \le 10^6)。

第三行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \ldots, b_n(1≤bi≤n1 \le b_i \le n 或 bi=−1b_i = -1)。

附加约束:保证对任意 ii(1≤i≤n1 \le i \le n),序列 bi,bbi,bbbi,…b_i, b_{b_i}, b_{b_{b_i}}, \ldots 不构成环,即该序列总会以 −1-1 结束。

输出格式

In the first line, print the maximum ansans you can get.

In the second line, print the order of operations: nn different integers p1,p2,…,pnp_1, p_2, \ldots, p_n (1≤pi≤n1 \le p_i \le n). The pip_i is the position which should be chosen at the ii-th step. If there are multiple orders, print any of them.

第一行输出你能得到的最大值 ansans。

第二行输出操作顺序:nn 个互不相同的整数 p1,p2,…,pnp_1, p_2, \ldots, p_n(满足 1≤pi≤n1 \le p_i \le n)。其中 pip_i 表示第 ii 步应选择的位置。若存在多种可行顺序,输出任意一种即可。

输入输出样例

  • 输入#1

    3
    1 2 3
    2 3 -1

    输出#1

    10
    1 2 3
  • 输入#2

    2
    -1 100
    2 -1

    输出#2

    99
    2 1
  • 输入#3

    10
    -10 -1 2 2 5 -2 -3 -4 2 -6
    -1 -1 2 2 -1 5 5 7 7 9

    输出#3

    -9
    3 5 6 1 9 4 10 7 8 2

输入解题思路,AI测评打分。不知道怎么写?

首页