88332024-02-01 10:27:53hackemonTársaság (50)cpp17Wrong answer 0/507ms5792 KiB
#include <bits/stdc++.h>
using namespace std;


int counter = 0; 
long long mid = 0;

vector<vector<pair<int, int>>> 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("be1.txt", "r", stdin);

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

     adj.resize(n+1);

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

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

     while(l + 1 < r) {
        mid = (l + r)/2;
        bejaras(1); 
        if(counter <= k) {
            r = mid;
        } else {
            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
1Accepted0/03ms1888 KiB
2Wrong answer0/04ms2740 KiB
3Wrong answer0/33ms2428 KiB
4Wrong answer0/33ms2792 KiB
5Wrong answer0/33ms2888 KiB
6Wrong answer0/33ms2880 KiB
7Wrong answer0/33ms2908 KiB
8Wrong answer0/33ms3348 KiB
9Wrong answer0/33ms3228 KiB
10Wrong answer0/34ms3668 KiB
11Wrong answer0/34ms3844 KiB
12Wrong answer0/34ms4296 KiB
13Wrong answer0/44ms4424 KiB
14Wrong answer0/46ms4724 KiB
15Wrong answer0/46ms5128 KiB
16Wrong answer0/46ms5636 KiB
17Wrong answer0/47ms5792 KiB