-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtres.cpp
76 lines (57 loc) · 1.24 KB
/
tres.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using vpi = vector<pi>;
using vb = vector<bool>;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
vi a(n);
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
vector<set<ll>> ed(n);
vi b(n);
vb chose(n);
for (ll i = 0; i < n; i++) {
cin >> b[i];
if (b[i] == -1) {
continue;
}
b[i]--;
ed[b[i]].insert(i);
}
vb vis(n);
vi res(n);
ll ans = 0;
vi path;
vi path2;
function<ll(ll)> dfs = [&](ll v) {
if (vis[v]) return res[v];
vis[v] = true;
ll sum = a[v];
for (ll ch : ed[v]) {
sum += max(0LL, dfs(ch));
}
res[v] = sum;
if (res[v] >= 0)
path.push_back(v);
else
path2.push_back(v);
ans += res[v];
return sum;
};
for (ll i = 0; i < n; i++) {
if (!vis[i]) dfs(i);
}
cout << ans << endl;
for (ll i : path) cout << i + 1 << " ";
reverse(path2.begin(), path2.end());
for (ll i : path2) cout << i + 1 << " ";
cout << endl;
return 0;
}