#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
int n, t[maxn], root, cnt, sum, ans[5];
int ver[2 * maxn], head[maxn * 2], nxt[maxn * 2], tem[maxn], tot;
void add(int x, int y) {
  tot++;
  ver[tot] = y;
  nxt[tot] = head[x];
  head[x] = tot;
}
void dfs(int x, int y) {
  tem[x] = t[x];
  for (int i = head[x]; i; i = nxt[i]) {
    int v = ver[i];
    if (v != y) {
      dfs(v, x);
      tem[x] += tem[v];
    }
  }
  if (tem[x] == sum) ans[++cnt] = x, tem[x] = 0;
}
int main() {
  int a;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &a, &t[i]);
    if (a)
      add(a, i), add(i, a);
    else
      root = i;
    sum += t[i];
  }
  if (sum % 3) {
    puts("-1");
    return 0;
  }
  sum /= 3;
  dfs(root, 0);
  if (cnt <= 2)
    puts("-1");
  else
    printf("%d %d\n", ans[1], ans[2]);
  return 0;
}
