#include <bits/stdc++.h>
using namespace std;
ifstream fin("date.in");
const int N_MAX = 1000000 + 5;
int temp[N_MAX];
int total_temp;
vector<int> vec[N_MAX];
int sub[N_MAX];
bool ans_found;
int root, n;
bool are_rost = true;
vector<int> ans;
int calc_subarbore(int nod) {
  sub[nod] = temp[nod];
  if (are_rost == true) {
    for (auto v : vec[nod]) sub[nod] += calc_subarbore(v);
    if (sub[nod] == total_temp / 3 && nod != root) {
      ans.push_back(nod);
      if (ans.size() >= 2) are_rost = false;
      return 0;
    } else
      return sub[nod];
  }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1, hangs_on; i <= n; ++i) {
    cin >> hangs_on >> temp[i];
    total_temp += temp[i];
    if (!hangs_on)
      root = i;
    else
      vec[hangs_on].push_back(i);
  }
  if (total_temp % 3 != 0) {
    cout << "-1";
    return 0;
  }
  calc_subarbore(root);
  if (ans.size() < 2) {
    cout << "-1";
    return 0;
  }
  cout << ans[0] << " " << ans[1];
  return 0;
}
