#include <bits/stdc++.h>
using namespace std;
int sum, t[1000100], c;
list<int> tree[1000100];
vector<int> ans;
int dfs(int v) {
  int s = t[v];
  list<int>::iterator it;
  for (it = tree[v].begin(); it != tree[v].end(); it++) {
    int l = dfs(*it);
    s += l;
  }
  if (s == sum / 3 && v != c) {
    ans.push_back(v);
    return 0;
  }
  return s;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int p;
    cin >> p >> t[i];
    if (p != 0) {
      tree[p].push_back(i);
    } else
      c = i;
    sum += t[i];
  }
  if (sum % 3 != 0) {
    cout << -1;
    return 0;
  }
  dfs(c);
  if (ans.size() < 2)
    cout << -1;
  else {
    for (int i = 0; i < 2; i++) {
      cout << ans[i] << " ";
    }
  }
  return 0;
}
