#include <bits/stdc++.h>
#pragma GCC optimize(3, "inline", "Ofast")
using namespace std;
const int N = 1e6 + 10;
int rt, n, nxt[N], to[N], hd[N], w[N], fa[N], tot, sz[N], gg;
int ok[N];
void add(int u, int v) { nxt[++tot] = hd[u], to[tot] = v, hd[u] = tot; }
void dfs(int pos) {
  sz[pos] = w[pos];
  for (int i = hd[pos]; i; i = nxt[i]) dfs(to[i]), sz[pos] += sz[to[i]];
}
void Dfs(int pos) {
  int cnt = 0, a;
  for (int i = hd[pos]; i; i = nxt[i]) {
    Dfs(to[i]);
    if (ok[to[i]]) {
      if (sz[pos] == gg + gg && pos != rt)
        cout << pos << " " << ok[to[i]], exit(0);
      ok[pos] = ok[to[i]];
      if (cnt == 1)
        cout << a << " " << ok[to[i]], exit(0);
      else
        a = ok[to[i]], ++cnt;
    }
  }
  if (sz[pos] == gg) ok[pos] = pos;
}
int main() {
  int a, b, cc = -1, dd = -1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a, &b);
    if (a == 0)
      rt = i, fa[i] = -1;
    else
      add(a, i), fa[i] = a;
    w[i] = b;
  }
  dfs(rt), gg = sz[rt];
  if (gg % 3) return puts("-1"), 0;
  gg /= 3;
  Dfs(rt);
  puts("-1");
}
