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
Using python3 can you solve the prior task?
assistant
#Here is Python program to find the optimal order of positions to perform the operation on them.

import os
import sys
from io import BytesIO, IOBase
 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#######################################
from collections import deque
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
adj=[0]*(n+1)
ind=[0]*(n+1)
for i in range(n):
    if b[i]!=-1:
        adj[i+1]=b[i]
        ind[b[i]]+=1
q=deque()
for i in range(1,n+1):
    if not ind[i]:
       ...