#include <bits/stdc++.h>
using namespace std;
const long long maxN = 1e6 + 1;
vector<long long> g[maxN];
long long n;
long long target;
long long val[maxN];
int erased[maxN];
long long bulb1 = -1, bulb2 = -1;
long long flag = 0;
long long root;
long long dfs1(long long node, long long par, long long b) {
  if (flag == 1) return 1e10;
  long long res = val[node];
  for (auto i : g[node]) {
    if (i == par || erased[i]) continue;
    res += dfs1(i, node, b);
  }
  if (res == target && node != root) {
    if (b == 1)
      bulb1 = node;
    else
      bulb2 = node;
    flag = 1;
    erased[node] = 1;
    return 1e10;
  } else {
    return res;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(erased, 0, sizeof(erased));
  cin >> n;
  long long sum = 0;
  for (long long i = 1; i <= n; i++) {
    long long par, value;
    cin >> par >> value;
    val[i] = value;
    sum += value;
    if (par) {
      g[i].push_back(par);
      g[par].push_back(i);
    } else
      root = i;
  }
  bool pos = true;
  if (sum % 3) pos = false;
  if (pos) {
    target = sum / 3;
    flag = 0;
    dfs1(root, 0, 1);
    if (flag == 1) {
      flag = 0;
      dfs1(root, 0, 2);
    }
    if (flag == 0) pos = false;
  }
  if (pos) {
    cout << bulb1 << " " << bulb2 << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}
