#include <bits/stdc++.h>
using namespace std;
long long delbit(long long a, long long k) { return a & (~(1 << k)); }
int delbit(int a, int k) { return a & (~(1 << k)); }
bool getbit(long long a, long long k) { return 1 & (a >> k); }
bool getbit(int a, int k) { return 1 & (a >> k); }
long long setbit(long long a, long long k) { return a |= (1 << k); }
int setbit(int a, int k) { return a |= (1 << k); }
inline long long mulmod(long long first, long long n, long long _mod) {
  long long res = 0;
  while (n) {
    if (n & 1) res = (res + first) % _mod;
    first = (first + first) % _mod;
    n >>= 1;
  }
  return res;
}
inline long long powmod(long long first, long long n, long long _mod) {
  long long res = 1;
  while (n) {
    if (n & 1) res = (res * first) % _mod;
    first = (first * first) % _mod;
    n >>= 1;
  }
  return res;
}
inline long long gcd(long long a, long long b) {
  long long t;
  while (b) {
    a = a % b;
    t = a;
    a = b;
    b = t;
  }
  return a;
}
inline int gcd(int a, int b) {
  int t;
  while (b) {
    a = a % b;
    t = a;
    a = b;
    b = t;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long gcd(long long a, long long b, long long c) {
  return gcd(gcd(a, b), c);
}
inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); }
template <typename T, typename T2>
void printarr(T a[], T2 sz, T2 beg = 0) {
  for (T2 i = beg; i < sz; i++) cout << a[i] << " ";
  cout << endl;
}
int n, a[1010100], aim, sum[1010100];
vector<int> g[1010100];
int v1 = -1, v2 = -1;
void dfs(int v, int prev) {
  sum[v] = a[v];
  for (auto to : g[v]) {
    if (to != prev) {
      dfs(to, v);
      if (to != v1 && to != v2) sum[v] += sum[to];
    }
  }
  if (sum[v] == aim) {
    if (v1 == -1)
      v1 = v;
    else if (v2 == -1)
      v2 = v;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  int s = 1;
  for (int(i) = (1); (i) < (n + 1); (i)++) {
    int p;
    cin >> p >> a[i];
    if (p == 0) s = i;
    aim += a[i];
    if (p != 0) {
      g[i].push_back(p);
      g[p].push_back(i);
    }
  }
  aim /= 3;
  dfs(s, -1);
  if (v1 != s && v2 != s && v1 != -1 && v2 != -1 && sum[v1] == aim &&
      sum[v2] == aim && sum[s] == aim) {
    cout << v1 << " " << v2 << endl;
  } else
    cout << "-1\n";
  return 0;
}
