#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100;
vector<int> G[maxn];
int d[maxn];
int _sum[maxn];
int s;
int k;
int ans1, ans2;
void dfs(int x) {
  int sum = d[x];
  for (int i = 0; i < G[x].size(); i++) {
    dfs(G[x][i]);
    sum = sum + _sum[G[x][i]];
  }
  if (sum == k && x != s && ans1 == -1) {
    ans1 = x;
    _sum[x] = 0;
  } else if (sum == k && x != s && ans2 == -1) {
    ans2 = x;
    _sum[x] = 0;
  } else
    _sum[x] = sum;
}
int main() {
  int n;
  scanf("%d", &n);
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    int u;
    scanf("%d%d", &u, &d[i]);
    if (u == 0)
      s = i;
    else
      G[u].push_back(i);
    sum = sum + d[i];
  }
  ans1 = -1;
  ans2 = -1;
  if (sum % 3 != 0)
    printf("-1\n");
  else {
    k = sum / 3;
    dfs(s);
    if (ans1 == -1 || ans2 == -1)
      printf("-1\n");
    else
      printf("%d %d\n", ans1, ans2);
  }
}
