88362024-02-01 10:38:14hackemonTársaság (50)cpp17Runtime error 0/5035ms65244 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
base0/50
1Runtime error0/029ms65244 KiB
2Runtime error0/035ms65004 KiB
3Runtime error0/330ms64760 KiB
4Runtime error0/330ms64520 KiB
5Runtime error0/329ms64420 KiB
6Runtime error0/329ms64184 KiB
7Runtime error0/329ms64084 KiB
8Runtime error0/329ms63968 KiB
9Runtime error0/329ms63752 KiB
10Runtime error0/329ms63584 KiB
11Runtime error0/326ms63440 KiB
12Runtime error0/334ms63204 KiB
13Runtime error0/426ms63196 KiB
14Runtime error0/428ms63196 KiB
15Runtime error0/434ms62952 KiB
16Runtime error0/428ms62944 KiB
17Runtime error0/428ms62940 KiB