#include <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <cassert>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <stack>
#include <chrono>
#include <cstring>
#include <numeric>
using namespace std;
typedef long long ll;
const int N = 200000 + 7;
const ll INF = (ll)1e18 + 7;
int n;
int par[N];
int h[N];
int c[N];
vector<int> g[N];
int dep[N];
ll dp[N];
ll dp2[N];
ll get(int a) {
ll sol = c[a];
for (auto& b : g[a]) {
sol += min(dp[b], dp2[b]);
}
return sol;
}
void compute(int a) {
ll init = min(dp[a], dp2[a]);
dp[a] = 0;
for (auto& b : g[a]) {
dp[a] += min(dp[b], dp2[b]);
}
dp2[a] = get(a);
while (par[a]) {
ll estim = dp2[par[a]] - init + min(dp[a], dp2[a]);
init = min(dp[par[a]], dp2[par[a]]);
dp2[par[a]] = get(par[a]);
assert(dp2[par[a]] == estim);
//cout << "dif : " << dp2[par[a]] - estim << "\n";
a = par[a];
}
}
void build1(int a) {
//dp2[a] = c[a];
for (auto& b : g[a]) {
dep[b] = 1 + dep[a];
build1(b);
//dp2[a] += dp2[b];
}
dp2[a] = get(a);
}
signed main() {
#ifdef ONPC
FILE* stream;
freopen_s(&stream, "input.txt", "r", stdin);
#else
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#endif
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> par[i] >> h[i] >> c[i];
dp[i] = INF;
}
assert(par[1] == 1);
par[1] = 0;
for (int i = 2; i <= n; i++) {
assert(1 <= par[i] && par[i] <= i - 1);
g[par[i]].push_back(i);
}
build1(1);
vector<int> ord(n);
iota(ord.begin(), ord.end(), 1);
sort(ord.begin(), ord.end(), [&](int a, int b) {
if (h[a] != h[b]) {
return h[a] > h[b];
}
return dep[a] > dep[b];
});
for (auto& a : ord) {
compute(a);
//cout << "\t\t\t a = " << a << "\n";
for (int i = 1; i <= n; i++) {
assert(dp2[i] == get(i));
}
}
cout << min(dp[1], dp2[1]) << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
115 ms |
5296 KB |
Output is correct |
6 |
Correct |
112 ms |
5300 KB |
Output is correct |
7 |
Correct |
133 ms |
5296 KB |
Output is correct |
8 |
Correct |
122 ms |
5296 KB |
Output is correct |
9 |
Correct |
131 ms |
5204 KB |
Output is correct |
10 |
Correct |
110 ms |
5296 KB |
Output is correct |
11 |
Correct |
100 ms |
5296 KB |
Output is correct |
12 |
Correct |
132 ms |
5612 KB |
Output is correct |
13 |
Correct |
133 ms |
5600 KB |
Output is correct |
14 |
Correct |
87 ms |
5460 KB |
Output is correct |
15 |
Correct |
100 ms |
5504 KB |
Output is correct |
16 |
Correct |
56 ms |
5292 KB |
Output is correct |
17 |
Correct |
62 ms |
5292 KB |
Output is correct |
18 |
Correct |
56 ms |
5288 KB |
Output is correct |
19 |
Correct |
82 ms |
5332 KB |
Output is correct |
20 |
Correct |
94 ms |
5404 KB |
Output is correct |
21 |
Correct |
99 ms |
5400 KB |
Output is correct |
22 |
Correct |
84 ms |
5252 KB |
Output is correct |
23 |
Correct |
78 ms |
5248 KB |
Output is correct |
24 |
Correct |
101 ms |
5436 KB |
Output is correct |
25 |
Correct |
103 ms |
5332 KB |
Output is correct |
26 |
Correct |
113 ms |
5600 KB |
Output is correct |
27 |
Correct |
135 ms |
5380 KB |
Output is correct |
28 |
Correct |
174 ms |
5460 KB |
Output is correct |
29 |
Correct |
158 ms |
5460 KB |
Output is correct |
30 |
Correct |
100 ms |
5592 KB |
Output is correct |
31 |
Correct |
101 ms |
5500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
115 ms |
5296 KB |
Output is correct |
6 |
Correct |
112 ms |
5300 KB |
Output is correct |
7 |
Correct |
133 ms |
5296 KB |
Output is correct |
8 |
Correct |
122 ms |
5296 KB |
Output is correct |
9 |
Correct |
131 ms |
5204 KB |
Output is correct |
10 |
Correct |
110 ms |
5296 KB |
Output is correct |
11 |
Correct |
100 ms |
5296 KB |
Output is correct |
12 |
Correct |
132 ms |
5612 KB |
Output is correct |
13 |
Correct |
133 ms |
5600 KB |
Output is correct |
14 |
Correct |
87 ms |
5460 KB |
Output is correct |
15 |
Correct |
100 ms |
5504 KB |
Output is correct |
16 |
Correct |
56 ms |
5292 KB |
Output is correct |
17 |
Correct |
62 ms |
5292 KB |
Output is correct |
18 |
Correct |
56 ms |
5288 KB |
Output is correct |
19 |
Correct |
82 ms |
5332 KB |
Output is correct |
20 |
Correct |
94 ms |
5404 KB |
Output is correct |
21 |
Correct |
99 ms |
5400 KB |
Output is correct |
22 |
Correct |
84 ms |
5252 KB |
Output is correct |
23 |
Correct |
78 ms |
5248 KB |
Output is correct |
24 |
Correct |
101 ms |
5436 KB |
Output is correct |
25 |
Correct |
103 ms |
5332 KB |
Output is correct |
26 |
Correct |
113 ms |
5600 KB |
Output is correct |
27 |
Correct |
135 ms |
5380 KB |
Output is correct |
28 |
Correct |
174 ms |
5460 KB |
Output is correct |
29 |
Correct |
158 ms |
5460 KB |
Output is correct |
30 |
Correct |
100 ms |
5592 KB |
Output is correct |
31 |
Correct |
101 ms |
5500 KB |
Output is correct |
32 |
Correct |
111 ms |
5204 KB |
Output is correct |
33 |
Execution timed out |
2090 ms |
15176 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
115 ms |
5296 KB |
Output is correct |
6 |
Correct |
112 ms |
5300 KB |
Output is correct |
7 |
Correct |
133 ms |
5296 KB |
Output is correct |
8 |
Correct |
122 ms |
5296 KB |
Output is correct |
9 |
Correct |
131 ms |
5204 KB |
Output is correct |
10 |
Correct |
110 ms |
5296 KB |
Output is correct |
11 |
Correct |
100 ms |
5296 KB |
Output is correct |
12 |
Correct |
132 ms |
5612 KB |
Output is correct |
13 |
Correct |
133 ms |
5600 KB |
Output is correct |
14 |
Correct |
87 ms |
5460 KB |
Output is correct |
15 |
Correct |
100 ms |
5504 KB |
Output is correct |
16 |
Correct |
56 ms |
5292 KB |
Output is correct |
17 |
Correct |
62 ms |
5292 KB |
Output is correct |
18 |
Correct |
56 ms |
5288 KB |
Output is correct |
19 |
Correct |
82 ms |
5332 KB |
Output is correct |
20 |
Correct |
94 ms |
5404 KB |
Output is correct |
21 |
Correct |
99 ms |
5400 KB |
Output is correct |
22 |
Correct |
84 ms |
5252 KB |
Output is correct |
23 |
Correct |
78 ms |
5248 KB |
Output is correct |
24 |
Correct |
101 ms |
5436 KB |
Output is correct |
25 |
Correct |
103 ms |
5332 KB |
Output is correct |
26 |
Correct |
113 ms |
5600 KB |
Output is correct |
27 |
Correct |
135 ms |
5380 KB |
Output is correct |
28 |
Correct |
174 ms |
5460 KB |
Output is correct |
29 |
Correct |
158 ms |
5460 KB |
Output is correct |
30 |
Correct |
100 ms |
5592 KB |
Output is correct |
31 |
Correct |
101 ms |
5500 KB |
Output is correct |
32 |
Correct |
111 ms |
5204 KB |
Output is correct |
33 |
Execution timed out |
2090 ms |
15176 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |