Language: C++

#include<bits/stdc++.h>
using namespace std;

#define int long long

typedef long long ll;
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>void chmax(T &t,U f){if(t<f)t=f;}

struct segtree{
    static const int SEG=1<<17;
    vint dat;
    segtree():dat(SEG*2){}
    void update(int k,int x){
        k+=SEG-1;
        dat[k]=x;
        while(k){
            k=(k-1)/2;
            dat[k]=max(dat[k*2+1],dat[k*2+2]);
        }
    }
    int get(int a,int b,int k=0,int l=0,int r=SEG){
        if(r<=a||b<=l)return 0;
        if(a<=l&&r<=b)return dat[k];
        return max(get(a,b,k*2+1,l,(l+r)/2),get(a,b,k*2+2,(l+r)/2,r));
    }
};

struct edge{
    int to,cost;
    edge(int to,int cost):to(to),cost(cost){}
};

const int SIZE=100000;
int N;
vector<edge>G[SIZE];

int tt,tin[SIZE],tout[SIZE];
segtree seg;

int ans;
void dfs(int v,int p){
    tin[v]=tt++;
    for(auto &e:G[v]){
        if(e.to==p)continue;
        seg.update(tt,e.cost);
        dfs(e.to,v);
    }
    tout[v]=tt;
}

struct data{
    int len,cost,id;
    data(int len,int cost,int id):len(len),cost(cost),id(id){}
    bool operator<(const data &d)const{
        return len<d.len;
    }
};

int maxlen[SIZE],maxcost[SIZE];
pint maxpair[SIZE];

inline bool comp(const pint &a,const pint &b){
    return a.fi+a.se>b.fi+b.se;
}

void solve(int v,int p){
    int hoge=max(seg.get(0,tin[v]+1),seg.get(tout[v],segtree::SEG));
    vector<data>vec(4,data(0,0,-1));
    for(auto &e:G[v]){
        if(e.to==p)continue;
        solve(e.to,v);
        maxlen[e.to]+=e.cost;
        chmax(maxcost[e.to],e.cost);
        maxpair[e.to].fi+=e.cost;
        vec.pb(data(maxlen[e.to],maxcost[e.to],e.to));
    }

    sort(all(vec));reverse(all(vec));
    chmax(ans,vec[0].len+vec[1].len+hoge);
    chmax(ans,vec[1].len+vec[2].len+vec[0].cost);
    chmax(ans,vec[0].len+vec[2].len+vec[1].cost);
    for(int i=2;i<vec.size();i++)chmax(ans,vec[0].len+vec[1].len+vec[i].cost);
    for(auto &e:G[v]){
        if(e.to==p)continue;
        int tmp=vec[0].len;
        if(e.to==vec[0].id)tmp=vec[1].len;
        chmax(ans,tmp+maxpair[e.to].fi+maxpair[e.to].se);

        chmax(maxcost[v],maxcost[e.to]);
        if(comp(maxpair[e.to],maxpair[v]))maxpair[v]=maxpair[e.to];
    }

    vint maxl(vec.size(),0),maxr(vec.size(),0);
    for(int i=0;i<vec.size()-1;i++){
        chmax(maxl[i+1],max(maxl[i],vec[i].cost));
    }
    for(int i=vec.size()-1;i>0;i--){
        chmax(maxr[i-1],max(maxr[i],vec[i].cost));
    }
    rep(i,vec.size()){
        int tmp=max(maxl[i],maxr[i]);
        pint p(vec[i].len,tmp);
        if(comp(p,maxpair[v]))maxpair[v]=p;
    }
    maxlen[v]=vec[0].len;
}

signed main(){
    scanf("%lld",&N);
    reps(i,1,N){
        int a,b;
        scanf("%lld%lld",&a,&b);
        G[i].pb(edge(a,b));
        G[a].pb(edge(i,b));
    }

    dfs(0,-1);
    solve(0,-1);
    printf("%lld\n",ans);

    return 0;
}

Compiler output:
a.cc: In function 'void solve(long long int, long long int)':
a.cc:80:16: error: template argument 1 is invalid
   80 |     vector<data>vec(4,data(0,0,-1));
      |                ^
a.cc:80:16: error: template argument 2 is invalid
a.cc:80:23: error: reference to 'data' is ambiguous
   80 |     vector<data>vec(4,data(0,0,-1));
      |                       ^~~~
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:63:8: note:                 'struct data'
   63 | struct data{
      |        ^~~~
a.cc:80:35: error: expression list treated as compound expression in initializer [-fpermissive]
   80 |     vector<data>vec(4,data(0,0,-1));
      |                                   ^
a.cc:10:12: error: request for member 'push_back' in 'vec', which is of non-class type 'int'
   10 | #define pb push_back
      |            ^~~~~~~~~
a.cc:87:13: note: in expansion of macro 'pb'
   87 |         vec.pb(data(maxlen[e.to],maxcost[e.to],e.to));
      |             ^~
a.cc:87:16: error: reference to 'data' is ambiguous
   87 |         vec.pb(data(maxlen[e.to],maxcost[e.to],e.to));
      |                ^~~~
/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:63:8: note:                 'struct data'
   63 | struct data{
      |        ^~~~
a.cc:14:20: error: request for member 'begin' in 'vec', which is of non-class type 'int'
   14 | #define all(v) (v).begin(),(v).end()
      |                    ^~~~~
a.cc:90:10: note: in expansion of macro 'all'
   90 |     sort(all(vec));reverse(all(vec));
      |          ^~~
a.cc:14:32: error: request for member 'end' in 'vec', which is of non-class type 'int'
   14 | #define all(v) (v).begin(),(v).end()
      |                                ^~~
a.cc:90:10: note: in expansion of macro 'all'
   90 |     sort(all(vec));reverse(all(vec));
      |          ^~~
a.cc:14:20: error: request for member 'begin' in 'vec', which is of non-class type 'int'
   14 | #define all(v) (v).begin(),(v).end()
      |                    ^~~~~
a.cc:90:28: note: in expansion of macro 'all'
   90 |     sort(all(vec));reverse(all(vec));
      |                            ^~~
a.cc:14:32: error: request for member 'end' in 'vec', which is of non-class type 'int'
   14 | #define all(v) (v).begin(),(v).end()
      |                                ^~~
a.cc:90:28: note: in expansion of macro 'all'
   90 |     sort(all(vec));reverse(all(vec));
      |                            ^~~
a.cc:91:18: error: invalid types 'int[int]' for array subscript
   91 |     chmax(ans,vec[0].len+vec[1].len+hoge);
      |                  ^
a.cc:91:29: error: invalid types 'int[int]' for array subscript
   91 |     chmax(ans,vec[0].len+vec[1].len+hoge);
      |                             ^
a.cc:92:18: error: invalid types 'int[int]' for array subscript
   92 |     chmax(ans,vec[1].len+vec[2].len+vec[0].cost);
      |                  ^
a.cc:92:29: error: invalid types 'int[int]' for array subscript
   92 |     chmax(ans,vec[1].len+vec[2].len+vec[0].cost);
      |                             ^
a.cc:92:40: error: invalid types 'int[int]' for array subscript
   92 |     chmax(ans,vec[1].len+vec[2].len+vec[0].cost);
      |                                        ^
a.cc:93:18: error: invalid types 'int[int]' for array subscript
   93 |     chmax(ans,vec[0].len+vec[2].len+vec[1].cost);
      |                  ^
a.cc:93:29: error: invalid types 'int[int]' for array subscript
   93 |     chmax(ans,vec[0].len+vec[2].len+vec[1].cost);
      |                             ^
a.cc:93:40: error: invalid types 'int[int]' for array subscript
   93 |     chmax(ans,vec[0].len+vec[2].len+vec[1].cost);
      |                                        ^
a.cc:94:23: error: request for member 'size' in 'vec', which is of non-class type 'int'
   94 |     for(int i=2;i<vec.size();i++)chmax(ans,vec[0].len+vec[1].len+vec[i].cost);
      |                       ^~~~
a.cc:94:47: error: invalid types 'int[int]' for array subscript
   94 |     for(int i=2;i<vec.size();i++)chmax(ans,vec[0].len+vec[1].len+vec[i].cost);
      |                                               ^
a.cc:94:58: error: invalid types 'int[int]' for array subscript
   94 |     for(int i=2;i<vec.size();i++)chmax(ans,vec[0].len+vec[1].len+vec[i].cost);
      |                                                          ^
a.cc:94:69: error: invalid types 'int[long long int]' for array subscript
   94 |     for(int i=2;i<vec.size();i++)chmax(ans,vec[0].len+vec[1].len+vec[i].cost);
      |                                                                     ^
a.cc:97:20: error: invalid types 'int[int]' for array subscript
   97 |         int tmp=vec[0].len;
      |                    ^
a.cc:98:21: error: invalid types 'int[int]' for array subscript
   98 |         if(e.to==vec[0].id)tmp=vec[1].len;
      |                     ^
a.cc:98:35: error: invalid types 'int[int]' for array subscript
   98 |         if(e.to==vec[0].id)tmp=vec[1].len;
      |                                   ^
a.cc:105:19: error: request for member 'size' in 'vec', which is of non-class type 'int'
  105 |     vint maxl(vec.size(),0),maxr(vec.size(),0);
      |                   ^~~~
a.cc:105:38: error: request for member 'size' in 'vec', which is of non-class type 'int'
  105 |     vint maxl(vec.size(),0),maxr(vec.size(),0);
      |                                      ^~~~
a.cc:106:23: error: request for member 'size' in 'vec', which is of non-class type 'int'
  106 |     for(int i=0;i<vec.size()-1;i++){
      |                       ^~~~
a.cc:107:40: error: invalid types 'int[long long int]' for array subscript
  107 |         chmax(maxl[i+1],max(maxl[i],vec[i].cost));
      |                                        ^
a.cc:109:19: error: request for member 'size' in 'vec', which is of non-class type 'int'
  109 |     for(int i=vec.size()-1;i>0;i--){
      |                   ^~~~
a.cc:110:40: error: invalid types 'int[long long int]' for array subscript
  110 |         chmax(maxr[i-1],max(maxr[i],vec[i].cost));
      |                                        ^
a.cc:112:15: error: request for member 'size' in 'vec', which is of non-class type 'int'
  112 |     rep(i,vec.size()){
      |               ^~~~
a.cc:15:33: note: in definition of macro 'rep'
   15 | #define rep(i,n) for(int i=0;i<(n);i++)
      |                                 ^
a.cc:114:19: error: invalid types 'int[long long int]' for array subscript
  114 |         pint p(vec[i].len,tmp);
      |                   ^
a.cc:117:18: error: invalid types 'int[int]' for array subscript
  117 |     maxlen[v]=vec[0].len;
      |                  ^