#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)1e6;
int N;
vector<int> G[MAXN];
int B[MAXN];
int root;
int btot;
int bsum[MAXN];
int ok[MAXN];
void dfs_bsum(int u, int fa) {
  bsum[u] = B[u];
  for (int v : G[u]) {
    if (v == fa) continue;
    dfs_bsum(v, u);
    bsum[u] += bsum[v];
    if (ok[v] != -1) {
      ok[u] = ok[v];
    }
  }
  if (bsum[u] * 3 == btot) {
    ok[u] = u;
  }
}
void dfs(int u, int fa, int up) {
  vector<int> ans;
  for (int v : G[u]) {
    if (v == fa) continue;
    dfs(v, u, up + bsum[u] - B[u] - bsum[v]);
    if (ok[v] != -1) {
      if (ok[v] != v and bsum[v] * 3 == btot * 2) {
        cout << v + 1 << " " << ok[v] + 1 << endl;
        exit(0);
      }
      ans.emplace_back(v);
    }
  }
  if (ans.size() >= 2) {
    cout << ok[ans[0]] + 1 << " " << ok[ans[1]] + 1 << endl;
    exit(0);
  }
}
signed main() {
  ios::sync_with_stdio(0);
  {
    cin >> N;
    for (int i = 0; i < N; ++i) {
      int fa, t;
      cin >> fa >> t;
      B[i] = t;
      btot += t;
      if (fa) {
        G[fa - 1].emplace_back(i);
        G[i].emplace_back(fa - 1);
      } else {
        root = i;
      }
    }
  }
  {
    memset(ok, -1, sizeof(ok));
    dfs_bsum(root, -1);
    dfs(root, -1, B[root]);
    cout << -1 << endl;
  }
  return 0;
}
