#include <bits/stdc++.h>
using namespace std;
const int N = 1E6 + 7;
int sum, n, dp[N], t[N], cnt, root;
vector<int> g[N];
vector<int> ans;
int dfs(int u, int pre) {
  dp[u] = t[u];
  if (g[u].empty()) {
    if (dp[u] == sum / 3 && cnt < 2) ans.push_back(u), dp[u] = 0, cnt++;
    return dp[u];
  }
  for (int i = 0; i < (int)g[u].size(); i++) {
    int son = g[u][i];
    dp[u] += dfs(son, u);
  }
  if (dp[u] == sum / 3 && cnt < 2 && u != root)
    ans.push_back(u), dp[u] = 0, cnt++;
  return dp[u];
}
void ptr() {
  for (int i = 1; i <= n; i++) cout << dp[i] << " ";
  cout << endl;
}
int main() {
  std::ios::sync_with_stdio(false), cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a >> t[i];
    sum += t[i];
    g[a].push_back(i);
    if (a == 0) root = i;
  }
  if (sum % 3 != 0) {
    cout << -1 << endl;
  } else {
    dfs(root, -1);
    if (ans.size() == 2) {
      for (int i = 0; i < 2; i++) cout << ans[i] << " ";
      cout << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}
