#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair<int,int> pii;
typedef vector<long long> vl;
typedef vector<int> vi;
int n, m;
const int N = 2e5 + 10, M = 3e2 + 10;
vector<pair<ll, ll> > G[N];
ll dp[M][M], out[N];
const ll inf = 1e15;
void dfs(int node, int parent) {
if(!out[node]) {
for(int i = 0; i < M; i++) {
dp[node][i] = inf;
}
dp[node][0] = 0;
return;
}
for(auto i: G[node]) {
if(i.first == parent) continue;
dfs(i.first, node);
for(int j = 0; j < M; j++) {
long long mini = INT64_MAX;
for(int k = 0; k <= j; k++) {
if(dp[i.first][k] == inf) {
continue;
}
mini = min(mini, dp[i.first][k] + abs(i.second - (j - k)));
}
dp[node][j] += mini;
}
}
}
int main(){
cin>>n>>m;
for(int i = 2; i <= n; i++) {
long long p, c; cin >> p >> c;
G[i].pb({p, c});
G[p].pb({i, c});
out[p]++;
}
for(int i = n + 1; i <= n + m; i++) {
long long p, c; cin >> p >> c;
G[i].push_back({p, c});
G[p].push_back({i, c});
out[p]++;
}
// cout<<endl;
dfs(1, 0);
ll ans = INT64_MAX;
for(int i = 0; i < M; i++) {
ans = min(ans, dp[1][i]);
}
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4948 KB |
Output is correct |
2 |
Correct |
4 ms |
5076 KB |
Output is correct |
3 |
Correct |
5 ms |
5128 KB |
Output is correct |
4 |
Correct |
7 ms |
5128 KB |
Output is correct |
5 |
Correct |
8 ms |
5204 KB |
Output is correct |
6 |
Correct |
12 ms |
5332 KB |
Output is correct |
7 |
Correct |
9 ms |
5384 KB |
Output is correct |
8 |
Correct |
12 ms |
5332 KB |
Output is correct |
9 |
Correct |
13 ms |
5460 KB |
Output is correct |
10 |
Correct |
12 ms |
5520 KB |
Output is correct |
11 |
Correct |
15 ms |
5592 KB |
Output is correct |
12 |
Correct |
18 ms |
5588 KB |
Output is correct |
13 |
Correct |
15 ms |
5564 KB |
Output is correct |
14 |
Correct |
19 ms |
5720 KB |
Output is correct |
15 |
Correct |
19 ms |
5656 KB |
Output is correct |
16 |
Correct |
20 ms |
5756 KB |
Output is correct |
17 |
Correct |
16 ms |
5648 KB |
Output is correct |
18 |
Correct |
16 ms |
5644 KB |
Output is correct |
19 |
Correct |
17 ms |
5684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |