#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<float.h>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
//#define int long long
#define double long double
const int mod=998244353,mxn=1e6,lg=22;//inf=1e18,minf=-1e18,Mxn=100000;
vector<int>adj[mxn+10];
ll dp[mxn+10][22];
map<int,ll>mp[mxn+10];
pair<int,ll> info[mxn+10];
int n,m,k;
ll ans=0;
void dfs2(int cur,int p){
for(auto i:adj[cur])dfs2(i,cur);
for(auto i:adj[cur]){
if(mp[i].size()>mp[cur].size())swap(mp[cur],mp[i]);
for(auto j:mp[i])mp[cur][j.f]+=j.s;
}
mp[cur][info[cur].f]+=info[cur].s;
auto it=mp[cur].upper_bound(info[cur].f);
while(it!=mp[cur].end()&&info[cur].s>0){
if(it==mp[cur].end())assert(0);
if(info[cur].s>=it->s)info[cur].s-=it->s,mp[cur].erase(it);
else {it->s-=info[cur].s;break;}
}
}
int32_t main(){
fastio
cin>>n>>m>>k;
for(int i=2;i<=n;i++){
int p;cin>>p;
adj[p].pb(i);
}
bool yes=true;
for(int i=0;i<m;i++){
int v,d,w;cin>>v>>d>>w;
info[v]={d,w};
}
ans=0;
dfs2(1,-1);
for(auto i:mp[1])ans+=i.s;
cout<<ans;
return 0;
}
Compilation message
magictree.cpp: In function 'int32_t main()':
magictree.cpp:65:7: warning: unused variable 'yes' [-Wunused-variable]
65 | bool yes=true;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
73820 KB |
Output is correct |
2 |
Correct |
15 ms |
73680 KB |
Output is correct |
3 |
Correct |
15 ms |
73816 KB |
Output is correct |
4 |
Correct |
15 ms |
73816 KB |
Output is correct |
5 |
Correct |
16 ms |
73820 KB |
Output is correct |
6 |
Correct |
16 ms |
74076 KB |
Output is correct |
7 |
Correct |
15 ms |
73820 KB |
Output is correct |
8 |
Correct |
15 ms |
73820 KB |
Output is correct |
9 |
Correct |
15 ms |
73816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
87928 KB |
Output is correct |
2 |
Correct |
53 ms |
87152 KB |
Output is correct |
3 |
Correct |
143 ms |
102864 KB |
Output is correct |
4 |
Correct |
66 ms |
86476 KB |
Output is correct |
5 |
Correct |
91 ms |
87332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
74072 KB |
Output is correct |
2 |
Correct |
15 ms |
74072 KB |
Output is correct |
3 |
Correct |
16 ms |
74072 KB |
Output is correct |
4 |
Correct |
53 ms |
93268 KB |
Output is correct |
5 |
Correct |
72 ms |
97108 KB |
Output is correct |
6 |
Correct |
63 ms |
93264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
104 ms |
157268 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
73820 KB |
Output is correct |
2 |
Correct |
15 ms |
73680 KB |
Output is correct |
3 |
Correct |
15 ms |
73816 KB |
Output is correct |
4 |
Correct |
15 ms |
73816 KB |
Output is correct |
5 |
Correct |
16 ms |
73820 KB |
Output is correct |
6 |
Correct |
16 ms |
74076 KB |
Output is correct |
7 |
Correct |
15 ms |
73820 KB |
Output is correct |
8 |
Correct |
15 ms |
73820 KB |
Output is correct |
9 |
Correct |
15 ms |
73816 KB |
Output is correct |
10 |
Correct |
71 ms |
85032 KB |
Output is correct |
11 |
Correct |
63 ms |
84068 KB |
Output is correct |
12 |
Correct |
51 ms |
85520 KB |
Output is correct |
13 |
Correct |
40 ms |
82636 KB |
Output is correct |
14 |
Correct |
50 ms |
93268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
77 ms |
154540 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
73820 KB |
Output is correct |
2 |
Correct |
15 ms |
73680 KB |
Output is correct |
3 |
Correct |
15 ms |
73816 KB |
Output is correct |
4 |
Correct |
15 ms |
73816 KB |
Output is correct |
5 |
Correct |
16 ms |
73820 KB |
Output is correct |
6 |
Correct |
16 ms |
74076 KB |
Output is correct |
7 |
Correct |
15 ms |
73820 KB |
Output is correct |
8 |
Correct |
15 ms |
73820 KB |
Output is correct |
9 |
Correct |
15 ms |
73816 KB |
Output is correct |
10 |
Correct |
16 ms |
74072 KB |
Output is correct |
11 |
Correct |
15 ms |
74072 KB |
Output is correct |
12 |
Correct |
16 ms |
74072 KB |
Output is correct |
13 |
Correct |
53 ms |
93268 KB |
Output is correct |
14 |
Correct |
72 ms |
97108 KB |
Output is correct |
15 |
Correct |
63 ms |
93264 KB |
Output is correct |
16 |
Correct |
71 ms |
85032 KB |
Output is correct |
17 |
Correct |
63 ms |
84068 KB |
Output is correct |
18 |
Correct |
51 ms |
85520 KB |
Output is correct |
19 |
Correct |
40 ms |
82636 KB |
Output is correct |
20 |
Correct |
50 ms |
93268 KB |
Output is correct |
21 |
Correct |
30 ms |
79192 KB |
Output is correct |
22 |
Correct |
86 ms |
88076 KB |
Output is correct |
23 |
Correct |
105 ms |
90960 KB |
Output is correct |
24 |
Correct |
106 ms |
97204 KB |
Output is correct |
25 |
Correct |
62 ms |
86388 KB |
Output is correct |
26 |
Correct |
86 ms |
88116 KB |
Output is correct |
27 |
Correct |
70 ms |
86868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
73820 KB |
Output is correct |
2 |
Correct |
15 ms |
73680 KB |
Output is correct |
3 |
Correct |
15 ms |
73816 KB |
Output is correct |
4 |
Correct |
15 ms |
73816 KB |
Output is correct |
5 |
Correct |
16 ms |
73820 KB |
Output is correct |
6 |
Correct |
16 ms |
74076 KB |
Output is correct |
7 |
Correct |
15 ms |
73820 KB |
Output is correct |
8 |
Correct |
15 ms |
73820 KB |
Output is correct |
9 |
Correct |
15 ms |
73816 KB |
Output is correct |
10 |
Correct |
77 ms |
87928 KB |
Output is correct |
11 |
Correct |
53 ms |
87152 KB |
Output is correct |
12 |
Correct |
143 ms |
102864 KB |
Output is correct |
13 |
Correct |
66 ms |
86476 KB |
Output is correct |
14 |
Correct |
91 ms |
87332 KB |
Output is correct |
15 |
Correct |
16 ms |
74072 KB |
Output is correct |
16 |
Correct |
15 ms |
74072 KB |
Output is correct |
17 |
Correct |
16 ms |
74072 KB |
Output is correct |
18 |
Correct |
53 ms |
93268 KB |
Output is correct |
19 |
Correct |
72 ms |
97108 KB |
Output is correct |
20 |
Correct |
63 ms |
93264 KB |
Output is correct |
21 |
Runtime error |
104 ms |
157268 KB |
Execution killed with signal 6 |
22 |
Halted |
0 ms |
0 KB |
- |