#include <bits/stdc++.h>
using namespace std;
long long nums[1000007], w[1000007], dp[1000007], n, root, sum;
pair<long long, long long> ans;
vector<long long> child[1000007];
void dfs(long long u) {
  if (ans.first) return;
  for (long long i = 0; long long v = child[u][i]; ++i) dfs(v);
  nums[u] = w[u];
  long long ok = 0;
  if (ans.first) return;
  for (long long i = 0; long long v = child[u][i]; ++i) {
    nums[u] += nums[v];
    if (dp[v]) {
      if (ok != 0) {
        ans = make_pair(ok, dp[v]);
        return;
      }
      ok = dp[v];
      dp[u] = dp[v];
    }
  }
  if (nums[u] == 2 * sum && dp[u] && u != root) {
    ans = make_pair(u, dp[u]);
    return;
  }
  if (nums[u] == sum) dp[u] = u;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (long long i = 1; i <= n; ++i) {
    long long u, v;
    cin >> u >> v;
    if (u)
      child[u].push_back(i);
    else
      root = i;
    w[i] = v;
    sum += v;
  }
  for (long long i = 1; i <= n; ++i) child[i].push_back(0);
  if (sum % 3) return cout << -1, 0;
  sum /= 3;
  dfs(root);
  if (!ans.first) return cout << -1, 0;
  cout << ans.first << " " << ans.second << '\n';
  return 0;
}
