Language: C++

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct star{int to,nxt,w;}a[1000010];
int head[1000010],cnt,n,fa[1000010],siz[1000010];
ll dis[1000010],a1,a2,b1,b2;
void add(int u,int v,int w){
    cnt++;
    a[cnt].to=v,a[cnt].nxt=head[u],a[cnt].w=w,head[u]=cnt;
}
struct data{
    long long a,b;
    bool operator <(data X) const{
        if(X.a!=a)return a<X.a;
        return b<X.b;
    }
};
vector<data>X[1000010];
vector<data>v;
ll l,r,mid;
int ef(int l1,int l2,int x,int y){
    if(l2==0)return l2;
    int ll=0,rr=l2-1;
    while(ll<rr-1){
        int md=(ll+rr)>>1;
        a2=X[y][md].a,b2=X[y][md].b;
        if(b1+a2+dis[x]+dis[y]<=mid)ll=md;
        else rr=md;
    }
    a2=X[y][rr].a,b2=X[y][rr].b;
    if(b1+a2+dis[x]+dis[y]<=mid)return rr;
    a2=X[y][ll].a,b2=X[y][ll].b;
    if(b1+a2+dis[x]+dis[y]<=mid)return ll;
    else return l2;
}
void update(int u){
    int x=0,y=0;
    for(int i=head[u];i;i=a[i].nxt){
        int to=a[i].to;
        if(to==fa[u])continue;
        if(x==0)x=a[i].to;
        else y=a[i].to;
    }
    if(siz[x]>siz[y])swap(x,y);
    v.clear();
    for(int i=0;i<X[x].size();i++){
        a1=X[x][i].a,b1=X[x][i].b;
        if(ef(X[x].size(),X[y].size(),x,y)>=X[y].size())continue;
        long long aaa=a1+dis[x],bbb=b2+dis[y];
        data xx;xx.a=aaa;xx.b=bbb;
        v.push_back((data){xx});
        aaa=b2+dis[y],bbb=a1+dis[x];
        xx.a=aaa;xx.b=bbb;
        v.push_back((data){xx});
    }
    sort(v.begin(),v.end());
    int len=v.size();
    long long maxx=1000000000000000000;
    for(int i=0;i<len;i++)
        if(v[i].b<maxx)X[u].push_back(v[i]),maxx=v[i].b;
}
void dfs(int u,int f){
    int fl=0;
    for(int i=head[u];i;i=a[i].nxt){
        int to=a[i].to;
        if(to==f)continue;
        dis[to]=a[i].w;
        dfs(to,u);
        fl++;
        if(fl==2)update(u);
    }
    if(fl==0) X[u].push_back((data){0,0});
}
void deal_first(int u,int f){
    siz[u]=1;
    for(int i=head[u];i;i=a[i].nxt){
        int to=a[i].to;
        if(to==f)continue;
        fa[to]=u;
        deal_first(to,u);
        siz[u]+=siz[to];
    }
}
int check(){
    dfs(1,0);
    return X[1].size()>0;
}
int main(){
    scanf("%d",&n);
    for(int i=2,fa,val;i<=n;i++){
        scanf("%d%d",&fa,&val);
        add(fa,i,val);add(i,fa,val);r+=val;
    }
    deal_first(1,0);
    while(l<r-1){
        mid=(l+r)>>1;
        for(int i=1;i<=n;i++)X[i].clear();
        if(check())r=mid;
        else l=mid;
    }
    mid=l;
    if(check())cout<<l<<endl;
    else cout<<r<<endl;
}


Compiler output:
a.cc:18:12: error: template argument 1 is invalid
   18 | vector<data>X[1000010];
      |            ^
a.cc:18:12: error: template argument 2 is invalid
a.cc:19:12: error: template argument 1 is invalid
   19 | vector<data>v;
      |            ^
a.cc:19:12: error: template argument 2 is invalid
a.cc: In function 'int ef(int, int, int, int)':
a.cc:26:16: error: invalid types 'int[int]' for array subscript
   26 |         a2=X[y][md].a,b2=X[y][md].b;
      |                ^
a.cc:26:30: error: invalid types 'int[int]' for array subscript
   26 |         a2=X[y][md].a,b2=X[y][md].b;
      |                              ^
a.cc:30:12: error: invalid types 'int[int]' for array subscript
   30 |     a2=X[y][rr].a,b2=X[y][rr].b;
      |            ^
a.cc:30:26: error: invalid types 'int[int]' for array subscript
   30 |     a2=X[y][rr].a,b2=X[y][rr].b;
      |                          ^
a.cc:32:12: error: invalid types 'int[int]' for array subscript
   32 |     a2=X[y][ll].a,b2=X[y][ll].b;
      |            ^
a.cc:32:26: error: invalid types 'int[int]' for array subscript
   32 |     a2=X[y][ll].a,b2=X[y][ll].b;
      |                          ^
a.cc: In function 'void update(int)':
a.cc:45:7: error: request for member 'clear' in 'v', which is of non-class type 'int'
   45 |     v.clear();
      |       ^~~~~
a.cc:46:24: error: request for member 'size' in 'X[x]', which is of non-class type 'int'
   46 |     for(int i=0;i<X[x].size();i++){
      |                        ^~~~
a.cc:47:16: error: invalid types 'int[int]' for array subscript
   47 |         a1=X[x][i].a,b1=X[x][i].b;
      |                ^
a.cc:47:29: error: invalid types 'int[int]' for array subscript
   47 |         a1=X[x][i].a,b1=X[x][i].b;
      |                             ^
a.cc:48:20: error: request for member 'size' in 'X[x]', which is of non-class type 'int'
   48 |         if(ef(X[x].size(),X[y].size(),x,y)>=X[y].size())continue;
      |                    ^~~~
a.cc:48:32: error: request for member 'size' in 'X[y]', which is of non-class type 'int'
   48 |         if(ef(X[x].size(),X[y].size(),x,y)>=X[y].size())continue;
      |                                ^~~~
a.cc:48:50: error: request for member 'size' in 'X[y]', which is of non-class type 'int'
   48 |         if(ef(X[x].size(),X[y].size(),x,y)>=X[y].size())continue;
      |                                                  ^~~~
a.cc:50:9: error: reference to 'data' is ambiguous
   50 |         data xx;xx.a=aaa;xx.b=bbb;
      |         ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:11:8: note:                 'struct data'
   11 | struct data{
      |        ^~~~
a.cc:50:17: error: 'xx' was not declared in this scope; did you mean 'x'?
   50 |         data xx;xx.a=aaa;xx.b=bbb;
      |                 ^~
      |                 x
a.cc:51:11: error: request for member 'push_back' in 'v', which is of non-class type 'int'
   51 |         v.push_back((data){xx});
      |           ^~~~~~~~~
a.cc:51:22: error: reference to 'data' is ambiguous
   51 |         v.push_back((data){xx});
      |                      ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:11:8: note:                 'struct data'
   11 | struct data{
      |        ^~~~
a.cc:54:11: error: request for member 'push_back' in 'v', which is of non-class type 'int'
   54 |         v.push_back((data){xx});
      |           ^~~~~~~~~
a.cc:54:22: error: reference to 'data' is ambiguous
   54 |         v.push_back((data){xx});
      |                      ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:11:8: note:                 'struct data'
   11 | struct data{
      |        ^~~~
a.cc:56:12: error: request for member 'begin' in 'v', which is of non-class type 'int'
   56 |     sort(v.begin(),v.end());
      |            ^~~~~
a.cc:56:22: error: request for member 'end' in 'v', which is of non-class type 'int'
   56 |     sort(v.begin(),v.end());
      |                      ^~~
a.cc:57:15: error: request for member 'size' in 'v', which is of non-class type 'int'
   57 |     int len=v.size();
      |               ^~~~
a.cc:60:13: error: invalid types 'int[int]' for array subscript
   60 |         if(v[i].b<maxx)X[u].push_back(v[i]),maxx=v[i].b;
      |             ^
a.cc:60:29: error: request for member 'push_back' in 'X[u]', which is of non-class type 'int'
   60 |         if(v[i].b<maxx)X[u].push_back(v[i]),maxx=v[i].b;
      |                             ^~~~~~~~~
a.cc:60:40: error: invalid types 'int[int]' for array subscript
   60 |         if(v[i].b<maxx)X[u].push_back(v[i]),maxx=v[i].b;
      |                                        ^
a.cc:60:51: error: invalid types 'int[int]' for array subscript
   60 |         if(v[i].b<maxx)X[u].push_back(v[i]),maxx=v[i].b;
      |                                                   ^
a.cc: In function 'void dfs(int, int)':
a.cc:72:20: error: request for member 'push_back' in 'X[u]', which is of non-class type 'int'
   72 |     if(fl==0) X[u].push_back((data){0,0});
      |                    ^~~~~~~~~
a.cc:72:31: error: reference to 'data' is ambiguous
   72 |     if(fl==0) X[u].push_back((data){0,0});
      |                               ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:11:8: note:                 'struct data'
   11 | struct data{
      |        ^~~~
a.cc: In function 'int check()':
a.cc:86:17: error: request for member 'size' in 'X[1]', which is of non-class type 'int'
   86 |     return X[1].size()>0;
      |                 ^~~~
a.cc: In function 'int main()':
a.cc:97:35: error: request for member 'clear' in 'X[i]', which is of non-class type 'int'
   97 |         for(int i=1;i<=n;i++)X[i].clear();
      |                                   ^~~~~