#include <bits/stdc++.h>
using namespace std;
int n, x, r, ans1, ans2, s, t[1000100];
vector<int> g[1000100];
int dfs(int x) {
  int ret = t[x], p;
  for (int i : g[x]) {
    p = dfs(i);
    if (ans2 == 0 && p == s)
      if (ans1)
        ans2 = i;
      else
        ans1 = i;
    else
      ret += p;
  }
  return ret;
}
int main() {
  cin >> n;
  for (int i = 0; i++ < n;) {
    scanf("%d%d", &x, t + i);
    s += t[i];
    if (!x) r = i;
    g[x].push_back(i);
  }
  if (s % 3 != 0) {
    cout << -1;
    return 0;
  }
  s /= 3;
  dfs(r);
  if (ans2)
    cout << ans1 << " " << ans2;
  else
    cout << -1;
}
