#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> a[1000010];
int t[1000010];
int sum[1000010];
int good_sum;
int ok_sum;
int dfs(int v, int& good) {
  sum[v] = t[v];
  for (int i : a[v]) {
    int g = 0;
    sum[v] += dfs(i, g);
    if (good && g) {
      cout << good << " " << g << endl;
      exit(0);
    }
    if (g && sum[i] == ok_sum && g != i) {
      cout << g << " " << i << endl;
      exit(0);
    }
    if (g) good = g;
  }
  if (sum[v] == good_sum) good = v;
  return sum[v];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  int whole = 0;
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    a[x].push_back(i);
    t[i] = y;
    whole += y;
  }
  if (whole % 3) {
    cout << -1 << endl;
    return 0;
  } else {
    good_sum = whole / 3;
    ok_sum = good_sum * 2;
  }
  int unneeded = 0;
  dfs(0, unneeded);
  cout << -1 << endl;
  return 0;
}
