This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=1e5+5;
ll n, m, k, t[nx], w[nx], p, l, res;
vector<int> d[nx];
map<ll, ll> dp[nx];
void dfs(int u)
{
for (auto v:d[u])
{
dfs(v);
if (dp[v].size()>dp[u].size()) swap(dp[u], dp[v]);
for (auto [x, y]:dp[v]) dp[u][x]+=y;
dp[v].clear();
}
int vl=w[u];
dp[u][t[u]]+=w[u];
for (auto itr=next(dp[u].find(t[u])); itr!=dp[u].end();)
{
if (vl>=itr->second)
{
vl-=itr->second;
itr=dp[u].erase(itr);
}
else {
itr->second-=vl;
break;
}
}
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m>>k;
for (int i=2; i<=n; i++) cin>>p, d[p].push_back(i);
for (int i=1; i<=m; i++) cin>>l>>t[l]>>w[l];
dfs(1);
for (auto [x, y]:dp[1]) res+=y;
cout<<res<<'\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |