#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(1e9+1,0)};
u = cycle;
while(true){
nodes.push_back(ii(original[u], u));
u = p[u];
if(u == cycle) break;
}
sort(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 |
14412 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
16 ms |
15540 KB |
Output is correct |
6 |
Correct |
14 ms |
15144 KB |
Output is correct |
7 |
Correct |
13 ms |
14948 KB |
Output is correct |
8 |
Correct |
16 ms |
15604 KB |
Output is correct |
9 |
Correct |
14 ms |
15144 KB |
Output is correct |
10 |
Correct |
13 ms |
14924 KB |
Output is correct |
11 |
Correct |
12 ms |
14672 KB |
Output is correct |
12 |
Correct |
13 ms |
15308 KB |
Output is correct |
13 |
Correct |
11 ms |
15288 KB |
Output is correct |
14 |
Correct |
13 ms |
14924 KB |
Output is correct |
15 |
Correct |
12 ms |
14924 KB |
Output is correct |
16 |
Correct |
16 ms |
15948 KB |
Output is correct |
17 |
Correct |
14 ms |
15308 KB |
Output is correct |
18 |
Correct |
11 ms |
14668 KB |
Output is correct |
19 |
Correct |
13 ms |
15184 KB |
Output is correct |
20 |
Correct |
12 ms |
15096 KB |
Output is correct |
21 |
Correct |
12 ms |
15076 KB |
Output is correct |
22 |
Correct |
12 ms |
15092 KB |
Output is correct |
23 |
Correct |
11 ms |
14796 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 |
15296 KB |
Output is correct |
27 |
Correct |
13 ms |
15308 KB |
Output is correct |
28 |
Correct |
13 ms |
15308 KB |
Output is correct |
29 |
Correct |
12 ms |
15436 KB |
Output is correct |
30 |
Correct |
13 ms |
15432 KB |
Output is correct |
31 |
Correct |
13 ms |
15452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
15564 KB |
Output is correct |
2 |
Correct |
435 ms |
76660 KB |
Output is correct |
3 |
Correct |
314 ms |
51780 KB |
Output is correct |
4 |
Correct |
431 ms |
73744 KB |
Output is correct |
5 |
Correct |
318 ms |
51832 KB |
Output is correct |
6 |
Correct |
207 ms |
32872 KB |
Output is correct |
7 |
Correct |
191 ms |
28484 KB |
Output is correct |
8 |
Correct |
174 ms |
50368 KB |
Output is correct |
9 |
Correct |
158 ms |
50372 KB |
Output is correct |
10 |
Correct |
117 ms |
50484 KB |
Output is correct |
11 |
Correct |
171 ms |
38212 KB |
Output is correct |
12 |
Correct |
155 ms |
37828 KB |
Output is correct |
13 |
Correct |
424 ms |
101808 KB |
Output is correct |
14 |
Correct |
286 ms |
66092 KB |
Output is correct |
15 |
Correct |
99 ms |
28424 KB |
Output is correct |
16 |
Correct |
300 ms |
47908 KB |
Output is correct |
17 |
Correct |
147 ms |
41044 KB |
Output is correct |
18 |
Correct |
106 ms |
40948 KB |
Output is correct |
19 |
Correct |
353 ms |
44988 KB |
Output is correct |
20 |
Correct |
118 ms |
32576 KB |
Output is correct |
21 |
Correct |
316 ms |
48572 KB |
Output is correct |
22 |
Correct |
136 ms |
41404 KB |
Output is correct |
23 |
Correct |
119 ms |
50488 KB |
Output is correct |
24 |
Correct |
236 ms |
50372 KB |
Output is correct |
25 |
Correct |
206 ms |
55632 KB |
Output is correct |
26 |
Correct |
194 ms |
58228 KB |
Output is correct |
27 |
Correct |
238 ms |
56516 KB |
Output is correct |
28 |
Correct |
234 ms |
56564 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 |
14412 KB |
Output is correct |
4 |
Correct |
9 ms |
14412 KB |
Output is correct |
5 |
Correct |
16 ms |
15540 KB |
Output is correct |
6 |
Correct |
14 ms |
15144 KB |
Output is correct |
7 |
Correct |
13 ms |
14948 KB |
Output is correct |
8 |
Correct |
16 ms |
15604 KB |
Output is correct |
9 |
Correct |
14 ms |
15144 KB |
Output is correct |
10 |
Correct |
13 ms |
14924 KB |
Output is correct |
11 |
Correct |
12 ms |
14672 KB |
Output is correct |
12 |
Correct |
13 ms |
15308 KB |
Output is correct |
13 |
Correct |
11 ms |
15288 KB |
Output is correct |
14 |
Correct |
13 ms |
14924 KB |
Output is correct |
15 |
Correct |
12 ms |
14924 KB |
Output is correct |
16 |
Correct |
16 ms |
15948 KB |
Output is correct |
17 |
Correct |
14 ms |
15308 KB |
Output is correct |
18 |
Correct |
11 ms |
14668 KB |
Output is correct |
19 |
Correct |
13 ms |
15184 KB |
Output is correct |
20 |
Correct |
12 ms |
15096 KB |
Output is correct |
21 |
Correct |
12 ms |
15076 KB |
Output is correct |
22 |
Correct |
12 ms |
15092 KB |
Output is correct |
23 |
Correct |
11 ms |
14796 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 |
15296 KB |
Output is correct |
27 |
Correct |
13 ms |
15308 KB |
Output is correct |
28 |
Correct |
13 ms |
15308 KB |
Output is correct |
29 |
Correct |
12 ms |
15436 KB |
Output is correct |
30 |
Correct |
13 ms |
15432 KB |
Output is correct |
31 |
Correct |
13 ms |
15452 KB |
Output is correct |
32 |
Correct |
15 ms |
15564 KB |
Output is correct |
33 |
Correct |
435 ms |
76660 KB |
Output is correct |
34 |
Correct |
314 ms |
51780 KB |
Output is correct |
35 |
Correct |
431 ms |
73744 KB |
Output is correct |
36 |
Correct |
318 ms |
51832 KB |
Output is correct |
37 |
Correct |
207 ms |
32872 KB |
Output is correct |
38 |
Correct |
191 ms |
28484 KB |
Output is correct |
39 |
Correct |
174 ms |
50368 KB |
Output is correct |
40 |
Correct |
158 ms |
50372 KB |
Output is correct |
41 |
Correct |
117 ms |
50484 KB |
Output is correct |
42 |
Correct |
171 ms |
38212 KB |
Output is correct |
43 |
Correct |
155 ms |
37828 KB |
Output is correct |
44 |
Correct |
424 ms |
101808 KB |
Output is correct |
45 |
Correct |
286 ms |
66092 KB |
Output is correct |
46 |
Correct |
99 ms |
28424 KB |
Output is correct |
47 |
Correct |
300 ms |
47908 KB |
Output is correct |
48 |
Correct |
147 ms |
41044 KB |
Output is correct |
49 |
Correct |
106 ms |
40948 KB |
Output is correct |
50 |
Correct |
353 ms |
44988 KB |
Output is correct |
51 |
Correct |
118 ms |
32576 KB |
Output is correct |
52 |
Correct |
316 ms |
48572 KB |
Output is correct |
53 |
Correct |
136 ms |
41404 KB |
Output is correct |
54 |
Correct |
119 ms |
50488 KB |
Output is correct |
55 |
Correct |
236 ms |
50372 KB |
Output is correct |
56 |
Correct |
206 ms |
55632 KB |
Output is correct |
57 |
Correct |
194 ms |
58228 KB |
Output is correct |
58 |
Correct |
238 ms |
56516 KB |
Output is correct |
59 |
Correct |
234 ms |
56564 KB |
Output is correct |
60 |
Correct |
9 ms |
14420 KB |
Output is correct |
61 |
Correct |
10 ms |
14344 KB |
Output is correct |
62 |
Correct |
9 ms |
14420 KB |
Output is correct |
63 |
Correct |
9 ms |
14412 KB |
Output is correct |
64 |
Incorrect |
410 ms |
66924 KB |
Output isn't correct |
65 |
Halted |
0 ms |
0 KB |
- |