307643: CF1388D. Captain Flint and Treasure

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Captain Flint and Treasure

题意翻译

## 题目描述: 这里有长度为 $n$ 的两个数组 $a$ 和 $b$ 。一开始,答案 $ans$ 等于 $0$ 。现在我们定义如下操作: - 选择一个位置 $i$ $(1\leq i \leq n)$ - 让 $ans$ 增大 $a_i$ - 如果 $b_i \neq -1 $ 就将 $a_{b_i}$ 增大 $a_i$ 如果每一个 $i$ $(1\leq i \leq n)$ 只能选一次,请问 $ans$ 最大是多少? 并给出 $ans$ 最大时选择 $i$ 的顺序。 ## 输入格式: 第一行包括一个整数 $n$ $(1\leq n\leq 2⋅10^5)$ ,表示数组 $a$ 和 $b$ 的长度 第二行包括 $n$ 个整数表示数组 $a$ $(-10^6 \leq a_i\leq 10^6)$ 第三行包括 $n$ 个整数表示数组 $b$ $(1\leq b_i\leq n $ 或者 $ b_i = -1)$ 保证对于每个 $1\le i\le n$,在进行有限次 $i\rightarrow b_i$ 的迭代后 $i$ 一定为 $-1$。 ## 输出格式: 第一行输出 $ans$ 第二行输出第 $i$ 位是第几个选择的

题目描述

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 $ a $ and $ b $ of length $ n $ . Initially, an $ ans $ is equal to $ 0 $ and the following operation is defined: 1. Choose position $ i $ ( $ 1 \le i \le n $ ); 2. Add $ a_i $ to $ ans $ ; 3. If $ b_i \neq -1 $ then add $ a_i $ to $ a_{b_i} $ . What is the maximum $ ans $ you can get by performing the operation on each $ i $ ( $ 1 \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.

输入输出格式

输入格式


The first line contains the integer $ n $ ( $ 1 \le n \le 2 \cdot 10^5 $ ) — the length of arrays $ a $ and $ b $ . The second line contains $ n $ integers $ a_1, a_2, \ldots, a_n $ ( $ −10^6 \le a_i \le 10^6 $ ). The third line contains $ n $ integers $ b_1, b_2, \ldots, b_n $ ( $ 1 \le b_i \le n $ or $ b_i = -1 $ ). Additional constraint: it's guaranteed that for any $ i $ ( $ 1 \le i \le n $ ) the sequence $ b_i, b_{b_i}, b_{b_{b_i}}, \ldots $ is not cyclic, in other words it will always end with $ -1 $ .

输出格式


In the first line, print the maximum $ ans $ you can get. In the second line, print the order of operations: $ n $ different integers $ p_1, p_2, \ldots, p_n $ ( $ 1 \le p_i \le n $ ). The $ p_i $ is the position which should be chosen at the $ i $ -th step. If there are multiple orders, print any of them.

输入输出样例

输入样例 #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

Input

题意翻译

## 题目描述: 这里有长度为 $n$ 的两个数组 $a$ 和 $b$ 。一开始,答案 $ans$ 等于 $0$ 。现在我们定义如下操作: - 选择一个位置 $i$ $(1\leq i \leq n)$ - 让 $ans$ 增大 $a_i$ - 如果 $b_i \neq -1 $ 就将 $a_{b_i}$ 增大 $a_i$ 如果每一个 $i$ $(1\leq i \leq n)$ 只能选一次,请问 $ans$ 最大是多少? 并给出 $ans$ 最大时选择 $i$ 的顺序。 ## 输入格式: 第一行包括一个整数 $n$ $(1\leq n\leq 2⋅10^5)$ ,表示数组 $a$ 和 $b$ 的长度 第二行包括 $n$ 个整数表示数组 $a$ $(-10^6 \leq a_i\leq 10^6)$ 第三行包括 $n$ 个整数表示数组 $b$ $(1\leq b_i\leq n $ 或者 $ b_i = -1)$ 保证对于每个 $1\le i\le n$,在进行有限次 $i\rightarrow b_i$ 的迭代后 $i$ 一定为 $-1$。 ## 输出格式: 第一行输出 $ans$ 第二行输出第 $i$ 位是第几个选择的

加入题单

算法标签: