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>
#define int long long
using namespace std;
const int N = 2e5 + 42, INF = 1e18 + 42;
bool vu[N];
vector<int> adj[N];
int n, h[N], cost[N];
multiset<pair<int, int>> DFS(int i, int pre) {
vu[i] = true;
multiset<pair<int, int>> ans;
ans.insert({0, cost[i]});
ans.insert({h[i]+1, cost[i]});
for(int j : adj[i])
if(j != pre) {
multiset<pair<int, int>> fils = DFS(j, i);
if(ans.size() < fils.size())
swap(ans, fils);
for(auto p : fils)
ans.insert(p);
}
int suppr = cost[i];
while(suppr) {
auto it = ans.lower_bound({h[i]+1, 0});
it--;
if((*it).second <= suppr) {
suppr -= (*it).second;
ans.erase(it);
} else {
auto copy = (*it);
ans.erase(it);
ans.insert({copy.first, copy.second - suppr});
suppr = 0;
}
}
return ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++) {
int a;
cin >> a >> h[i] >> cost[i];
if(i != a-1)
adj[a-1].push_back(i);
}
int ans = 0;
for(int i = 0; i < n; i++)
if(!vu[i]) {
multiset<pair<int, int>> slope = DFS(i, i);
for(auto p : slope)
if(p.first == 0)
ans += p.second;
else
break;
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |