#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
void File() {
  freopen("D:\\in.txt", "r", stdin);
  freopen("D:\\out.txt", "w", stdout);
}
template <class T>
inline void read(T &x) {
  char c = getchar();
  x = 0;
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
}
int n, root, c[1000010], p[1000010], sum, ans1, ans2;
vector<int> v[1000010];
int flag1, flag2;
void dfs1(int x) {
  c[x] = p[x];
  for (int i = 0; i < v[x].size(); i++) {
    dfs1(v[x][i]);
    if (flag1) return;
    c[x] += c[v[x][i]];
  }
  if (x != root && c[x] == sum / 3) {
    flag1 = 1;
    ans1 = x;
  }
}
void dfs2(int x) {
  c[x] = p[x];
  for (int i = 0; i < v[x].size(); i++) {
    if (v[x][i] == ans1) continue;
    dfs2(v[x][i]);
    if (flag2) return;
    c[x] += c[v[x][i]];
  }
  if (x != root && c[x] == sum / 3) {
    flag2 = 1;
    ans2 = x;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int fa;
    scanf("%d%d", &fa, &p[i]);
    sum = sum + p[i];
    if (fa == 0)
      root = i;
    else
      v[fa].push_back(i);
  }
  if (sum % 3 != 0) {
    printf("-1\n");
    return 0;
  }
  dfs1(root);
  if (flag1 == 0) {
    printf("-1\n");
    return 0;
  }
  memset(c, 0, sizeof c);
  dfs2(root);
  if (flag2 == 0) {
    printf("-1\n");
    return 0;
  } else {
    printf("%d %d\n", ans1, ans2);
  }
  return 0;
}
