#include "bits/stdc++.h"
using namespace std;
#define ar array
#define int long long
const int N = 305;
const int inf = 1e18;
vector<ar<int, 2>> edges[N];
int dp[N][N];
void dfs(int u){
for(auto x : edges[u]){
dfs(x[0]);
for(int j=0;j<N;j++){
int pre = dp[u][j]; dp[u][j] = inf;
for(int l=0;l<=j;l++){
dp[u][j] = min(dp[u][j], pre + dp[x[0]][l] + abs(j - l - x[1]));
}
}
}
if(edges[u].empty()){
memset(dp[u], 63, sizeof dp[u]);
dp[u][0] = 0;
}
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, m; cin>>n>>m;
for(int i=1;i<n+m;i++){
int p, c; cin>>p>>c;
edges[--p].push_back({i, c});
}
if(n == 1){
sort(edges[0].begin(), edges[0].end(), [&](auto& a, auto& b){
return a[1] < b[1];
});
int C = edges[0][m / 2][1];
long long res = 0;
for(auto x : edges[0]) res += abs(C - x[1]);
cout<<res<<"\n";
return 0;
}
dfs(0);
int i = 1;
while(i < N && dp[0][i] <= dp[0][i-1]) i++;
cout<<dp[0][i-1]<<"\n";
while(i < N && dp[0][i] > dp[0][i-1]) i++;
assert(i == N);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
340 KB |
Output is correct |
3 |
Correct |
4 ms |
460 KB |
Output is correct |
4 |
Correct |
6 ms |
468 KB |
Output is correct |
5 |
Correct |
8 ms |
596 KB |
Output is correct |
6 |
Correct |
9 ms |
592 KB |
Output is correct |
7 |
Correct |
10 ms |
596 KB |
Output is correct |
8 |
Correct |
12 ms |
744 KB |
Output is correct |
9 |
Correct |
12 ms |
724 KB |
Output is correct |
10 |
Correct |
13 ms |
724 KB |
Output is correct |
11 |
Correct |
16 ms |
852 KB |
Output is correct |
12 |
Correct |
17 ms |
840 KB |
Output is correct |
13 |
Correct |
17 ms |
920 KB |
Output is correct |
14 |
Correct |
26 ms |
1000 KB |
Output is correct |
15 |
Correct |
20 ms |
980 KB |
Output is correct |
16 |
Correct |
20 ms |
1052 KB |
Output is correct |
17 |
Correct |
19 ms |
980 KB |
Output is correct |
18 |
Correct |
22 ms |
960 KB |
Output is correct |
19 |
Correct |
20 ms |
1144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
4 ms |
460 KB |
Output is correct |
14 |
Correct |
6 ms |
468 KB |
Output is correct |
15 |
Correct |
8 ms |
596 KB |
Output is correct |
16 |
Correct |
9 ms |
592 KB |
Output is correct |
17 |
Correct |
10 ms |
596 KB |
Output is correct |
18 |
Correct |
12 ms |
744 KB |
Output is correct |
19 |
Correct |
12 ms |
724 KB |
Output is correct |
20 |
Correct |
13 ms |
724 KB |
Output is correct |
21 |
Correct |
16 ms |
852 KB |
Output is correct |
22 |
Correct |
17 ms |
840 KB |
Output is correct |
23 |
Correct |
17 ms |
920 KB |
Output is correct |
24 |
Correct |
26 ms |
1000 KB |
Output is correct |
25 |
Correct |
20 ms |
980 KB |
Output is correct |
26 |
Correct |
20 ms |
1052 KB |
Output is correct |
27 |
Correct |
19 ms |
980 KB |
Output is correct |
28 |
Correct |
22 ms |
960 KB |
Output is correct |
29 |
Correct |
20 ms |
1144 KB |
Output is correct |
30 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
4 ms |
460 KB |
Output is correct |
14 |
Correct |
6 ms |
468 KB |
Output is correct |
15 |
Correct |
8 ms |
596 KB |
Output is correct |
16 |
Correct |
9 ms |
592 KB |
Output is correct |
17 |
Correct |
10 ms |
596 KB |
Output is correct |
18 |
Correct |
12 ms |
744 KB |
Output is correct |
19 |
Correct |
12 ms |
724 KB |
Output is correct |
20 |
Correct |
13 ms |
724 KB |
Output is correct |
21 |
Correct |
16 ms |
852 KB |
Output is correct |
22 |
Correct |
17 ms |
840 KB |
Output is correct |
23 |
Correct |
17 ms |
920 KB |
Output is correct |
24 |
Correct |
26 ms |
1000 KB |
Output is correct |
25 |
Correct |
20 ms |
980 KB |
Output is correct |
26 |
Correct |
20 ms |
1052 KB |
Output is correct |
27 |
Correct |
19 ms |
980 KB |
Output is correct |
28 |
Correct |
22 ms |
960 KB |
Output is correct |
29 |
Correct |
20 ms |
1144 KB |
Output is correct |
30 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
31 |
Halted |
0 ms |
0 KB |
- |