#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)1e18 + 7;
const int N = (int)2e5 + 7;
int n,m;
int x,w;
int dp[302][302];
int ans = inf;
int sum,sm;
vector <int> vec;
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));
if (n == 1) {
vec.pb(w);
}
}
if (n == 1) {
for (int j = 0; j < vec.size(); j ++) {
sm = 0;
for (int i = 0; i < vec.size(); i ++) {
sm += abs(vec[i] - vec[j]);
}
ans = min(ans,sm);
}
cout << ans << endl;
return 0;
}
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:40:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main () {
^
fireworks.cpp: In function 'int main()':
fireworks.cpp:51:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < vec.size(); j ++) {
~~^~~~~~~~~~~~
fireworks.cpp:53:37: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < vec.size(); i ++) {
~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
5 ms |
5096 KB |
Output is correct |
3 |
Correct |
5 ms |
5168 KB |
Output is correct |
4 |
Correct |
6 ms |
5168 KB |
Output is correct |
5 |
Correct |
5 ms |
5168 KB |
Output is correct |
6 |
Correct |
5 ms |
5168 KB |
Output is correct |
7 |
Correct |
6 ms |
5276 KB |
Output is correct |
8 |
Correct |
6 ms |
5276 KB |
Output is correct |
9 |
Correct |
5 ms |
5276 KB |
Output is correct |
10 |
Correct |
5 ms |
5336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5336 KB |
Output is correct |
2 |
Correct |
8 ms |
5392 KB |
Output is correct |
3 |
Correct |
9 ms |
5392 KB |
Output is correct |
4 |
Correct |
11 ms |
5540 KB |
Output is correct |
5 |
Correct |
14 ms |
5540 KB |
Output is correct |
6 |
Correct |
14 ms |
5648 KB |
Output is correct |
7 |
Correct |
16 ms |
5648 KB |
Output is correct |
8 |
Correct |
18 ms |
5648 KB |
Output is correct |
9 |
Correct |
19 ms |
5776 KB |
Output is correct |
10 |
Correct |
37 ms |
5776 KB |
Output is correct |
11 |
Correct |
21 ms |
5904 KB |
Output is correct |
12 |
Correct |
34 ms |
6040 KB |
Output is correct |
13 |
Correct |
36 ms |
6040 KB |
Output is correct |
14 |
Correct |
37 ms |
6040 KB |
Output is correct |
15 |
Correct |
26 ms |
6060 KB |
Output is correct |
16 |
Correct |
25 ms |
6064 KB |
Output is correct |
17 |
Correct |
30 ms |
6064 KB |
Output is correct |
18 |
Correct |
26 ms |
6064 KB |
Output is correct |
19 |
Correct |
29 ms |
6088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
5 ms |
5096 KB |
Output is correct |
3 |
Correct |
5 ms |
5168 KB |
Output is correct |
4 |
Correct |
6 ms |
5168 KB |
Output is correct |
5 |
Correct |
5 ms |
5168 KB |
Output is correct |
6 |
Correct |
5 ms |
5168 KB |
Output is correct |
7 |
Correct |
6 ms |
5276 KB |
Output is correct |
8 |
Correct |
6 ms |
5276 KB |
Output is correct |
9 |
Correct |
5 ms |
5276 KB |
Output is correct |
10 |
Correct |
5 ms |
5336 KB |
Output is correct |
11 |
Correct |
6 ms |
5336 KB |
Output is correct |
12 |
Correct |
8 ms |
5392 KB |
Output is correct |
13 |
Correct |
9 ms |
5392 KB |
Output is correct |
14 |
Correct |
11 ms |
5540 KB |
Output is correct |
15 |
Correct |
14 ms |
5540 KB |
Output is correct |
16 |
Correct |
14 ms |
5648 KB |
Output is correct |
17 |
Correct |
16 ms |
5648 KB |
Output is correct |
18 |
Correct |
18 ms |
5648 KB |
Output is correct |
19 |
Correct |
19 ms |
5776 KB |
Output is correct |
20 |
Correct |
37 ms |
5776 KB |
Output is correct |
21 |
Correct |
21 ms |
5904 KB |
Output is correct |
22 |
Correct |
34 ms |
6040 KB |
Output is correct |
23 |
Correct |
36 ms |
6040 KB |
Output is correct |
24 |
Correct |
37 ms |
6040 KB |
Output is correct |
25 |
Correct |
26 ms |
6060 KB |
Output is correct |
26 |
Correct |
25 ms |
6064 KB |
Output is correct |
27 |
Correct |
30 ms |
6064 KB |
Output is correct |
28 |
Correct |
26 ms |
6064 KB |
Output is correct |
29 |
Correct |
29 ms |
6088 KB |
Output is correct |
30 |
Runtime error |
10 ms |
10160 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
5 ms |
5096 KB |
Output is correct |
3 |
Correct |
5 ms |
5168 KB |
Output is correct |
4 |
Correct |
6 ms |
5168 KB |
Output is correct |
5 |
Correct |
5 ms |
5168 KB |
Output is correct |
6 |
Correct |
5 ms |
5168 KB |
Output is correct |
7 |
Correct |
6 ms |
5276 KB |
Output is correct |
8 |
Correct |
6 ms |
5276 KB |
Output is correct |
9 |
Correct |
5 ms |
5276 KB |
Output is correct |
10 |
Correct |
5 ms |
5336 KB |
Output is correct |
11 |
Correct |
6 ms |
5336 KB |
Output is correct |
12 |
Correct |
8 ms |
5392 KB |
Output is correct |
13 |
Correct |
9 ms |
5392 KB |
Output is correct |
14 |
Correct |
11 ms |
5540 KB |
Output is correct |
15 |
Correct |
14 ms |
5540 KB |
Output is correct |
16 |
Correct |
14 ms |
5648 KB |
Output is correct |
17 |
Correct |
16 ms |
5648 KB |
Output is correct |
18 |
Correct |
18 ms |
5648 KB |
Output is correct |
19 |
Correct |
19 ms |
5776 KB |
Output is correct |
20 |
Correct |
37 ms |
5776 KB |
Output is correct |
21 |
Correct |
21 ms |
5904 KB |
Output is correct |
22 |
Correct |
34 ms |
6040 KB |
Output is correct |
23 |
Correct |
36 ms |
6040 KB |
Output is correct |
24 |
Correct |
37 ms |
6040 KB |
Output is correct |
25 |
Correct |
26 ms |
6060 KB |
Output is correct |
26 |
Correct |
25 ms |
6064 KB |
Output is correct |
27 |
Correct |
30 ms |
6064 KB |
Output is correct |
28 |
Correct |
26 ms |
6064 KB |
Output is correct |
29 |
Correct |
29 ms |
6088 KB |
Output is correct |
30 |
Runtime error |
10 ms |
10160 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
31 |
Halted |
0 ms |
0 KB |
- |