#include <bits/stdc++.h>
using namespace std;
long long int c = 1, vis[1000005], in[1000005], out[1000005], val[1000005],
              sum = 0;
vector<long long int> v[1000005];
void dfs(long long int src) {
  vis[src] = 1;
  in[src] = c++;
  for (long long int i = 0; i < v[src].size(); i++) {
    if (vis[v[src][i]] != 1) {
      dfs(v[src][i]);
      val[src] += val[v[src][i]];
    }
  }
  out[src] = c;
}
long long int flag1 = 0;
long long int flag2 = 0;
long long int ans1;
long long int par = -1;
long long int ans2;
void dfs_1(long long int src) {
  vis[src] = 1;
  if (flag1 == 1 && in[src] > in[par] && out[src] <= out[par] &&
      val[src] == sum / 3) {
    flag2 = 1;
    ans1 = src;
    ans2 = par;
  }
  if (val[src] == 2 * (sum / 3)) {
    flag1 = 1;
    par = src;
  }
  for (long long int i = 0; i < v[src].size(); i++) {
    if (vis[v[src][i]] != 1) {
      dfs_1(v[src][i]);
    }
  }
}
void dfs_2(long long int src) {
  vis[src] = 1;
  if (flag1 == 1 && !(in[src] > in[par] && out[src] <= out[par]) &&
      val[src] == sum / 3) {
    flag2 = 1;
    ans1 = src;
    ans2 = par;
  }
  if (val[src] == sum / 3) {
    flag1 = 1;
    par = src;
  }
  for (long long int i = 0; i < v[src].size(); i++) {
    if (vis[v[src][i]] != 1) {
      dfs_2(v[src][i]);
    }
  }
}
int main() {
  long long int n, i, x, root;
  scanf("%lld", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld %lld", &x, &val[i]);
    sum += val[i];
    if (x != 0) {
      v[x].push_back(i);
      v[i].push_back(x);
    } else {
      root = i;
    }
  }
  if (sum % 3 != 0 || sum == 0) {
    printf("-1\n");
    return 0;
  }
  for (i = 1; i <= n; i++) {
    vis[i] = 0;
  }
  dfs(root);
  for (i = 1; i <= n; i++) {
    vis[i] = 0;
  }
  dfs_1(root);
  if (flag2 == 1) {
    printf("%lld %lld\n", ans1, ans2);
    return 0;
  }
  flag1 = 0;
  flag2 = 0;
  ans1;
  ans2;
  for (i = 1; i <= n; i++) {
    vis[i] = 0;
  }
  dfs_2(root);
  if (flag2 == 1) {
    printf("%lld %lld\n", ans1, ans2);
    return 0;
  } else {
    printf("-1\n");
    return 0;
  }
}
