#include using namespace std; #include using namespace atcoder; using mint = atcoder::modint998244353; // using mint = double; #define rep(i, l, r) for (int i = (int)(l); i<(int)(r); i++) #define ll long long #define ld long double #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define siz(x) (int)(x).size() template bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } const int inf = 1e9; const ll INF = 4e18; template using pq = priority_queue, less>; template using spq = priority_queue, greater>; vector di = {0, 0, 1, -1}; vector dj = {1, -1, 0, 0}; struct Edge { int to, cost; }; void solve() { int N, X; cin >> N >> X; vector> cld(N); rep(i, 1, N) { int p; cin >> p; p--; cld[p].push_back(i); } vector c(N); vector s(N); c[0] = 0; s[0] = 0; rep(i, 1, N) cin >> c[i] >> s[i]; vector size(N); vector> rem(N); auto dfs = [&](auto dfs, int v) -> void { int csiz = s[v]; vector dp(csiz+1, INF); dp[s[v]] = c[v]; for (int to : cld[v]) { dfs(dfs, to); int nsiz = csiz + size[to]; vector ndp(nsiz + 1, INF); rep(j, 0, csiz + 1) { rep(k, 0, size[to] + 1) { chmin(ndp[j+k], dp[j] + rem[to][k]); } chmin(ndp[j], dp[j]); } swap(csiz, nsiz); swap(dp, ndp); } swap(size[v], csiz); swap(dp, rem[v]); }; dfs(dfs, 0); // rep(i, 0, N) cout << size[i] << " "; // cout << endl; // rep(i, 0, N) { // for (ll v : rem[i]) cout << v << " "; // cout << endl; // } ll ans = INF; rep(i, X, size[0]+1) chmin(ans, rem[0][i]); cout << ans << endl; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int T = 1; // cin >> T; while(T--) { solve(); } }