#include <bits/stdc++.h>
using namespace std;
const int MMOD = int(1e9 + 7);
const int MMAX = int(1e6 + 5);
int n, p, t[MMAX], root;
long long sum = 0, f[MMAX];
bool isV[MMAX];
vector<int> edge[MMAX];
vector<int> ans;
void dfs(int u, long long total) {
  isV[u] = true;
  f[u] = t[u];
  for (int i = 0; i < edge[u].size(); i++) {
    int v = edge[u][i];
    if (!isV[v]) {
      dfs(v, total);
      f[u] += f[v];
    }
  }
  if (f[u] == total && u != root) {
    ans.push_back(u);
    f[u] = 0;
  }
}
int main() {
  scanf("%d", &n);
  memset(isV, false, sizeof(isV));
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &p, &t[i + 1]);
    edge[p].push_back(i + 1);
    if (p == 0) root = i + 1;
    sum += 1LL * t[i + 1];
  }
  if (sum % 3)
    printf("-1");
  else {
    dfs(root, sum / 3);
    if (ans.size() > 1)
      printf("%d %d", ans[0], ans[1]);
    else
      printf("-1");
  }
  return 0;
}
