#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
  char c;
  bool nega = 0;
  while ((!isdigit(c = getchar())) && (c != '-'))
    ;
  if (c == '-') {
    nega = 1;
    c = getchar();
  }
  x = c - 48;
  while (isdigit(c = getchar())) x = x * 10 + c - 48;
  if (nega) x = -x;
}
template <typename T>
inline void writep(T x) {
  if (x > 9) writep(x / 10);
  putchar(x % 10 + 48);
}
template <typename T>
inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  writep(x);
}
template <typename T>
inline void writeln(T x) {
  write(x);
  putchar('\n');
}
int n, w[1000003], s = 0, sw[1000003], x1, x2, flag, sx;
vector<int> g[1000003];
void dfs(int cur, int pa) {
  int i, now;
  for (i = 0; i < g[cur].size(); i++) {
    now = g[cur][i];
    if (now == pa) continue;
    dfs(now, cur);
    sw[cur] += sw[now];
  }
  if (sw[cur] == s / 3 && cur != sx) {
    if (!x1)
      x1 = cur;
    else if (!x2)
      x2 = cur;
    sw[cur] = 0;
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int i, j, t, x;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> x >> w[i];
    if (x != 0) {
      g[x].push_back(i);
      g[i].push_back(x);
    } else
      sx = i;
    s += w[i];
    sw[i] = w[i];
  }
  if (s % 3) {
    cout << -1 << endl;
    return 0;
  }
  dfs(sx, 0);
  if (x1 * x2)
    cout << x1 << ' ' << x2 << endl;
  else
    cout << -1 << endl;
  return 0;
}
