#include <bits/stdc++.h>
#define mk make_pair
#define sc second
#define fr first
#define pb emplace_back
#define all(s) s.begin(), s.end()
#define sz(s) ( (int)s.size() )
#define int long long
using namespace std;
const int inf = (int)1e9 + 7;
const int N = (int)2e5 + 7;
int n,m;
int x,w;
int dp[302][302];
int ans = inf;
vector <pair<int,int> > g[N];
void dfs (int v) {
for (auto to : g[v]) {
dfs(to.fr);
int u = to.fr;
int w = to.sc;
for (int i = 0; i <= 300; i ++) {
int mn = inf;
for (int j = 0; j <= i; j ++) {
mn = min(mn,dp[u][j] + abs(i - j - w));
}
dp[v][i] += mn;
}
}
}
main () {
cin >> n >> m;
for (int i = 2; i <= n + m; i ++) {
cin >> x >> w;
g[x].pb(mk(i,w));
}
for (int i = n + 1; i <= n + m; i ++)
for (int j = 1; j <= 300; j ++)
dp[i][j] = inf;
dfs(1);
for (int i = 0; i <= 300; i ++) {
ans = min(ans,dp[1][i]);
}
cout << ans << endl;
}
Compilation message
fireworks.cpp:38:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main () {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
4984 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5100 KB |
Output is correct |
2 |
Correct |
8 ms |
5156 KB |
Output is correct |
3 |
Correct |
10 ms |
5340 KB |
Output is correct |
4 |
Correct |
13 ms |
5452 KB |
Output is correct |
5 |
Correct |
13 ms |
5456 KB |
Output is correct |
6 |
Correct |
14 ms |
5588 KB |
Output is correct |
7 |
Correct |
17 ms |
5592 KB |
Output is correct |
8 |
Correct |
19 ms |
5744 KB |
Output is correct |
9 |
Correct |
20 ms |
5744 KB |
Output is correct |
10 |
Correct |
25 ms |
5912 KB |
Output is correct |
11 |
Correct |
22 ms |
5964 KB |
Output is correct |
12 |
Correct |
27 ms |
5968 KB |
Output is correct |
13 |
Correct |
24 ms |
5972 KB |
Output is correct |
14 |
Correct |
26 ms |
6108 KB |
Output is correct |
15 |
Correct |
26 ms |
6108 KB |
Output is correct |
16 |
Correct |
32 ms |
6108 KB |
Output is correct |
17 |
Correct |
25 ms |
6164 KB |
Output is correct |
18 |
Correct |
29 ms |
6164 KB |
Output is correct |
19 |
Correct |
28 ms |
6164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
4984 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
4984 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |