#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int) x.size())
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x, y) cerr << #x << " is " << x << ", " << #y << " is " << y << endl;
typedef long long lint;
typedef pair<lint, lint> ii;
int p[200005];
int vis[200005];
vector<int> adj[200005];
map<int, lint> dp[200005];
lint cost[200005];
lint original[200005];
void dfs(int u){
for(int v : adj[u]){
dfs(v);
if(sz(dp[u]) < sz(dp[v])) swap(dp[u], dp[v]);
map<int, lint> &cur = dp[u];
for(ii x : dp[v]){
if(cur.count(x.first)) cur[x.first] += x.second;
else cur[x.first] = x.second;
}
}
lint O = original[u]; lint C = cost[u];
map<int, lint> &cur = dp[u];
if(cur.count(O)) cur[O] += C ;
else cur[O] = C;
while(true){
auto it = cur.upper_bound(O);
if(it == cur.end()) break;
if(it->second > C){
it->second -= C;
break;
}
else{
C -= it->second;
cur.erase(it);
}
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
lint ans = 0;
int n; cin >> n;
for(int i = 1;i <= n;i++){
cin >> p[i] >> original[i] >> cost[i];
ans += cost[i];
}
for(int s = 1;s <= n;s++){
int u = s;
int cycle = -1;
while(true){
if(vis[u] != 0){
if(vis[u] == s) cycle = u;
break;
}
vis[u] = s;
u = p[u];
}
if(cycle == -1) continue;
vector<ii> nodes = {ii(-1,0)};
u = cycle;
while(true){
nodes.push_back(ii(original[u], u));
u = p[u];
if(u == cycle) break;
}
sort(all(nodes));
reverse(all(nodes));
for(int i = 0;i < sz(nodes)-1;i++){
p[nodes[i].second] = nodes[i+1].second;
}
}
for(int i = 1;i <= n;i++) adj[p[i]].push_back(i);
for(int i = 0;i <= n;i++) original[i] *= -1;
dfs(0);
for(ii x : dp[0]) ans -= x.second;
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14412 KB |
Output is correct |
3 |
Correct |
9 ms |
14320 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
16 ms |
15564 KB |
Output is correct |
6 |
Correct |
14 ms |
15076 KB |
Output is correct |
7 |
Correct |
12 ms |
14924 KB |
Output is correct |
8 |
Correct |
15 ms |
15580 KB |
Output is correct |
9 |
Correct |
13 ms |
15052 KB |
Output is correct |
10 |
Correct |
12 ms |
14968 KB |
Output is correct |
11 |
Correct |
12 ms |
14796 KB |
Output is correct |
12 |
Correct |
12 ms |
15224 KB |
Output is correct |
13 |
Correct |
11 ms |
15320 KB |
Output is correct |
14 |
Correct |
12 ms |
15048 KB |
Output is correct |
15 |
Correct |
12 ms |
14916 KB |
Output is correct |
16 |
Correct |
16 ms |
15948 KB |
Output is correct |
17 |
Correct |
14 ms |
15364 KB |
Output is correct |
18 |
Correct |
11 ms |
14668 KB |
Output is correct |
19 |
Correct |
13 ms |
15240 KB |
Output is correct |
20 |
Correct |
12 ms |
15092 KB |
Output is correct |
21 |
Correct |
11 ms |
15076 KB |
Output is correct |
22 |
Correct |
13 ms |
15180 KB |
Output is correct |
23 |
Correct |
12 ms |
14816 KB |
Output is correct |
24 |
Correct |
13 ms |
15180 KB |
Output is correct |
25 |
Correct |
12 ms |
15052 KB |
Output is correct |
26 |
Correct |
11 ms |
15308 KB |
Output is correct |
27 |
Correct |
13 ms |
15308 KB |
Output is correct |
28 |
Correct |
13 ms |
15372 KB |
Output is correct |
29 |
Correct |
12 ms |
15436 KB |
Output is correct |
30 |
Correct |
13 ms |
15464 KB |
Output is correct |
31 |
Correct |
13 ms |
15436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
15528 KB |
Output is correct |
2 |
Correct |
433 ms |
76652 KB |
Output is correct |
3 |
Correct |
321 ms |
51816 KB |
Output is correct |
4 |
Correct |
437 ms |
73688 KB |
Output is correct |
5 |
Correct |
320 ms |
51908 KB |
Output is correct |
6 |
Correct |
207 ms |
32960 KB |
Output is correct |
7 |
Correct |
196 ms |
28484 KB |
Output is correct |
8 |
Correct |
174 ms |
50516 KB |
Output is correct |
9 |
Correct |
155 ms |
50372 KB |
Output is correct |
10 |
Correct |
109 ms |
50424 KB |
Output is correct |
11 |
Correct |
169 ms |
38084 KB |
Output is correct |
12 |
Correct |
150 ms |
37828 KB |
Output is correct |
13 |
Correct |
417 ms |
101620 KB |
Output is correct |
14 |
Correct |
288 ms |
66100 KB |
Output is correct |
15 |
Correct |
100 ms |
28480 KB |
Output is correct |
16 |
Correct |
288 ms |
47940 KB |
Output is correct |
17 |
Correct |
145 ms |
41064 KB |
Output is correct |
18 |
Correct |
104 ms |
40900 KB |
Output is correct |
19 |
Correct |
354 ms |
44896 KB |
Output is correct |
20 |
Correct |
118 ms |
32448 KB |
Output is correct |
21 |
Correct |
337 ms |
48480 KB |
Output is correct |
22 |
Correct |
134 ms |
41404 KB |
Output is correct |
23 |
Correct |
114 ms |
50444 KB |
Output is correct |
24 |
Correct |
238 ms |
50332 KB |
Output is correct |
25 |
Correct |
202 ms |
55492 KB |
Output is correct |
26 |
Correct |
190 ms |
58208 KB |
Output is correct |
27 |
Correct |
234 ms |
56520 KB |
Output is correct |
28 |
Correct |
237 ms |
56600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14412 KB |
Output is correct |
2 |
Correct |
9 ms |
14412 KB |
Output is correct |
3 |
Correct |
9 ms |
14320 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
16 ms |
15564 KB |
Output is correct |
6 |
Correct |
14 ms |
15076 KB |
Output is correct |
7 |
Correct |
12 ms |
14924 KB |
Output is correct |
8 |
Correct |
15 ms |
15580 KB |
Output is correct |
9 |
Correct |
13 ms |
15052 KB |
Output is correct |
10 |
Correct |
12 ms |
14968 KB |
Output is correct |
11 |
Correct |
12 ms |
14796 KB |
Output is correct |
12 |
Correct |
12 ms |
15224 KB |
Output is correct |
13 |
Correct |
11 ms |
15320 KB |
Output is correct |
14 |
Correct |
12 ms |
15048 KB |
Output is correct |
15 |
Correct |
12 ms |
14916 KB |
Output is correct |
16 |
Correct |
16 ms |
15948 KB |
Output is correct |
17 |
Correct |
14 ms |
15364 KB |
Output is correct |
18 |
Correct |
11 ms |
14668 KB |
Output is correct |
19 |
Correct |
13 ms |
15240 KB |
Output is correct |
20 |
Correct |
12 ms |
15092 KB |
Output is correct |
21 |
Correct |
11 ms |
15076 KB |
Output is correct |
22 |
Correct |
13 ms |
15180 KB |
Output is correct |
23 |
Correct |
12 ms |
14816 KB |
Output is correct |
24 |
Correct |
13 ms |
15180 KB |
Output is correct |
25 |
Correct |
12 ms |
15052 KB |
Output is correct |
26 |
Correct |
11 ms |
15308 KB |
Output is correct |
27 |
Correct |
13 ms |
15308 KB |
Output is correct |
28 |
Correct |
13 ms |
15372 KB |
Output is correct |
29 |
Correct |
12 ms |
15436 KB |
Output is correct |
30 |
Correct |
13 ms |
15464 KB |
Output is correct |
31 |
Correct |
13 ms |
15436 KB |
Output is correct |
32 |
Correct |
16 ms |
15528 KB |
Output is correct |
33 |
Correct |
433 ms |
76652 KB |
Output is correct |
34 |
Correct |
321 ms |
51816 KB |
Output is correct |
35 |
Correct |
437 ms |
73688 KB |
Output is correct |
36 |
Correct |
320 ms |
51908 KB |
Output is correct |
37 |
Correct |
207 ms |
32960 KB |
Output is correct |
38 |
Correct |
196 ms |
28484 KB |
Output is correct |
39 |
Correct |
174 ms |
50516 KB |
Output is correct |
40 |
Correct |
155 ms |
50372 KB |
Output is correct |
41 |
Correct |
109 ms |
50424 KB |
Output is correct |
42 |
Correct |
169 ms |
38084 KB |
Output is correct |
43 |
Correct |
150 ms |
37828 KB |
Output is correct |
44 |
Correct |
417 ms |
101620 KB |
Output is correct |
45 |
Correct |
288 ms |
66100 KB |
Output is correct |
46 |
Correct |
100 ms |
28480 KB |
Output is correct |
47 |
Correct |
288 ms |
47940 KB |
Output is correct |
48 |
Correct |
145 ms |
41064 KB |
Output is correct |
49 |
Correct |
104 ms |
40900 KB |
Output is correct |
50 |
Correct |
354 ms |
44896 KB |
Output is correct |
51 |
Correct |
118 ms |
32448 KB |
Output is correct |
52 |
Correct |
337 ms |
48480 KB |
Output is correct |
53 |
Correct |
134 ms |
41404 KB |
Output is correct |
54 |
Correct |
114 ms |
50444 KB |
Output is correct |
55 |
Correct |
238 ms |
50332 KB |
Output is correct |
56 |
Correct |
202 ms |
55492 KB |
Output is correct |
57 |
Correct |
190 ms |
58208 KB |
Output is correct |
58 |
Correct |
234 ms |
56520 KB |
Output is correct |
59 |
Correct |
237 ms |
56600 KB |
Output is correct |
60 |
Incorrect |
9 ms |
14396 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |