#include <bits/stdc++.h>
using namespace std;
vector<long long int> v[1000005];
vector<long long int> vec;
vector<long long int>::iterator it;
long long int temp[1000005], vis[1000005], par[1000005], n, sum = 0;
void initialize() {
  for (long long int i = 1; i <= n; i++) vis[i] = 0;
}
void dfs(long long int src) {
  vis[src] = 1;
  for (long long int i = 0; i < v[src].size(); i++) {
    if (vis[v[src][i]] != 1) {
      dfs(v[src][i]);
      temp[src] += temp[v[src][i]];
    }
  }
}
long long int res = -1;
void dfs_find(long long int src) {
  vis[src] = 1;
  if (temp[src] == sum / 3) res = src;
  for (long long int i = 0; i < v[src].size(); i++) {
    if (vis[v[src][i]] != 1) {
      dfs_find(v[src][i]);
    }
  }
}
int main() {
  long long int i, x, root;
  scanf("%lld", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld %lld", &x, &temp[i]);
    sum += temp[i];
    par[i] = x;
    if (x == 0) root = i;
    v[i].push_back(x);
    v[x].push_back(i);
  }
  if (sum % 3 != 0) {
    printf("-1\n");
    return 0;
  }
  initialize();
  dfs(root);
  res = -1;
  initialize();
  dfs_find(root);
  if (res == -1) {
    printf("-1\n");
    return 0;
  }
  long long int papa = par[res];
  for (it = v[papa].begin(); it != v[papa].end(); it++) {
    if (*it == res) {
      v[papa].erase(it);
      it--;
    }
  }
  for (it = v[res].begin(); it != v[res].end(); it++) {
    if (*it == papa) {
      v[res].erase(it);
      it--;
    }
  }
  if (res != root) vec.push_back(res);
  while (par[res] != 0) {
    temp[par[res]] -= sum / 3;
    res = par[res];
  }
  res = -1;
  initialize();
  dfs_find(root);
  if (res == -1) {
    printf("-1\n");
    return 0;
  }
  papa = par[res];
  for (it = v[papa].begin(); it != v[papa].end(); it++) {
    if (*it == res) {
      v[papa].erase(it);
      it--;
    }
  }
  for (it = v[res].begin(); it != v[res].end(); it++) {
    if (*it == papa) {
      v[res].erase(it);
      it--;
    }
  }
  if (res != root) vec.push_back(res);
  while (par[res] != 0) {
    temp[par[res]] -= sum / 3;
    res = par[res];
  }
  if (vec.size() != 2) {
    printf("-1\n");
    return 0;
  }
  for (i = 0; i < vec.size(); i++) printf("%lld ", vec[i]);
  printf("\n");
}
