#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
struct edge {
  int v, nxt;
} e[maxn << 1];
int head[maxn], kt;
int t[maxn], rt, n;
int ans[10], tot;
int sum;
template <typename T>
inline void read(T &x) {
  char c;
  int f = 1;
  while (!isdigit(c = getchar())) (c == '-') && (f = -1);
  x = c ^ 48;
  while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c ^ 48);
  x *= f;
}
inline void add(int u, int v) {
  e[++kt] = (edge){v, head[u]};
  head[u] = kt;
}
void dfs(int u, int f) {
  for (int i = head[u]; i; i = e[i].nxt)
    if (e[i].v != f) {
      dfs(e[i].v, u);
      t[u] += t[e[i].v];
    }
  if (t[u] == sum / 3) ans[++tot] = u, t[u] = 0;
}
int main() {
  int x;
  read(n);
  for (int i = 1; i <= n; i++) {
    read(x);
    read(t[i]);
    sum += t[i];
    if (x)
      add(x, i), add(i, x);
    else
      rt = i;
  }
  if (sum % 3) {
    puts("-1");
    return 0;
  }
  dfs(rt, 0);
  if (tot <= 2)
    puts("-1");
  else
    printf("%d %d\n", ans[1], ans[2]);
  return 0;
}
