#include <bits/stdc++.h>
using namespace std;
int n, cnt[1000006], s[5];
int a, b, sum = 0, nu;
vector<int> p[1000006];
int dfs(int x, int fx) {
  int len = p[x].size();
  for (int i = 0; i < len; i++) cnt[x] += dfs(p[x][i], x);
  if (cnt[x] == sum / 3 && fx != 0 && nu <= 2) {
    s[++nu] = x;
    return 0;
  }
  return cnt[x];
}
int main() {
  scanf("%d", &n);
  nu = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a, &b);
    cnt[i] = b;
    sum += b;
    p[a].push_back(i);
  }
  if (sum % 3)
    printf("-1\n");
  else {
    cnt[0] = dfs(p[0][0], 0);
    if (nu < 2)
      printf("-1\n");
    else
      printf("%d %d\n", s[1], s[2]);
  }
  return 0;
}
