#include <bits/stdc++.h>
using namespace std;
vector<int> v[1000005];
int tem[1000005];
int dp[1000005];
int p[1000005];
int n, x1, x2, sum;
bool found;
void dfs(int node, int par = -1) {
  int i;
  for (i = 0; i < v[node].size(); i++) {
    if (v[node][i] != par) {
      dfs(v[node][i], node);
      dp[node] += dp[v[node][i]];
    }
  }
  dp[node] += tem[node];
  if (p[node] != 0 && !found) {
    if (dp[node] == sum / 3) {
      found = true;
      x1 = node;
    }
  }
}
void dfs1(int node, int par = -1) {
  int i;
  for (i = 0; i < v[node].size(); i++) {
    if (v[node][i] != par && v[node][i] != x1) {
      dfs1(v[node][i], node);
      dp[node] += dp[v[node][i]];
    }
  }
  dp[node] += tem[node];
  if (p[node] != 0 && !found) {
    if (dp[node] == sum / 3) {
      found = true;
      x2 = node;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int i, x, y, root;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> x >> y;
    sum += y;
    tem[i] = y;
    if (x != 0) {
      v[i].push_back(x);
      v[x].push_back(i);
    } else {
      root = i;
    }
    p[i] = x;
  }
  if (sum % 3 != 0) {
    cout << "-1\n";
    return 0;
  }
  dfs(root);
  if (!found) {
    cout << "-1\n";
    return 0;
  }
  for (i = 1; i <= n; i++) dp[i] = 0;
  found = false;
  dfs1(root);
  if (!found) {
    cout << "-1\n";
    return 0;
  }
  cout << x1 << " " << x2 << "\n";
  return 0;
}
