#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
const int M = 1e6 + 100;
int w[N];
int cnt;
int sum;
int num;
int head[N];
int dp[N];
int ans[2];
struct edge {
  int v;
  int next;
} e[M];
void init() {
  memset(head, -1, sizeof(head));
  cnt = 0;
  num = 0;
}
void add(int u, int v) {
  e[cnt].v = v;
  e[cnt].next = head[u];
  head[u] = cnt++;
}
void dfs(int u) {
  dp[u] = w[u];
  for (int i = head[u]; i != -1; i = e[i].next) {
    dfs(e[i].v);
    if (num >= 2) return;
    dp[u] += dp[e[i].v];
  }
  if (dp[u] == sum) {
    ans[num++] = u;
    if (num >= 2) return;
    dp[u] = 0;
  }
}
int main() {
  int n;
  int s;
  int u;
  while (~scanf("%d", &n)) {
    init();
    sum = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d%d", &u, &w[i]);
      sum += w[i];
      if (u)
        add(u, i);
      else
        s = i;
    }
    if (sum % 3 == 0) {
      sum /= 3;
      ans[0] = 0, ans[1] = 0;
      dfs(s);
      if (ans[0] && ans[1] && ans[1] != s)
        printf("%d% d\n", ans[0], ans[1]);
      else
        printf("-1\n");
    } else
      printf("-1\n");
  }
  return 0;
}
