#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000 + 100;
int n, root, u, Sum;
int num[maxn], sum[maxn];
vector<int> G[maxn];
int ans1, ans2;
void dfs1(int x) {
  sum[x] = num[x];
  int len = G[x].size();
  for (int i = 0; i < len; ++i) {
    int pos = G[x][i];
    dfs1(pos);
    if (ans1 != 0) {
      return;
    }
    sum[x] += sum[pos];
  }
  if (sum[x] == Sum / 3) {
    ans1 = x;
  }
}
void dfs2(int x) {
  num[x] = 0;
  int len = G[x].size();
  for (int i = 0; i < len; ++i) {
    int pos = G[x][i];
    dfs2(pos);
  }
}
void dfs3(int x) {
  sum[x] = num[x];
  int len = G[x].size();
  for (int i = 0; i < len; ++i) {
    int pos = G[x][i];
    if (pos != ans1) {
      dfs3(pos);
      if (ans2 != 0) {
        return;
      }
      sum[x] += sum[pos];
    }
  }
  if (sum[x] == Sum / 3) {
    ans2 = x;
  }
}
int main() {
  ios::sync_with_stdio(false);
  while (scanf("%d", &n) != EOF) {
    for (int i = 1; i <= n; ++i) {
      G[i].clear();
    }
    Sum = 0;
    ans1 = ans2 = 0;
    for (int i = 1; i <= n; ++i) {
      scanf("%d%d", &u, &num[i]);
      Sum += num[i];
      if (u != 0) {
        G[u].push_back(i);
      } else {
        root = i;
      }
    }
    if (Sum % 3 != 0) {
      printf("-1\n");
      continue;
    }
    dfs1(root);
    if (ans1 == 0 || ans1 == root) {
      printf("-1\n");
      continue;
    }
    dfs2(ans1);
    dfs3(root);
    if (ans2 == 0 || ans2 == root) {
      printf("-1\n");
    } else {
      printf("%d %d\n", ans1, ans2);
    }
  }
  return 0;
}
