#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;
}
}
/*ll extra = 0;
for (auto p : dp[u]) {
if (p.first <= h[u]) {
extra += p.second;
} else {
break;
}
}
if (extra > c[u]) {
dp[u][1] += c[u];
dp[u][h[u]] -= c[u];
dp[u][h[u] + 1] += c[u];
} else {
while (!dp[u].empty() && dp[u].begin()->first <= h[u]) {
dp[u].erase(dp[u].begin());
}
dp[u][1] += extra;
dp[u][h[u] + 1] += c[u];
}*/
dp[u][1] += c[u];
dp[u][h[u]] -= c[u];
dp[u][h[u] + 1] += c[u];
vector <pair <int, ll>> cur = vector <pair <int, ll>> (dp[u].begin(), dp[u].end());
for (int i = 1; i < (int)cur.size(); ++i) cur[i].second += cur[i - 1].second;
for (int i = (int)cur.size() - 2; i >= 0; --i) cur[i].second = min(cur[i].second, cur[i + 1].second);
dp[u].clear();
for (int i = 0; i < (int)cur.size(); ++i) {
if (i == 0 || cur[i].second != cur[i - 1].second) dp[u][cur[i].first] += cur[i].second - (i == 0 ? 0 : cur[i - 1].second);
}
/*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 |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
11 ms |
1996 KB |
Output is correct |
6 |
Correct |
8 ms |
1612 KB |
Output is correct |
7 |
Correct |
6 ms |
1356 KB |
Output is correct |
8 |
Correct |
11 ms |
2124 KB |
Output is correct |
9 |
Correct |
8 ms |
1612 KB |
Output is correct |
10 |
Correct |
6 ms |
1356 KB |
Output is correct |
11 |
Correct |
5 ms |
1228 KB |
Output is correct |
12 |
Correct |
65 ms |
1836 KB |
Output is correct |
13 |
Correct |
8 ms |
1740 KB |
Output is correct |
14 |
Correct |
46 ms |
1456 KB |
Output is correct |
15 |
Correct |
28 ms |
1356 KB |
Output is correct |
16 |
Correct |
13 ms |
2508 KB |
Output is correct |
17 |
Correct |
8 ms |
1820 KB |
Output is correct |
18 |
Correct |
5 ms |
1228 KB |
Output is correct |
19 |
Correct |
449 ms |
1812 KB |
Output is correct |
20 |
Correct |
34 ms |
1648 KB |
Output is correct |
21 |
Correct |
5 ms |
1612 KB |
Output is correct |
22 |
Correct |
7 ms |
1868 KB |
Output is correct |
23 |
Correct |
5 ms |
1484 KB |
Output is correct |
24 |
Correct |
895 ms |
1864 KB |
Output is correct |
25 |
Correct |
27 ms |
1612 KB |
Output is correct |
26 |
Correct |
5 ms |
1736 KB |
Output is correct |
27 |
Correct |
267 ms |
1876 KB |
Output is correct |
28 |
Correct |
539 ms |
1904 KB |
Output is correct |
29 |
Correct |
971 ms |
2096 KB |
Output is correct |
30 |
Correct |
745 ms |
1872 KB |
Output is correct |
31 |
Correct |
713 ms |
2184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
1948 KB |
Output is correct |
2 |
Correct |
643 ms |
88304 KB |
Output is correct |
3 |
Correct |
453 ms |
60872 KB |
Output is correct |
4 |
Correct |
654 ms |
85140 KB |
Output is correct |
5 |
Correct |
424 ms |
60996 KB |
Output is correct |
6 |
Correct |
251 ms |
41888 KB |
Output is correct |
7 |
Correct |
242 ms |
37340 KB |
Output is correct |
8 |
Execution timed out |
2086 ms |
59552 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
11 ms |
1996 KB |
Output is correct |
6 |
Correct |
8 ms |
1612 KB |
Output is correct |
7 |
Correct |
6 ms |
1356 KB |
Output is correct |
8 |
Correct |
11 ms |
2124 KB |
Output is correct |
9 |
Correct |
8 ms |
1612 KB |
Output is correct |
10 |
Correct |
6 ms |
1356 KB |
Output is correct |
11 |
Correct |
5 ms |
1228 KB |
Output is correct |
12 |
Correct |
65 ms |
1836 KB |
Output is correct |
13 |
Correct |
8 ms |
1740 KB |
Output is correct |
14 |
Correct |
46 ms |
1456 KB |
Output is correct |
15 |
Correct |
28 ms |
1356 KB |
Output is correct |
16 |
Correct |
13 ms |
2508 KB |
Output is correct |
17 |
Correct |
8 ms |
1820 KB |
Output is correct |
18 |
Correct |
5 ms |
1228 KB |
Output is correct |
19 |
Correct |
449 ms |
1812 KB |
Output is correct |
20 |
Correct |
34 ms |
1648 KB |
Output is correct |
21 |
Correct |
5 ms |
1612 KB |
Output is correct |
22 |
Correct |
7 ms |
1868 KB |
Output is correct |
23 |
Correct |
5 ms |
1484 KB |
Output is correct |
24 |
Correct |
895 ms |
1864 KB |
Output is correct |
25 |
Correct |
27 ms |
1612 KB |
Output is correct |
26 |
Correct |
5 ms |
1736 KB |
Output is correct |
27 |
Correct |
267 ms |
1876 KB |
Output is correct |
28 |
Correct |
539 ms |
1904 KB |
Output is correct |
29 |
Correct |
971 ms |
2096 KB |
Output is correct |
30 |
Correct |
745 ms |
1872 KB |
Output is correct |
31 |
Correct |
713 ms |
2184 KB |
Output is correct |
32 |
Correct |
13 ms |
1948 KB |
Output is correct |
33 |
Correct |
643 ms |
88304 KB |
Output is correct |
34 |
Correct |
453 ms |
60872 KB |
Output is correct |
35 |
Correct |
654 ms |
85140 KB |
Output is correct |
36 |
Correct |
424 ms |
60996 KB |
Output is correct |
37 |
Correct |
251 ms |
41888 KB |
Output is correct |
38 |
Correct |
242 ms |
37340 KB |
Output is correct |
39 |
Execution timed out |
2086 ms |
59552 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |