#include <bits/stdc++.h>
using namespace std;
int p[1000001];
long long t[1000001];
bool isParent[1000001];
vector<int> g[1000001];
vector<int> ans;
int root;
long long sum;
void DFS(int v, int parent = 0) {
  for (int i = 0; i < g[v].size(); i++) {
    if (parent == g[v][i]) continue;
    DFS(g[v][i], v);
  }
  if (parent == 0) return;
  t[parent] += t[v];
  if (t[v] == sum / 3) {
    if (ans.size() == 1) {
      ans.push_back(v);
      return;
    }
    if (ans.size() == 0) {
      ans.push_back(v);
      t[parent] -= t[v];
    }
  }
}
int main() {
  sum = 0;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p[i] >> t[i];
    sum += t[i];
    isParent[p[i]] = true;
    if (p[i] == 0) {
      root = i;
      continue;
    }
    g[i].push_back(p[i]);
    g[p[i]].push_back(i);
  }
  if (sum % 3 != 0) {
    cout << -1;
    return 0;
  }
  DFS(root);
  if (ans.size() != 2) {
    cout << -1;
    return 0;
  }
  cout << ans[0] << " " << ans[1] << endl;
  return 0;
}
