#include <bits/stdc++.h>
using namespace std;
vector<int> son[1000005];
int dp[1000005];
int v1, v2;
long long sum = 0;
int root = 0;
int dfs(int u) {
  for (int i = 0; i < son[u].size(); i++) {
    int v = son[u][i];
    dp[u] += dfs(v);
  }
  if (dp[u] == sum / 3 && u != root) {
    if (!v1)
      v1 = u, dp[u] = 0;
    else if (!v2) {
      v2 = u, dp[u] = 0;
    }
  }
  return dp[u];
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int p;
    scanf("%d%d", &p, &dp[i]);
    ;
    sum += dp[i];
    if (p == 0) root = i;
    son[p].push_back(i);
  }
  if (sum % 3) {
    printf("-1\n");
  } else {
    dfs(root);
    if (v1 && v2)
      printf("%d %d\n", v1, v2);
    else
      printf("-1\n");
  }
  return 0;
}
