#include <iostream>
using namespace std;
#include <vector>
#define vi vector<int>
#define ll long long
#include <algorithm>
#include <set>
#include <string>
#include <bitset>
#include <cmath>
#include <math.h>
#define pll pair<ll,ll>
#define vll vector<ll>
#define pi pair<int,int>
#include <map>
#include <queue>
#define x first
#define y second
#define pd pair<double,double>
const int siz = 301;
int pad[siz];
int w[siz];
vi son[siz];
ll dp[siz][siz];
int main()
{
int n, m;
cin >> n >> m;
if (n == 1) {
vi v;
for (int i = 1; i < n + m; i++) {
int p, c;
cin >> p >> c;
p--;
v.push_back(c);
}
sort(v.begin(), v.end());
ll ans = 0;
for (int i = 0; i < m; i++) {
ans += abs(v[m / 2] - v[i]);
}
cout << ans;
}
else {
for (int i = 1; i < n + m; i++) {
int p, c;
cin >> p >> c;
p--;
pad[i] = p, w[i] = c;
son[p].push_back(i);
}
for (int i = n + m - 1; i >= 0; i--) {
for (int j = 0; j < siz; j++) {
if (i >= n && j > 0)dp[i][j] = 1e9;
}
}
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < siz; j++) {
for (int s : son[i]) {
int pl = 1e9;
for (int k = 0; k <= j; k++) {
pl = min((ll)pl, abs(w[s] - k) + dp[s][j - k]);
}
dp[i][j] += pl;
}
}
}
ll ans = 1e9;
for (int i = 0; i < siz; i++)ans = min(ans, dp[0][i]);
cout << ans;
}
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
604 KB |
Output is correct |
5 |
Correct |
8 ms |
756 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
9 ms |
604 KB |
Output is correct |
8 |
Correct |
11 ms |
768 KB |
Output is correct |
9 |
Correct |
12 ms |
812 KB |
Output is correct |
10 |
Correct |
13 ms |
860 KB |
Output is correct |
11 |
Correct |
14 ms |
860 KB |
Output is correct |
12 |
Correct |
15 ms |
860 KB |
Output is correct |
13 |
Correct |
16 ms |
916 KB |
Output is correct |
14 |
Correct |
18 ms |
1116 KB |
Output is correct |
15 |
Correct |
18 ms |
1116 KB |
Output is correct |
16 |
Correct |
18 ms |
1128 KB |
Output is correct |
17 |
Correct |
18 ms |
1116 KB |
Output is correct |
18 |
Correct |
18 ms |
1112 KB |
Output is correct |
19 |
Correct |
18 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
3 ms |
344 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
14 |
Correct |
6 ms |
604 KB |
Output is correct |
15 |
Correct |
8 ms |
756 KB |
Output is correct |
16 |
Correct |
8 ms |
604 KB |
Output is correct |
17 |
Correct |
9 ms |
604 KB |
Output is correct |
18 |
Correct |
11 ms |
768 KB |
Output is correct |
19 |
Correct |
12 ms |
812 KB |
Output is correct |
20 |
Correct |
13 ms |
860 KB |
Output is correct |
21 |
Correct |
14 ms |
860 KB |
Output is correct |
22 |
Correct |
15 ms |
860 KB |
Output is correct |
23 |
Correct |
16 ms |
916 KB |
Output is correct |
24 |
Correct |
18 ms |
1116 KB |
Output is correct |
25 |
Correct |
18 ms |
1116 KB |
Output is correct |
26 |
Correct |
18 ms |
1128 KB |
Output is correct |
27 |
Correct |
18 ms |
1116 KB |
Output is correct |
28 |
Correct |
18 ms |
1112 KB |
Output is correct |
29 |
Correct |
18 ms |
1116 KB |
Output is correct |
30 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1 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 |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
3 ms |
344 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
14 |
Correct |
6 ms |
604 KB |
Output is correct |
15 |
Correct |
8 ms |
756 KB |
Output is correct |
16 |
Correct |
8 ms |
604 KB |
Output is correct |
17 |
Correct |
9 ms |
604 KB |
Output is correct |
18 |
Correct |
11 ms |
768 KB |
Output is correct |
19 |
Correct |
12 ms |
812 KB |
Output is correct |
20 |
Correct |
13 ms |
860 KB |
Output is correct |
21 |
Correct |
14 ms |
860 KB |
Output is correct |
22 |
Correct |
15 ms |
860 KB |
Output is correct |
23 |
Correct |
16 ms |
916 KB |
Output is correct |
24 |
Correct |
18 ms |
1116 KB |
Output is correct |
25 |
Correct |
18 ms |
1116 KB |
Output is correct |
26 |
Correct |
18 ms |
1128 KB |
Output is correct |
27 |
Correct |
18 ms |
1116 KB |
Output is correct |
28 |
Correct |
18 ms |
1112 KB |
Output is correct |
29 |
Correct |
18 ms |
1116 KB |
Output is correct |
30 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |