#include <bits/stdc++.h>
using namespace std;
int n, x, fir, sec;
long long wght[1000010], each, y;
vector<int> adj[1000010];
void go(int u, int pa) {
  int sz = adj[u].size();
  for (int j = 0; j < adj[u].size(); j++) {
    int v = adj[u][j];
    if (v != pa) {
      go(v, u);
      wght[u] += wght[v];
    }
  }
  if (u != pa) {
    if (wght[u] == each) {
      if (fir == 0) {
        fir = u;
        wght[u] = 0LL;
      } else if (sec == 0) {
        sec = u;
        wght[u] = 0LL;
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  int root = -1;
  long long sum = 0;
  for (int i = 1; i < n + 1; i++) {
    scanf("%d%I64d", &x, &y);
    sum += y;
    if (x == 0)
      root = i;
    else {
      wght[i] = y;
      adj[x].push_back(i);
    }
  }
  if (sum % 3 != 0) {
    printf("-1\n");
    return 0;
  }
  each = sum / 3;
  go(root, root);
  if (fir != 0 && sec != 0) {
    printf("%d %d\n", fir, sec);
  } else {
    printf("-1\n");
  }
  return 0;
}
