#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 7;   int n;   int cnt;   int sum;   int head[maxn];   int size[maxn];   int T[maxn];   int top;   int temp[2];   struct edge {     int u, v, w, next;   } e[maxn * 2];   void addedge(int u, int v, int w) {     e[cnt].u = u;     e[cnt].v = v;     e[cnt].w = w;     e[cnt].next = head[u];     head[u] = cnt++;   }   void add_edge(int u, int v, int w) {     addedge(u, v, w);     addedge(v, u, w);   }   void dfs(int u, int f) {     size[u] = T[u];     for (int i = head[u]; ~i; i = e[i].next) {       int v = e[i].v;       if (v != f) {         dfs(v, u);         size[u] += size[v];       }     }     if (size[u] * 3 == sum && u != e[head[0]].v) {       temp[top++] = u;       size[u] = 0;     }   }   int main() {     cnt = 0;     memset(head, -1, sizeof(head));     scanf( %d , &n);     int a, b;     sum = 0;     for (int i = 1; i <= n; i++) {       scanf( %d%d , &a, &b);       add_edge(a, i, 1);       T[i] = b;       sum += b;     }     if (sum % 3) {       cout << -1 << endl;     } else {       top = 0;       temp[0] = -1;       temp[1] = -1;       dfs(e[head[0]].v, 0);       if (temp[0] != -1 && temp[1] != -1) {         cout << temp[0] <<     << temp[1] << endl;       } else {         cout << -1 << endl;       }     }   }   