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:   Choose position $i$ ($1 \le i \le n$);  Add $a_i$ to $ans$;  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.


-----Input-----

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$.


-----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, \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.


-----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

Difficulty: interview

Starter code:

Solutions:
n=int(input())
val=list(map(int,input().split()))
b=list(map(int,input().split()))
neig=[0]*n
for i in range(n):
    neig[i]=[0]
inedges=[0]*n
for i in range(n):
    if b[i]!=-1:
        neig[i][0]+=1
        neig[i].append(b[i]-1)
        inedges[b[i]-1]+=1
ans=0
beg=[]
en=[]
tod=[]
for i in range(n):
    if inedges[i]==0:
        tod.append(i)
while len(tod)>0:
    x=tod.pop()
    ans+=val[x]
    if val[x]>0:
        beg.append(x+1)
    else:
        en.append(x+1)
    if neig[x][0]==1:
        inedges[neig[x][1]]-=1
        if inedges[neig[x][1]]==0:
            tod.append(neig[x][1])
        if val[x]>0:
            val[neig[x][1]]+=val[x]
print(ans)
print(*beg,end=" ")
en.reverse()
print(*en)

from sys import stdin
from collections import deque

n = int(stdin.readline())
a = list(map(int,stdin.readline().split()))
p = list(map(int,stdin.readline().split()))

chnum = [0] * n
for i in range(n):
    if p[i]-1 >= 0:
        chnum[p[i]-1] += 1

q = deque([])
for i in range(n):
    if chnum[i] == 0:
        q.append(i)

ans = 0
alis = []
end = []

while len(q) > 0:

    v = q.popleft()
    ans += a[v]

    if a[v] >= 0:
        alis.append(v+1)
        if p[v]-1 >= 0:
            chnum[p[v]-1] -= 1
            a[p[v]-1] += a[v]
            if chnum[p[v]-1] == 0:
                q.append(p[v]-1)
    else:
        end.append(v+1)
        if p[v]-1 >= 0:
            chnum[p[v]-1] -= 1
            if chnum[p[v]-1] == 0:
                q.append(p[v]-1)

end.reverse()
for i in end:
    alis.append(i)

print (ans)
print (*alis)

import sys
input = sys.stdin.readline


def topological_sort(graph) -> list:
    n = len(graph)
    degree = [0] * n
    for g in graph:
        for next_pos in g:
            degree[next_pos] += 1
    
    ans = [i for i in range(n) if degree[i] == 0]

    stack = ans[:]
    while stack:
        pos = stack.pop()
        for next_pos in graph[pos]:
            degree[next_pos] -= 1
            if degree[next_pos] == 0:
                stack.append(next_pos)
                ans.append(next_pos)
    return ans



n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

graph = [[] for i in range(n)]
inv_graph = [[] for i in range(n)]
for v, nxt_v in enumerate(b):
    if nxt_v == -1:
        continue
    nxt_v -= 1
    graph[v].append(nxt_v)
    inv_graph[nxt_v].append(v)

ans = 0
res = []
used = [False] * n

tp_sorted = topological_sort(graph)
for v in tp_sorted:
    if used[v]:
        continue
    if a[v] >= 0:
        ans += a[v]
        for nxt_v in graph[v]:
            a[nxt_v] += a[v]
        used[v] = True
        res.append(v + 1)

tp_sorted = topological_sort(inv_graph)
for v in tp_sorted:
    if used[v]:
        continue
    ans += a[v]
    used[v] = True
    res.append(v + 1)

print(ans)
print(*res)

n = int(input())

a = list(map(int, input().split()))
b = list(int(x)-1 for x in input().split())

indeg = [0] * n
for x in b:
    if x >= 0:
        indeg[x] += 1
#print(*indeg)
q = [i for i in range(n) if indeg[i] == 0]
ans = 0
res = []
end = []
while q:
    top = q.pop()
    #print("yo", top)
    if a[top] < 0:
        end.append(top)
    else:
        res.append(top)    
        ans += a[top]
        if b[top] >= 0:
            a[b[top]] += a[top]
    if b[top] >= 0:
        nei = b[top]
        indeg[nei] -= 1
        if indeg[nei] == 0:
            q.append(nei)

for i in end[::-1]:
    ans += a[i]
    if b[i] >= 0:
        a[b[i]] += a[i]
    res.append(i)

print(ans)        
print(*[x+1 for x in res])