#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(a) a.begin(),a.end()
#define sz(a) (int)(a.size())
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
const int MAXN = 1e5;
vii adj(MAXN);
ll d[MAXN],w[MAXN];
map<ll,ll>dp[MAXN];
void dfs(int v){
if(!sz(adj[v])){
dp[v][d[v]]+=w[v];
return;
}
for(int x:adj[v]){
dfs(x);
if(sz(dp[x]) > sz(dp[v]))swap(dp[x],dp[v]);
for(auto z:dp[x])dp[v][z.fi]+=z.se;
}
if(w[v]){
dp[v][d[v]]+=w[v];
ll cur = w[v];
while(true){
auto it = dp[v].ub(cur);
if(it == dp[v].end())break;
if(it->se <= cur){
cur-=it->se;
dp[v].erase(it);
}else{
it->se -= cur;
break;
}
}
}
}
int main()
{
owo
int n,m,k;
cin>>n>>m>>k;
for(int i=1;i<n;i++){
int p;
cin>>p;
adj[p-1].pb(i);
}
while(m--){
int a,b,c;
cin>>a>>b>>c;
a--;
d[a] = b;
w[a] = c;
}
dfs(0);
ll ans = 0;
for(auto x:dp[0])ans+=x.se;
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7372 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
21760 KB |
Output is correct |
2 |
Correct |
55 ms |
19780 KB |
Output is correct |
3 |
Correct |
157 ms |
37808 KB |
Output is correct |
4 |
Correct |
93 ms |
21316 KB |
Output is correct |
5 |
Correct |
135 ms |
22620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7500 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
102 ms |
17000 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7372 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
8920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7372 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
7372 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |