//In the name of God
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int maxn = 1024;
const ll mod = 1e9 + 7;
const ll inf = 1e15;
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r+", stdin);freopen("output.txt", "w+", stdout);
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) ll((x).size())
#define all(x) (x).begin(), (x).end()
ll pw(ll a, ll b){
ll c = 1;
while(b){
if(b & 1) c = c * a % mod;
a = a * a % mod;
b >>= 1;
}
return c;
}
int n, m, p[maxn], c[maxn];
vector<int> g[maxn];
ll dp[maxn][maxn];
void dfs(int v){
for(int u : g[v]){
dfs(u);
for(int i = c[v]; i < maxn; i++){
dp[v][i] += dp[u][i - c[v]];
}
}
if(g[v].empty()){
fill(dp[v], dp[v] + maxn, inf);
dp[v][c[v]] = 0;
}
if(v == 1) return;
for(int i = 0; i < c[v]; i++){
dp[v][i] = inf;
}
ll mn = inf;
for(int i = 0; i < maxn; i++){
dp[v][i] = min(dp[v][i], i + mn);
mn = min(mn, dp[v][i] - i);
}
mn = inf;
for(int i = maxn; i--;){
dp[v][i] = min(dp[v][i], mn - i);
mn = min(mn, dp[v][i] + i);
}
return;
}
int main(){
fast_io;
cin >> n >> m;
for(int i = 2; i <= n + m; i++){
cin >> p[i] >> c[i];
assert(i <= n || p[i] <= n);
g[p[i]].pb(i);
}
dfs(1);
ll ans = inf;
for(int i = 0; i < maxn; i++){
ans = min(ans, dp[1][i]);
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |