#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//slope trick
int n,m,k,d[100001],w[100001];
vector<int> adj[100001];
map<int,ll> mp[100001]; //(x,y) : add y to position x.
void dfs(int x) {
if (adj[x].size()) {
int mnode=-1;
//merge
for (auto s : adj[x]) {
dfs(s);
if (mnode==-1) mnode=s;
else {
if (mp[mnode].size()<mp[s].size()) swap(s,mnode);
for (auto it=mp[s].begin(); it!=mp[s].end(); ++it) mp[mnode][(*it).first]+=(*it).second; //add s to mnode
}
}
auto it=mp[mnode].upper_bound(d[x]);
ll sum=0;
while (sum+(*it).second<=w[x] && it!=mp[mnode].end()) {
sum+=(*it).second;
auto h=it++;
mp[mnode].erase(h);
}
if (it!=mp[mnode].end()) (*it).second=(*it).second+sum-w[x];
mp[mnode][d[x]]+=w[x];
swap(mp[x],mp[mnode]);
} else {
mp[x][d[x]]=w[x];
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>m>>k;
for (int i=2; i<=n; ++i) {
int x; cin>>x;
adj[x].push_back(i);
}
for (int i=0; i<m; ++i) {
int x; cin>>x;
cin>>d[x]>>w[x];
}
dfs(1);
ll ans=0;
for (auto it=mp[1].begin(); it!=mp[1].end(); ++it) ans+=(*it).second;
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7264 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7376 KB |
Output is correct |
6 |
Correct |
4 ms |
7376 KB |
Output is correct |
7 |
Correct |
5 ms |
7364 KB |
Output is correct |
8 |
Correct |
6 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
20052 KB |
Output is correct |
2 |
Correct |
45 ms |
19252 KB |
Output is correct |
3 |
Correct |
121 ms |
34928 KB |
Output is correct |
4 |
Correct |
76 ms |
18508 KB |
Output is correct |
5 |
Correct |
102 ms |
19700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7508 KB |
Output is correct |
2 |
Correct |
4 ms |
7508 KB |
Output is correct |
3 |
Correct |
5 ms |
7508 KB |
Output is correct |
4 |
Correct |
52 ms |
28828 KB |
Output is correct |
5 |
Correct |
66 ms |
32724 KB |
Output is correct |
6 |
Correct |
66 ms |
28860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
14156 KB |
Output is correct |
2 |
Correct |
56 ms |
13216 KB |
Output is correct |
3 |
Correct |
66 ms |
18040 KB |
Output is correct |
4 |
Correct |
38 ms |
14700 KB |
Output is correct |
5 |
Correct |
46 ms |
26908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7264 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7376 KB |
Output is correct |
6 |
Correct |
4 ms |
7376 KB |
Output is correct |
7 |
Correct |
5 ms |
7364 KB |
Output is correct |
8 |
Correct |
6 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
78 ms |
18444 KB |
Output is correct |
11 |
Correct |
64 ms |
17380 KB |
Output is correct |
12 |
Correct |
55 ms |
19536 KB |
Output is correct |
13 |
Correct |
38 ms |
15832 KB |
Output is correct |
14 |
Correct |
54 ms |
28304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
8660 KB |
Output is correct |
2 |
Correct |
38 ms |
13524 KB |
Output is correct |
3 |
Correct |
40 ms |
13516 KB |
Output is correct |
4 |
Correct |
48 ms |
13604 KB |
Output is correct |
5 |
Correct |
19 ms |
15100 KB |
Output is correct |
6 |
Correct |
38 ms |
17016 KB |
Output is correct |
7 |
Correct |
34 ms |
18764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7264 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7376 KB |
Output is correct |
6 |
Correct |
4 ms |
7376 KB |
Output is correct |
7 |
Correct |
5 ms |
7364 KB |
Output is correct |
8 |
Correct |
6 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7508 KB |
Output is correct |
12 |
Correct |
5 ms |
7508 KB |
Output is correct |
13 |
Correct |
52 ms |
28828 KB |
Output is correct |
14 |
Correct |
66 ms |
32724 KB |
Output is correct |
15 |
Correct |
66 ms |
28860 KB |
Output is correct |
16 |
Correct |
78 ms |
18444 KB |
Output is correct |
17 |
Correct |
64 ms |
17380 KB |
Output is correct |
18 |
Correct |
55 ms |
19536 KB |
Output is correct |
19 |
Correct |
38 ms |
15832 KB |
Output is correct |
20 |
Correct |
54 ms |
28304 KB |
Output is correct |
21 |
Correct |
19 ms |
10836 KB |
Output is correct |
22 |
Correct |
82 ms |
21104 KB |
Output is correct |
23 |
Correct |
94 ms |
24220 KB |
Output is correct |
24 |
Correct |
121 ms |
31056 KB |
Output is correct |
25 |
Correct |
72 ms |
19804 KB |
Output is correct |
26 |
Correct |
90 ms |
22056 KB |
Output is correct |
27 |
Correct |
76 ms |
21492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7264 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7372 KB |
Output is correct |
5 |
Correct |
4 ms |
7376 KB |
Output is correct |
6 |
Correct |
4 ms |
7376 KB |
Output is correct |
7 |
Correct |
5 ms |
7364 KB |
Output is correct |
8 |
Correct |
6 ms |
7380 KB |
Output is correct |
9 |
Correct |
4 ms |
7252 KB |
Output is correct |
10 |
Correct |
91 ms |
20052 KB |
Output is correct |
11 |
Correct |
45 ms |
19252 KB |
Output is correct |
12 |
Correct |
121 ms |
34928 KB |
Output is correct |
13 |
Correct |
76 ms |
18508 KB |
Output is correct |
14 |
Correct |
102 ms |
19700 KB |
Output is correct |
15 |
Correct |
4 ms |
7508 KB |
Output is correct |
16 |
Correct |
4 ms |
7508 KB |
Output is correct |
17 |
Correct |
5 ms |
7508 KB |
Output is correct |
18 |
Correct |
52 ms |
28828 KB |
Output is correct |
19 |
Correct |
66 ms |
32724 KB |
Output is correct |
20 |
Correct |
66 ms |
28860 KB |
Output is correct |
21 |
Correct |
62 ms |
14156 KB |
Output is correct |
22 |
Correct |
56 ms |
13216 KB |
Output is correct |
23 |
Correct |
66 ms |
18040 KB |
Output is correct |
24 |
Correct |
38 ms |
14700 KB |
Output is correct |
25 |
Correct |
46 ms |
26908 KB |
Output is correct |
26 |
Correct |
78 ms |
18444 KB |
Output is correct |
27 |
Correct |
64 ms |
17380 KB |
Output is correct |
28 |
Correct |
55 ms |
19536 KB |
Output is correct |
29 |
Correct |
38 ms |
15832 KB |
Output is correct |
30 |
Correct |
54 ms |
28304 KB |
Output is correct |
31 |
Correct |
10 ms |
8660 KB |
Output is correct |
32 |
Correct |
38 ms |
13524 KB |
Output is correct |
33 |
Correct |
40 ms |
13516 KB |
Output is correct |
34 |
Correct |
48 ms |
13604 KB |
Output is correct |
35 |
Correct |
19 ms |
15100 KB |
Output is correct |
36 |
Correct |
38 ms |
17016 KB |
Output is correct |
37 |
Correct |
34 ms |
18764 KB |
Output is correct |
38 |
Correct |
19 ms |
10836 KB |
Output is correct |
39 |
Correct |
82 ms |
21104 KB |
Output is correct |
40 |
Correct |
94 ms |
24220 KB |
Output is correct |
41 |
Correct |
121 ms |
31056 KB |
Output is correct |
42 |
Correct |
72 ms |
19804 KB |
Output is correct |
43 |
Correct |
90 ms |
22056 KB |
Output is correct |
44 |
Correct |
76 ms |
21492 KB |
Output is correct |
45 |
Correct |
22 ms |
11348 KB |
Output is correct |
46 |
Correct |
84 ms |
22160 KB |
Output is correct |
47 |
Correct |
99 ms |
25600 KB |
Output is correct |
48 |
Correct |
131 ms |
34068 KB |
Output is correct |
49 |
Correct |
72 ms |
20560 KB |
Output is correct |
50 |
Correct |
92 ms |
22988 KB |
Output is correct |
51 |
Correct |
97 ms |
22320 KB |
Output is correct |