#include <bits/stdc++.h>
using namespace std;
template <typename T, typename T1>
ostream &operator<<(ostream &out, pair<T, T1> obj) {
  out << "(" << obj.first << "," << obj.second << ")";
  return out;
}
template <typename T, typename T1>
ostream &operator<<(ostream &out, map<T, T1> cont) {
  typename map<T, T1>::const_iterator itr = cont.begin();
  typename map<T, T1>::const_iterator ends = cont.end();
  for (; itr != ends; ++itr) out << *itr << " ";
  out << endl;
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, set<T> cont) {
  typename set<T>::const_iterator itr = cont.begin();
  typename set<T>::const_iterator ends = cont.end();
  for (; itr != ends; ++itr) out << *itr << " ";
  out << endl;
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, multiset<T> cont) {
  typename multiset<T>::const_iterator itr = cont.begin();
  typename multiset<T>::const_iterator ends = cont.end();
  for (; itr != ends; ++itr) out << *itr << " ";
  out << endl;
  return out;
}
template <typename T,
          template <typename ELEM, typename ALLOC = allocator<ELEM>> class CONT>
ostream &operator<<(ostream &out, CONT<T> cont) {
  typename CONT<T>::const_iterator itr = cont.begin();
  typename CONT<T>::const_iterator ends = cont.end();
  for (; itr != ends; ++itr) out << *itr << " ";
  out << endl;
  return out;
}
template <typename T, unsigned int N, typename CTy, typename CTr>
typename enable_if<!is_same<T, char>::value, basic_ostream<CTy, CTr> &>::type
operator<<(basic_ostream<CTy, CTr> &out, const T (&arr)[N]) {
  for (auto i = 0; i < N; ++i) out << arr[i] << " ";
  out << endl;
  return out;
}
template <typename T>
T gcd(T a, T b) {
  T min_v = min(a, b);
  T max_v = max(a, b);
  while (min_v) {
    T temp = max_v % min_v;
    max_v = min_v;
    min_v = temp;
  }
  return max_v;
}
template <typename T>
T lcm(T a, T b) {
  return (a * b) / gcd(a, b);
}
template <typename T>
T fast_exp_pow(T base, T exp, T mod) {
  long long res = 1;
  while (exp) {
    if (exp & 1) {
      res *= base;
      res %= mod;
    }
    exp >>= 1;
    base *= base;
    base %= mod;
  }
  return res;
}
int N, P, A, source, source_val;
long long dp_solve[200010][2];
vector<pair<int, int>> graph[200010];
void dfs(int node, int node_value) {
  long long even_sum = 0, odd_sum = 0;
  vector<long long> diffs;
  for (auto elem : graph[node]) {
    int conn_node = elem.first;
    int value = elem.second;
    dfs(conn_node, value);
    dp_solve[node][0] = max(dp_solve[node][0], dp_solve[conn_node][0]);
    dp_solve[node][1] = max(dp_solve[node][1], dp_solve[conn_node][1]);
    even_sum += dp_solve[conn_node][0];
    diffs.push_back(dp_solve[conn_node][0] - dp_solve[conn_node][1]);
  }
  odd_sum = even_sum;
  sort(diffs.begin(), diffs.end());
  for (int i = 0; i < (int)diffs.size() - 1; i += 2) {
    if (diffs[i] + diffs[i + 1] < 0)
      even_sum -= diffs[i] + diffs[i + 1];
    else
      break;
  }
  if (!diffs.empty()) odd_sum -= diffs[0];
  for (int i = 1; i < (int)diffs.size() - 1; i += 2) {
    if (diffs[i] + diffs[i + 1] < 0)
      odd_sum -= diffs[i] + diffs[i + 1];
    else
      break;
  }
  dp_solve[node][1] = max(dp_solve[node][1], even_sum + node_value);
  dp_solve[node][1] = max(dp_solve[node][1], odd_sum);
  dp_solve[node][0] = max(dp_solve[node][0], even_sum);
}
int main() {
  scanf("%d", &N);
  for (auto i = 1; i <= N; ++i) {
    scanf("%d%d", &P, &A);
    if (P == -1) {
      source = i;
      source_val = A;
      continue;
    }
    graph[P].emplace_back(i, A);
  }
  dfs(source, source_val);
  printf("%I64d", max(dp_solve[source][0], dp_solve[source][1]));
  return 0;
}

### Prompt

In CPP, your task is to solve the following problem:

One Big Software Company has n employees numbered from 1 to n. The director is assigned number 1. Every employee of the company except the director has exactly one immediate superior. The director, of course, doesn't have a superior.

We will call person a a subordinates of another person b, if either b is an immediate supervisor of a, or the immediate supervisor of a is a subordinate to person b. In particular, subordinates of the head are all other employees of the company.

To solve achieve an Important Goal we need to form a workgroup. Every person has some efficiency, expressed by a positive integer ai, where i is the person's number. The efficiency of the workgroup is defined as the total efficiency of all the people included in it.

The employees of the big software company are obsessed with modern ways of work process organization. Today pair programming is at the peak of popularity, so the workgroup should be formed with the following condition. Each person entering the workgroup should be able to sort all of his subordinates who are also in the workgroup into pairs. In other words, for each of the members of the workgroup the number of his subordinates within the workgroup should be even.

Your task is to determine the maximum possible efficiency of the workgroup formed at observing the given condition. Any person including the director of company can enter the workgroup.

Input

The first line contains integer n (1 ≤ n ≤ 2·105) — the number of workers of the Big Software Company. 

Then n lines follow, describing the company employees. The i-th line contains two integers pi, ai (1 ≤ ai ≤ 105) — the number of the person who is the i-th employee's immediate superior and i-th employee's efficiency. For the director p1 = - 1, for all other people the condition 1 ≤ pi < i is fulfilled.

Output

Print a single integer — the maximum possible efficiency of the workgroup.

Examples

Input

7
-1 3
1 2
1 1
1 4
4 5
4 3
5 2


Output

17

Note

In the sample test the most effective way is to make a workgroup from employees number 1, 2, 4, 5, 6.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename T1>
ostream &operator<<(ostream &out, pair<T, T1> obj) {
  out << "(" << obj.first << "," << obj.second << ")";
  return out;
}
template <typename T, typename T1>
ostream &operator<<(ostream &out, map<T, T1> cont) {
  typename map<T, T1>::const_iterator itr = cont.begin();
  typename map<T, T1>::const_iterator ends = cont.end();
  for (; itr != ends; ++itr) out << *itr << " ";
  out << endl;
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, set<T> cont) {
  typename set<T>::const_iterator itr = cont.begin();
  typename set<T>::const_iterator ends = cont.end();
  for (; itr != ends; ++itr) out << *itr << " ";
  out << endl;
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, multiset<T> cont) {
  typename multiset<T>::const_iterator itr = cont.begin();
  typename multiset<T>::const_iterator ends = cont.end();
  for (; itr != ends; ++itr) out << *itr << " ";
  out << endl;
  return out;
}
template <typename T,
          template <typename ELEM, typename ALLOC = allocator<ELEM>> class CONT>
ostream &operator<<(ostream &out, CONT<T> cont) {
  typename CONT<T>::const_iterator itr = cont.begin();
  typename CONT<T>::const_iterator ends = cont.end();
  for (; itr != ends; ++itr) out << *itr << " ";
  out << endl;
  return out;
}
template <typename T, unsigned int N, typename CTy, typename CTr>
typename enable_if<!is_same<T, char>::value, basic_ostream<CTy, CTr> &>::type
operator<<(basic_ostream<CTy, CTr> &out, const T (&arr)[N]) {
  for (auto i = 0; i < N; ++i) out << arr[i] << " ";
  out << endl;
  return out;
}
template <typename T>
T gcd(T a, T b) {
  T min_v = min(a, b);
  T max_v = max(a, b);
  while (min_v) {
    T temp = max_v % min_v;
    max_v = min_v;
    min_v = temp;
  }
  return max_v;
}
template <typename T>
T lcm(T a, T b) {
  return (a * b) / gcd(a, b);
}
template <typename T>
T fast_exp_pow(T base, T exp, T mod) {
  long long res = 1;
  while (exp) {
    if (exp & 1) {
      res *= base;
      res %= mod;
    }
    exp >>= 1;
    base *= base;
    base %= mod;
  }
  return res;
}
int N, P, A, source, source_val;
long long dp_solve[200010][2];
vector<pair<int, int>> graph[200010];
void dfs(int node, int node_value) {
  long long even_sum = 0, odd_sum = 0;
  vector<long long> diffs;
  for (auto elem : graph[node]) {
    int conn_node = elem.first;
    int value = elem.second;
    dfs(conn_node, value);
    dp_solve[node][0] = max(dp_solve[node][0], dp_solve[conn_node][0]);
    dp_solve[node][1] = max(dp_solve[node][1], dp_solve[conn_node][1]);
    even_sum += dp_solve[conn_node][0];
    diffs.push_back(dp_solve[conn_node][0] - dp_solve[conn_node][1]);
  }
  odd_sum = even_sum;
  sort(diffs.begin(), diffs.end());
  for (int i = 0; i < (int)diffs.size() - 1; i += 2) {
    if (diffs[i] + diffs[i + 1] < 0)
      even_sum -= diffs[i] + diffs[i + 1];
    else
      break;
  }
  if (!diffs.empty()) odd_sum -= diffs[0];
  for (int i = 1; i < (int)diffs.size() - 1; i += 2) {
    if (diffs[i] + diffs[i + 1] < 0)
      odd_sum -= diffs[i] + diffs[i + 1];
    else
      break;
  }
  dp_solve[node][1] = max(dp_solve[node][1], even_sum + node_value);
  dp_solve[node][1] = max(dp_solve[node][1], odd_sum);
  dp_solve[node][0] = max(dp_solve[node][0], even_sum);
}
int main() {
  scanf("%d", &N);
  for (auto i = 1; i <= N; ++i) {
    scanf("%d%d", &P, &A);
    if (P == -1) {
      source = i;
      source_val = A;
      continue;
    }
    graph[P].emplace_back(i, A);
  }
  dfs(source, source_val);
  printf("%I64d", max(dp_solve[source][0], dp_solve[source][1]));
  return 0;
}

```