#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 505;
int n, m, L[MAXN], R[MAXN];
vector<pair<int, int> > adj[MAXN];
ll walk[MAXN], dp[MAXN][MAXN];
void precalc(int node){
for(pair<int, int> x : adj[node]){
walk[x.first] = walk[node] + x.second;
precalc(x.first);
}
}
void dfs(int node){
if(node >= n){
for(int i = 0; i < MAXN; ++i) dp[node][i] = 1e9;
dp[node][walk[node]] = 0;
}
for(pair<int, int> x : adj[node]){
dfs(x.first);
for(int i = 0; i < MAXN; ++i){
ll mini = 1e9;
for(int j = 0; j < MAXN; ++j){
if(x.second + i - j >= 0) mini = min(mini, dp[x.first][j] + abs(i - j));
}
dp[node][i] += mini;
}
}
}
int main(){
cin >> n >> m;
for(int i = 1; i < n + m; ++i){
int p, c;
cin >> p >> c, --p;
adj[p].push_back({i, c});
}
precalc(0);
dfs(0);
ll ans = 1e9;
for(int i = 0; i < MAXN; ++i) ans = min(ans, dp[0][i]);
for(int i = 0; i < MAXN; ++i){
if(dp[0][i] == ans){
for(int j = 0; j < i; ++j) assert(dp[0][j] > dp[0][j + 1]);
for(int j = i; j < n - 1; ++j) assert(dp[0][j] < dp[0][j + 1]);
}
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
456 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
6 ms |
580 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
456 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
456 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |