251612026-02-18 09:56:53szabelrTársaság (50)cpp17Accepted 50/5013ms824 KiB
#include <iostream>
#include <vector>
using namespace std;
long long dfs(int v,
         vector<vector<pair<int, int>>>& adj,
         int &count,long long x
         )
{
    long long maxi=0;
    for(auto to:adj[v])
    {

        long long child = dfs(to.first,adj,count,x)+to.second;
        if(child>x)
            count++;
        else
            if(child>maxi) maxi=child;
    }

    return maxi;

}
bool joe(long long x,
         vector<vector<pair<int, int>>>& adj,
         int n,int k)
{
    int count=0;
    vector<int> ertekek(n+1);
    dfs(1,adj,count,x);
    if(count<=k)
        return true;
    else
        return false;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,k;
    cin >> n>>k;
    vector<vector<pair<int, int>>> adj(n + 1);
    for (int i = 2; i <= n; i++)
    {
        int a, b;
        cin >> a >> b;
        adj[a].push_back({ i,b });
    }
    long long cmin = 0;
    long long cmax = 1000000000000000;
    long long res=-1;
    while (cmin <= cmax)
    {
        long long mid = (cmin + cmax) / 2;
        if (joe(mid, adj,n,k))
        {
            res=mid;
            cmax = mid - 1;
        }
        else
            cmin = mid + 1;
    }
    cout<<res;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/07ms572 KiB
3Accepted3/31ms500 KiB
4Accepted3/31ms316 KiB
5Accepted3/31ms316 KiB
6Accepted3/31ms508 KiB
7Accepted3/31ms508 KiB
8Accepted3/32ms464 KiB
9Accepted3/33ms316 KiB
10Accepted3/34ms316 KiB
11Accepted3/34ms564 KiB
12Accepted3/37ms648 KiB
13Accepted4/48ms640 KiB
14Accepted4/48ms564 KiB
15Accepted4/49ms564 KiB
16Accepted4/412ms820 KiB
17Accepted4/413ms824 KiB