#include <bits/stdc++.h>
using namespace std;
vector<int> v[1000005];
int m = 0, p = 0, b[3], sum = 0, a[1000010];
int shu(int h) {
  int i, k = a[h];
  for (i = 0; i < v[h].size(); i++) {
    k += shu(v[h][i]);
  }
  if (k == (sum / 3)) {
    b[m++] = h;
    return 0;
  } else {
    return k;
  }
}
int main() {
  int i, j, n, x, y;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &x, &a[i]);
    v[x].push_back(i);
    sum += a[i];
    if (x == 0) {
      y = i;
    }
  }
  if (sum % 3 == 0) {
    shu(y);
    if (m <= 2) {
      cout << "-1" << endl;
    } else {
      cout << b[0] << " " << b[1] << endl;
    }
  } else {
    cout << "-1" << endl;
  }
}
