230742026-01-16 11:39:19ercseferencTársaság (50)cpp17Accepted 50/5018ms820 KiB
#include <bits/stdc++.h>
using namespace std;
int n,k,l;
bool jo;
struct munk{vector<int>kim; long long fon;};
vector<munk>a;
long long tav(int x, long long maxh){
    if(jo){
        long long d=0;
        for(int i:a[x].kim){
            d=max(d, tav(i,maxh)+a[i].fon);}
        if(d+a[x].fon>maxh){
            l++; return -a[x].fon;}
        if(l>k)jo=0;
        return d;}}
int main()
{
    //ifstream f("szamok.txt");
    cin>>n>>k; a.resize(n+1);
    vector<bool>b(n+1);
    for(int i=2; i<=n; i++){
        long long x,y; cin>>x>>y;
        a[x].kim.push_back(i);
        a[i].fon=y;}
    long long s=0,t=10e14,koz;
    while(s!=t){
        jo=1;
        l=0; koz=(s+t)/2;
        tav(1,koz);
        if(jo)t=koz;
        else s=koz+1;}
    cout<<t;
    return 0;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms508 KiB
2Accepted0/09ms748 KiB
3Accepted3/32ms508 KiB
4Accepted3/32ms316 KiB
5Accepted3/31ms316 KiB
6Accepted3/31ms368 KiB
7Accepted3/32ms532 KiB
8Accepted3/32ms316 KiB
9Accepted3/34ms316 KiB
10Accepted3/36ms316 KiB
11Accepted3/37ms576 KiB
12Accepted3/310ms640 KiB
13Accepted4/412ms624 KiB
14Accepted4/413ms728 KiB
15Accepted4/414ms764 KiB
16Accepted4/417ms820 KiB
17Accepted4/418ms724 KiB