#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 5;
long long up[N], size[N], root, sum = 0, need, check[N];
vector<long long> anss, adj[N];
void dfs(long long x, long long p) {
  long long i;
  for (i = 0; i < adj[x].size(); i++) {
    if (adj[x][i] != p) {
      dfs(adj[x][i], x);
      size[x] += size[adj[x][i]];
      if (!check[x]) {
        check[x] = check[adj[x][i]];
      }
    }
  }
  if (check[x]) {
    if (size[x] == 2 * need && x != root) {
      cout << check[x] << " " << x;
      exit(0);
    }
  }
  if (size[x] == need) {
    if (!check[x]) {
      anss.push_back(x);
      if (anss.size() == 2) {
        cout << anss[0] << " " << anss[1];
        exit(0);
      }
    }
    check[x] = x;
  }
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, i, j;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> up[i] >> size[i];
    if (up[i] == 0) {
      root = i;
    } else {
      adj[up[i]].push_back(i);
      adj[i].push_back(up[i]);
    }
    sum += size[i];
  }
  if (sum % 3 != 0) {
    cout << -1;
    return 0;
  }
  need = sum / 3;
  dfs(root, root);
  cout << -1;
  return 0;
}
