human
Write me a Python program to find the optimal order of positions to perform the operation on them given the following:
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 ≤ i ≤ n).

  2. Add a_i to ans.

  3. If b_i ≠ -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 ≤ i ≤ 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.

Input

The first line contains the integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the length of arrays a and b.

The second line contains n integers a_1, a_2, …, a_n (−10^6 ≤ a_i ≤ 10^6).

The third line contains n integers b_1, b_2, …, b_n (1 ≤ b_i ≤ n or b_i = -1).

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

Output

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, …, p_n (1 ≤ p_i ≤ 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.

Examples

Input

3
1 2 3
2 3 -1

Output

10
1 2 3 

Input

2
-1 100
2 -1

Output

99
2 1 

Input

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

Output

-9
3 5 6 1 9 4 10 7 8 2
Solve the task in PYTHON.
assistant
#Here is Python program to find the optimal order of positions to perform the operation on them.

from __future__ import division, print_function
from future_builtins import *
_interactive = False

def main():
    n = int(input())
    a = input_as_list()
    par = input_with_offset(-1)

    deg = [0]*n
    for p in par:
        if p >= 0:
            deg[p] += 1

    leaves = [v for v in range(n) if deg[v]==0]
    visited = [0]*n
    queue = leaves[:]
    negatives = []
    ans = 0
    order = []
    for ver in queue:
        visited[ver] += 1
        if visited[ver] >= deg[ver]:
            if a[ver] > 0:
                ans += a[ver]
                order += [ver]
            else:
                negatives += [ver]
            p = par[ver]
            if p >= 0:
                queue += [p]
                if a[ver] > 0:
                   a[p] += a[ver]

    for ver in negatives:
        ans += a[ver]

    order += negatives[::-1]
    order = [(v+1) for v in order]

    print(ans)
    print(*order)

# Constants
INF = float('inf')
MOD = 10**9+7
from string import ascii_lowercase
alphabets = ascii_lowercase

# Python3 equivalent names
import os, sys, itertools
if sys.version_info[0] < 3:
    input = raw_input
    range = xrange

    filter = itertools.ifilter
    map = itertools.imap
    zip = itertools.izip

# print-flush in interactive problems
if _interactive:
    flush = sys.stdout.flush
    def printf(*args, **kwargs):
        print(*args, **kwargs)
        flush()

# Debug print, only works on local machine
LOCAL = "LOCAL_" in os.environ
debug_print = (print) if LOCAL else (lambda *x, **y: None)

# Fast IO
if (not LOCAL) and (not _interactive):
    from io import BytesIO
    from atexit import register
    sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
    sys.stdout = BytesIO()
    register(lambda: os.write(1, sys.stdout.getvalue()))
    input = lambda: sys.stdin.readline().rstrip('\r\n')

# Some utility functions(Input, N-dimensional lists, ...)
def input_as_list():
    return [int(x) for x in input().split()]

def input_with_offset(o):
    return [int(x)+o for x in input().split()]

def...