88382024-02-01 10:39:15hackemonTársaság (50)cpp17Accepted 50/5014ms4276 KiB
#include <bits/stdc++.h>
using namespace std;


int counter = 0; 
long long mid = 0;

vector<vector<pair<long long, long long>>> adj;


long long bejaras(int start) {
    long long maxi = 0;

    for(int i = 0;i <adj[start].size();i++ ) {
        long long val = bejaras(adj[start][i].first) + adj[start][i].second;
        if(val > mid) {
            val = 0; 
            counter++;
        }
        maxi = max(maxi, val); 
    }

    return maxi;
}   

void solve() { 
     //freopen("be2.txt", "r", stdin);

     int n, k;
     cin >> n >> k;

     adj.resize(n+1);

     for(int i = 2;i <= n;i++) {
        long long a, b;
        cin >> a >> b;
        adj[a].push_back({i, b});
     }

     long long l = 0, r = 1e13;
     
     //use a binary search to estimate the result

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


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

    int t = 1;

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

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1892 KiB
2Accepted0/08ms2456 KiB
3Accepted3/33ms2184 KiB
4Accepted3/33ms2396 KiB
5Accepted3/33ms2608 KiB
6Accepted3/33ms2832 KiB
7Accepted3/33ms2932 KiB
8Accepted3/33ms3052 KiB
9Accepted3/34ms3376 KiB
10Accepted3/36ms3504 KiB
11Accepted3/36ms3448 KiB
12Accepted3/38ms3620 KiB
13Accepted4/49ms3900 KiB
14Accepted4/49ms3896 KiB
15Accepted4/410ms4024 KiB
16Accepted4/413ms4024 KiB
17Accepted4/414ms4276 KiB