#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;
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
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
4 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
604 KB |
Output is correct |
5 |
Correct |
7 ms |
716 KB |
Output is correct |
6 |
Correct |
9 ms |
716 KB |
Output is correct |
7 |
Correct |
9 ms |
604 KB |
Output is correct |
8 |
Correct |
11 ms |
856 KB |
Output is correct |
9 |
Correct |
12 ms |
860 KB |
Output is correct |
10 |
Correct |
13 ms |
860 KB |
Output is correct |
11 |
Correct |
15 ms |
860 KB |
Output is correct |
12 |
Correct |
16 ms |
1016 KB |
Output is correct |
13 |
Correct |
16 ms |
860 KB |
Output is correct |
14 |
Correct |
17 ms |
1152 KB |
Output is correct |
15 |
Correct |
18 ms |
1032 KB |
Output is correct |
16 |
Correct |
18 ms |
1116 KB |
Output is correct |
17 |
Correct |
18 ms |
1116 KB |
Output is correct |
18 |
Correct |
18 ms |
1128 KB |
Output is correct |
19 |
Correct |
18 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |