Problem statement

There is a village called Biwako, which consists of $ N $ islets floating on the lake. Biwako Village has a simple bridge with $ N-1 $ books. The islands are numbered from $ 0 $ to $ N-1 $, and the bridges are numbered from $ 0 $ to $ N-2 $. The $ i $ bridge directly connects the $ i + 1 $ island and the $ p_i $ island, and is $ w_i $ in length. Villagers can move between islands through several bridges.

At the suggestion of a villager, a relay tournament will be held in Biwako Village. However, since there is no closed road in Biwako Village and trucks cannot be prepared, I decided to make a closed road by replacing the existing bridge by only $ 1 $. Find the length of the cycle that can be prepared by this operation and that has the maximum length.

Constraint

$ 2 \ leq N \ leq 100000 $
$ 0 \ leq p_i \ leq N-1 $
$ 1 \ leq w_i \ leq 1000 $
All integers
Reachable between all islands

sample

The illustration of each sample is as follows.

<image>

Sample input 1


Five
0 1
0 2
0 3
0 4


Sample output 1


9


Sample input 2


12
0 6
1 1
0 3
3 4
0 2
5 1
6 1
0 2
8 1
9 1
10 2


Sample output 2


19


Sample input 3


2
0 1


Sample output 3


1




input

$ N $
$ p_0 \ w_0 $
$ \ vdots $
$ p_ {n-2} \ w_ {n-2} $

output

Print the answer on the $ 1 $ line.

Example

Input

5
0 1
0 2
0 3
0 4


Output

9
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct SegmentTree
{
  vector< int > seg;
  int sz;

  SegmentTree(int n)
  {
    sz = 1;
    while(sz < n) sz <<= 1;
    seg.assign(2 * sz - 1, 0);
  }

  void set(int k, int x)
  {
    seg[k + sz - 1] = x;
  }

  void build()
  {
    for(int k = sz - 2; k >= 0; k--) {
      seg[k] = max(seg[2 * k + 1], seg[2 * k + 2]);
    }
  }

  int rmq(int a, int b, int k, int l, int r)
  {
    if(a >= r || b <= l) return (0);
    if(a <= l && r <= b) return (seg[k]);
    return (max(rmq(a, b, 2 * k + 1, l, (l + r) >> 1),
                rmq(a, b, 2 * k + 2, (l + r) >> 1, r)));
  }

  int rmq(int a, int b)
  {
    return (rmq(a, b, 0, 0, sz));
  }
};

struct edge
{
  int to, cost;
};

int N;
vector< edge > g[100000];

int start[100000], stop[100000], ptr, depth[100000];

void dfs(int idx, int par)
{
  start[idx] = ptr++;
  for(edge& e : g[idx]) {
    if(e.to == par) continue;
    depth[e.to] = depth[idx] + e.cost;
    dfs(e.to, idx);
  }
  stop[idx] = ptr;
}

pair< int, int > dia(int idx, int par)
{
  pair< int, int > ret(0, idx);
  for(edge& e : g[idx]) {
    if(e.to == par) continue;
    auto child = dia(e.to, idx);
    ret = max(ret, {child.first + e.cost, child.second});
  }
  return (ret);
}

int calc(int idx)
{
  ptr = 0;
  depth[idx] = 0;
  dfs(idx, -1);
  SegmentTree tree(2 * N);
  for(int i = 0; i < N; i++) tree.set(start[i], depth[i]);
  tree.build();

  int ret = 0;
  for(int i = 0; i < N; i++) {
    for(edge e : g[i]) {
      if(depth[e.to] < depth[i]) e.to = i;
      ret = max(ret, max(tree.rmq(0, start[e.to]), tree.rmq(stop[e.to], 2 * N)) + e.cost);
    }
  }
  return (ret);
}

int main()
{
  scanf("%d", &N);
  for(int i = 1; i < N; i++) {
    int P, W;
    scanf("%d %d", &P, &W);
    g[P].push_back((edge) {i, W});
    g[i].push_back((edge) {P, W});
  }
  auto p = dia(0, -1);
  auto q = dia(p.second, -1);
  cout << max(calc(p.second), calc(q.second)) << endl;
}