#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2000000 + 9;
int dp[MAXN];
struct Node {
  int nxt, to;
} e[MAXN];
int tr = 0, Sum = 0;
int cnt = 0, head[MAXN];
int n;
int ans[MAXN];
void add_edge(int x, int y) {
  e[++cnt].to = y;
  e[cnt].nxt = head[x];
  head[x] = cnt;
}
void DP(int x, int fa) {
  for (int i = head[x]; i; i = e[i].nxt) {
    int y = e[i].to;
    if (y == fa) continue;
    DP(y, x);
    dp[x] += dp[y];
  }
  if (dp[x] * 3 == Sum) {
    ans[++tr] = x;
    dp[x] = 0;
  }
}
int main() {
  int root;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int y;
    scanf("%d%d", &y, &dp[i]);
    if (y == 0)
      root = i;
    else
      add_edge(y, i), add_edge(i, y);
    Sum += dp[i];
  }
  DP(root, 0);
  if (tr > 2)
    cout << ans[1] << ' ' << ans[2] << endl;
  else
    cout << "-1" << endl;
  return 0;
}
