#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
typedef long long ll;
using namespace std;
const int maxi = 307, inf = 1e9 + 79;
vector<vector<int> > dp(maxi, vector<int>(maxi, inf));
vector<vector<pair<int, int> > > g(maxi);
int n, m;
int rek(int u, int dist, int c)
{
if (u >= n) return abs(c - dist);
if (dp[u][dist] != inf) return dp[u][dist];
for (int cl = 0; cl <= dist; cl++)
{
int sum = abs(cl - c);
for (pair<int, int> v : g[u])
sum += rek(v.first, dist - cl, v.second);
dp[u][dist] = min(dp[u][dist], sum);
if (!c) break;
}
return dp[u][dist];
}
void subtask1()
{
vector<int> v;
for (int i = 1, p, c; i < n + m; i++)
{
cin >> p >> c;
v.push_back(c);
}
sort(v.begin(), v.end());
ll ans = 0;
for (int i = 0; i < v.size(); i++) ans += abs(v[i] - v[(v.size() / 2)]);
cout << ans << "\n";
exit(0);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
if (n == 1) subtask1();
for (int i = 1, p, c; i < n + m; i++)
{
cin >> p >> c;
g[--p].push_back({ i, c });
}
int ans = inf;
for (int dist = 0; dist < maxi; dist++)
ans = min(ans, rek(0, dist, 0));
cout << ans << "\n";
return 0;
}
Compilation message
fireworks.cpp: In function 'void subtask1()':
fireworks.cpp:40:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i = 0; i < v.size(); i++) ans += abs(v[i] - v[(v.size() / 2)]);
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
712 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
708 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
588 KB |
Output is correct |
2 |
Correct |
8 ms |
708 KB |
Output is correct |
3 |
Correct |
14 ms |
592 KB |
Output is correct |
4 |
Correct |
54 ms |
716 KB |
Output is correct |
5 |
Correct |
37 ms |
588 KB |
Output is correct |
6 |
Correct |
41 ms |
588 KB |
Output is correct |
7 |
Correct |
46 ms |
588 KB |
Output is correct |
8 |
Correct |
53 ms |
588 KB |
Output is correct |
9 |
Correct |
61 ms |
588 KB |
Output is correct |
10 |
Correct |
69 ms |
588 KB |
Output is correct |
11 |
Correct |
87 ms |
588 KB |
Output is correct |
12 |
Correct |
82 ms |
708 KB |
Output is correct |
13 |
Correct |
89 ms |
588 KB |
Output is correct |
14 |
Correct |
170 ms |
720 KB |
Output is correct |
15 |
Correct |
97 ms |
696 KB |
Output is correct |
16 |
Correct |
94 ms |
588 KB |
Output is correct |
17 |
Correct |
94 ms |
716 KB |
Output is correct |
18 |
Correct |
95 ms |
716 KB |
Output is correct |
19 |
Correct |
87 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
712 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
708 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
3 ms |
588 KB |
Output is correct |
12 |
Correct |
8 ms |
708 KB |
Output is correct |
13 |
Correct |
14 ms |
592 KB |
Output is correct |
14 |
Correct |
54 ms |
716 KB |
Output is correct |
15 |
Correct |
37 ms |
588 KB |
Output is correct |
16 |
Correct |
41 ms |
588 KB |
Output is correct |
17 |
Correct |
46 ms |
588 KB |
Output is correct |
18 |
Correct |
53 ms |
588 KB |
Output is correct |
19 |
Correct |
61 ms |
588 KB |
Output is correct |
20 |
Correct |
69 ms |
588 KB |
Output is correct |
21 |
Correct |
87 ms |
588 KB |
Output is correct |
22 |
Correct |
82 ms |
708 KB |
Output is correct |
23 |
Correct |
89 ms |
588 KB |
Output is correct |
24 |
Correct |
170 ms |
720 KB |
Output is correct |
25 |
Correct |
97 ms |
696 KB |
Output is correct |
26 |
Correct |
94 ms |
588 KB |
Output is correct |
27 |
Correct |
94 ms |
716 KB |
Output is correct |
28 |
Correct |
95 ms |
716 KB |
Output is correct |
29 |
Correct |
87 ms |
588 KB |
Output is correct |
30 |
Execution timed out |
2082 ms |
716 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
712 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
708 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
3 ms |
588 KB |
Output is correct |
12 |
Correct |
8 ms |
708 KB |
Output is correct |
13 |
Correct |
14 ms |
592 KB |
Output is correct |
14 |
Correct |
54 ms |
716 KB |
Output is correct |
15 |
Correct |
37 ms |
588 KB |
Output is correct |
16 |
Correct |
41 ms |
588 KB |
Output is correct |
17 |
Correct |
46 ms |
588 KB |
Output is correct |
18 |
Correct |
53 ms |
588 KB |
Output is correct |
19 |
Correct |
61 ms |
588 KB |
Output is correct |
20 |
Correct |
69 ms |
588 KB |
Output is correct |
21 |
Correct |
87 ms |
588 KB |
Output is correct |
22 |
Correct |
82 ms |
708 KB |
Output is correct |
23 |
Correct |
89 ms |
588 KB |
Output is correct |
24 |
Correct |
170 ms |
720 KB |
Output is correct |
25 |
Correct |
97 ms |
696 KB |
Output is correct |
26 |
Correct |
94 ms |
588 KB |
Output is correct |
27 |
Correct |
94 ms |
716 KB |
Output is correct |
28 |
Correct |
95 ms |
716 KB |
Output is correct |
29 |
Correct |
87 ms |
588 KB |
Output is correct |
30 |
Execution timed out |
2082 ms |
716 KB |
Time limit exceeded |
31 |
Halted |
0 ms |
0 KB |
- |