#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(0); cin.tie(nullptr)
#define rall(x) (x).rbegin(), (x).rend()
#define all(x) (x).begin(), (x).end()
#define sz(v) int(v.size())
#define ss second
#define ff first
#define endl '\n'
using namespace std;
typedef long long Long;
typedef pair<int, int> ii;
const Long INF = 1e13;
const int N = 1e5 + 7;
vector<int> adj[N];
set<ii> dif[N];
int dia[N], peso[N], pt[N];
void dfs(int from){
int big = 0;
for(int to: adj[from]){
dfs(to);
if(big == 0 || sz(dif[pt[to]]) > sz(dif[pt[big]])) big = to;
}
if(big == 0){
if(dia[from] == 0) return;
dif[pt[from]].insert({dia[from], peso[from]});
return;
}
pt[from] = pt[big];
for(int to: adj[from]){
if(to == big) continue;
for(auto p: dif[pt[to]]) dif[pt[from]].insert(p);
}
if(dia[from] != 0){
ii par = {dia[from] + 1, 0};
auto it = dif[pt[from]].lower_bound(par);
ii ins = {dia[from], peso[from]};
for(; it != dif[pt[from]].end(); it++){
ii npar = *(it);
if(npar.ss <= ins.ss){
ins.ss -= npar.ss;
dif[pt[from]].erase(it);
}
else{
npar.ss -= ins.ss;
dif[pt[from]].erase(it);
dif[pt[from]].insert(npar);
break;
}
}
dif[pt[from]].insert(ins);
}
}
void solve(){
int n, m, k; cin >> n >> m >> k;
for(int i = 2; i <= n; i++){
int p; cin >> p;
adj[p].push_back(i);
}
for(int i = 0; i < m; i++){
int v; cin >> v;
cin >> dia[v] >> peso[v];
}
for(int i = 1; i <= n; i++) pt[i] = i;
dfs(1);
Long ans = 0;
for(auto p: dif[pt[1]]) ans += p.ss;
cout << ans << endl;
}
int main(){
FAST_IO;
int tt = 1;
// cin >> tt;
while(tt--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
12 ms |
14684 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
15880 KB |
Output is correct |
2 |
Correct |
47 ms |
18676 KB |
Output is correct |
3 |
Correct |
135 ms |
31556 KB |
Output is correct |
4 |
Correct |
80 ms |
19392 KB |
Output is correct |
5 |
Correct |
111 ms |
21188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
12 ms |
15104 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
51 ms |
20376 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
12 ms |
14684 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
19 ms |
15880 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
12 ms |
14684 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
12 ms |
14684 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |