#include <iostream>
#include <vector>
#include <map>
#define int int64_t
using namespace std;
struct graph
{
vector<vector<int>> adjlist;
vector<int> ds;
vector<int> js;
map<int,int>* dfs(int curr)
{
auto curr_map = new map<int,int>(); // Create a new map-pointer
for (auto next : adjlist[curr])
{
auto new_map = dfs(next); // Get map of child
// Make sure that we merge into the smaller map
if (curr_map->size() < new_map->size()) swap(curr_map, new_map);
// Merge the maps
for (auto p : *new_map)
{
(*curr_map)[p.first] += p.second;
}
}
// Insert the element of the current vertex
(*curr_map)[ds[curr]] += js[curr];
auto curr_el = curr_map->find(ds[curr]);
int left = js[curr];
// Subtract from the following entries
while (left > 0 && next(curr_el) != curr_map->end())
{
auto next_el = next(curr_el); // Next element
if (left >= next_el->second)
{
// Remove next element
left -= next_el->second;
curr_map->erase(next_el);
}
else
{
curr_map->at(next_el->first) -= left;
break; // We subtracted enough
}
}
return curr_map;
}
};
signed main()
{
// I/O things
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
graph g;
g.adjlist.resize(n);
g.ds.resize(n, 1e16);
g.js.resize(n, 0);
for (int i = 1; i < n; i++)
{
int p;
cin >> p; p--;
g.adjlist[p].push_back(i);
}
for (int i = 0; i < m; i++)
{
int v;
cin >> v; v--;
cin >> g.ds[v] >> g.js[v];
}
// Calculate the result
auto fm = *g.dfs(0);
int res = 0;
for (auto p : fm)
{
res += p.second;
}
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... |