#include <bits/stdc++.h>
using namespace std;
vector<int> arr[1000100];
int dp[1000100];
int totalsum = 0;
int root = -1;
int visited[1000100];
int ans = -1;
int secondans = -1;
void dfs(int x) {
  if (visited[x] == 1) {
    return;
  }
  visited[x] = 1;
  for (int i = 0; i < arr[x].size(); i++) {
    dfs(arr[x][i]);
    dp[x] += dp[arr[x][i]];
  }
  if (dp[x] == totalsum / 3 && x != root) {
    if (ans == -1) {
      ans = x;
      dp[x] = 0;
    } else if (secondans == -1) {
      secondans = x;
      dp[x] = 0;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int a, b;
    cin >> a >> b;
    arr[a].push_back(i);
    dp[i] = b;
    totalsum += b;
    if (a == 0) {
      root = i;
    }
  }
  if (totalsum % 3 != 0) {
    std::cout << -1;
    return 0;
  }
  dfs(root);
  if (ans != -1 && secondans != -1) {
    std::cout << ans << " " << secondans << endl;
  } else {
    std::cout << -1;
  }
}
