#include <bits/stdc++.h>
using namespace std;
int m, cut = 0, sum = 0, c[3], n = 1, top;
bool flag[1000010] = {0};
struct node {
  int ans;
  vector<int> child;
} t[1000010];
int dfs(int x) {
  if (flag[x] == 1)
    return t[x].ans;
  else {
    if (t[x].child.size() != 0)
      for (int i = 0; i <= t[x].child.size() - 1; i++)
        t[x].ans += dfs(t[x].child[i]);
    flag[x] = 1;
  }
  if (t[x].ans == sum && cut <= 1 && x != top) {
    cut++;
    c[n] = x;
    n = 2;
    t[x].ans = 0;
  }
  return t[x].ans;
}
int main() {
  int i, a, b;
  cin >> m;
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &a, &b);
    if (a != 0) {
      t[a].child.push_back(i);
      t[i].ans = b;
    } else if (a == 0) {
      t[i].ans = b;
      top = i;
    }
    sum += b;
  }
  if (sum % 3 != 0) {
    cout << -1;
    return 0;
  }
  sum = sum / 3;
  dfs(top);
  if (cut == 2)
    cout << c[1] << " " << c[2];
  else
    cout << -1;
}
