#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
const double sn = 1e-6;
int n;
vector<int> ch[1000006];
int te[1000006];
int to[1000006];
bool v[1000006];
vector<int> res;
int c;
int tot;
int dfs(int i) {
  if (v[i]) return to[i];
  v[i] = true;
  int sz = ch[i].size();
  int sum = 0;
  for (int j = 0; j < sz; j++) {
    int ts = dfs(ch[i][j]);
    if (ts == tot / 3)
      res.push_back(ch[i][j]);
    else
      sum += ts;
  }
  return to[i] = sum + te[i];
}
int main() {
  scanf("%d", &n);
  int sc = 0;
  for (int i = 1; i <= n; i++) {
    int t;
    scanf("%d%d", &t, &te[i]);
    if (t != 0)
      ch[t].push_back(i);
    else
      sc = i;
    tot += te[i];
  }
  if (tot % 3 != 0) {
    printf("-1\n");
    return 0;
  }
  dfs(sc);
  int sz = res.size();
  if (sz >= 2) {
    printf("%d %d\n", res[0], res[1]);
  } else
    printf("-1\n");
  return 0;
}
