#include <bits/stdc++.h>
using namespace std;
const int mod = 1e4 + 7;
const int maxn = 1e6 + 5;
const long long INF = 1e14;
int ans, x, flag;
int cont, head[maxn], num[maxn];
struct node {
  int next;
  int to;
  int w;
} edge[maxn];
void add_edge(int u, int v, int w) {
  edge[cont].to = v;
  edge[cont].w = w;
  edge[cont].next = head[u];
  head[u] = cont++;
}
void dfs(int u) {
  for (int i = head[u]; i != -1; i = edge[i].next) {
    int v = edge[i].to;
    dfs(v);
    if (num[v] == x && u != 0) {
      if (!flag) {
        ans = v;
        flag++;
      } else if (flag == 1) {
        printf("%d %d\n", ans, v);
        flag++;
      }
    } else
      num[u] += num[v];
  }
}
int main() {
  int n;
  scanf("%d", &n);
  int sum = 0;
  memset(head, -1, sizeof head);
  for (int i = 1; i <= n; i++) {
    int v, a;
    scanf("%d%d", &v, &a);
    add_edge(v, i, a);
    sum += a;
    num[i] = a;
  }
  x = sum / 3;
  if (sum % 3) {
    printf("-1\n");
  } else {
    dfs(0);
    if (flag != 2) {
      printf("-1\n");
    }
  }
  return 0;
}
