#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int inf = 1e9 + 7;
const int INF = 0x3f3f3f3f;
vector<int> g[1000010];
int a[1000010];
int c[1000010];
int sum, cnt;
int dfs(int x) {
  int num = c[x];
  for (auto i : g[x]) {
    num += dfs(i);
  }
  if (num == sum) {
    a[cnt++] = x;
    num = 0;
  }
  if (x && cnt == 3) {
    printf("%d %d\n", a[0], a[1]);
    exit(0);
  }
  return num;
}
int main() {
  int n, b, t;
  scanf("%d", &n);
  sum = 0, cnt = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &b, &c[i]);
    g[b].push_back(i);
    sum += c[i];
  }
  if (sum % 3 != 0) return 0 * puts("-1");
  sum /= 3;
  dfs(0);
  puts("-1");
  return 0;
}
