#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
const int N = 1e6 + 5;
vector<vector<int>> edges(N);
vector<int> t(N), ans;
vector<bool> isVisited(N, false);
int n, root, t_sum;
int dfs(int v) {
  isVisited[v] = true;
  int sum = t[v];
  for (auto u : edges[v]) {
    if (!isVisited[u]) {
      sum += dfs(u);
    }
  }
  if (sum == t_sum) {
    if (v != root) ans.push_back(v);
    return 0;
  } else {
    return sum;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a >> t[i];
    t_sum += t[i];
    if (a) {
      edges[a].push_back(i);
      edges[i].push_back(a);
    } else {
      root = i;
    }
  }
  if (t_sum % 3 == 0) {
    t_sum /= 3;
    dfs(root);
    if (ans.size() >= 2) {
      cout << ans[0] << " " << ans[1] << endl;
      return 0;
    }
  }
  cout << "-1\n";
  return 0;
}
