88372024-02-01 10:38:52hackemonTársaság (50)cpp17Runtime error 0/5035ms65240 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/029ms65240 KiB
2Runtime error0/029ms65004 KiB
3Runtime error0/330ms64772 KiB
4Runtime error0/330ms64516 KiB
5Runtime error0/329ms64280 KiB
6Runtime error0/329ms64172 KiB
7Runtime error0/329ms64160 KiB
8Runtime error0/330ms64128 KiB
9Runtime error0/329ms63912 KiB
10Runtime error0/335ms63676 KiB
11Runtime error0/334ms63436 KiB
12Runtime error0/326ms63432 KiB
13Runtime error0/435ms63312 KiB
14Runtime error0/428ms63176 KiB
15Runtime error0/428ms63172 KiB
16Runtime error0/429ms63160 KiB
17Runtime error0/429ms63156 KiB