#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ld long double
#define pii pair<int, int>
#define f first
#define s second
#define boost() cin.tie(0), cin.sync_with_stdio(0)
const int MN = 5005;
int n, a[MN], h[MN], c[MN], dp[MN][MN];
vector<int> hs, adj[MN];
map<int, int> mp;
void dfs(int cur) {
for (int i = 1; i <= hs.size(); i++) dp[cur][i] = i == h[cur] ? 0 : c[cur];
for (int nxt : adj[cur]) {
dfs(nxt);
for (int i = 1; i <= hs.size(); i++) dp[cur][i] += dp[nxt][i];
}
for (int i = hs.size(); i > 0; i--) dp[cur][i] = min(dp[cur][i], dp[cur][i + 1]);
}
int32_t main() {
boost();
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> h[i] >> c[i];
if (i > 1) adj[a[i]].push_back(i);
hs.push_back(h[i]);
}
sort(hs.begin(), hs.end());
hs.erase(unique(hs.begin(), hs.end()), hs.end());
for (int i = 0; i < hs.size(); i++) mp[hs[i]] = i + 1;
for (int i = 1; i <= n; i++) h[i] = mp[h[i]];
memset(dp, 0x3f, sizeof(dp));
dfs(1);
printf("%lld\n", dp[1][1]);
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'void dfs(long long int)':
worst_reporter2.cpp:18:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for (int i = 1; i <= hs.size(); i++) dp[cur][i] = i == h[cur] ? 0 : c[cur];
| ~~^~~~~~~~~~~~
worst_reporter2.cpp:21:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i = 1; i <= hs.size(); i++) dp[cur][i] += dp[nxt][i];
| ~~^~~~~~~~~~~~
worst_reporter2.cpp: In function 'int32_t main()':
worst_reporter2.cpp:37:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for (int i = 0; i < hs.size(); i++) mp[hs[i]] = i + 1;
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
196448 KB |
Output is correct |
2 |
Correct |
87 ms |
196452 KB |
Output is correct |
3 |
Correct |
87 ms |
196516 KB |
Output is correct |
4 |
Correct |
88 ms |
196420 KB |
Output is correct |
5 |
Correct |
237 ms |
197172 KB |
Output is correct |
6 |
Correct |
94 ms |
196776 KB |
Output is correct |
7 |
Correct |
89 ms |
196796 KB |
Output is correct |
8 |
Correct |
241 ms |
197200 KB |
Output is correct |
9 |
Correct |
94 ms |
196808 KB |
Output is correct |
10 |
Correct |
90 ms |
196812 KB |
Output is correct |
11 |
Correct |
91 ms |
196876 KB |
Output is correct |
12 |
Correct |
250 ms |
197700 KB |
Output is correct |
13 |
Correct |
91 ms |
197328 KB |
Output is correct |
14 |
Correct |
244 ms |
197440 KB |
Output is correct |
15 |
Correct |
95 ms |
197040 KB |
Output is correct |
16 |
Correct |
240 ms |
197188 KB |
Output is correct |
17 |
Correct |
94 ms |
196824 KB |
Output is correct |
18 |
Correct |
89 ms |
196820 KB |
Output is correct |
19 |
Correct |
239 ms |
197320 KB |
Output is correct |
20 |
Correct |
97 ms |
197068 KB |
Output is correct |
21 |
Correct |
89 ms |
196976 KB |
Output is correct |
22 |
Correct |
236 ms |
197060 KB |
Output is correct |
23 |
Correct |
92 ms |
196800 KB |
Output is correct |
24 |
Correct |
241 ms |
197436 KB |
Output is correct |
25 |
Correct |
93 ms |
197060 KB |
Output is correct |
26 |
Correct |
246 ms |
197644 KB |
Output is correct |
27 |
Correct |
240 ms |
197444 KB |
Output is correct |
28 |
Correct |
240 ms |
197616 KB |
Output is correct |
29 |
Correct |
241 ms |
197444 KB |
Output is correct |
30 |
Correct |
244 ms |
197484 KB |
Output is correct |
31 |
Correct |
245 ms |
197372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
237 ms |
197184 KB |
Output is correct |
2 |
Runtime error |
345 ms |
398052 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
196448 KB |
Output is correct |
2 |
Correct |
87 ms |
196452 KB |
Output is correct |
3 |
Correct |
87 ms |
196516 KB |
Output is correct |
4 |
Correct |
88 ms |
196420 KB |
Output is correct |
5 |
Correct |
237 ms |
197172 KB |
Output is correct |
6 |
Correct |
94 ms |
196776 KB |
Output is correct |
7 |
Correct |
89 ms |
196796 KB |
Output is correct |
8 |
Correct |
241 ms |
197200 KB |
Output is correct |
9 |
Correct |
94 ms |
196808 KB |
Output is correct |
10 |
Correct |
90 ms |
196812 KB |
Output is correct |
11 |
Correct |
91 ms |
196876 KB |
Output is correct |
12 |
Correct |
250 ms |
197700 KB |
Output is correct |
13 |
Correct |
91 ms |
197328 KB |
Output is correct |
14 |
Correct |
244 ms |
197440 KB |
Output is correct |
15 |
Correct |
95 ms |
197040 KB |
Output is correct |
16 |
Correct |
240 ms |
197188 KB |
Output is correct |
17 |
Correct |
94 ms |
196824 KB |
Output is correct |
18 |
Correct |
89 ms |
196820 KB |
Output is correct |
19 |
Correct |
239 ms |
197320 KB |
Output is correct |
20 |
Correct |
97 ms |
197068 KB |
Output is correct |
21 |
Correct |
89 ms |
196976 KB |
Output is correct |
22 |
Correct |
236 ms |
197060 KB |
Output is correct |
23 |
Correct |
92 ms |
196800 KB |
Output is correct |
24 |
Correct |
241 ms |
197436 KB |
Output is correct |
25 |
Correct |
93 ms |
197060 KB |
Output is correct |
26 |
Correct |
246 ms |
197644 KB |
Output is correct |
27 |
Correct |
240 ms |
197444 KB |
Output is correct |
28 |
Correct |
240 ms |
197616 KB |
Output is correct |
29 |
Correct |
241 ms |
197444 KB |
Output is correct |
30 |
Correct |
244 ms |
197484 KB |
Output is correct |
31 |
Correct |
245 ms |
197372 KB |
Output is correct |
32 |
Correct |
237 ms |
197184 KB |
Output is correct |
33 |
Runtime error |
345 ms |
398052 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |