#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
int n, m;
vector< pair<int, int> > adj[310];
long long dp[310][310];
void dfs (int x) {
if (adj[x].size() == 0) {
dp[x][0] = 0;
for (int i=1; i<=300; i++) {
dp[x][i] = 1e16;
}
return;
}
for (auto i: adj[x]) {
dfs(i.first);
}
for (int i=0; i<=300; i++) {
for (auto j: adj[x]) {
long long best = 1e16;
for (int k=0; k<=i; k++) {
best = min(best, dp[j.first][k]+abs(i-k-j.second));
}
dp[x][i] += best;
}
}
}
int main () {
cin >> n >> m;
if (n == 1) {
int a[m];
int x;
for (int i=0; i<m; i++) {
cin >> x >> a[i];
}
long long o = 1e16;
for (int i=0; i<m; i++) {
long long cur = 0;
for (int j=0; j<m; j++) {
cur += abs(a[i]-a[j]);
}
o = min(o, cur);
}
cout << o << endl;
return 0;
}
int pi, wi;
for (int i=2; i<=n+m; i++) {
cin >> pi >> wi;
adj[pi].push_back({i, wi});
}
dfs(1);
long long o = 1e16;
for (int i=0; i<=300; i++) {
o = min(o, dp[1][i]);
}
cout << o << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
8 ms |
640 KB |
Output is correct |
6 |
Correct |
8 ms |
640 KB |
Output is correct |
7 |
Correct |
12 ms |
640 KB |
Output is correct |
8 |
Correct |
18 ms |
768 KB |
Output is correct |
9 |
Correct |
19 ms |
768 KB |
Output is correct |
10 |
Correct |
14 ms |
896 KB |
Output is correct |
11 |
Correct |
24 ms |
896 KB |
Output is correct |
12 |
Correct |
16 ms |
896 KB |
Output is correct |
13 |
Correct |
17 ms |
1024 KB |
Output is correct |
14 |
Correct |
18 ms |
1024 KB |
Output is correct |
15 |
Correct |
19 ms |
1080 KB |
Output is correct |
16 |
Correct |
19 ms |
1024 KB |
Output is correct |
17 |
Correct |
18 ms |
1024 KB |
Output is correct |
18 |
Correct |
19 ms |
1024 KB |
Output is correct |
19 |
Correct |
23 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
8 ms |
640 KB |
Output is correct |
16 |
Correct |
8 ms |
640 KB |
Output is correct |
17 |
Correct |
12 ms |
640 KB |
Output is correct |
18 |
Correct |
18 ms |
768 KB |
Output is correct |
19 |
Correct |
19 ms |
768 KB |
Output is correct |
20 |
Correct |
14 ms |
896 KB |
Output is correct |
21 |
Correct |
24 ms |
896 KB |
Output is correct |
22 |
Correct |
16 ms |
896 KB |
Output is correct |
23 |
Correct |
17 ms |
1024 KB |
Output is correct |
24 |
Correct |
18 ms |
1024 KB |
Output is correct |
25 |
Correct |
19 ms |
1080 KB |
Output is correct |
26 |
Correct |
19 ms |
1024 KB |
Output is correct |
27 |
Correct |
18 ms |
1024 KB |
Output is correct |
28 |
Correct |
19 ms |
1024 KB |
Output is correct |
29 |
Correct |
23 ms |
1024 KB |
Output is correct |
30 |
Runtime error |
1 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
8 ms |
640 KB |
Output is correct |
16 |
Correct |
8 ms |
640 KB |
Output is correct |
17 |
Correct |
12 ms |
640 KB |
Output is correct |
18 |
Correct |
18 ms |
768 KB |
Output is correct |
19 |
Correct |
19 ms |
768 KB |
Output is correct |
20 |
Correct |
14 ms |
896 KB |
Output is correct |
21 |
Correct |
24 ms |
896 KB |
Output is correct |
22 |
Correct |
16 ms |
896 KB |
Output is correct |
23 |
Correct |
17 ms |
1024 KB |
Output is correct |
24 |
Correct |
18 ms |
1024 KB |
Output is correct |
25 |
Correct |
19 ms |
1080 KB |
Output is correct |
26 |
Correct |
19 ms |
1024 KB |
Output is correct |
27 |
Correct |
18 ms |
1024 KB |
Output is correct |
28 |
Correct |
19 ms |
1024 KB |
Output is correct |
29 |
Correct |
23 ms |
1024 KB |
Output is correct |
30 |
Runtime error |
1 ms |
512 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
31 |
Halted |
0 ms |
0 KB |
- |