92682024-02-19 16:09:48PallanekPéterTársaság (50)cpp17Wrong answer 42/5014ms4056 KiB
#include <bits/stdc++.h>
using namespace std;

int db = 0;
long long mid = 0;

vector<vector<pair<int,int>>> graf;


int bejaras(int u) {
    int mx=0;
    for(pair<int,int> v:graf[u]){
        int ossz = bejaras(v.first)+v.second;
        if(ossz > mid) {
            ossz = 0;
            db++;
        }
        mx=max(mx, ossz);
    }
    return mx;
}

void solve(){
    int n, k;
    cin >> n >> k;
    graf.resize(n+1);

    for(int i=2;i<=n;i++){
        long long u, v;
        cin >> u >> v;
        graf[u].push_back({i, v});
    }

    long long l = 0, r = 1e13;
    while(l<r){
        mid=(l+r)/2;
        bejaras(1);
        if(db<=k){
            r = mid;
        } else{
            if(l + 1 == r) break;
            l = mid;
        }
        db = 0;
    }
    cout << r << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t = 1;

    while(t-- ) {
        solve();
    }

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base42/50
1Accepted0/03ms1764 KiB
2Accepted0/08ms2568 KiB
3Accepted3/33ms2180 KiB
4Accepted3/32ms2260 KiB
5Accepted3/33ms2392 KiB
6Accepted3/33ms2608 KiB
7Accepted3/33ms2712 KiB
8Accepted3/33ms3064 KiB
9Accepted3/34ms3148 KiB
10Accepted3/36ms3212 KiB
11Accepted3/36ms3260 KiB
12Accepted3/38ms3564 KiB
13Accepted4/48ms3520 KiB
14Accepted4/49ms3788 KiB
15Wrong answer0/410ms3868 KiB
16Accepted4/413ms4056 KiB
17Wrong answer0/414ms4056 KiB