#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 50;
struct Edge {
  int to, next;
} edge[maxn * 2];
int k = 1, head[maxn];
void add(int u, int v) {
  edge[++k].to = v;
  edge[k].next = head[u];
  head[u] = k;
}
int a[maxn], sum[maxn], root, res, ans[maxn], n;
void dfs(int u, int f, int opt) {
  sum[u] = a[u];
  for (int i = head[u]; i; i = edge[i].next) {
    if (edge[i].to == f) continue;
    dfs(edge[i].to, u, i);
    sum[u] += sum[edge[i].to];
  }
  if (sum[u] == res) ans[++ans[0]] = opt / 2, sum[u] = 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x >> a[i];
    res += a[i];
    if (x == 0) {
      root = i;
      k += 2;
      continue;
    }
    add(i, x);
    add(x, i);
  }
  if (res % 3) {
    cout << "-1" << endl;
    return 0;
  }
  res /= 3;
  dfs(root, root, 0);
  if (ans[0] <= 2) {
    cout << "-1" << endl;
    return 0;
  }
  cout << ans[1] << ' ' << ans[2] << endl;
  return 0;
}
