#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
int main(){
int N, M;
cin >> N >> M;
vector<int> P(N + M), C(N + M);
for (int i = 1; i < N + M; i++){
cin >> P[i] >> C[i];
P[i]--;
}
vector<vector<int>> c(N + M);
for (int i = 1; i < N + M; i++){
c[P[i]].push_back(i);
}
vector<vector<int>> dp(N + M, vector<int>(301));
for (int i = N; i < N + M; i++){
dp[i][0] = 0;
for (int j = 1; j <= 300; j++){
dp[i][j] = INF;
}
}
for (int i = N - 1; i >= 0; i--){
for (int j : c[i]){
vector<int> dp2(301, INF);
for (int k = 0; k <= 300; k++){
for (int l = 0; k + l <= 300; l++){
dp2[k + l] = min(dp2[k + l], dp[j][k] + abs(C[j] - l));
}
}
for (int k = 0; k <= 300; k++){
dp[i][k] += dp2[k];
}
}
}
int ans = INF;
for (int i = 0; i <= 300; i++){
ans = min(ans, dp[0][i]);
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
340 KB |
Output is correct |
4 |
Correct |
7 ms |
412 KB |
Output is correct |
5 |
Correct |
9 ms |
444 KB |
Output is correct |
6 |
Correct |
9 ms |
340 KB |
Output is correct |
7 |
Correct |
15 ms |
468 KB |
Output is correct |
8 |
Correct |
14 ms |
468 KB |
Output is correct |
9 |
Correct |
18 ms |
468 KB |
Output is correct |
10 |
Correct |
20 ms |
560 KB |
Output is correct |
11 |
Correct |
18 ms |
596 KB |
Output is correct |
12 |
Correct |
19 ms |
620 KB |
Output is correct |
13 |
Correct |
24 ms |
596 KB |
Output is correct |
14 |
Correct |
21 ms |
668 KB |
Output is correct |
15 |
Correct |
20 ms |
596 KB |
Output is correct |
16 |
Correct |
21 ms |
596 KB |
Output is correct |
17 |
Correct |
19 ms |
596 KB |
Output is correct |
18 |
Correct |
21 ms |
672 KB |
Output is correct |
19 |
Correct |
27 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |