252682026-02-18 22:05:14xxxTársaság (50)cpp17Wrong answer 7/5018ms980 KiB
// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
using namespace std;

#define int long long

vector<vector<pair<int, int> > > adj;
int ans = 0;
int m;
vector<int> dist;

void dfs(int v) {
    int distmx = 0;
    for(auto [u, w] : adj[v]) {
        dfs(u);
        distmx = max(distmx, dist[u]+w);
    }
    //cout << v << ' ' << m << ' ' << distmx << endl;

    if (distmx > m) {
        ans++;
        dist[v] = 0;
    } else {
        dist[v] = distmx;
    }
    return;
}

signed main() {
    int n, k;
    cin >> n >> k;
    adj.resize(n+1);

    dist.assign(n+1, 0);
    for(int i = 2; i <= n; i++) {
        int x, y;
        cin >> x >> y;

        adj[x].push_back({i, y});
    }

    int l = 0, r = 1e14;
    int realans = 1e14;

    while(l <= r) {
        //cout << l << ' ' << r << endl;
        m = (l+r)/2;

        ans = 0;
        dfs(1);

        if (ans <= k) {
            realans = min(realans, m);
            r = m-1;
        } else {
            l = m+1;
        }
    }

    cout << realans << endl;



}
/*
5 1
1 5
1 2
2 4
2 2
*/
SubtaskSumTestVerdictTimeMemory
base7/50
1Accepted0/01ms316 KiB
2Wrong answer0/08ms564 KiB
3Wrong answer0/31ms508 KiB
4Wrong answer0/31ms316 KiB
5Accepted3/31ms316 KiB
6Wrong answer0/31ms316 KiB
7Wrong answer0/31ms512 KiB
8Wrong answer0/32ms316 KiB
9Wrong answer0/34ms500 KiB
10Wrong answer0/34ms572 KiB
11Wrong answer0/36ms632 KiB
12Wrong answer0/39ms564 KiB
13Wrong answer0/410ms564 KiB
14Wrong answer0/413ms812 KiB
15Wrong answer0/414ms820 KiB
16Accepted4/417ms916 KiB
17Wrong answer0/418ms980 KiB