#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
template <class T>
inline void read(T& x) {
  bool f;
  char ch = getchar();
  for (f = false; !isdigit(ch); ch = getchar())
    if (ch == '-') f = true;
  for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar())
    ;
  x *= f == 1 ? -1 : 1;
}
long long n, sum, rt, a[maxn];
long long head[maxn], tot;
long long dp[maxn], ans[10], num;
struct node {
  long long v, nex;
} e[maxn * 2];
void add(int u, int v) { e[++tot].v = v, e[tot].nex = head[u], head[u] = tot; }
void dfs(int u, int fa) {
  dp[u] = a[u];
  for (int i = head[u]; i; i = e[i].nex) {
    int v = e[i].v;
    if (v == fa) continue;
    dfs(v, u), dp[u] += dp[v];
  }
  if (dp[u] == sum) dp[u] = 0, ans[++num] = u;
}
int main() {
  read(n);
  for (int i = 1, v; i <= n; i++) {
    read(v), read(a[i]);
    if (v)
      add(i, v), add(v, i);
    else
      rt = i;
    sum += a[i];
  }
  if (sum % 3) return puts("-1"), 0;
  sum /= 3;
  dfs(rt, 0);
  if (num >= 3)
    return printf("%lld %lld\n", ans[1], ans[2]), 0;
  else
    return puts("-1"), 0;
}
