/**
____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N_MAX = 5000;
int N;
int A[N_MAX + 2], H[N_MAX + 2], C[N_MAX + 2];
vector <int> sons[N_MAX + 2];
int p[N_MAX + 2];
ll dp[N_MAX + 2][N_MAX + 2];
ll dps[N_MAX + 2][N_MAX + 2];
void dfs (int u)
{
for(int v : sons[u])
dfs(v);
for(int i = 1; i <= N; i++)
{
for(int v : sons[u])
dp[u][i] += dps[v][i];
if(H[u] != H[p[i]])
dp[u][i] += C[u];
dps[u][i] = dp[u][i];
if(i > 1)
dps[u][i] = min(dps[u][i], dps[u][i - 1]);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N;
for(int u = 1; u <= N; u++)
cin >> A[u] >> H[u] >> C[u];
for(int u = 2; u <= N; u++)
sons[A[u]].push_back(u);
for(int i = 1; i <= N; i++)
p[i] = i;
sort(p + 1, p + N + 1, [&] (const int &i, const int &j)
{
return H[i] > H[j];
});
dfs(1);
cout << dps[1][N] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
349 ms |
392036 KB |
Output is correct |
6 |
Correct |
282 ms |
392016 KB |
Output is correct |
7 |
Correct |
268 ms |
392016 KB |
Output is correct |
8 |
Correct |
263 ms |
392252 KB |
Output is correct |
9 |
Correct |
269 ms |
392120 KB |
Output is correct |
10 |
Correct |
260 ms |
392000 KB |
Output is correct |
11 |
Correct |
219 ms |
296724 KB |
Output is correct |
12 |
Correct |
260 ms |
392624 KB |
Output is correct |
13 |
Correct |
254 ms |
392516 KB |
Output is correct |
14 |
Correct |
272 ms |
392356 KB |
Output is correct |
15 |
Correct |
256 ms |
392516 KB |
Output is correct |
16 |
Correct |
254 ms |
392128 KB |
Output is correct |
17 |
Correct |
256 ms |
392144 KB |
Output is correct |
18 |
Correct |
235 ms |
294548 KB |
Output is correct |
19 |
Correct |
259 ms |
392380 KB |
Output is correct |
20 |
Correct |
255 ms |
392336 KB |
Output is correct |
21 |
Correct |
257 ms |
382432 KB |
Output is correct |
22 |
Correct |
513 ms |
392204 KB |
Output is correct |
23 |
Correct |
515 ms |
392132 KB |
Output is correct |
24 |
Correct |
393 ms |
392516 KB |
Output is correct |
25 |
Correct |
386 ms |
392352 KB |
Output is correct |
26 |
Correct |
257 ms |
392596 KB |
Output is correct |
27 |
Correct |
262 ms |
392376 KB |
Output is correct |
28 |
Correct |
258 ms |
392328 KB |
Output is correct |
29 |
Correct |
256 ms |
392448 KB |
Output is correct |
30 |
Correct |
255 ms |
392224 KB |
Output is correct |
31 |
Correct |
256 ms |
392388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
349 ms |
392036 KB |
Output is correct |
6 |
Correct |
282 ms |
392016 KB |
Output is correct |
7 |
Correct |
268 ms |
392016 KB |
Output is correct |
8 |
Correct |
263 ms |
392252 KB |
Output is correct |
9 |
Correct |
269 ms |
392120 KB |
Output is correct |
10 |
Correct |
260 ms |
392000 KB |
Output is correct |
11 |
Correct |
219 ms |
296724 KB |
Output is correct |
12 |
Correct |
260 ms |
392624 KB |
Output is correct |
13 |
Correct |
254 ms |
392516 KB |
Output is correct |
14 |
Correct |
272 ms |
392356 KB |
Output is correct |
15 |
Correct |
256 ms |
392516 KB |
Output is correct |
16 |
Correct |
254 ms |
392128 KB |
Output is correct |
17 |
Correct |
256 ms |
392144 KB |
Output is correct |
18 |
Correct |
235 ms |
294548 KB |
Output is correct |
19 |
Correct |
259 ms |
392380 KB |
Output is correct |
20 |
Correct |
255 ms |
392336 KB |
Output is correct |
21 |
Correct |
257 ms |
382432 KB |
Output is correct |
22 |
Correct |
513 ms |
392204 KB |
Output is correct |
23 |
Correct |
515 ms |
392132 KB |
Output is correct |
24 |
Correct |
393 ms |
392516 KB |
Output is correct |
25 |
Correct |
386 ms |
392352 KB |
Output is correct |
26 |
Correct |
257 ms |
392596 KB |
Output is correct |
27 |
Correct |
262 ms |
392376 KB |
Output is correct |
28 |
Correct |
258 ms |
392328 KB |
Output is correct |
29 |
Correct |
256 ms |
392448 KB |
Output is correct |
30 |
Correct |
255 ms |
392224 KB |
Output is correct |
31 |
Correct |
256 ms |
392388 KB |
Output is correct |
32 |
Correct |
272 ms |
392396 KB |
Output is correct |
33 |
Incorrect |
22 ms |
32976 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
349 ms |
392036 KB |
Output is correct |
6 |
Correct |
282 ms |
392016 KB |
Output is correct |
7 |
Correct |
268 ms |
392016 KB |
Output is correct |
8 |
Correct |
263 ms |
392252 KB |
Output is correct |
9 |
Correct |
269 ms |
392120 KB |
Output is correct |
10 |
Correct |
260 ms |
392000 KB |
Output is correct |
11 |
Correct |
219 ms |
296724 KB |
Output is correct |
12 |
Correct |
260 ms |
392624 KB |
Output is correct |
13 |
Correct |
254 ms |
392516 KB |
Output is correct |
14 |
Correct |
272 ms |
392356 KB |
Output is correct |
15 |
Correct |
256 ms |
392516 KB |
Output is correct |
16 |
Correct |
254 ms |
392128 KB |
Output is correct |
17 |
Correct |
256 ms |
392144 KB |
Output is correct |
18 |
Correct |
235 ms |
294548 KB |
Output is correct |
19 |
Correct |
259 ms |
392380 KB |
Output is correct |
20 |
Correct |
255 ms |
392336 KB |
Output is correct |
21 |
Correct |
257 ms |
382432 KB |
Output is correct |
22 |
Correct |
513 ms |
392204 KB |
Output is correct |
23 |
Correct |
515 ms |
392132 KB |
Output is correct |
24 |
Correct |
393 ms |
392516 KB |
Output is correct |
25 |
Correct |
386 ms |
392352 KB |
Output is correct |
26 |
Correct |
257 ms |
392596 KB |
Output is correct |
27 |
Correct |
262 ms |
392376 KB |
Output is correct |
28 |
Correct |
258 ms |
392328 KB |
Output is correct |
29 |
Correct |
256 ms |
392448 KB |
Output is correct |
30 |
Correct |
255 ms |
392224 KB |
Output is correct |
31 |
Correct |
256 ms |
392388 KB |
Output is correct |
32 |
Correct |
272 ms |
392396 KB |
Output is correct |
33 |
Incorrect |
22 ms |
32976 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |