#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int md = 1e9 + 7;
int n;
int m;
vector<int> adj[N];
int temperature[N];
int root;
int subsize[N];
void dfs(int u, int par) {
  subsize[u] += temperature[u];
  for (int x : adj[u]) {
    if (x != par) {
      dfs(x, u);
      subsize[u] += subsize[x];
    }
  }
}
vector<int> v;
vector<int> solve(int u, int par, int want) {
  bool ch = 0;
  vector<int> from_child;
  for (int x : adj[u]) {
    if (x != par) {
      vector<int> res = solve(x, u, want);
      for (int i = 0; i < (int)res.size(); ++i) {
        if (res[i] != -1) {
          from_child.push_back(res[i]);
          ch = 1;
          if (subsize[u] - subsize[res[i]] == want && u != root) {
            v.push_back(u);
          }
        }
      }
    }
  }
  if (subsize[u] == want && u != root && !ch) {
    from_child.push_back(u);
    v.push_back(u);
    return from_child;
  }
  return from_child;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    int par, temp;
    scanf("%d %d", &par, &temp);
    if (par != 0) {
      adj[par].push_back(i);
      adj[i].push_back(par);
    }
    temperature[i] = temp;
    if (!par) {
      root = i;
    }
  }
  dfs(root, -1);
  int total = subsize[root];
  if (abs(total) % 3 != 0) {
    printf("-1\n");
    return 0;
  }
  int want = total / 3;
  solve(root, -1, want);
  int to = (int)v.size();
  if (to < 2) {
    printf("-1\n");
    return 0;
  }
  int ans1 = v[0], ans2 = -1;
  for (int i = 1; i < to; ++i) {
    if (v[i] != ans1) {
      ans2 = v[i];
      break;
    }
  }
  if (ans2 == -1) {
    printf("-1\n");
    return 0;
  }
  printf("%d %d\n", ans1, ans2);
  return 0;
}
