88352024-02-01 10:36:27hackemonTársaság (50)cpp17Wrong answer 7/504ms3852 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("be2.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 = 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
base7/50
1Wrong answer0/04ms2276 KiB
2Wrong answer0/04ms2548 KiB
3Wrong answer0/34ms2760 KiB
4Wrong answer0/34ms2908 KiB
5Accepted3/34ms3008 KiB
6Wrong answer0/34ms2968 KiB
7Wrong answer0/34ms3112 KiB
8Wrong answer0/34ms3324 KiB
9Wrong answer0/34ms3280 KiB
10Wrong answer0/34ms3412 KiB
11Wrong answer0/34ms3384 KiB
12Wrong answer0/34ms3392 KiB
13Wrong answer0/44ms3388 KiB
14Wrong answer0/44ms3388 KiB
15Wrong answer0/44ms3412 KiB
16Accepted4/44ms3652 KiB
17Wrong answer0/44ms3852 KiB