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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tree<iii, null_type, greater<iii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int N, sumC, A[200005], H[200005], C[200005], mem[5005][2][5005];
vector<int> adj[5005];
int dp(int n, bool b, int h = 0, int e = -1) {
if (mem[n][b][h] != -1) return mem[n][b][h];
int ans = (b ? C[n] : 0);
for (auto u : adj[n]) if (u != e) {
int ret = dp(u, 0, h, n);
if (h == 0 || H[h] <= H[u]) ret = max(ret, dp(u, 1, u, n));
ans += ret;
}
return mem[n][b][h] = ans;
}
main() {
memset(mem, -1, sizeof mem);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> A[i] >> H[i] >> C[i];
if (i != A[i])
adj[A[i]].pb(i);
sumC += C[i];
}
int x = max(dp(1, 0), dp(1, 1, 1));
cout << sumC - x << '\n';
}
Compilation message (stderr)
worst_reporter2.cpp:42:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
42 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |