#include <bits/stdc++.h>
using namespace std;
const int N=5001;
const long long inf = 1e9;
int n,m;
vector<pair<int,long long>> edge[N];
long long dp[N][302];
long long sol(int u, int left)
{
if (edge[u].size() == 0)
{
return 0;
}
long long& ret = dp[u][left];
if (ret != -1)
{
return ret;
}
ret = 0;
for (pair<int, long long> p : edge[u])
{
long long sub = inf;
int v = p.first;
long long w = p.second;
if (edge[v].size() == 0)
{
sub = abs(w - left);
ret += sub;
continue;
}
for (int j = 0; j <= left; j++)
{
sub = min(sub, abs(w - j) + sol(v, left - j));
}
ret += sub;
}
return ret;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("dut03phoa.inp","r",stdin);
// freopen("dut03phoa.out","w",stdout);
cin >> n >> m;
if (n == 1)
{
vector<long long> v;
for (int i = 1; i < n + m; i++)
{
int p;
long long c;
cin >> p >> c;
v.emplace_back(c);
}
sort(v.begin(), v.end());
long long trungvi = v[v.size() / 2];
long long ans = 0;
for (int i = 0; i < v.size(); i++)
{
ans += abs(trungvi - v[i]);
}
cout << ans;
}
else
{
long long ans = 0;
for (int i = 1; i < n + m; i++)
{
int p;
long long c;
cin >> p >> c;
edge[p - 1].emplace_back(i, c);
ans += c;
}
memset(dp, -1, sizeof dp);
for (int i = 0; i <= 300; i++)
{
ans = min(ans, sol(0, i));
}
cout << ans;
}
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:60:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int i = 0; i < v.size(); i++)
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
440 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12236 KB |
Output is correct |
2 |
Correct |
8 ms |
12236 KB |
Output is correct |
3 |
Correct |
10 ms |
12176 KB |
Output is correct |
4 |
Correct |
29 ms |
12264 KB |
Output is correct |
5 |
Correct |
21 ms |
12256 KB |
Output is correct |
6 |
Correct |
22 ms |
12252 KB |
Output is correct |
7 |
Correct |
27 ms |
12236 KB |
Output is correct |
8 |
Correct |
33 ms |
12236 KB |
Output is correct |
9 |
Correct |
31 ms |
12236 KB |
Output is correct |
10 |
Correct |
34 ms |
12236 KB |
Output is correct |
11 |
Correct |
42 ms |
12236 KB |
Output is correct |
12 |
Correct |
39 ms |
12236 KB |
Output is correct |
13 |
Correct |
42 ms |
12248 KB |
Output is correct |
14 |
Correct |
91 ms |
12236 KB |
Output is correct |
15 |
Correct |
47 ms |
12236 KB |
Output is correct |
16 |
Correct |
40 ms |
12236 KB |
Output is correct |
17 |
Correct |
48 ms |
12236 KB |
Output is correct |
18 |
Correct |
58 ms |
12240 KB |
Output is correct |
19 |
Correct |
43 ms |
12236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
440 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
6 ms |
12236 KB |
Output is correct |
12 |
Correct |
8 ms |
12236 KB |
Output is correct |
13 |
Correct |
10 ms |
12176 KB |
Output is correct |
14 |
Correct |
29 ms |
12264 KB |
Output is correct |
15 |
Correct |
21 ms |
12256 KB |
Output is correct |
16 |
Correct |
22 ms |
12252 KB |
Output is correct |
17 |
Correct |
27 ms |
12236 KB |
Output is correct |
18 |
Correct |
33 ms |
12236 KB |
Output is correct |
19 |
Correct |
31 ms |
12236 KB |
Output is correct |
20 |
Correct |
34 ms |
12236 KB |
Output is correct |
21 |
Correct |
42 ms |
12236 KB |
Output is correct |
22 |
Correct |
39 ms |
12236 KB |
Output is correct |
23 |
Correct |
42 ms |
12248 KB |
Output is correct |
24 |
Correct |
91 ms |
12236 KB |
Output is correct |
25 |
Correct |
47 ms |
12236 KB |
Output is correct |
26 |
Correct |
40 ms |
12236 KB |
Output is correct |
27 |
Correct |
48 ms |
12236 KB |
Output is correct |
28 |
Correct |
58 ms |
12240 KB |
Output is correct |
29 |
Correct |
43 ms |
12236 KB |
Output is correct |
30 |
Incorrect |
91 ms |
12252 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
440 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
6 ms |
12236 KB |
Output is correct |
12 |
Correct |
8 ms |
12236 KB |
Output is correct |
13 |
Correct |
10 ms |
12176 KB |
Output is correct |
14 |
Correct |
29 ms |
12264 KB |
Output is correct |
15 |
Correct |
21 ms |
12256 KB |
Output is correct |
16 |
Correct |
22 ms |
12252 KB |
Output is correct |
17 |
Correct |
27 ms |
12236 KB |
Output is correct |
18 |
Correct |
33 ms |
12236 KB |
Output is correct |
19 |
Correct |
31 ms |
12236 KB |
Output is correct |
20 |
Correct |
34 ms |
12236 KB |
Output is correct |
21 |
Correct |
42 ms |
12236 KB |
Output is correct |
22 |
Correct |
39 ms |
12236 KB |
Output is correct |
23 |
Correct |
42 ms |
12248 KB |
Output is correct |
24 |
Correct |
91 ms |
12236 KB |
Output is correct |
25 |
Correct |
47 ms |
12236 KB |
Output is correct |
26 |
Correct |
40 ms |
12236 KB |
Output is correct |
27 |
Correct |
48 ms |
12236 KB |
Output is correct |
28 |
Correct |
58 ms |
12240 KB |
Output is correct |
29 |
Correct |
43 ms |
12236 KB |
Output is correct |
30 |
Incorrect |
91 ms |
12252 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |