#include <bits/stdc++.h>
using namespace std;
const int maxn = int(1e6 + 500);
vector<int> G[maxn];
vector<int> ans;
int fa[maxn], val[maxn];
int sz[maxn];
int64_t sum = 0;
int root = -1;
void dfs(int x, int _fa) {
  sz[x] = val[x];
  for (int i = 0; i < G[x].size(); ++i) {
    int v = G[x][i];
    if (v != _fa) {
      dfs(v, x);
      sz[x] += sz[v];
    }
  }
  if (sz[x] == sum / 3 && x != root && ans.size() < 2) {
    ans.push_back(x);
    sz[x] = 0;
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &fa[i], &val[i]);
    sum += val[i];
  }
  for (int i = 1; i <= n; ++i) {
    if (fa[i] != 0) {
      G[i].push_back(fa[i]);
      G[fa[i]].push_back(i);
    } else
      root = i;
  }
  dfs(root, -1);
  if (sum % 3 != 0 || ans.size() < 2)
    printf("-1\n");
  else {
    for (int i = 0; i < ans.size(); ++i) printf("%d ", ans[i]);
    printf("\n");
  }
  return 0;
}
