#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];
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);
int val[M];
fill(val,val+M,1e18);
for(int i = 0;i < M;i++){
for(int j = 0;j <= i;j++){
val[i] = min(val[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[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+m,x});
adj[i+m].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 |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
336 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 |
1740 KB |
Output is correct |
4 |
Runtime error |
362 ms |
524292 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
336 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 |
1740 KB |
Output is correct |
14 |
Runtime error |
362 ms |
524292 KB |
Execution killed with signal 9 |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
336 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 |
1740 KB |
Output is correct |
14 |
Runtime error |
362 ms |
524292 KB |
Execution killed with signal 9 |
15 |
Halted |
0 ms |
0 KB |
- |