#include <bits/stdc++.h>
#pragma GCC optimize("O0")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("-ffloat-store")
using namespace std;
long long n, m, a, b, c, k, temp, x, y;
const int MAXN = 100000 + 11;
void read(vector<long long> &v) {
  for (int i = 0; i < v.size(); i++) cin >> v[i];
}
inline long long max(long long a, long long b) { return ((a > b) ? a : b); }
inline long long min(long long a, long long b) { return ((a > b) ? b : a); }
inline long long int gcd(long long int a, long long int b) {
  if (b == 0) return a;
  a %= b;
  return gcd(b, a);
}
void storepowers(int n, vector<int> &a);
vector<long long> v[1000000 + 1];
long long t[1000000 + 1];
long long total;
long long sum[1000000 + 1];
int child[1000000 + 1];
int anc = -1;
void dfsforsum(int u, int p = -1) {
  sum[u] += t[u];
  for (auto x : v[u]) {
    dfsforsum(x, u);
    sum[u] += sum[x];
    child[u] = max(child[u], child[x]);
  }
  if (p != -1) {
    if (sum[u] == 2 * ((total) / 3) && child[u]) anc = u;
  }
  if (sum[u] == total / 3) {
    child[u] = 1;
  }
}
long long forsec[1000000 + 1];
int sec = -1;
void dfsforsec(int u, int type) {
  forsec[u] += t[u];
  for (auto x : v[u]) {
    dfsforsec(x, type);
    forsec[u] += forsec[x];
  }
  if (type == 3) {
    if (forsec[u] == total / 3) sec = u;
  } else if (type == 2) {
    if (forsec[u] == total / 3 && u != anc) {
      sec = u;
    }
  }
}
void solveforthiscase(const int &test) {
  cin >> n;
  int root = -1;
  for (int i = 1; i <= n; i++) {
    cin >> a >> t[i];
    if (a == 0)
      root = i;
    else
      v[a].push_back(i);
    total += t[i];
  }
  if (total % 3) {
    cout << -1;
    return;
  }
  dfsforsum(root);
  if (anc != -1) {
    cout << anc << ' ';
    dfsforsec(anc, 2);
    cout << sec;
    cout << '\n';
    return;
  }
  for (int i = 1; i <= n; i++) {
    if (child[i] != 1) continue;
    set<int> two;
    for (auto x : v[i]) {
      if (child[x] == 1) {
        two.insert(x);
      }
    }
    if (two.size() > 1) {
      sec = -1;
      anc = *two.begin();
      two.erase(anc);
      dfsforsec(anc, 3);
      cout << sec << ' ';
      sec = -1;
      anc = *two.begin();
      dfsforsec(anc, 3);
      cout << sec;
      return;
    }
  }
  cout << -1;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  cin.tie(NULL);
  cout.tie(NULL);
  int test = 1;
  for (int i = 1; i <= test; i++) {
    solveforthiscase(i);
  }
}
void storepowers(int n, vector<int> &a) {
  a.resize(n + 1);
  a[0] = 1;
  for (int i = 1; i <= n; i++) a[i] = a[i - 1] * 2;
  return;
}
