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 IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
string to_string(pi p) {
return "(" + to_string(p.F) + ", " + to_string(p.S) + ")";
}
template<class T> string to_string(T t) {
string s = "[";
bool f = false;
for(auto&e:t) {
if(f) s += ", ";
s += to_string(e);
f = true;
}
s += "]";
return s;
}
void dbg() { cerr << "]" << endl; }
template<class H, class...T> void dbg(H h, T... t) {
cerr << to_string(h);
if(sizeof...(t)) cerr << ", ";
dbg(t...);
}
#define debug(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", dbg(__VA_ARGS__)
const int INF = 1e9 + 7, N = 2e5 + 7;
int p[N], h[N], c[N];
vi G[N];
multiset<pi> dfs(int u) {
multiset<pi> s;
for(int v:G[u]) {
auto t = dfs(v);
if(SZ(s) < SZ(t)) swap(s, t);
for(auto& e:t) s.insert(e);
}
s.insert({h[u], c[u]});
auto it = s.lower_bound({h[u] + 1, -INF});
int res = -c[u], lst = -1;
while(it != s.end() && res < 0) {
res += it -> S;
lst = it -> F;
it = s.erase(it);
}
if(res > 0 && lst != -1)
s.insert({lst, res});
return s;
}
signed main()
{
IO_OP;
int n;
cin >> n;
ll tot = 0;
for(int i = 1; i <= n; i++) {
cin >> p[i] >> h[i] >> c[i];
h[i] = 1e9 + 1 - h[i];
tot += c[i];
if(i != 1) G[p[i]].PB(i);
}
auto s = dfs(1);
ll ans = 0;
for(auto[x, y]:s)
ans += y;
cout << tot - ans << '\n';
}
Compilation message (stderr)
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:83:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
83 | for(auto[x, y]:s)
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |