#include <bits/stdc++.h>
using namespace std;
map<int, int> mapp;
vector<pair<int, int> > otr;
vector<int> g[1000005];
int w[1000005];
long long sum = 0;
vector<int> answ;
int start = 0;
long long dfs(int v) {
  long long count = w[v];
  for (int i = 0; i < g[v].size(); i++) {
    count += dfs(g[v][i]);
  }
  if (count == sum && v != start && answ.size() != 2) {
    answ.push_back(v);
    return 0;
  }
  if (v == start && count != sum) {
    cout << -1;
    exit(0);
  }
  return count;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    sum += b;
    if (a == 0)
      start = i + 1;
    else
      g[a].push_back(i + 1);
    w[i + 1] = b;
  }
  if (sum % 3 != 0) {
    cout << -1;
    return 0;
  }
  sum /= 3;
  dfs(start);
  if (answ.size() != 2)
    cout << -1;
  else
    cout << answ[0] << " " << answ[1];
}
