//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()){
assert(i > n);
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;
}
Compilation message
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from fireworks.cpp:2:
fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:44:15: error: 'i' was not declared in this scope
44 | assert(i > n);
| ^