#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1e6 + 5;
int tot, head[N], to[N], nex[N];
int n, root, temp[N], sum[N], ans, SUM, avoid;
void add(int a, int b) {
  ++tot;
  to[tot] = b;
  nex[tot] = head[a];
  head[a] = tot;
}
void dfs(int now) {
  if (ans != 0 || now == avoid) return;
  sum[now] = temp[now];
  for (int i = head[now]; i != 0; i = nex[i]) {
    dfs(to[i]);
    if (ans != 0) return;
    sum[now] += sum[to[i]];
  }
  if (sum[now] == SUM / 3) {
    ans = now;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int pre, tmp;
    cin >> pre >> tmp;
    temp[i] = tmp;
    if (pre == 0)
      root = i;
    else
      add(pre, i);
    SUM += tmp;
  }
  if (SUM % 3 != 0) {
    cout << -1 << endl;
  } else {
    ans = 0;
    dfs(root);
    if (ans == 0 || ans == root) {
      cout << -1 << endl;
      return 0;
    }
    avoid = ans;
    ans = 0;
    for (int i = 1; i <= n; ++i) sum[i] = 0;
    dfs(root);
    if (ans == 0 || ans == root) {
      cout << -1 << endl;
      return 0;
    }
    cout << avoid << " " << ans << endl;
  }
}
