#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
#include <chrono>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n;
cin >> n;
vector <int> a(n), h(n), c(n);
vector <vector <int>> g(n);
for (int i = 0; i < n; ++i) {
cin >> a[i] >> h[i] >> c[i];
--a[i];
if (a[i] != i) {
g[a[i]].push_back(i);
}
}
vector <map <int, ll>> dp(n);
auto dfs = [&] (int u, int p, auto&& dfs) -> void {
for (auto v : g[u]) {
if (v != p) {
dfs(v, u, dfs);
if (dp[v].size() > dp[u].size()) swap(dp[u], dp[v]);
for (auto p : dp[v]) dp[u][p.first] += p.second;
}
}
if (!dp[u].empty() && dp[u].begin()->first <= h[u]) {
dp[u][1] += c[u];
dp[u][h[u]] -= c[u];
dp[u][h[u] + 1] += c[u];
} else {
dp[u][h[u] + 1] += c[u];
}
/*cout << "start " << u + 1 << endl;
for (auto p : dp[u]) {
cout << p.first << ' ' << p.second << '\n';
}
cout << "finish\n";*/
};
dfs(0, -1, dfs);
//cout << dp[0].begin()->second << '\n';
ll ans = 2e18;
ll sum = 0;
for (auto p : dp[0]) {
sum += p.second;
ans = min(ans, sum);
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |