#include <bits/stdc++.h>
using namespace std;
int const N = 1000 * 1000 + 16;
int n;
int one, root;
int t[N];
int sz[N];
vector<int> g[N];
int dfs(int u, int p) {
  sz[u] = t[u];
  int cnt = 0;
  for (int v : g[u]) {
    if (v == p) continue;
    int ret = dfs(v, u);
    if (ret) {
      if (cnt == 0) {
        cnt = ret;
      } else {
        cout << cnt << " " << ret;
        exit(0);
      }
    }
    sz[u] += sz[v];
  }
  if (u != root && cnt && sz[u] == one + one) {
    cout << u << " " << cnt;
    exit(0);
  }
  if (sz[u] == one) return u;
  return cnt;
}
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  cin >> n;
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    int p;
    cin >> p >> t[i];
    if (p)
      g[p].emplace_back(i);
    else
      root = i;
    sum += t[i];
  }
  if (sum % 3) {
    cout << -1;
    return 0;
  }
  one = sum / 3;
  dfs(root, 0);
  cout << -1;
}
