#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()) {
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
21152 KB |
Output is correct |
2 |
Correct |
44 ms |
19656 KB |
Output is correct |
3 |
Correct |
131 ms |
37332 KB |
Output is correct |
4 |
Correct |
75 ms |
20564 KB |
Output is correct |
5 |
Correct |
131 ms |
21976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
16256 KB |
Output is correct |
2 |
Correct |
73 ms |
15364 KB |
Output is correct |
3 |
Correct |
52 ms |
19640 KB |
Output is correct |
4 |
Correct |
43 ms |
16464 KB |
Output is correct |
5 |
Correct |
51 ms |
27568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
8660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |