#include <bits/stdc++.h>
using namespace std;
int tot;
struct node {
  int v, next;
} e[1000100 * 2];
int n, s, sum;
int head[1000100];
int w[1000100];
int sz[1000100], ans[15];
int cnt;
inline void add(int u, int v) {
  e[tot].next = head[u];
  e[tot].v = v;
  head[u] = tot++;
}
void dfs(int x, int fa) {
  sz[x] = w[x];
  for (int i = head[x]; i; i = e[i].next) {
    int v = e[i].v;
    if (v != fa) {
      dfs(v, x);
      sz[x] += sz[v];
    }
  }
  if (sz[x] == sum) ans[++cnt] = x, sz[x] = 0;
}
int main() {
  int v, val;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &v, &val);
    if (v != 0)
      add(i, v), add(v, i);
    else {
      s = i;
    }
    w[i] = val;
    sum += val;
  }
  if (sum % 3 != 0) {
    puts("-1");
  } else {
    sum /= 3;
    dfs(s, 0);
    if (cnt <= 2)
      puts("-1");
    else
      printf("%d %d\n", ans[1], ans[2]);
  }
  return 0;
}
