#include <bits/stdc++.h>
using namespace std;
void setup()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int n, m, k, a, b[100000], c[100000];
vector<int> g[100000];
long long res;
map<int, long long> mx;
inline map<int, long long> DFS(int node)
{
map<int, long long> cur, temp;
map<int, long long>::iterator pos;
for (auto &i : g[node])
{
temp = DFS(i);
if (temp.size() > cur.size())
{
swap(temp, cur);
}
for (auto &j : temp)
{
cur[j.first] += j.second;
}
}
if (c[node] > 0)
{
cur[b[node]] += c[node];
pos = cur.upper_bound(b[node]);
while (pos != cur.end())
{
if ((*pos).second > c[node])
{
(*pos).second -= c[node];
break;
}
else
{
c[node] -= (*pos).second;
pos = cur.erase(pos);
}
}
}
return cur;
}
int main()
{
setup();
cin >> n >> m >> k;
for (int i = 1; i < n; ++i)
{
cin >> a;
g[a - 1].push_back(i);
}
while (m--)
{
cin >> a >> b[a - 1] >> c[a - 1];
}
mx = DFS(0);
for (auto & i : mx)
{
res += i.second;
}
cout << res;
return 0;
}
# | 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... |