#include <bits/stdc++.h>
using namespace std;
const unsigned long long int MOD = 1e9 + 7;
void swap(int *a, int *b) {
  int temp = *a;
  *a = *b;
  *b = temp;
}
unsigned long long int POW(unsigned long long int p, int b) {
  unsigned long long int ans = 1LL;
  while (b > 0) {
    if (b & 1) {
      ans = ans * 1LL * p;
    }
    b /= 2;
    p = p * 1LL * p;
  }
  return ans;
}
long long int power(long long int a, long long int b, long long int mod) {
  long long int x = 1;
  long long int y = a;
  while (b > 0) {
    if (b & 1) {
      x = x * y;
      x %= mod;
    }
    y = y * y;
    y %= mod;
    b /= 2;
  }
  return x;
}
long long int add(long long int a, long long int b, long long int m = MOD) {
  long long int x = a + b;
  while (x >= m) x -= m;
  while (x < 0) x += m;
  return x;
}
long long int sub(long long int a, long long int b, long long int m = MOD) {
  long long int x = a - b;
  while (x < 0) x += m;
  while (x >= m) x -= m;
  return x;
}
long long int mul(long long int a, long long int b, long long int m = MOD) {
  long long int x = a * 1ll * b;
  x %= m;
  if (x < 0) x += m;
  return x;
}
const int N = 1e6 + 2;
bool used[N];
bool isans[N];
vector<int> g[N];
vector<int> fk;
int data[N];
int root = 0;
long long int total = 0;
int kitna = 0;
int n;
int dp[N];
void dfs(int u, int p) {
  dp[u] = data[u];
  for (int j = 0; j < g[u].size(); j++) {
    int v = g[u][j];
    if (v == p) continue;
    dfs(v, u);
    if (dp[v] == kitna) {
      fk.push_back(v);
    } else {
      dp[u] += dp[v];
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int p, ai;
    cin >> p >> ai;
    data[i] = ai;
    total += 1LL * ai;
    g[p].push_back(i);
    g[i].push_back(p);
    if (p == 0) root = i;
  }
  if (total % 3 != 0) cout << "-1", exit(0);
  kitna = (int)total / 3;
  dfs(root, 0);
  if (fk.size() < 2) cout << "-1", exit(0);
  cout << fk[0] << " " << fk[1] << endl;
}
