#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, x; cin >> n >> x; VVI to(n); for (int i = 1; i < n; i++) { int p; cin >> p; to[p-1].emplace_back(i); } VI c(n), s(n); for (int i = 1; i < n; i++) cin >> c[i] >> s[i]; constexpr long long INF = 1002003004005006007; auto dfs = [&](auto&& self, int u) -> VL { VL res{0}; for (int v : to[u]) { auto r = self(self, v); static VL tmp; int sz1 = res.size(), sz2 = r.size(); tmp.assign(sz1 + sz2 - 1, INF); rep(i, sz1) rep(j, sz2) chmin(tmp[i+j], res[i] + r[j]); res = tmp; } for (auto& x : res) x += c[u]; res.insert(res.begin(), s[u], INF); res[0] = 0; return res; }; auto res = dfs(dfs, 0); ll ans = INF; for (ll x : res | views::drop(x)) chmin(ans, x); cout << ans << '\n'; }