#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
const int maxn = (long long)1e6 + 5;
const int mod = 1000000007;
const int inf = 0x3f3f3f3f;
vector<int> v[maxn];
int sum, n, a[maxn];
pair<int, int> ans;
int rt = 0;
void dfs(int now) {
  for (auto &to : v[now]) {
    dfs(to);
    a[now] += a[to];
  }
  if (a[now] == sum / 3) {
    if (ans.first == 0)
      ans.first = now;
    else if (now != rt) {
      cout << ans.first << ' ' << now;
      exit(0);
    }
    a[now] = 0;
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x >> a[i];
    if (x == 0)
      rt = i;
    else
      v[x].push_back(i);
    sum += a[i];
  }
  if (sum % 3 != 0) {
    cout << -1;
    return 0;
  }
  dfs(rt);
  cout << -1;
  return 0;
}
