#include<bits/stdc++.h>
using ll = long long;
using namespace std;
#define pll pair <ll,ll>
#define fi first
#define se second
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL << (i))
#define MP make_pair
const ll MAXN = 2e5+100;
const ll INF = 1e18;
ll A[MAXN],H[MAXN],C[MAXN];
ll in[MAXN];
vector <ll> g[MAXN];
map <ll,ll> mp[MAXN];
vector <pll> root[MAXN];
ll re[MAXN];
ll n;
void dfs(ll u){
for (auto v:g[u]){
// cout<<u<<' '<<v<<endl;
dfs(v);
}
for (auto v:g[u]){
if (sz(mp[v]) > sz(mp[u]))swap(mp[u],mp[v]);
}
for (auto v:g[u]){
for (auto x:mp[v]){
mp[u][x.fi]+=x.se;
}
}
root[u].push_back(MP(H[u],C[u]));
map <ll,ll> tmp1;
for (auto x:root[u])tmp1[x.fi] += x.se;
for (auto x:tmp1){
mp[u][-INF]+=x.se;
auto tmp = mp[u].upper_bound(x.fi);
ll sum = 0;
while (sum < x.se){
tmp = prev(tmp);
sum += (*tmp).se;
}
(*tmp).se -= x.se - (sum - (*tmp).se);
for (auto it = next(tmp);it != mp[u].end() && (*it).fi <= x.fi;it = mp[u].erase(it)){}
mp[u][x.fi+1] += x.se;
}
// cout<<"MAP "<<u<<endl;
// for (auto x:mp[u])cout<<x.fi<<' '<<x.se<<endl;
// cout<<endl;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(nullptr);
cin>>n;
for (ll i = 1;i <= n;i ++){
cin>>A[i]>>H[i]>>C[i];
re[i] = i;
}
for (ll i = 1;i <= n;i ++){
// cout<<i<<endl;
if (!in[i]){
ll cur = i;
vector <ll> all;
while (!in[cur]){
all.push_back(cur);
in[cur] = 1;
cur = re[A[cur]];
}
if (in[cur] == 1){
while (all.back() != cur){
root[cur].push_back({H[all.back()],C[all.back()]});
re[all.back()] = cur;
all.pop_back();
}
in[all.back()] = 2;
for (ll j = sz(all) - 1;j >= 1;j --){
g[re[all[j]]].push_back(all[j-1]);
}
}
else{
for (ll j = sz(all) - 1;j >= 1;j --){
g[re[all[j]]].push_back(all[j-1]);
}
g[re[cur]].push_back(all.back());
}
for (auto x:all)in[x]++;
}
}
// for (ll i = 1;i <= n;i ++){
// for (auto v:g[i])cout<<i<<' '<<v<<endl;
// }
ll ans = 0;
for (ll i = 1;i <= n;i ++){
// cout<<i<<endl;
if (in[i] == 3){
dfs(i);
ans += (*mp[i].begin()).se;
}
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
24924 KB |
Output is correct |
2 |
Correct |
3 ms |
24924 KB |
Output is correct |
3 |
Correct |
3 ms |
25044 KB |
Output is correct |
4 |
Correct |
3 ms |
24924 KB |
Output is correct |
5 |
Correct |
7 ms |
26460 KB |
Output is correct |
6 |
Correct |
7 ms |
25980 KB |
Output is correct |
7 |
Correct |
6 ms |
25948 KB |
Output is correct |
8 |
Correct |
11 ms |
26540 KB |
Output is correct |
9 |
Correct |
11 ms |
26204 KB |
Output is correct |
10 |
Correct |
7 ms |
25948 KB |
Output is correct |
11 |
Correct |
7 ms |
25692 KB |
Output is correct |
12 |
Correct |
6 ms |
26372 KB |
Output is correct |
13 |
Correct |
6 ms |
26460 KB |
Output is correct |
14 |
Correct |
7 ms |
25948 KB |
Output is correct |
15 |
Correct |
6 ms |
26068 KB |
Output is correct |
16 |
Correct |
8 ms |
26784 KB |
Output is correct |
17 |
Correct |
8 ms |
26204 KB |
Output is correct |
18 |
Correct |
6 ms |
25692 KB |
Output is correct |
19 |
Correct |
7 ms |
26300 KB |
Output is correct |
20 |
Correct |
6 ms |
26204 KB |
Output is correct |
21 |
Correct |
5 ms |
26128 KB |
Output is correct |
22 |
Correct |
6 ms |
26204 KB |
Output is correct |
23 |
Correct |
6 ms |
25948 KB |
Output is correct |
24 |
Correct |
6 ms |
26460 KB |
Output is correct |
25 |
Correct |
6 ms |
26204 KB |
Output is correct |
26 |
Correct |
7 ms |
26716 KB |
Output is correct |
27 |
Correct |
6 ms |
26460 KB |
Output is correct |
28 |
Correct |
6 ms |
26528 KB |
Output is correct |
29 |
Correct |
7 ms |
26460 KB |
Output is correct |
30 |
Correct |
6 ms |
26392 KB |
Output is correct |
31 |
Correct |
7 ms |
26460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
24924 KB |
Output is correct |
2 |
Correct |
3 ms |
24924 KB |
Output is correct |
3 |
Correct |
3 ms |
25044 KB |
Output is correct |
4 |
Correct |
3 ms |
24924 KB |
Output is correct |
5 |
Correct |
7 ms |
26460 KB |
Output is correct |
6 |
Correct |
7 ms |
25980 KB |
Output is correct |
7 |
Correct |
6 ms |
25948 KB |
Output is correct |
8 |
Correct |
11 ms |
26540 KB |
Output is correct |
9 |
Correct |
11 ms |
26204 KB |
Output is correct |
10 |
Correct |
7 ms |
25948 KB |
Output is correct |
11 |
Correct |
7 ms |
25692 KB |
Output is correct |
12 |
Correct |
6 ms |
26372 KB |
Output is correct |
13 |
Correct |
6 ms |
26460 KB |
Output is correct |
14 |
Correct |
7 ms |
25948 KB |
Output is correct |
15 |
Correct |
6 ms |
26068 KB |
Output is correct |
16 |
Correct |
8 ms |
26784 KB |
Output is correct |
17 |
Correct |
8 ms |
26204 KB |
Output is correct |
18 |
Correct |
6 ms |
25692 KB |
Output is correct |
19 |
Correct |
7 ms |
26300 KB |
Output is correct |
20 |
Correct |
6 ms |
26204 KB |
Output is correct |
21 |
Correct |
5 ms |
26128 KB |
Output is correct |
22 |
Correct |
6 ms |
26204 KB |
Output is correct |
23 |
Correct |
6 ms |
25948 KB |
Output is correct |
24 |
Correct |
6 ms |
26460 KB |
Output is correct |
25 |
Correct |
6 ms |
26204 KB |
Output is correct |
26 |
Correct |
7 ms |
26716 KB |
Output is correct |
27 |
Correct |
6 ms |
26460 KB |
Output is correct |
28 |
Correct |
6 ms |
26528 KB |
Output is correct |
29 |
Correct |
7 ms |
26460 KB |
Output is correct |
30 |
Correct |
6 ms |
26392 KB |
Output is correct |
31 |
Correct |
7 ms |
26460 KB |
Output is correct |
32 |
Correct |
8 ms |
26460 KB |
Output is correct |
33 |
Correct |
298 ms |
101924 KB |
Output is correct |
34 |
Correct |
234 ms |
77096 KB |
Output is correct |
35 |
Correct |
305 ms |
98920 KB |
Output is correct |
36 |
Correct |
286 ms |
77460 KB |
Output is correct |
37 |
Correct |
174 ms |
58748 KB |
Output is correct |
38 |
Correct |
139 ms |
54356 KB |
Output is correct |
39 |
Correct |
130 ms |
82256 KB |
Output is correct |
40 |
Correct |
139 ms |
82124 KB |
Output is correct |
41 |
Correct |
89 ms |
82304 KB |
Output is correct |
42 |
Correct |
120 ms |
63532 KB |
Output is correct |
43 |
Correct |
115 ms |
63488 KB |
Output is correct |
44 |
Correct |
270 ms |
127060 KB |
Output is correct |
45 |
Correct |
209 ms |
91624 KB |
Output is correct |
46 |
Correct |
84 ms |
53872 KB |
Output is correct |
47 |
Correct |
189 ms |
79548 KB |
Output is correct |
48 |
Correct |
118 ms |
72916 KB |
Output is correct |
49 |
Correct |
92 ms |
72612 KB |
Output is correct |
50 |
Correct |
184 ms |
76744 KB |
Output is correct |
51 |
Correct |
88 ms |
64204 KB |
Output is correct |
52 |
Correct |
170 ms |
80836 KB |
Output is correct |
53 |
Correct |
118 ms |
73528 KB |
Output is correct |
54 |
Correct |
158 ms |
94896 KB |
Output is correct |
55 |
Correct |
178 ms |
81492 KB |
Output is correct |
56 |
Correct |
141 ms |
86356 KB |
Output is correct |
57 |
Correct |
170 ms |
89160 KB |
Output is correct |
58 |
Correct |
140 ms |
81492 KB |
Output is correct |
59 |
Correct |
144 ms |
81372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
24924 KB |
Output is correct |
2 |
Correct |
3 ms |
24924 KB |
Output is correct |
3 |
Correct |
3 ms |
25044 KB |
Output is correct |
4 |
Correct |
3 ms |
24924 KB |
Output is correct |
5 |
Correct |
7 ms |
26460 KB |
Output is correct |
6 |
Correct |
7 ms |
25980 KB |
Output is correct |
7 |
Correct |
6 ms |
25948 KB |
Output is correct |
8 |
Correct |
11 ms |
26540 KB |
Output is correct |
9 |
Correct |
11 ms |
26204 KB |
Output is correct |
10 |
Correct |
7 ms |
25948 KB |
Output is correct |
11 |
Correct |
7 ms |
25692 KB |
Output is correct |
12 |
Correct |
6 ms |
26372 KB |
Output is correct |
13 |
Correct |
6 ms |
26460 KB |
Output is correct |
14 |
Correct |
7 ms |
25948 KB |
Output is correct |
15 |
Correct |
6 ms |
26068 KB |
Output is correct |
16 |
Correct |
8 ms |
26784 KB |
Output is correct |
17 |
Correct |
8 ms |
26204 KB |
Output is correct |
18 |
Correct |
6 ms |
25692 KB |
Output is correct |
19 |
Correct |
7 ms |
26300 KB |
Output is correct |
20 |
Correct |
6 ms |
26204 KB |
Output is correct |
21 |
Correct |
5 ms |
26128 KB |
Output is correct |
22 |
Correct |
6 ms |
26204 KB |
Output is correct |
23 |
Correct |
6 ms |
25948 KB |
Output is correct |
24 |
Correct |
6 ms |
26460 KB |
Output is correct |
25 |
Correct |
6 ms |
26204 KB |
Output is correct |
26 |
Correct |
7 ms |
26716 KB |
Output is correct |
27 |
Correct |
6 ms |
26460 KB |
Output is correct |
28 |
Correct |
6 ms |
26528 KB |
Output is correct |
29 |
Correct |
7 ms |
26460 KB |
Output is correct |
30 |
Correct |
6 ms |
26392 KB |
Output is correct |
31 |
Correct |
7 ms |
26460 KB |
Output is correct |
32 |
Correct |
8 ms |
26460 KB |
Output is correct |
33 |
Correct |
298 ms |
101924 KB |
Output is correct |
34 |
Correct |
234 ms |
77096 KB |
Output is correct |
35 |
Correct |
305 ms |
98920 KB |
Output is correct |
36 |
Correct |
286 ms |
77460 KB |
Output is correct |
37 |
Correct |
174 ms |
58748 KB |
Output is correct |
38 |
Correct |
139 ms |
54356 KB |
Output is correct |
39 |
Correct |
130 ms |
82256 KB |
Output is correct |
40 |
Correct |
139 ms |
82124 KB |
Output is correct |
41 |
Correct |
89 ms |
82304 KB |
Output is correct |
42 |
Correct |
120 ms |
63532 KB |
Output is correct |
43 |
Correct |
115 ms |
63488 KB |
Output is correct |
44 |
Correct |
270 ms |
127060 KB |
Output is correct |
45 |
Correct |
209 ms |
91624 KB |
Output is correct |
46 |
Correct |
84 ms |
53872 KB |
Output is correct |
47 |
Correct |
189 ms |
79548 KB |
Output is correct |
48 |
Correct |
118 ms |
72916 KB |
Output is correct |
49 |
Correct |
92 ms |
72612 KB |
Output is correct |
50 |
Correct |
184 ms |
76744 KB |
Output is correct |
51 |
Correct |
88 ms |
64204 KB |
Output is correct |
52 |
Correct |
170 ms |
80836 KB |
Output is correct |
53 |
Correct |
118 ms |
73528 KB |
Output is correct |
54 |
Correct |
158 ms |
94896 KB |
Output is correct |
55 |
Correct |
178 ms |
81492 KB |
Output is correct |
56 |
Correct |
141 ms |
86356 KB |
Output is correct |
57 |
Correct |
170 ms |
89160 KB |
Output is correct |
58 |
Correct |
140 ms |
81492 KB |
Output is correct |
59 |
Correct |
144 ms |
81372 KB |
Output is correct |
60 |
Correct |
3 ms |
24924 KB |
Output is correct |
61 |
Correct |
3 ms |
24924 KB |
Output is correct |
62 |
Correct |
3 ms |
25092 KB |
Output is correct |
63 |
Correct |
4 ms |
24924 KB |
Output is correct |
64 |
Correct |
255 ms |
85140 KB |
Output is correct |
65 |
Correct |
222 ms |
68436 KB |
Output is correct |
66 |
Correct |
287 ms |
84308 KB |
Output is correct |
67 |
Correct |
206 ms |
68236 KB |
Output is correct |
68 |
Correct |
158 ms |
55124 KB |
Output is correct |
69 |
Correct |
144 ms |
52052 KB |
Output is correct |
70 |
Correct |
128 ms |
45000 KB |
Output is correct |
71 |
Correct |
64 ms |
37600 KB |
Output is correct |
72 |
Correct |
140 ms |
48784 KB |
Output is correct |
73 |
Correct |
64 ms |
39308 KB |
Output is correct |
74 |
Correct |
165 ms |
64056 KB |
Output is correct |
75 |
Correct |
84 ms |
50316 KB |
Output is correct |
76 |
Correct |
82 ms |
50236 KB |
Output is correct |
77 |
Correct |
160 ms |
64048 KB |
Output is correct |
78 |
Correct |
84 ms |
50240 KB |
Output is correct |
79 |
Correct |
205 ms |
74292 KB |
Output is correct |
80 |
Correct |
141 ms |
56380 KB |
Output is correct |
81 |
Correct |
110 ms |
46908 KB |
Output is correct |
82 |
Correct |
174 ms |
60704 KB |
Output is correct |
83 |
Correct |
78 ms |
63316 KB |
Output is correct |
84 |
Correct |
202 ms |
66076 KB |
Output is correct |
85 |
Correct |
208 ms |
66108 KB |
Output is correct |
86 |
Correct |
179 ms |
62696 KB |
Output is correct |
87 |
Correct |
198 ms |
65888 KB |
Output is correct |
88 |
Correct |
182 ms |
65980 KB |
Output is correct |