#include <bits/stdc++.h>
using namespace std;
int n, t[1000010], sum[1000010], sumt, top;
vector<int> G[1000010], ans[1000010];
bool flag[1000010];
int ANS1 = -1, ANS2 = -1;
void dfs(int now) {
  int j;
  flag[now] = 1;
  sum[now] = t[now];
  for (int i = 0; i < G[now].size(); i++) {
    int x = G[now][i];
    if (flag[x]) continue;
    dfs(x);
    sum[now] += sum[x];
    for (int j = 0; j < ans[x].size(); j++) ans[now].push_back(ans[x][j]);
  }
  if (sum[now] == sumt / 3 * 2 && ans[now].size() != 0 && top != now) {
    ANS1 = now;
    ANS2 = ans[now][0];
  }
  if (ans[now].size() >= 2) {
    ANS1 = ans[now][0];
    ANS2 = ans[now][1];
  }
  if (sum[now] == sumt / 3 && ans[now].size() == 0) ans[now].push_back(now);
  flag[now] = 0;
}
int main() {
  int i, x, y;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d %d", &x, &t[i]);
    if (x != 0) {
      G[x].push_back(i);
      G[i].push_back(x);
    } else
      top = i;
    sumt += t[i];
  }
  if (sumt % 3 == 0) {
    dfs(top);
    if (ANS1 == -1)
      printf("-1");
    else
      printf("%d %d", ANS1, ANS2);
  } else
    printf("-1");
}
