#include <bits/stdc++.h>
using namespace std;
const long long ll_max = 9223372036854775800;
const long long ll_min = -9223372036854775800;
long long binpow(long long a, long long n) {
  long long res = 1;
  while (n) {
    if (n & 1) res *= a;
    a *= a;
    n >>= 1;
  }
  return res;
}
vector<int> subt;
vector<vector<int> > g;
vector<int> t, cutted;
int n, ai, sum = 0, top;
int calc(int cur) {
  subt[cur] = t[cur];
  for (int i = 0; i < g[cur].size(); ++i) {
    subt[cur] += calc(g[cur][i]);
  }
  return subt[cur];
}
int find(int cur) {
  if (subt[cur] == sum / 3) {
    for (int i = 0; i < g[cur].size(); ++i) {
      if (g[cur][i] != -1) {
        int res = find(g[cur][i]);
        if (res == 2) {
          g[cur][i] = -1;
          --res;
        }
        if (res == 1) {
          subt[cur] -= sum / 3;
          return 1;
        }
      }
    }
    cutted.push_back(cur);
    return 2;
  } else {
    for (int i = 0; i < g[cur].size(); ++i) {
      if (g[cur][i] != -1) {
        int res = find(g[cur][i]);
        if (res == 2) {
          g[cur][i] = -1;
          --res;
        }
        if (res == 1) {
          subt[cur] -= sum / 3;
          return 1;
        }
      }
    }
    return 0;
  }
}
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cin >> n;
  g.resize(n);
  t.resize(n);
  subt.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> ai >> t[i];
    --ai;
    sum += t[i];
    if (ai == -1) {
      top = i;
    } else {
      g[ai].push_back(i);
    }
  }
  if (sum % 3 != 0) {
    cout << -1;
    return 0;
  }
  calc(top);
  for (int i = 0; i < g[top].size(); ++i) {
    if (g[top][i] != -1) {
      int res = find(g[top][i]);
      if (res == 2) {
        g[top][i] = -1;
        --res;
      }
      if (res == 1) {
        subt[top] -= sum / 3;
        break;
      }
    }
  }
  for (int i = 0; i < g[top].size(); ++i) {
    if (g[top][i] != -1) {
      int res = find(g[top][i]);
      if (res == 2) {
        g[top][i] = -1;
        --res;
      }
      if (res == 1) {
        subt[top] -= sum / 3;
        break;
      }
    }
  }
  if (cutted.size() != 2 || subt[top] != sum / 3) {
    cout << -1;
  } else {
    cout << cutted[0] + 1 << " " << cutted[1] + 1;
  }
  int letsmakepause;
  cin >> letsmakepause;
  return 0;
}
