結果

問題 No.3283 Labyrinth and Friends
ユーザー Iroha_3856
提出日時 2025-09-26 21:59:30
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 2,199 bytes
コンパイル時間 5,681 ms
コンパイル使用メモリ 335,016 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-26 21:59:38
合計ジャッジ時間 7,099 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
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<class T> bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

const int inf = 1e9;
const ll INF = 4e18;

template<class T> using pq = priority_queue<T, vector<T>, less<T>>;
template<class T> using spq = priority_queue<T, vector<T>, greater<T>>;

vector<int> di = {0, 0, 1, -1};
vector<int> dj = {1, -1, 0, 0};

struct Edge {
    int to, cost;
};

void solve() {
    int N, X; cin >> N >> X;
    vector<vector<int>> cld(N);
    rep(i, 1, N) {
        int p; cin >> p;
        p--;
        cld[p].push_back(i);
    }
    vector<ll> c(N); vector<int> s(N);
    c[0] = 0; s[0] = 0;
    rep(i, 1, N) cin >> c[i] >> s[i];

    vector<int> size(N);
    vector<vector<ll>> rem(N);
    auto dfs = [&](auto dfs, int v) -> void {
        int csiz = s[v];
        vector<ll> dp(csiz+1, INF);
        dp[s[v]] = c[v];
        for (int to : cld[v]) {
            dfs(dfs, to);
            int nsiz = csiz + size[to];
            vector<ll> 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();
    }
}
0