#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
int n, num, cnt, root, sum;
int dp[MAXN][5], head[MAXN], val[MAXN], ans[MAXN];
struct node {
  int from, to, nxt;
} e[MAXN << 1];
void add(int u, int v) {
  e[++num].from = u;
  e[num].to = v;
  e[num].nxt = head[u];
  head[u] = num;
}
void DP(int x) {
  dp[x][0] = val[x];
  for (int i = head[x]; i; i = e[i].nxt) {
    int y = e[i].to;
    DP(y);
    dp[x][0] += dp[y][0];
  }
  if (dp[x][0] * 3 == sum) {
    ans[++cnt] = x;
    dp[x][0] = 0;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    sum += y;
    if (x == 0) {
      val[i] = y;
      root = i;
      continue;
    } else {
      add(x, i);
      val[i] = y;
    }
  }
  DP(root);
  if (cnt >= 3) {
    printf("%d %d \n", ans[2], ans[1]);
  } else
    printf("-1\n");
  return 0;
}
