#include <bits/stdc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 605, M = 305;
vector<pair<int,int>> adj[N];
int dp[N][M], val[N][M];
void dfs(int u, int p){
if(adj[u].size()==1&&u!=1){
dp[u][0] = 0;
}
for(auto [v,w] : adj[u]){
if(v == p) continue;
dfs(v,u);
fill(val[u],val[u]+M,1e18);
for(int i = 0;i < M;i++){
for(int j = 0;j <= i;j++){
val[u][i] = min(val[u][i], abs(w-(i-j))+dp[v][j]);
}
}
for(int i = 0;i < M;i++){
if(dp[u][i]==1e18) dp[u][i] = 0;
dp[u][i] += val[u][i];
}
}
}
signed main(){
//fastio
int n,m;
cin >> n >> m;
if(n==1){
vector<int> tmp;
for(int i = 0;i < m;i++){
int a,x;
cin >> a >> x;
tmp.push_back(x);
}
sort(tmp.begin(),tmp.end());
int to = tmp[tmp.size()/2];
int ans = 0;
for(auto x : tmp){
ans += abs(x-to);
}
cout << ans << "\n";
}else{
for(int i = 2;i <= n;i++){
int p,x;
cin >> p >> x;
adj[p].push_back({i,x});
adj[i].push_back({p,x});
}
for(int i = 1;i <= m;i++){
int p,x;
cin >> p >> x;
adj[p].push_back({i+n,x});
adj[i+n].push_back({p,x});
}
fill(&dp[0][0],&dp[N-1][M-1],1e18);
dfs(1,1);
cout << *min_element(dp[1],dp[1]+M) << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1740 KB |
Output is correct |
2 |
Correct |
3 ms |
1740 KB |
Output is correct |
3 |
Correct |
5 ms |
1808 KB |
Output is correct |
4 |
Correct |
7 ms |
1952 KB |
Output is correct |
5 |
Correct |
8 ms |
1908 KB |
Output is correct |
6 |
Correct |
9 ms |
1924 KB |
Output is correct |
7 |
Correct |
10 ms |
1868 KB |
Output is correct |
8 |
Correct |
15 ms |
1936 KB |
Output is correct |
9 |
Correct |
12 ms |
1868 KB |
Output is correct |
10 |
Correct |
13 ms |
1996 KB |
Output is correct |
11 |
Correct |
15 ms |
1960 KB |
Output is correct |
12 |
Correct |
16 ms |
1968 KB |
Output is correct |
13 |
Correct |
17 ms |
1976 KB |
Output is correct |
14 |
Correct |
18 ms |
2404 KB |
Output is correct |
15 |
Correct |
21 ms |
1996 KB |
Output is correct |
16 |
Correct |
18 ms |
1996 KB |
Output is correct |
17 |
Correct |
23 ms |
1996 KB |
Output is correct |
18 |
Correct |
18 ms |
1996 KB |
Output is correct |
19 |
Correct |
19 ms |
1996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
3 ms |
1740 KB |
Output is correct |
12 |
Correct |
3 ms |
1740 KB |
Output is correct |
13 |
Correct |
5 ms |
1808 KB |
Output is correct |
14 |
Correct |
7 ms |
1952 KB |
Output is correct |
15 |
Correct |
8 ms |
1908 KB |
Output is correct |
16 |
Correct |
9 ms |
1924 KB |
Output is correct |
17 |
Correct |
10 ms |
1868 KB |
Output is correct |
18 |
Correct |
15 ms |
1936 KB |
Output is correct |
19 |
Correct |
12 ms |
1868 KB |
Output is correct |
20 |
Correct |
13 ms |
1996 KB |
Output is correct |
21 |
Correct |
15 ms |
1960 KB |
Output is correct |
22 |
Correct |
16 ms |
1968 KB |
Output is correct |
23 |
Correct |
17 ms |
1976 KB |
Output is correct |
24 |
Correct |
18 ms |
2404 KB |
Output is correct |
25 |
Correct |
21 ms |
1996 KB |
Output is correct |
26 |
Correct |
18 ms |
1996 KB |
Output is correct |
27 |
Correct |
23 ms |
1996 KB |
Output is correct |
28 |
Correct |
18 ms |
1996 KB |
Output is correct |
29 |
Correct |
19 ms |
1996 KB |
Output is correct |
30 |
Incorrect |
28 ms |
2456 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
3 ms |
1740 KB |
Output is correct |
12 |
Correct |
3 ms |
1740 KB |
Output is correct |
13 |
Correct |
5 ms |
1808 KB |
Output is correct |
14 |
Correct |
7 ms |
1952 KB |
Output is correct |
15 |
Correct |
8 ms |
1908 KB |
Output is correct |
16 |
Correct |
9 ms |
1924 KB |
Output is correct |
17 |
Correct |
10 ms |
1868 KB |
Output is correct |
18 |
Correct |
15 ms |
1936 KB |
Output is correct |
19 |
Correct |
12 ms |
1868 KB |
Output is correct |
20 |
Correct |
13 ms |
1996 KB |
Output is correct |
21 |
Correct |
15 ms |
1960 KB |
Output is correct |
22 |
Correct |
16 ms |
1968 KB |
Output is correct |
23 |
Correct |
17 ms |
1976 KB |
Output is correct |
24 |
Correct |
18 ms |
2404 KB |
Output is correct |
25 |
Correct |
21 ms |
1996 KB |
Output is correct |
26 |
Correct |
18 ms |
1996 KB |
Output is correct |
27 |
Correct |
23 ms |
1996 KB |
Output is correct |
28 |
Correct |
18 ms |
1996 KB |
Output is correct |
29 |
Correct |
19 ms |
1996 KB |
Output is correct |
30 |
Incorrect |
28 ms |
2456 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |