#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 6;
int n, root, a[maxn], p[maxn];
vector<int> v, g[maxn];
bool mark[maxn];
long long s, c[maxn];
void dfs(int u) {
  c[u] = a[u];
  for (int i = 0; i < g[u].size(); ++i) {
    int v = g[u][i];
    dfs(v);
    if (!mark[v]) c[u] += c[v];
  }
  if (c[u] == s / 3 && u != root) mark[u] = 1, v.push_back(u);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &p[i], &a[i]);
    s += a[i];
    if (!p[i]) root = i;
    g[p[i]].push_back(i);
  }
  if (s % 3 != 0) return puts("-1");
  dfs(root);
  if (v.size() < 2) return puts("-1");
  printf("%d %d", v[0], v[1]);
}
