#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e2+6;
const ll inf = 1e18;
int n, m;
ll dp[N][N];
vector<pair<int,int> > child[N];
void subtask1()
{
sort(child[1].begin(), child[1].end());
ll suf = 0, prf = 0;
for(auto [w, u] : child[1])
suf += w;
ll ans = inf;
for(int i = 0; i < child[1].size(); i++)
{
ll w = child[1][i].first, rem = child[1].size() - i;
ans = min(ans, i * w - prf + suf - rem * w);
prf += w;
suf -= w;
}
cout << ans << endl;
}
void dfs(int v)
{
if(child[v].empty())
{
for(int i = 1; i < N; i ++)
dp[v][i] = inf;
return;
}
for(auto [w, u] : child[v])
{
dfs(u);
for(int i = 0; i < N; i ++)
{
ll ans = N * N;
for(int j = 0; j <= i; j++)
ans = min(ans, abs(w - j) + dp[u][i - j]);
dp[v][i] += ans;
dp[v][i] = min(dp[v][i], inf);
}
}
}
void subtask2()
{
dfs(1);
for(int i = 0; i < N; i ++)
dp[1][0] = min(dp[1][i], dp[1][0]);
cout << dp[1][0] << endl;
}
int main()
{
cin >> n >> m;
for(int i = 2; i <= n + m; i ++)
{
int p, w;
cin >> p >> w;
child[p].push_back({w, i});
}
if(n == 1)
subtask1();
else
subtask2();
return 0;
}
Compilation message
fireworks.cpp: In function 'void subtask1()':
fireworks.cpp:20:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for(int i = 0; i < child[1].size(); i++)
| ~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
4 ms |
604 KB |
Output is correct |
5 |
Correct |
7 ms |
860 KB |
Output is correct |
6 |
Correct |
6 ms |
728 KB |
Output is correct |
7 |
Correct |
8 ms |
600 KB |
Output is correct |
8 |
Correct |
8 ms |
856 KB |
Output is correct |
9 |
Correct |
9 ms |
860 KB |
Output is correct |
10 |
Correct |
12 ms |
772 KB |
Output is correct |
11 |
Correct |
11 ms |
860 KB |
Output is correct |
12 |
Correct |
12 ms |
860 KB |
Output is correct |
13 |
Correct |
14 ms |
908 KB |
Output is correct |
14 |
Correct |
14 ms |
1068 KB |
Output is correct |
15 |
Correct |
14 ms |
1176 KB |
Output is correct |
16 |
Correct |
14 ms |
1176 KB |
Output is correct |
17 |
Correct |
14 ms |
1112 KB |
Output is correct |
18 |
Correct |
14 ms |
1116 KB |
Output is correct |
19 |
Correct |
14 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
4 ms |
604 KB |
Output is correct |
15 |
Correct |
7 ms |
860 KB |
Output is correct |
16 |
Correct |
6 ms |
728 KB |
Output is correct |
17 |
Correct |
8 ms |
600 KB |
Output is correct |
18 |
Correct |
8 ms |
856 KB |
Output is correct |
19 |
Correct |
9 ms |
860 KB |
Output is correct |
20 |
Correct |
12 ms |
772 KB |
Output is correct |
21 |
Correct |
11 ms |
860 KB |
Output is correct |
22 |
Correct |
12 ms |
860 KB |
Output is correct |
23 |
Correct |
14 ms |
908 KB |
Output is correct |
24 |
Correct |
14 ms |
1068 KB |
Output is correct |
25 |
Correct |
14 ms |
1176 KB |
Output is correct |
26 |
Correct |
14 ms |
1176 KB |
Output is correct |
27 |
Correct |
14 ms |
1112 KB |
Output is correct |
28 |
Correct |
14 ms |
1116 KB |
Output is correct |
29 |
Correct |
14 ms |
1116 KB |
Output is correct |
30 |
Runtime error |
1 ms |
856 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
4 ms |
604 KB |
Output is correct |
15 |
Correct |
7 ms |
860 KB |
Output is correct |
16 |
Correct |
6 ms |
728 KB |
Output is correct |
17 |
Correct |
8 ms |
600 KB |
Output is correct |
18 |
Correct |
8 ms |
856 KB |
Output is correct |
19 |
Correct |
9 ms |
860 KB |
Output is correct |
20 |
Correct |
12 ms |
772 KB |
Output is correct |
21 |
Correct |
11 ms |
860 KB |
Output is correct |
22 |
Correct |
12 ms |
860 KB |
Output is correct |
23 |
Correct |
14 ms |
908 KB |
Output is correct |
24 |
Correct |
14 ms |
1068 KB |
Output is correct |
25 |
Correct |
14 ms |
1176 KB |
Output is correct |
26 |
Correct |
14 ms |
1176 KB |
Output is correct |
27 |
Correct |
14 ms |
1112 KB |
Output is correct |
28 |
Correct |
14 ms |
1116 KB |
Output is correct |
29 |
Correct |
14 ms |
1116 KB |
Output is correct |
30 |
Runtime error |
1 ms |
856 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |