//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->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->se=0;
}
it=ret.find(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 |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
11 ms |
5396 KB |
Output is correct |
6 |
Correct |
8 ms |
5068 KB |
Output is correct |
7 |
Correct |
8 ms |
5068 KB |
Output is correct |
8 |
Correct |
12 ms |
5492 KB |
Output is correct |
9 |
Correct |
8 ms |
5068 KB |
Output is correct |
10 |
Correct |
6 ms |
5068 KB |
Output is correct |
11 |
Correct |
6 ms |
5104 KB |
Output is correct |
12 |
Correct |
7 ms |
6244 KB |
Output is correct |
13 |
Correct |
6 ms |
6116 KB |
Output is correct |
14 |
Correct |
7 ms |
5708 KB |
Output is correct |
15 |
Correct |
6 ms |
5708 KB |
Output is correct |
16 |
Correct |
13 ms |
5460 KB |
Output is correct |
17 |
Correct |
9 ms |
5168 KB |
Output is correct |
18 |
Correct |
6 ms |
5068 KB |
Output is correct |
19 |
Correct |
8 ms |
5708 KB |
Output is correct |
20 |
Correct |
7 ms |
5580 KB |
Output is correct |
21 |
Correct |
6 ms |
5580 KB |
Output is correct |
22 |
Correct |
9 ms |
5324 KB |
Output is correct |
23 |
Correct |
6 ms |
5068 KB |
Output is correct |
24 |
Correct |
9 ms |
5836 KB |
Output is correct |
25 |
Correct |
9 ms |
5664 KB |
Output is correct |
26 |
Correct |
6 ms |
6220 KB |
Output is correct |
27 |
Correct |
7 ms |
5708 KB |
Output is correct |
28 |
Correct |
7 ms |
5964 KB |
Output is correct |
29 |
Correct |
7 ms |
6220 KB |
Output is correct |
30 |
Correct |
8 ms |
6348 KB |
Output is correct |
31 |
Correct |
8 ms |
6268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
5324 KB |
Output is correct |
2 |
Correct |
530 ms |
25128 KB |
Output is correct |
3 |
Correct |
291 ms |
10184 KB |
Output is correct |
4 |
Correct |
521 ms |
21136 KB |
Output is correct |
5 |
Correct |
287 ms |
10052 KB |
Output is correct |
6 |
Correct |
190 ms |
9772 KB |
Output is correct |
7 |
Correct |
174 ms |
9920 KB |
Output is correct |
8 |
Correct |
185 ms |
53572 KB |
Output is correct |
9 |
Correct |
174 ms |
53472 KB |
Output is correct |
10 |
Correct |
140 ms |
53444 KB |
Output is correct |
11 |
Correct |
181 ms |
33348 KB |
Output is correct |
12 |
Correct |
157 ms |
33220 KB |
Output is correct |
13 |
Correct |
575 ms |
25192 KB |
Output is correct |
14 |
Correct |
281 ms |
10052 KB |
Output is correct |
15 |
Correct |
116 ms |
9688 KB |
Output is correct |
16 |
Correct |
305 ms |
37060 KB |
Output is correct |
17 |
Correct |
146 ms |
30100 KB |
Output is correct |
18 |
Correct |
122 ms |
29956 KB |
Output is correct |
19 |
Correct |
389 ms |
19840 KB |
Output is correct |
20 |
Correct |
121 ms |
7484 KB |
Output is correct |
21 |
Correct |
296 ms |
37684 KB |
Output is correct |
22 |
Correct |
154 ms |
30556 KB |
Output is correct |
23 |
Correct |
130 ms |
53460 KB |
Output is correct |
24 |
Correct |
228 ms |
39408 KB |
Output is correct |
25 |
Correct |
269 ms |
51600 KB |
Output is correct |
26 |
Correct |
198 ms |
57076 KB |
Output is correct |
27 |
Correct |
258 ms |
58180 KB |
Output is correct |
28 |
Correct |
300 ms |
58188 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
11 ms |
5396 KB |
Output is correct |
6 |
Correct |
8 ms |
5068 KB |
Output is correct |
7 |
Correct |
8 ms |
5068 KB |
Output is correct |
8 |
Correct |
12 ms |
5492 KB |
Output is correct |
9 |
Correct |
8 ms |
5068 KB |
Output is correct |
10 |
Correct |
6 ms |
5068 KB |
Output is correct |
11 |
Correct |
6 ms |
5104 KB |
Output is correct |
12 |
Correct |
7 ms |
6244 KB |
Output is correct |
13 |
Correct |
6 ms |
6116 KB |
Output is correct |
14 |
Correct |
7 ms |
5708 KB |
Output is correct |
15 |
Correct |
6 ms |
5708 KB |
Output is correct |
16 |
Correct |
13 ms |
5460 KB |
Output is correct |
17 |
Correct |
9 ms |
5168 KB |
Output is correct |
18 |
Correct |
6 ms |
5068 KB |
Output is correct |
19 |
Correct |
8 ms |
5708 KB |
Output is correct |
20 |
Correct |
7 ms |
5580 KB |
Output is correct |
21 |
Correct |
6 ms |
5580 KB |
Output is correct |
22 |
Correct |
9 ms |
5324 KB |
Output is correct |
23 |
Correct |
6 ms |
5068 KB |
Output is correct |
24 |
Correct |
9 ms |
5836 KB |
Output is correct |
25 |
Correct |
9 ms |
5664 KB |
Output is correct |
26 |
Correct |
6 ms |
6220 KB |
Output is correct |
27 |
Correct |
7 ms |
5708 KB |
Output is correct |
28 |
Correct |
7 ms |
5964 KB |
Output is correct |
29 |
Correct |
7 ms |
6220 KB |
Output is correct |
30 |
Correct |
8 ms |
6348 KB |
Output is correct |
31 |
Correct |
8 ms |
6268 KB |
Output is correct |
32 |
Correct |
10 ms |
5324 KB |
Output is correct |
33 |
Correct |
530 ms |
25128 KB |
Output is correct |
34 |
Correct |
291 ms |
10184 KB |
Output is correct |
35 |
Correct |
521 ms |
21136 KB |
Output is correct |
36 |
Correct |
287 ms |
10052 KB |
Output is correct |
37 |
Correct |
190 ms |
9772 KB |
Output is correct |
38 |
Correct |
174 ms |
9920 KB |
Output is correct |
39 |
Correct |
185 ms |
53572 KB |
Output is correct |
40 |
Correct |
174 ms |
53472 KB |
Output is correct |
41 |
Correct |
140 ms |
53444 KB |
Output is correct |
42 |
Correct |
181 ms |
33348 KB |
Output is correct |
43 |
Correct |
157 ms |
33220 KB |
Output is correct |
44 |
Correct |
575 ms |
25192 KB |
Output is correct |
45 |
Correct |
281 ms |
10052 KB |
Output is correct |
46 |
Correct |
116 ms |
9688 KB |
Output is correct |
47 |
Correct |
305 ms |
37060 KB |
Output is correct |
48 |
Correct |
146 ms |
30100 KB |
Output is correct |
49 |
Correct |
122 ms |
29956 KB |
Output is correct |
50 |
Correct |
389 ms |
19840 KB |
Output is correct |
51 |
Correct |
121 ms |
7484 KB |
Output is correct |
52 |
Correct |
296 ms |
37684 KB |
Output is correct |
53 |
Correct |
154 ms |
30556 KB |
Output is correct |
54 |
Correct |
130 ms |
53460 KB |
Output is correct |
55 |
Correct |
228 ms |
39408 KB |
Output is correct |
56 |
Correct |
269 ms |
51600 KB |
Output is correct |
57 |
Correct |
198 ms |
57076 KB |
Output is correct |
58 |
Correct |
258 ms |
58180 KB |
Output is correct |
59 |
Correct |
300 ms |
58188 KB |
Output is correct |
60 |
Incorrect |
3 ms |
4940 KB |
Output isn't correct |
61 |
Halted |
0 ms |
0 KB |
- |