44842023-03-28 13:58:24ZsofiaKeresztelyTársaság (50)cpp14Accepted 50/5018ms5224 KiB
#include <bits/stdc++.h>
using namespace std;
#define pii pair<ll, ll>
#define fi first
#define se second
typedef long long ll;
vector<vector<pii> > g;

pii dist(int v, ll val){
    pii r = {0, 0};
    for (pii x : g[v]){
        pii d = dist(x.fi, val);
        r.se += d.se;
        if (d.fi + x.se > val){
            r.fi = max(r.fi, (ll)0);
            r.se++;
        }
        else{
            r.fi = max(r.fi, d.fi + x.se);
        }
    }
    return r;
}

int main()
{
    int n, k;
    cin >> n >> k;
    g.resize(n+1);
    for (int i=2; i<=n; i++){
        int a, b;
        cin >> a >> b;
        g[a].push_back({i, b});
    }
    ll l = -1, r = 1e13;
    while (l + 1 < r){
        ll m = (l + r) / 2;
        if (dist(1, m).se > k){
            l = m;
        }
        else{
            r = m;
        }
    }
    cout << r;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1884 KiB
2Accepted0/09ms2564 KiB
3Accepted3/33ms2280 KiB
4Accepted3/33ms2520 KiB
5Accepted3/32ms2608 KiB
6Accepted3/33ms2708 KiB
7Accepted3/33ms2940 KiB
8Accepted3/34ms3300 KiB
9Accepted3/34ms3312 KiB
10Accepted3/37ms3484 KiB
11Accepted3/38ms3528 KiB
12Accepted3/310ms3836 KiB
13Accepted4/412ms4084 KiB
14Accepted4/413ms4320 KiB
15Accepted4/414ms4676 KiB
16Accepted4/417ms5032 KiB
17Accepted4/418ms5224 KiB