//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define big 1000000000
#define ALL(x) x.begin(),x.end()
int lowbit(int x) {return x&(-x);}
const int maxn=2e5+10;
int n;
vector <int> edge[maxn];
int h[maxn];
int c[maxn];
void combine(map<int,ll> &a,map<int,ll> b) {
auto cur=a.begin();
ll tmp=0;
for (auto it=b.begin();it!=b.end();it++) {
tmp+=it->se;
cur->se+=tmp;
cur=a.upper_bound(it->fi);
if (cur!=a.end()) cur->se-=tmp;
if (a.find(it->fi)==a.end()) {
if (cur==a.end()) break;
a[it->fi]=cur->se+tmp;
cur=a.upper_bound(it->fi);
cur->se=-tmp;
}
}
while (cur!=a.end()) cur=a.erase(cur);
return;
}
map<int,ll> solve(int u) {
map<int,ll> ret;
if (edge[u].empty()) {
ret[big]=c[u];
ret[h[u]]=0;
return ret;
}
ret=solve(edge[u][0]);
rep(i,1,SZ(edge[u])) {
auto cur=solve(edge[u][i]);
if (SZ(ret)<SZ(cur)) swap(cur,ret);
combine(ret,cur);
}
// debug(ret.begin()->fi);debug(ret.begin()->se);
auto it=ret.lower_bound(h[u]);
if ((it->fi)!=h[u]) {
ret[h[u]]=(it->se);
it=ret.upper_bound(h[u]);
it->se=0;
}
it=ret.lower_bound(h[u]);
while (it!=ret.begin()&&((it->se)<=c[u])) {
auto tmp=it;tmp--;
it->se+=tmp->se;
it=ret.erase(tmp);
}
it->se-=(ll)c[u];
ret.begin()->se+=(ll)c[u];
it=ret.upper_bound(h[u]);
it->se+=(ll)c[u];
// debug(u);
// for (auto it=ret.begin();it!=ret.end();it++) cout<<(it->fi)<<" "<<(it->se)<<endl;
return ret;
}
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie();
cin>>n;
h[0]=c[0]=0;
rep(i,1,n+1) {
int x;
cin>>x>>h[i]>>c[i];
if (x==i) edge[0].pb(i);
else edge[x].pb(i);
}
cout<<(*solve(0).begin()).se;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
4 ms |
5016 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
12 ms |
5452 KB |
Output is correct |
6 |
Correct |
11 ms |
5292 KB |
Output is correct |
7 |
Correct |
12 ms |
5196 KB |
Output is correct |
8 |
Correct |
15 ms |
5580 KB |
Output is correct |
9 |
Correct |
8 ms |
5288 KB |
Output is correct |
10 |
Correct |
7 ms |
5196 KB |
Output is correct |
11 |
Correct |
6 ms |
5196 KB |
Output is correct |
12 |
Correct |
7 ms |
6348 KB |
Output is correct |
13 |
Correct |
6 ms |
6348 KB |
Output is correct |
14 |
Correct |
7 ms |
5836 KB |
Output is correct |
15 |
Correct |
7 ms |
5836 KB |
Output is correct |
16 |
Correct |
12 ms |
5672 KB |
Output is correct |
17 |
Correct |
9 ms |
5284 KB |
Output is correct |
18 |
Correct |
7 ms |
5196 KB |
Output is correct |
19 |
Correct |
12 ms |
5928 KB |
Output is correct |
20 |
Correct |
7 ms |
5824 KB |
Output is correct |
21 |
Correct |
7 ms |
5716 KB |
Output is correct |
22 |
Correct |
9 ms |
5452 KB |
Output is correct |
23 |
Correct |
6 ms |
5156 KB |
Output is correct |
24 |
Correct |
8 ms |
5836 KB |
Output is correct |
25 |
Correct |
6 ms |
5708 KB |
Output is correct |
26 |
Correct |
7 ms |
6348 KB |
Output is correct |
27 |
Correct |
8 ms |
5836 KB |
Output is correct |
28 |
Correct |
8 ms |
5964 KB |
Output is correct |
29 |
Correct |
8 ms |
6312 KB |
Output is correct |
30 |
Correct |
9 ms |
6348 KB |
Output is correct |
31 |
Correct |
9 ms |
6348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5540 KB |
Output is correct |
2 |
Correct |
597 ms |
25352 KB |
Output is correct |
3 |
Correct |
333 ms |
10588 KB |
Output is correct |
4 |
Correct |
542 ms |
21224 KB |
Output is correct |
5 |
Correct |
358 ms |
10308 KB |
Output is correct |
6 |
Correct |
195 ms |
10028 KB |
Output is correct |
7 |
Correct |
157 ms |
9944 KB |
Output is correct |
8 |
Correct |
206 ms |
53776 KB |
Output is correct |
9 |
Correct |
181 ms |
53756 KB |
Output is correct |
10 |
Correct |
114 ms |
53708 KB |
Output is correct |
11 |
Correct |
182 ms |
33580 KB |
Output is correct |
12 |
Correct |
208 ms |
33536 KB |
Output is correct |
13 |
Correct |
558 ms |
25400 KB |
Output is correct |
14 |
Correct |
334 ms |
10380 KB |
Output is correct |
15 |
Correct |
101 ms |
9808 KB |
Output is correct |
16 |
Correct |
312 ms |
37280 KB |
Output is correct |
17 |
Correct |
159 ms |
30320 KB |
Output is correct |
18 |
Correct |
110 ms |
30148 KB |
Output is correct |
19 |
Correct |
320 ms |
20008 KB |
Output is correct |
20 |
Correct |
136 ms |
7616 KB |
Output is correct |
21 |
Correct |
298 ms |
37796 KB |
Output is correct |
22 |
Correct |
139 ms |
30740 KB |
Output is correct |
23 |
Correct |
157 ms |
53684 KB |
Output is correct |
24 |
Correct |
251 ms |
39588 KB |
Output is correct |
25 |
Correct |
220 ms |
51712 KB |
Output is correct |
26 |
Correct |
239 ms |
57316 KB |
Output is correct |
27 |
Correct |
271 ms |
58360 KB |
Output is correct |
28 |
Correct |
293 ms |
58308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5024 KB |
Output is correct |
3 |
Correct |
4 ms |
5016 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
12 ms |
5452 KB |
Output is correct |
6 |
Correct |
11 ms |
5292 KB |
Output is correct |
7 |
Correct |
12 ms |
5196 KB |
Output is correct |
8 |
Correct |
15 ms |
5580 KB |
Output is correct |
9 |
Correct |
8 ms |
5288 KB |
Output is correct |
10 |
Correct |
7 ms |
5196 KB |
Output is correct |
11 |
Correct |
6 ms |
5196 KB |
Output is correct |
12 |
Correct |
7 ms |
6348 KB |
Output is correct |
13 |
Correct |
6 ms |
6348 KB |
Output is correct |
14 |
Correct |
7 ms |
5836 KB |
Output is correct |
15 |
Correct |
7 ms |
5836 KB |
Output is correct |
16 |
Correct |
12 ms |
5672 KB |
Output is correct |
17 |
Correct |
9 ms |
5284 KB |
Output is correct |
18 |
Correct |
7 ms |
5196 KB |
Output is correct |
19 |
Correct |
12 ms |
5928 KB |
Output is correct |
20 |
Correct |
7 ms |
5824 KB |
Output is correct |
21 |
Correct |
7 ms |
5716 KB |
Output is correct |
22 |
Correct |
9 ms |
5452 KB |
Output is correct |
23 |
Correct |
6 ms |
5156 KB |
Output is correct |
24 |
Correct |
8 ms |
5836 KB |
Output is correct |
25 |
Correct |
6 ms |
5708 KB |
Output is correct |
26 |
Correct |
7 ms |
6348 KB |
Output is correct |
27 |
Correct |
8 ms |
5836 KB |
Output is correct |
28 |
Correct |
8 ms |
5964 KB |
Output is correct |
29 |
Correct |
8 ms |
6312 KB |
Output is correct |
30 |
Correct |
9 ms |
6348 KB |
Output is correct |
31 |
Correct |
9 ms |
6348 KB |
Output is correct |
32 |
Correct |
12 ms |
5540 KB |
Output is correct |
33 |
Correct |
597 ms |
25352 KB |
Output is correct |
34 |
Correct |
333 ms |
10588 KB |
Output is correct |
35 |
Correct |
542 ms |
21224 KB |
Output is correct |
36 |
Correct |
358 ms |
10308 KB |
Output is correct |
37 |
Correct |
195 ms |
10028 KB |
Output is correct |
38 |
Correct |
157 ms |
9944 KB |
Output is correct |
39 |
Correct |
206 ms |
53776 KB |
Output is correct |
40 |
Correct |
181 ms |
53756 KB |
Output is correct |
41 |
Correct |
114 ms |
53708 KB |
Output is correct |
42 |
Correct |
182 ms |
33580 KB |
Output is correct |
43 |
Correct |
208 ms |
33536 KB |
Output is correct |
44 |
Correct |
558 ms |
25400 KB |
Output is correct |
45 |
Correct |
334 ms |
10380 KB |
Output is correct |
46 |
Correct |
101 ms |
9808 KB |
Output is correct |
47 |
Correct |
312 ms |
37280 KB |
Output is correct |
48 |
Correct |
159 ms |
30320 KB |
Output is correct |
49 |
Correct |
110 ms |
30148 KB |
Output is correct |
50 |
Correct |
320 ms |
20008 KB |
Output is correct |
51 |
Correct |
136 ms |
7616 KB |
Output is correct |
52 |
Correct |
298 ms |
37796 KB |
Output is correct |
53 |
Correct |
139 ms |
30740 KB |
Output is correct |
54 |
Correct |
157 ms |
53684 KB |
Output is correct |
55 |
Correct |
251 ms |
39588 KB |
Output is correct |
56 |
Correct |
220 ms |
51712 KB |
Output is correct |
57 |
Correct |
239 ms |
57316 KB |
Output is correct |
58 |
Correct |
271 ms |
58360 KB |
Output is correct |
59 |
Correct |
293 ms |
58308 KB |
Output is correct |
60 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |