#include <bits/stdc++.h>
using namespace std;
struct node {
  int v, next;
} p[1000086];
int sum, ans[3], t, val[1000086], head[1000086], cnt;
int dfs(int x) {
  int num = 0, i;
  for (i = head[x]; ~i; i = p[i].next)
    num += dfs(p[i].v), val[x] += val[p[i].v];
  if (val[x] == sum * (num + 1)) ans[t++] = x, num++;
  return num;
}
int main() {
  int i, fa, n;
  memset(head, -1, sizeof head);
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d %d", &fa, &val[i]);
    p[cnt].v = i, p[cnt].next = head[fa], head[fa] = cnt++, sum += val[i];
  }
  if (sum % 3)
    printf("-1\n");
  else {
    sum /= 3;
    if (dfs(p[head[0]].v) < 3)
      printf("-1\n");
    else
      printf("%d %d\n", ans[0], ans[1]);
  }
  return 0;
}
