import java.util.Scanner;

public class WeekThreeA {
    public static void main(String[] args) {

        int a;

        Scanner scan = new Scanner(System.in);
        a = scan.nextInt();

        int arr[] = new int[a];
        for (int i = 0; i < a; i++){
            arr[i] = scan.nextInt();
        }

        for (int i = 0; i < a; i++){
            if (arr[i] == 1){

                System.out.println(-1);
                return;
            }
        }

        int min = arr[0];
        for (int i = 1; i < a; i++){
            if (arr[i] < min)
                min = arr[i];
        }

        System.out.println(min);

    }
}


import java.util.Scanner;

public class WeekThreeA {
    public static void main(String[] args) {

        int a;

        Scanner scan = new Scanner(System.in);
        a = scan.nextInt();

        int arr[] = new int[a];
        for (int i = 0; i < a; i++){
            arr[i] = scan.nextInt();
        }

        for (int i = 0; i < a; i++){
            if (arr[i] == 1){

                System.out.println(-1);
                return;
            }
        }


import java.util.Scanner;

public class WeekThreeA {
    public static void main(String[] args) {

        int a;

        Scanner scan = new Scanner(System.in);
        a = scan.nextInt();

        int arr[] = new int[a];
        for (int i = 0; i < a; i++){
            arr[i] = scan.nextInt();
        }

        for (int i = 0; i < a; i++){
            if (arr[i] == 1){

                System.out.println(-1);
                return;
            }
        }

        System.out.println("1");

    }
}


import java.util.Scanner;

public class WeekThreeA {
    public static void main(String[] args) {

        int a;

        Scanner scan = new Scanner(System.in);
        a = scan.nextInt();

        int arr[] = new int[a];
        for (int i = 0; i < a; i++){
            arr[i] = scan.nextInt();
        }

        for (int i = 0; i < a; i++){
            if (arr[i] == 1){

                System.out.println(-1);
                return;
            }
        }

        int min = arr[0];
        for (int i = 1; i < a; i++){
            if (arr[i] < min)
                min = arr[i];
        }

        min = min - 1;

        System.out.println(min);

    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CurrencySystems {

    public static void main(String [] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());

        String line = br.readLine();

        String [] parts = line.split(" ");

        boolean hasOne = false;

        for(int i=0; i<parts.length; i++) {
            if(parts[i].length() == 1 && parts[i].charAt(0) == '1') {
                hasOne = true;
                break;
            }
        }

        if(hasOne) {
            System.out.println(-1);
        }else {
            System.out.println(1);
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CurrencySystem {

    public static void main(String [] args) extends IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());

        String line = br.readLine();

        String [] parts = line.split(“ “);

        boolean hasOne = false;

        for(int i=0; i<parts.length; i++) {
            if(parts[i].length() == 1 && parts[i].charAt(0) == ‘1’) {
                hasOne = true;
                break;
            }
        }

        if(hasOne) {
            System.out.println(-1);
        }else {
            System.out.println(1);
        }
    }
}


package _313;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CurrencySystems {

    public static void main(String [] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine());

        String line = br.readLine();

        String [] parts = line.split(" ");

        boolean hasOne = false;

        for(int i=0; i<parts.length; i++) {
            if(parts[i].length() == 1 && parts[i].charAt(0) == '1') {
                hasOne = true;
                break;
            }
        }

        if(hasOne) {
            System.out.println(-1);
        }else {
            System.out.println(1);
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class P560A_Currency_System_in_Geraldion {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	public static void main(String[] args) throws NumberFormatException, IOException {
		int n = Integer.parseInt(br.readLine());
		int min = Integer.MAX_VALUE;
		int aux;
		String[] lineas = br.readLine().split(" ");
		for (int i = 0; i < n; i++) {
			aux = Integer.parseInt(lineas[i]);
			min = aux < min? aux: min;
		}
		if(min == 1)
			System.out.println("-1");
		else{
			System.out.println(min - 1);
		}

	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class P560A_Currency_System_in_Geraldion {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	public static void main(String[] args) throws NumberFormatException, IOException {
		int n = Integer.parseInt(br.readLine());
		int min = Integer.MAX_VALUE;
		int aux;
		String[] lineas = br.readLine().split(" ");
		for (int i = 0; i < n; i++) {
			aux = Integer.parseInt(lineas[i]);
			min = aux < min? aux: min;
		}
		if(min == 1)
			System.out.println("-1");
		else{
			System.out.println(1);
		}

	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class P560A_Currency_System_in_Geraldion {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	public static void main(String[] args) throws NumberFormatException, IOException {
		int n = Integer.parseInt(br.readLine());
		int min = Integer.MAX_VALUE;
		int aux;
		for (int i = 0; i < n; i++) {
			aux = Integer.parseInt(br.readLine());
			min = aux < min? aux: min;
		}
		if(min == 1)
			System.out.println("-1");
		else{
			System.out.println(min - 1);
		}

	}

}


import java.util.Scanner;

public class CurrencySystemInGeraldion {

	public static Scanner scanner = new Scanner(System.in);
	public static void main(String[] args) {
		int size = scanner.nextInt();
		for (int i=0 ; i<size; i++){
			if(scanner.nextInt()==1) {
				System.out.println("-1");
				return;
			}
		}
		System.out.println("1");
		return;
	}

}


import java.util.Scanner;

public class CurrencySystemInGeraldion {

	public static Scanner scanner = new Scanner(System.in);
	public static void main(String[] args) {
		int size = scanner.nextInt();
		for (int i=0 ; i<size; i++){
			if(scanner.nextInt()==1) {
				System.out.println("1");
				return;
			}
		}
		System.out.println("-1");
		return;
	}

}


import java.io.*;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * Created by Юля on 15.09.2015.
 */
public class SolverA560 {
    public static void main(String[] args) throws IOException {
        new SolverA560().run();
    }

    BufferedReader br;
    PrintWriter pw;
    StringTokenizer tokenizer;

    public String nextToken() throws IOException {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(br.readLine());
        }
        return tokenizer.nextToken();
    }

    public int nextInt() throws IOException, NumberFormatException {
        return Integer.parseInt(nextToken());
    }

    public long nextLong() throws IOException, NumberFormatException {
        return Long.parseLong(nextToken());
    }

    public double nextDouble() throws IOException, NumberFormatException {
        return Double.parseDouble(nextToken());
    }

    public void run() throws IOException {
//        br = new BufferedReader(new FileReader("input.txt"));
//        pw = new PrintWriter("output.txt");
        br = new BufferedReader(new InputStreamReader(System.in));
        pw = new PrintWriter(new OutputStreamWriter(System.out));

        solve();

        pw.flush();
        pw.close();
    }

    private void solve() throws IOException {
        int n= nextInt();
        int[] mas=new int[n];
        for(int i=0;i<n;i++){
            mas[i]=nextInt();
        }
        Arrays.sort(mas);
        if(mas[0]==1){
            pw.println(-1);
        }else
            pw.println(1);
    }
}


import java.util.*;
import java.io.*;

public class Main{
	public static void main(String[] jack) throws IOException{
		BufferedReader a = new BufferedReader(new InputStreamReader(System.in));
		String b = a.readLine();
		String out = "";
		int c = Integer.parseInt(b);
		String[] d = a.readLine().split(" ");

		boolean flag = false;
		for(String abc : d){
			if(abc.equals("1")){
				flag = true;
				break;
			}
		}

		out = flag ? "-1" : "1";
		System.out.println(out);
	}
}


import java.util.Scanner;

public class A560 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int i;
		int count=1;
		for (i = 1; i < n; i++) {
			int x=sc.nextInt();
			if (x==1) {
				count=-1;
			}
		}
		System.out.print(count+"");

	}

}


import java.util.Scanner;

public class A560 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int i;
		int count=1;
		for (i = 0; i < n; i++) {
			int x=sc.nextInt();
			if (x==1) {
				count=-1;
			}
		}
		System.out.print(count+"\n");

	}

}


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;

public class Main
{
	public void foo()
	{
		MyScanner scan = new MyScanner();
		int n = scan.nextInt();
		int minValue = Integer.MAX_VALUE;
		for(int i = 0;i < n;++i)
		{
			int a = scan.nextInt();
			minValue = Math.min(a, minValue);
		}
		System.out.println(1 == minValue ? -1 : 1);
	}

	public static void main(String[] args)
	{
		new Main().foo();
	}

	class MyScanner
	{
	    private byte[] buf = new byte[1024];
	    private int curChar;
	    private int numChars;
	    BufferedInputStream bis = new BufferedInputStream(System.in);

	    public int read()
	    {
	        if (-1 == numChars)
	        {
	        	throw new InputMismatchException();
	        }
	        if (curChar >= numChars)
	        {
	            curChar = 0;
	            try
	            {
	                numChars = bis.read(buf);
	            }
	            catch (IOException e)
	            {
	                throw new InputMismatchException();
	            }
	            if (numChars <= 0)
	            {
	            	return -1;
	            }
	        }
	        return buf[curChar++];
	    }

	    public int nextInt()
	    {
	        int c = read();
	        while (isSpaceChar(c))
	        {
	        	c = read();
	        }
	        int sgn = 1;
	        if (c == '-')
	        {
	            sgn = -1;
	            c = read();
	        }
	        int res = 0;
	        do
	        {
	            if (c < '0' || c > '9')
	            {
	            	throw new InputMismatchException();
	            }
	            res *= 10;
	            res += c - '0';
	            c = read();
	        } while (!isSpaceChar(c));
	        return res * sgn;
	    }

	    public long nextLong()
	    {
	    	int c = read();
	    	while (isSpaceChar(c))
	    	{
	    		c = read();
	    	}
	    	int sgn = 1;
	    	if (c == '-')
	    	{
	    		sgn = -1;
	    		c = read();
	    	}
	    	long res = 0;
	    	do
	    	{
	    		if (c < '0' || c > '9')
	    		{
	    			throw new InputMismatchException();
	    		}
	    		res *= 10;
	    		res += c - '0';
	    		c = read();
	    	} while (!isSpaceChar(c));
	    	return res * sgn;
	    }

	    public String next()
	    {
	        int c = read();
	        while (isSpaceChar(c))
	        {
	        	c = read();
	        }
	        StringBuilder res = new StringBuilder();
	        do
	        {
	            res.appendCodePoint(c);
	            c = read();
	        } while (!isSpaceChar(c));
	        return res.toString();
	    }

	    private boolean isSpaceChar(int c)
	    {
	        return ' ' == c || '\n' == c || '\r' == c || '\t' == c || -1 == c;
	    }
	}
}


#include<iostream>
#include<math.h>
#include<string>
#include<string.h>
#include <stack>
#include <map>
#include <vector>
#include <algorithm>
#include <stack>
#include <stdio.h>
#include <cstdio>
#include <cstdlib>
#define ll long long
//#define scanf scanf_s
#define pii pair<ll,ll>
#define FOR(i,a,n) for (ll i=(a);i<(n);++i)
#define For(i,n) for (ll i=0;i<(n);++i)
using namespace std;
const long long INF = (long long)1e+9;
#define MAX 10000006
int arr[MAX];
int arr1[MAX];
int main()
{
	std::ios::sync_with_stdio(false);
	//printf("%I64d ", arr[i]);
	vector<int> V;
	map<int, int> M;
	int n, m, a = 0 , b=0, p, q;
	cin >> n;
	For(i, n)
	{
		cin >> arr[i];
		arr1[arr[i]]++;
	}
	bool bol = false;
	sort(arr, arr + n);
	For(j, n)
	{
		for (int i = 0; i < n; i++)
		{
			arr1[arr[j] + arr[i]]++;
		}
	}
	int g = -1;
	for (int i = 1; i <= arr[n-1]+arr[n-1]; i++)
	{
		if (arr1[i] == 0)
		{
			g = i;
			break;
		}
	}
	cout << g;
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<string.h>
#include <stack>
#include <map>
#include <vector>
#include <algorithm>
#include <stack>
#include <stdio.h>
#include <cstdio>
#include <cstdlib>
#define ll long long
//#define scanf scanf_s
#define pii pair<ll,ll>
#define FOR(i,a,n) for (ll i=(a);i<(n);++i)
#define For(i,n) for (ll i=0;i<(n);++i)
using namespace std;
const long long INF = (long long)1e+9;
#define MAX 10000006
int arr[MAX];
int arr1[MAX];
int main()
{
	std::ios::sync_with_stdio(false);
	//printf("%I64d ", arr[i]);
	vector<int> V;
	map<int, int> M;
	int n, m, a = 0 , b=0, p, q;
	cin >> n;
	For(i, n)
	{
		cin >> a;
		if (a == 1)
		{
			cout << -1;
			return 0;
		}
	}
	cout << 1;
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<string.h>
#include <stack>
#include <map>
#include <vector>
#include <algorithm>
#include <stack>
#include <stdio.h>
#include <cstdio>
#include <cstdlib>
#define ll long long
//#define scanf scanf_s
#define pii pair<ll,ll>
#define FOR(i,a,n) for (ll i=(a);i<(n);++i)
#define For(i,n) for (ll i=0;i<(n);++i)
using namespace std;
const long long INF = (long long)1e+9;
#define MAX 10000006
int arr[MAX];
int arr1[MAX];
int main()
{
	std::ios::sync_with_stdio(false);
	//printf("%I64d ", arr[i]);
	vector<int> V;
	map<int, int> M;
	int n, m, a = 0 , b=0, p, q;
	cin >> n;
	For(i, n)
	{
		cin >> arr[i];
		arr1[arr[i]]++;
	}
	bool bol = false;
	sort(arr, arr + n);
	for (int i = 0; i <n; i++)
	{
		arr1[arr[0] + arr[i]]++;
	}
	int g = -1;
	for (int i = 1; i <= arr[0]+arr[n-1]; i++)
	{
		if (arr1[i] == 0)
		{
			g = arr1[i];
		}
	}
	cout << g;
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<string.h>
#include <stack>
#include <map>
#include <vector>
#include <algorithm>
#include <stack>
#include <stdio.h>
#include <cstdio>
#include <cstdlib>
#define ll long long
//#define scanf scanf_s
#define pii pair<ll,ll>
#define FOR(i,a,n) for (ll i=(a);i<(n);++i)
#define For(i,n) for (ll i=0;i<(n);++i)
using namespace std;
const long long INF = (long long)1e+9;
#define MAX 10000006
int arr[MAX];
int arr1[MAX];
int main()
{
	std::ios::sync_with_stdio(false);
	//printf("%I64d ", arr[i]);
	vector<int> V;
	map<int, int> M;
	int n, m, a = 0 , b=0, p, q;
	cin >> n;
	For(i, n)
	{
		cin >> arr[i];
		arr1[arr[i]]++;
	}
	bool bol = false;
	sort(arr, arr + n);
	For(j,n)
	for (int i = 0; i <n; i++)
	{
		arr1[arr[j] + arr[i]]++;
	}
	int g = -1;
	for (int i = 1; i <= arr[n-1]+arr[n-1]; i++)
	{
		if (arr1[i] == 0)
		{
			g = arr1[i];
		}
	}
	cout << g;
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<string.h>
#include <stack>
#include <map>
#include <vector>
#include <algorithm>
#include <stack>
#include <stdio.h>
#include <cstdio>
#include <cstdlib>
#define ll long long
//#define scanf scanf_s
#define pii pair<ll,ll>
#define FOR(i,a,n) for (ll i=(a);i<(n);++i)
#define For(i,n) for (ll i=0;i<(n);++i)
using namespace std;
const long long INF = (long long)1e+9;
#define MAX 100000006
int arr[MAX];
int arr1[MAX];
int main()
{
	std::ios::sync_with_stdio(false);
	//printf("%I64d ", arr[i]);
	vector<int> V;
	map<int, int> M;
	int n, m, a = 0 , b=0, p, q;
	cin >> n;
	For(i, n)
	{
		cin >> arr[i];
		arr1[arr[i]]++;
	}
	bool bol = false;
	sort(arr, arr + n);
	for (int i = 0; i <n; i++)
	{
		arr1[arr[0] + arr[i]]++;
	}
	int g = -1;
	for (int i = 1; i <= arr[0]+arr[n-1]; i++)
	{
		if (arr1[i] == 0)
		{
			g = arr1[i];
		}
	}
	cout << g;
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<string.h>
#include <stack>
#include <map>
#include <vector>
#include <algorithm>
#include <stack>
#include <stdio.h>
#include <cstdio>
#include <cstdlib>
#define ll long long
//#define scanf scanf_s
#define pii pair<ll,ll>
#define FOR(i,a,n) for (ll i=(a);i<(n);++i)
#define For(i,n) for (ll i=0;i<(n);++i)
using namespace std;
const long long INF = (long long)1e+9;
#define MAX 1000006
int arr[MAX];
int main()
{
	std::ios::sync_with_stdio(false);
	//printf("%I64d ", arr[i]);
	vector<int> V;
	map<int, int> M;
	int n, m, a = 0 , b=0, p, q;
	cin >> n;
	For(i, n)
	{
		cin >> a;
		arr[a]++;
	}
	bool bol = false;
	for (int i = 0; i < MAX; i++)
	{
		if (arr[i])
		{
			b += i;
			if (arr[i]>1)bol = true;
		}
	}
	if (bol)
	{
		cout << b;
	}
	else
		cout << -1;
	return 0;
}


#include<iostream>
#include<math.h>
#include<string>
#include<string.h>
#include <stack>
#include <map>
#include <vector>
#include <algorithm>
#include <stack>
#include <stdio.h>
#include <cstdio>
#include <cstdlib>
#define ll long long
//#define scanf scanf_s
#define pii pair<ll,ll>
#define FOR(i,a,n) for (ll i=(a);i<(n);++i)
#define For(i,n) for (ll i=0;i<(n);++i)
using namespace std;
const long long INF = (long long)1e+9;
#define MAX 10000006
int arr[MAX];
int arr1[MAX];
int main()
{
	std::ios::sync_with_stdio(false);
	//printf("%I64d ", arr[i]);
	vector<int> V;
	map<int, int> M;
	int n, m, a = 0 , b=0, p, q;
	cin >> n;
	For(i, n)
	{
		cin >> arr[i];
		arr1[arr[i]]++;
	}
	bool bol = false;
	sort(arr, arr + n);
	For(j,n)
	for (int i = 0; i <n; i++)
	{
		arr1[arr[j] + arr[i]]++;
	}
	int g = -1;
	for (int i = 1; i <= arr[n-1]+arr[n-1]; i++)
	{
		if (arr1[i] == 0)
		{
			g = i;
		}
	}
	cout << g;
	return 0;
}


//
//  main.cpp
//  A
//
//  Created by nicomazz on 24/06/15.
//  Copyright (c) 2015 nicomazz. All rights reserved.
//

#include <cstdlib>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <cstring>
#include <climits>
#include <cmath>
#include <algorithm>
#include <utility>
#include <numeric>
#include <queue>
#include <deque>
#include <vector>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <bitset>

using namespace std;

#define TEST
#define MAXN 1000010

typedef pair<int, int> pi;
typedef pair<int,pi> pii;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef  long long ll;

#define Foreach(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))

#define iOS ios_base::sync_with_stdio(false)
#define error(x) cerr << #x << " = " << (x) <<endl
#define all(x) (x).begin(), (x).end()
template <class T>  inline void smax(T &x,T y){ x = max((x), (y));}
template <class T>  inline void smin(T &x,T y){ x = min((x), (y));}

#define gc getchar
inline void scanInt(int &x)
{
    register char c=gc();
    x = 0;
    bool neg = false;
    while( c<48 || c>57) {if (c == '-')neg = true; c=gc();}
    for(;c>47 && c<58;c = gc()){x = (x<<1) + (x<<3) + c - 48;}
    if (neg) x*=-1;
}
int banc[1001];

int main(int argc, const char * argv[])
{
#ifndef TEST
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif

    int N;

    scanInt(N);
    for (int i = 0; i < N; i++)
    {
        scanInt(banc[i]);
    }
    sort(banc,banc+N);
    if (banc[0] == 1) {cout<<"-1"; return 0;}
    cout<<(banc[0]-1); return 0;
    /*for (int i = 0; i < N; i++)
    {
        if ( dest[banc[i]-1] == false){ cout<<banc[i]-1; return 0;}
        for (int j = banc[i]; j < MAXN; j++)
            if ( dest[j-banc[i]]) dest[j] = true;
    }

    for (int i = 0; i < MAXN; i++)
    {
        if ( dest[i] == false) {cout<<i; return 0;}
    }
    cout<<-1;*/

    return 0;
}


//
//  main.cpp
//  A
//
//  Created by nicomazz on 24/06/15.
//  Copyright (c) 2015 nicomazz. All rights reserved.
//

#include <cstdlib>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <cstring>
#include <climits>
#include <cmath>
#include <algorithm>
#include <utility>
#include <numeric>
#include <queue>
#include <deque>
#include <vector>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <bitset>

using namespace std;

#define TEST
#define MAXN 1000010

typedef pair<int, int> pi;
typedef pair<int,pi> pii;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef  long long ll;

#define Foreach(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))

#define iOS ios_base::sync_with_stdio(false)
#define error(x) cerr << #x << " = " << (x) <<endl
#define all(x) (x).begin(), (x).end()
template <class T>  inline void smax(T &x,T y){ x = max((x), (y));}
template <class T>  inline void smin(T &x,T y){ x = min((x), (y));}

#define gc getchar
inline void scanInt(int &x)
{
    register char c=gc();
    x = 0;
    bool neg = false;
    while( c<48 || c>57) {if (c == '-')neg = true; c=gc();}
    for(;c>47 && c<58;c = gc()){x = (x<<1) + (x<<3) + c - 48;}
    if (neg) x*=-1;
}
bool dest[MAXN];
int banc[1001];
int bitmask;

int main(int argc, const char * argv[])
{
#ifndef TEST
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif

    int N;

    scanInt(N);
    for (int i = 0; i < N; i++)
    {
        scanInt(banc[i]);
    }
    sort(banc,banc+N);
//dest[0] = true;
    if (banc[0] == 1) {cout<<"-1"; return 0;}
    else {cout<<1; return 0;}

   /* for (int i = 0; i < N; i++)
    {
        if ( dest[banc[i]-1] == false){ cout<<banc[i]-1; return 0;}
        for (int j = banc[i]; j < MAXN; j++)
            if ( dest[j-banc[i]]) dest[j] = true;
    }

    for (int i = 0; i < MAXN; i++)
    {
        if ( dest[i] == false) {cout<<i; return 0;}
    }
    cout<<-1;
    */
    return 0;
}


//
//  main.cpp
//  A
//
//  Created by nicomazz on 24/06/15.
//  Copyright (c) 2015 nicomazz. All rights reserved.
//

#include <cstdlib>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <cstring>
#include <climits>
#include <cmath>
#include <algorithm>
#include <utility>
#include <numeric>
#include <queue>
#include <deque>
#include <vector>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <bitset>

using namespace std;

#define TEST
#define MAXN 1000010

typedef pair<int, int> pi;
typedef pair<int,pi> pii;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef  long long ll;

#define Foreach(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))

#define iOS ios_base::sync_with_stdio(false)
#define error(x) cerr << #x << " = " << (x) <<endl
#define all(x) (x).begin(), (x).end()
template <class T>  inline void smax(T &x,T y){ x = max((x), (y));}
template <class T>  inline void smin(T &x,T y){ x = min((x), (y));}

#define gc getchar
inline void scanInt(int &x)
{
    register char c=gc();
    x = 0;
    bool neg = false;
    while( c<48 || c>57) {if (c == '-')neg = true; c=gc();}
    for(;c>47 && c<58;c = gc()){x = (x<<1) + (x<<3) + c - 48;}
    if (neg) x*=-1;
}
bool dest[MAXN];
int banc[1001];
int bitmask;

int main(int argc, const char * argv[])
{
#ifndef TEST
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif

    int N;

    scanInt(N);
    for (int i = 0; i < N; i++)
    {
        scanInt(banc[i]);
    }
    sort(banc,banc+N);
    dest[0] = true;
    if (banc[0] == 1) {cout<<"-1"; return 0;}

    for (int i = 0; i < N; i++)
    {
        if ( dest[banc[i]-1] == false){ cout<<banc[i]-1; return 0;}
        for (int j = banc[i]; j < MAXN; j++)
            if ( dest[j-banc[i]]) dest[j] = true;
    }

    for (int i = 0; i < MAXN; i++)
    {
        if ( dest[i] == false) {cout<<i; return 0;}
    }
    cout<<-1;

    return 0;
}


//
//  main.cpp
//  A
//
//  Created by nicomazz on 24/06/15.
//  Copyright (c) 2015 nicomazz. All rights reserved.
//

#include <cstdlib>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <cstring>
#include <climits>
#include <cmath>
#include <algorithm>
#include <utility>
#include <numeric>
#include <queue>
#include <deque>
#include <vector>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <bitset>

using namespace std;

#define TEST
#define MAXN 1000010

typedef pair<int, int> pi;
typedef pair<int,pi> pii;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef  long long ll;

#define Foreach(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))

#define iOS ios_base::sync_with_stdio(false)
#define error(x) cerr << #x << " = " << (x) <<endl
#define all(x) (x).begin(), (x).end()
template <class T>  inline void smax(T &x,T y){ x = max((x), (y));}
template <class T>  inline void smin(T &x,T y){ x = min((x), (y));}

#define gc getchar
inline void scanInt(int &x)
{
    register char c=gc();
    x = 0;
    bool neg = false;
    while( c<48 || c>57) {if (c == '-')neg = true; c=gc();}
    for(;c>47 && c<58;c = gc()){x = (x<<1) + (x<<3) + c - 48;}
    if (neg) x*=-1;
}
bool dest[MAXN];
int banc[1001];
int bitmask;

int main(int argc, const char * argv[])
{
#ifndef TEST
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif

    int N;

    scanInt(N);
    for (int i = 0; i < N; i++)
    {
        scanInt(banc[i]);
    }
    sort(banc,banc+N);
    dest[0] = true;
    if (banc[0] == 1) {cout<<"-1"; return 0;}
    cout<<banc[0]-1; return 0;
    for (int i = 0; i < N; i++)
    {
        if ( dest[banc[i]-1] == false){ cout<<banc[i]-1; return 0;}
        for (int j = banc[i]; j < MAXN; j++)
            if ( dest[j-banc[i]]) dest[j] = true;
    }

    for (int i = 0; i < MAXN; i++)
    {
        if ( dest[i] == false) {cout<<i; return 0;}
    }
    cout<<-1;

    return 0;
}


//
//  main.cpp
//  A
//
//  Created by nicomazz on 24/06/15.
//  Copyright (c) 2015 nicomazz. All rights reserved.
//

#include <cstdlib>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <cstring>
#include <climits>
#include <cmath>
#include <algorithm>
#include <utility>
#include <numeric>
#include <queue>
#include <deque>
#include <vector>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <bitset>

using namespace std;

#define TEST
#define MAXN 1000010

typedef pair<int, int> pi;
typedef pair<int,pi> pii;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef  long long ll;

#define Foreach(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))

#define iOS ios_base::sync_with_stdio(false)
#define error(x) cerr << #x << " = " << (x) <<endl
#define all(x) (x).begin(), (x).end()
template <class T>  inline void smax(T &x,T y){ x = max((x), (y));}
template <class T>  inline void smin(T &x,T y){ x = min((x), (y));}

#define gc getchar
inline void scanInt(int &x)
{
    register char c=gc();
    x = 0;
    bool neg = false;
    while( c<48 || c>57) {if (c == '-')neg = true; c=gc();}
    for(;c>47 && c<58;c = gc()){x = (x<<1) + (x<<3) + c - 48;}
    if (neg) x*=-1;
}
bool dest[MAXN];
int banc[1001];
int bitmask;

int main(int argc, const char * argv[])
{
#ifndef TEST
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif

    int N;

    scanInt(N);
    for (int i = 0; i < N; i++)
    {
        scanInt(banc[i]);
    }
    sort(banc,banc+N);
    dest[0] = true;
    if (banc[0] == 1) {cout<<"-1"; return 0;}

    for (int i = 0; i < N; i++)
    {
        if ( dest[banc[i]-1] == false){ cout<<banc[i]; return 0;}
        for (int j = banc[i]; j < MAXN; j++)
            if ( dest[j-banc[i]]) dest[j] = true;
    }

    for (int i = 0; i < MAXN; i++)
    {
        if ( dest[i] == false) {cout<<i; return 0;}
    }
    cout<<-1;

    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    long long int n,k=1,s=0,i;
    cin >> n;
    int arr [n];
    for(i=0;i<n;i++){
        cin >>arr[i];
        s+=arr[i];
    }
    sort(arr,arr+n);
    for(i=0;i<n;i++){
        if(k<arr[i])
            break;
        k+=arr[i];
    }
    if(k>s)
        cout<<"-1";
    else
        cout<<k;
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;

int main(){
    long long n,k=1,s=0,i;
    cin >> n;
    int arr [n];
    for(i=0;i<n;i++){
        cin >>arr[i];
        s+=arr[i];
    }
    for(i=0;i<n;i++){
        if(k<arr[i])
            break;
        k+=arr[i];
    }
    if(k>s)
        cout<<"-1";
    else
        cout<<k;
    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
    long long int k=1,s=0;
    int n,i;
    bool flag=1;
    cin >> n;
    int arr [n];
    for(i=0;i<n;i++){
        cin >>arr[i];
        s+=arr[i];
    }
    sort(arr,arr+n);

    for(i=0;i<n;i++){
        if(arr[i]!=i+1)
            flag=0;
        if(k<arr[i])
            break;
        k+=arr[i];

    }
    if(flag)
        cout<<"-1";
    else
        cout<<k;
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;

int main(){
    int n,k=1,s=0,i;
    cin >> n;
    int arr [n];
    for(i=0;i<n;i++){
        cin >>arr[i];
        s+=arr[i];
    }
    for(i=0;i<n;i++){
        if(k<arr[i])
            break;
        k+=arr[i];
    }
    if(k>s)
        cout<<"-1";
    else
        cout<<k;
    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){

    long long int k=1,s=0;
    int n,i;
    bool flag=0;
    cin >> n;
    for(i=0;i<n;i++){
        cin >>k;
        if(k==1)
            flag=1;

    }
    if(flag)
        cout<<"-1";
    else
        cout<<"1";

    return 0;
}


#include <bits/stdc++.h>
#define   rep(i,t)   for(int i=0;i<t;i++)
using namespace std;

int main(){
    long int a[1001];
	int n;
	cin>>n;
	rep(i,n){
		cin>>a[i];

	}
	sort(a,a+n);
	int flag =0;
	long int ans =0;
	rep(i,n){
		if(a[i]-a[i-1] > 2) {
			flag =1;
			if(a[i-1] -a[i-2] == 1){
				ans = a[i-1] + 2;
			}
			else    ans =  a[i-1]+1;
	}
   }
	if(flag == 0)   cout<<"-1"<<endl;
	else            cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define   rep(i,t)   for(int i=0;i<t;i++)
using namespace std;

int main(){
    long int a[1001];
	int n;
	cin>>n;
	rep(i,n){
		cin>>a[i];

	}
	sort(a,a+n);
	int flag =0;
	long int ans =0;
/*	rep(i,n){
		if(a[i] != (i+1)){
			ans = (i+1);
			cout<<ans<<endl;
			return 0;
		}
	} */
	if(a[0] != 1)   ans = 1;
	else {
	 rep(i,n){
		if(a[i]-a[i-1] > 2) {
			flag =1;
			if(a[i-1] -a[i-2] == 1){
				ans = a[i-1] + 2;
			}
			else    ans =  a[i-1]+1;
	}
   }
 }
	if(flag == 0)   cout<<"-1"<<endl;
	else            cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define   rep(i,t)   for(int i=0;i<t;i++)
using namespace std;

int main(){
    long int a[1001];
	int n;
	cin>>n;
	rep(i,n){
		cin>>a[i];

	}
	sort(a,a+n);
	int flag = 1;
	long int ans =0;
	if(a[0] != 1)        ans = 1;
	else if(a[0] == 1)   ans = -1;
	else {
		flag = 0;
		//cout<<"u"<<endl;
	 rep(i,n){
		if(a[i]-a[i-1] > 2) {
			flag =1;
			if(a[i-1] -a[i-2] == 1){
				ans = a[i-1] + 2;
			}
			else    ans =  a[i-1]+1;
	}
   }
 }
	if(flag == 0)   cout<<"-1"<<endl;
	else            cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define   rep(i,t)   for(int i=0;i<t;i++)
using namespace std;

int main(){
    long int a[1001];
	int n;
	cin>>n;
	rep(i,n){
		cin>>a[i];

	}
	sort(a,a+n);
	int flag = 1;
	long int ans =0;
/*	rep(i,n){
		if(a[i] != (i+1)){
			ans = (i+1);
			cout<<ans<<endl;
			return 0;
		}
	} */
	if(a[0] != 1)        ans = 1;
	else if(a[0] != 2)   ans = 2;
	else {
		flag = 0;
		//cout<<"u"<<endl;
	 rep(i,n){
		if(a[i]-a[i-1] > 2) {
			flag =1;
			if(a[i-1] -a[i-2] == 1){
				ans = a[i-1] + 2;
			}
			else    ans =  a[i-1]+1;
	}
   }
 }
	if(flag == 0)   cout<<"-1"<<endl;
	else            cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define   rep(i,t)   for(int i=0;i<t;i++)
using namespace std;

int main(){
    long int a[1001];
	int n;
	cin>>n;
	rep(i,n){
		cin>>a[i];

	}
	sort(a,a+n);
	int flag = 1;
	long int ans =0;
	if(a[0] != 1)        ans = 1;
	else if(a[1] != 2)   ans = 2;
	else {
		flag = 0;
		//cout<<"u"<<endl;
	 rep(i,n){
		if(a[i]-a[i-1] > 2) {
			flag =1;
			if(a[i-1] -a[i-2] == 1){
				ans = a[i-1] + 2;
			}
			else    ans =  a[i-1]+1;
	}
   }
 }
	if(flag == 0)   cout<<"-1"<<endl;
	else            cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define   rep(i,t)   for(int i=0;i<t;i++)
using namespace std;

int main(){
    long int a[1001];
	int n;
	cin>>n;
	rep(i,n){
		cin>>a[i];

	}
	sort(a,a+n);
	int flag = 1;
	long int ans =0;
	if(a[0] != 1)        ans = 1;
	//else if(a[1] != 2)   ans = 2;
	else {
		flag = 0;
		//cout<<"u"<<endl;
	 rep(i,n){
		if(a[i]-a[i-1] > 2) {
			flag =1;
			if(a[i-1] -a[i-2] == 1){
				ans = a[i-1] + 2;
			}
			else    ans =  a[i-1]+1;
	}
   }
 }
	if(flag == 0)   cout<<"-1"<<endl;
	else            cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
int a[1005];
int v[1000005];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("date.in","r",stdin);
        freopen("date.out","w",stdout);
    #endif // ONLINE_JUDGE
    cin.sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    long long s=0;
    int sm=(n*(n+1))/2;
    int maax=-1000005;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        v[a[i]]++;
        if(a[i]>maax)
            maax=a[i];
    }
    /*if(sm==s)
        cout<<"-1";
    else
        if(sm>s)
            cout<<sm-s;
        else
            cout<<"-1";*/
        if(v[1]>=1)
        {
            cout<<"-1";
        }
        else
            cout<<"1";
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
int a[1005];
int v[1000005];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("date.in","r",stdin);
        freopen("date.out","w",stdout);
    #endif // ONLINE_JUDGE
    cin.sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    long long s=0;
    int sm=(n*(n+1))/2;
    int maax=-1000005;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        v[a[i]]++;
        if(a[i]>maax)
            maax=a[i];
    }
    /*if(sm==s)
        cout<<"-1";
    else
        if(sm>s)
            cout<<sm-s;
        else
            cout<<"-1";*/
    int sol=0;
    for(int i=1;i<=maax;i++)
        if(v[i]==0)
            sol+=i;
    if(sol==0)
        cout<<"-1";
    else
        cout<<sol;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
int a[1005];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("date.in","r",stdin);
        freopen("date.out","w",stdout);
    #endif // ONLINE_JUDGE
    cin.sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    long long s=0;
    int sm=(n*(n+1))/2;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        s+=a[i];
    }
    if(sm-s==0)
        cout<<"-1";
    else
        cout<<abs(sm-s);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
int a[1005];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("date.in","r",stdin);
        freopen("date.out","w",stdout);
    #endif // ONLINE_JUDGE
    cin.sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    long long s=0;
    int sm=(n*(n+1))/2;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        s+=a[i];
    }
    if(sm==s)
        cout<<"-1";
    else
        if(sm>s)
            cout<<sm-s;
        else
            cout<<"-1";
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
int a[1005];
int v[1000005];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("date.in","r",stdin);
        freopen("date.out","w",stdout);
    #endif // ONLINE_JUDGE
    cin.sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    long long s=0;
    int sm=(n*(n+1))/2;
    int maax=-1000005;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        v[a[i]]++;
        if(a[i]>maax)
            maax=a[i];
    }
    /*if(sm==s)
        cout<<"-1";
    else
        if(sm>s)
            cout<<sm-s;
        else
            cout<<"-1";*/
    int sol=0;
    for(int i=1;i<=maax;i++)
        if(v[i]==0)
            sol++;
    if(sol==0)
        cout<<"-1";
    else
        cout<<sol;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[1005];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("date.in","r",stdin);
        freopen("date.out","w",stdout);
    #endif // ONLINE_JUDGE
    cin.sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin>>n;
    long long s=0;
    long long sm=(n*(n+1))/2;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        s+=a[i];
    }
    if(sm-s==0)
        cout<<"-1";
    else
        if(s<sm)
            cout<<sm-s<<"\n";
        else
            cout<<s-sm<<"\n";
    return 0;
}


#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<cstring>
int main()
{
	int a[2000100],b[1010];
	int n,maxx=0,minn=1000000;
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
    {
	    scanf("%d",&b[i]);
	    if (b[i]>maxx) maxx=b[i];
	    if (b[i]<minn) minn=b[i];
	}
	for (int i=1;i<=maxx+minn;i++)
	  a[i]=0;
	a[0]=1;
	for (int i=1;i<=n;i++)
	  for (int j=0;j<=maxx-b[i]+minn;j++)
	    if (a[j]==1) a[j+b[i]]=1;
	int flag=0;
	for (int i=1;i<=maxx+minn;i++)
	  if (a[i]==0)
      {
      	 flag=1;
      	 printf("%d\n",i);
      	 break;
	  }
	 if (flag==0) printf("-1\n");
	 return 0;
}


#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<cstring>
int main()
{
	int a[1000100],b[1010];
	int n,maxx=0,minn=1000000;
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
    {
	    scanf("%d",&b[i]);
	    if (b[i]>maxx) maxx=b[i];
	    if (b[i]<minn) minn=b[i];
	}
	for (int i=1;i<=maxx+minn;i++)
	  a[i]=0;
	a[0]=1;
	for (int i=1;i<=n;i++)
	  for (int j=0;j<=maxx-b[i]+minn;j++)
	    if (a[j]==1) a[j+b[i]]=1;
	int flag=0;
	for (int i=1;i<=maxx+minn;i++)
	  if (a[i]==0)
      {
      	 flag=1;
      	 printf("%d\n",i);
      	 break;
	  }
	 if (flag==0) printf("-1\n");
	 return 0;
}


#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<cstring>
int main()
{
	int a[1001000],b[1010];
	int n,maxx;
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
    {
	    scanf("%d",&b[i]);
	    if (b[i]>maxx) maxx=b[i];
	}
	for (int i=1;i<=maxx;i++)
	  a[i]=0;
	a[0]=1;
	for (int i=1;i<=n;i++)
	  for (int j=maxx-b[i];j>=0;j++)
	    if (a[i]==1) a[j+b[i]]=1;
	int flag=0;
	for (int i=1;i<=maxx;i++)
	  if (a[i]==0)
      {
      	 flag=1;
      	 printf("%d\n",i);
      	 break;
	  }
	 if (flag==0) printf("-1\n");
	 return 0;
}


#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<cstring>
int main()
{
	int a[100100],b[1010];
	int n,maxx=0,minn=1000000;
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
    {
	    scanf("%d",&b[i]);
	    if (b[i]>maxx) maxx=b[i];
	    if (b[i]<minn) minn=b[i];
	}
	for (int i=1;i<=maxx+minn;i++)
	  a[i]=0;
	a[0]=1;
	for (int i=1;i<=n;i++)
	  for (int j=maxx-b[i]+minn;j>=0;j--)
	    if (a[j]==1) a[j+b[i]]=1;
	int flag=0;
	for (int i=1;i<=maxx+minn;i++)
	  if (a[i]==0)
      {
      	 flag=1;
      	 printf("%d\n",i);
      	 break;
	  }
	 if (flag==0) printf("-1\n");
	 return 0;
}


#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<cstring>
int main()
{
	int a[1000100],b[1010];
	int n,maxx=0,minn=1000000;
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
    {
	    scanf("%d",&b[i]);
	    if (b[i]>maxx) maxx=b[i];
	    if (b[i]<minn) minn=b[i];
	}
	for (int i=1;i<=maxx+minn;i++)
	  a[i]=0;
	a[0]=1;
	for (int i=1;i<=n;i++)
	  for (int j=maxx-b[i]+minn;j>=0;j--)
	    if (a[j]==1) a[j+b[i]]=1;
	int flag=0;
	for (int i=1;i<=maxx+minn;i++)
	  if (a[i]==0)
      {
      	 flag=1;
      	 printf("%d\n",i);
      	 break;
	  }
	 if (flag==0) printf("-1\n");
	 return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector<int>v;
    int x;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        v.push_back(x);
    }
    sort(v.begin(),v.end());
    int sum=0;
    sum=v[0];
    if(sum>0)

    for(int i=0;i<n;)
    {
        if(sum%v[i]==0)
            sum=sum/v[i];
        else i++;

    }
    if(sum<=0) cout<<-1<<endl;
    else cout<<sum<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector<int>v;
    int x;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        v.push_back(x);
    }

    int sum=0;
    for(int i=0;i<n-1;i++)
    {
        if(abs(v[i]-v[i+1]>1))
        {
            sum+=abs(v[i]-v[i+1])-1;
        }
    }
    if(sum==0) cout<<-1<<endl;
    else cout<<sum<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector<int>v;
    int x;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        v.push_back(x);
    }
    sort(v.begin(),v.end());
    int sum=0;
    sum=v[0];
    if(sum>1)

    for(int i=0;i<n;)
    {
        if(sum%v[i]==0)
            sum=sum/v[i];
        else i++;

    }
    if(sum<=0) cout<<-1<<endl;
    else cout<<sum<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector<int>v,l;
    int x;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        v.push_back(x);
    }
    sort(v.begin(),v.end());
    int sum=0,s=0;

    for(int j=0;j<n;j++)
    {
        sum=v[j];
        if(sum>1){

        int k=0;
        for(int i=0;i<n;)
        {
            if(sum%v[i]==0&&k<=v[i])
           {
                sum=sum/v[i];
                k++;

           }
            else {
                k=0;i++;

            }

        }}
         if(sum>0) break;
    }

    if(sum<=0) cout<<-1<<endl;
    else cout<<sum<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector<int>v;
    int x;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        v.push_back(x);
    }
    sort(v.begin(),v.end());
    int sum=0;
    for(int i=0;i<n-1;i++)
    {
        if(abs(v[i]-v[i+1]>1))
        {
            sum+=abs(v[i]-v[i+1]);
        }
    }
    if(sum==0) cout<<-1<<endl;
    else cout<<sum<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector<int>v;
    int x;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        v.push_back(x);
    }
    sort(v.begin(),v.end());
    int sum=0;
    for(int i=0;i<n-1;i++)
    {
        if(abs(v[i]-v[i+1]>1))
        {
            sum+=abs(v[i]-v[i+1])-1;
        }
    }
    if(sum==0) cout<<-1<<endl;
    else cout<<sum<<endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector<int>v;
    int x;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        v.push_back(x);
    }
    sort(v.begin(),v.end());
    int sum=0;
    sum=v[0]-1;
    if(sum>0)

    for(int i=0;i<n;)
    {
        if(sum%v[i]==0)
            sum=sum%v[i];
        else i++;

    }
    if(sum<=0) cout<<-1<<endl;
    else cout<<sum<<endl;

    return 0;
}


#include <bits/stdc++.h>
#define N 2005
#define ll long long
#define x first
#define y second
#define all(v) v.begin(),v.end()
using namespace std;
int n;
int val[N];
int vis[N];
vector< vector< pair<int,int> >  > hel(N);
int ans[N];
int sz[N];
vector< vector< int > > adj(N);
int ma;
vector< pair<int,int> > chi;
int no=0;
void traverse( int a){
  //  cout<<a<<" ddd "<<no<<"\n";
    if(no!=a){
    chi.push_back({ans[a],a});
    hel[no].push_back({ans[a],a});
    }
    for(auto j : adj[a])
        traverse(j);
}
void dfs( int ver){
    if(vis[ver])return ;
    ma++;
 //   cout<<ver<<" hell \n ";
    vis[ver]=1;
    for(auto j : adj[ver]){
        if(vis[j])continue;
       // cout<<ver<<" "<<j<<"\n";
        dfs(j);
        sz[ver]+= sz[j]+1;
    }
    if(sz[ver]<val[ver]){
        cout<<"NO";
        exit(0);
    }
    no=ver;
    traverse(ver);
   // hel[ver]=chi;
   int h= chi.size();
    if(val[ver]<h){
    sort(all(chi));
    ans[ver]= chi[val[ver]-1].x+1;
    }
    else ans[ver]= ma+1;
    ma++;
    ma=max(ma,ans[ver]+1);
    for(auto j: chi){
        if(ans[j.y]>=ans[ver])ans[j.y]++;
        ma= max(ma, ans[j.y]);
    }
    ma++;
    chi.clear();
}
void che( int a){
    for(auto j : adj[a])
        che(j);
    int g=0;
  //  cout<<a<<" "<<hel[a].size()<<" hel \n";
    for(int i=0;i<hel[a].size();i++){
     //       cout<<a<<" "<<hel[a][i].y<<" hgr\n";
        if(ans[a]>ans[hel[a][i].y])g++;
        hel[a][i].x=ans[hel[a][i].y];
    }
    if(hel[a].empty())return ;
    sort(all(hel[a]));
    if(g==val[a])return ;
   // cout<<g<< " " << val[a]<<" "<<ans[a]<<"\n";
  //  return ;
  if((val[a]-1)>=hel[a].size()){
      ans[a]=ans[ hel[a][hel[a].size()-1].y]+1;
      return ;
  }
    ans[a]= ans[hel[a][val[a]-1].y]+1;
    for(int i=0;i<hel[a].size();i++)
        if(ans[a]<=ans[hel[a][i].y])ans[hel[a][i].y]++;

}
int main() {
            ios_base::sync_with_stdio(false);
            cin.tie(NULL);
            cout.tie(NULL);
            cin>>n;
            int root=1;
            for(int i=0;i<n;i++){
                int p;
                cin>>p>>val[i+1];
                if(p){
                    adj[p].push_back(i+1);
                }
                else root=i+1;
            }
            dfs(root);
            che(root);
            cout<<"YES\n";
            for(int i=1;i<=n;i++)
                cout<<ans[i]<<" ";
            return 0;
}


#include <bits/stdc++.h>
#define N 2005
#define ll long long
#define x first
#define y second
#define all(v) v.begin(),v.end()
using namespace std;
int n;
int val[N];
int vis[N];
int ans[N];
int sz[N];
vector< vector< int > > adj(N);
int ma;
vector< pair<int,int> > chi;
int no=0;
void traverse( int a){
    if(no!=a)
    chi.push_back({ans[a],a});
    for(auto j : adj[a])
        traverse(j);
}
void dfs( int ver){
    if(vis[ver])return ;
    ma++;
 //   cout<<ver<<" hell \n ";
    vis[ver]=1;
    for(auto j : adj[ver]){
        if(vis[j])continue;
       // cout<<ver<<" "<<j<<"\n";
        dfs(j);
        sz[ver]+= sz[j]+1;
    }
    if(sz[ver]<val[ver]){
        cout<<"NO";
        exit(0);
    }
    no=ver;
    traverse(ver);
    if(!chi.empty())
    sort(all(chi));
    if(!chi.empty())
    ans[ver]= chi[val[ver]-1].x+1;
    else ans[ver]= ma;
    ma=max(ma,ans[ver]+1);
    for(auto j: chi){
        if(ans[j.y]>=ans[ver])ans[j.y]++;
        ma= max(ma, ans[j.y]);
    }
    chi.clear();
}
int main() {
            ios_base::sync_with_stdio(false);
            cin.tie(NULL);
            cout.tie(NULL);
            cin>>n;
            int root=1;
            for(int i=0;i<n;i++){
                int p;
                cin>>p>>val[i+1];
                if(p){
                    adj[p].push_back(i+1);
                }
                else root=i+1;
            }
            dfs(root);
            cout<<"YES\n";
            for(int i=1;i<=n;i++)
                cout<<ans[i]<<" ";
            return 0;
}


#include <bits/stdc++.h>
#define N 2005
#define ll long long
#define x first
#define y second
#define all(v) v.begin(),v.end()
using namespace std;
int n;
int val[N];
int vis[N];
vector< vector< pair<int,int> >  > hel(N);
int ans[N];
int sz[N];
vector< vector< int > > adj(N);
int ma;
vector< pair<int,int> > chi;
int no=0;
void traverse( int a){
    if(no!=a)
    chi.push_back({ans[a],a});
    for(auto j : adj[a])
        traverse(j);
}
void dfs( int ver){
    if(vis[ver])return ;
    ma++;
 //   cout<<ver<<" hell \n ";
    vis[ver]=1;
    for(auto j : adj[ver]){
        if(vis[j])continue;
       // cout<<ver<<" "<<j<<"\n";
        dfs(j);
        sz[ver]+= sz[j]+1;
    }
    if(sz[ver]<val[ver]){
        cout<<"NO";
        exit(0);
    }
    no=ver;
    traverse(ver);
    hel[ver]=chi;
   int h= chi.size();
    if(val[ver]<h){
    sort(all(chi));
    ans[ver]= chi[val[ver]-1].x+1;
    }
    else ans[ver]= ma+1;
    ma++;
    ma=max(ma,ans[ver]+1);
    for(auto j: chi){
        if(ans[j.y]>=ans[ver])ans[j.y]++;
        ma= max(ma, ans[j.y]);
    }
    ma++;
    chi.clear();
}
void che( int a){
    for(auto j : adj[a])
        che(j);
    int g=0;
    for(int i=0;i<hel[a].size();i++)
        if(ans[a]>ans[hel[a][i].y])g++;
    //cout<<g<< " " << val[a]<<"\n";
    if(g<val[a])while(1){
        ;
    }
}
int main() {
            ios_base::sync_with_stdio(false);
            cin.tie(NULL);
            cout.tie(NULL);
            cin>>n;
            int root=1;
            for(int i=0;i<n;i++){
                int p;
                cin>>p>>val[i+1];
                if(p){
                    adj[p].push_back(i+1);
                }
                else root=i+1;
            }
            dfs(root);
            che(root);
            cout<<"YES\n";
            for(int i=1;i<=n;i++)
                cout<<ans[i]<<" ";
            return 0;
}


#include <bits/stdc++.h>
#define N 2005
#define ll long long
#define x first
#define y second
#define all(v) v.begin(),v.end()
using namespace std;
int n;
int val[N];
int vis[N];
vector< vector< pair<int,int> >  > hel(N);
int ans[N];
int sz[N];
vector< vector< int > > adj(N);
int ma;
vector< pair<int,int> > chi;
int no=0;
void traverse( int a){
    if(no!=a)
    chi.push_back({ans[a],a});
    for(auto j : adj[a])
        traverse(j);
}
void dfs( int ver){
    if(vis[ver])return ;
    ma++;
 //   cout<<ver<<" hell \n ";
    vis[ver]=1;
    for(auto j : adj[ver]){
        if(vis[j])continue;
       // cout<<ver<<" "<<j<<"\n";
        dfs(j);
        sz[ver]+= sz[j]+1;
    }
    if(sz[ver]<val[ver]){
        cout<<"NO";
        exit(0);
    }
    no=ver;
    traverse(ver);
    hel[ver]=chi;
   int h= chi.size();
    if(val[ver]<h){
    sort(all(chi));
    ans[ver]= chi[val[ver]-1].x+1;
    }
    else ans[ver]= ma+1;
    ma++;
    ma=max(ma,ans[ver]+1);
    for(auto j: chi){
        if(ans[j.y]>=ans[ver])ans[j.y]++;
        ma= max(ma, ans[j.y]);
    }
    ma++;
    chi.clear();
}
void che( int a){
    for(auto j : adj[a])
        che(j);
    int g=0;
    for(int i=0;i<hel[a].size();i++){
        if(ans[a]>ans[hel[a][i].y])g++;
        hel[a][i].x=ans[hel[a][i].y];
    }
    sort(all(hel));
    if(g==val[a])return ;
    //cout<<g<< " " << val[a]<<"\n";
    ans[a]= ans[hel[a][val[a]-1].y]+1;
    for(int i=0;i<hel[a].size();i++)
        if(ans[a]<=ans[hel[a][i].y])ans[hel[a][i].y]++;

}
int main() {
            ios_base::sync_with_stdio(false);
            cin.tie(NULL);
            cout.tie(NULL);
            cin>>n;
            int root=1;
            for(int i=0;i<n;i++){
                int p;
                cin>>p>>val[i+1];
                if(p){
                    adj[p].push_back(i+1);
                }
                else root=i+1;
            }
            dfs(root);
         //   che(root);
            cout<<"YES\n";
            for(int i=1;i<=n;i++)
                cout<<ans[i]<<" ";
            return 0;
}


#include <bits/stdc++.h>
#define N 2005
#define ll long long
#define x first
#define y second
#define all(v) v.begin(),v.end()
using namespace std;
int n;
int val[N];
int vis[N];
vector< vector< pair<int,int> >  > hel(N);
int ans[N];
int sz[N];
vector< vector< int > > adj(N);
int ma;
vector< pair<int,int> > chi;
int no=0;
void traverse( int a){
    if(no!=a)
    chi.push_back({ans[a],a});
    for(auto j : adj[a])
        traverse(j);
}
void dfs( int ver){
    if(vis[ver])return ;
    ma++;
 //   cout<<ver<<" hell \n ";
    vis[ver]=1;
    for(auto j : adj[ver]){
        if(vis[j])continue;
       // cout<<ver<<" "<<j<<"\n";
        dfs(j);
        sz[ver]+= sz[j]+1;
    }
    if(sz[ver]<val[ver]){
        cout<<"NO";
        exit(0);
    }
    no=ver;
    traverse(ver);
    hel[ver]=chi;
    if(!chi.empty())
    sort(all(chi));
    if(!chi.empty())
    ans[ver]= chi[val[ver]-1].x+1;
    else ans[ver]= ma+1;
    ma=max(ma,ans[ver]+1);
    for(auto j: chi){
        if(ans[j.y]>=ans[ver])ans[j.y]++;
        ma= max(ma, ans[j.y]);
    }
    ma++;
    chi.clear();
}
void che( int a){
    for(auto j : adj[a])
        traverse(j);
    int g=0;
    for(int i=0;i<hel[a].size();i++)
        if(ans[a]>ans[hel[a][i].y])g++;
    if(g!=val[a])while(1){
        ;
    }
}
int main() {
            ios_base::sync_with_stdio(false);
            cin.tie(NULL);
            cout.tie(NULL);
            cin>>n;
            int root=1;
            for(int i=0;i<n;i++){
                int p;
                cin>>p>>val[i+1];
                if(p){
                    adj[p].push_back(i+1);
                }
                else root=i+1;
            }
            dfs(root);
            che(root);
            cout<<"YES\n";
            for(int i=1;i<=n;i++)
                cout<<ans[i]<<" ";
            return 0;
}


#include <bits/stdc++.h>
#define N 2005
#define ll long long
#define x first
#define y second
#define all(v) v.begin(),v.end()
using namespace std;
int n;
int val[N];
int vis[N];
int ans[N];
int sz[N];
vector< vector< int > > adj(N);
int ma;
vector< pair<int,int> > chi;
int no=0;
void traverse( int a){
    if(no!=a)
    chi.push_back({ans[a],a});
    for(auto j : adj[a])
        traverse(j);
}
void dfs( int ver){
    if(vis[ver])return ;
    ma++;
 //   cout<<ver<<" hell \n ";
    vis[ver]=1;
    for(auto j : adj[ver]){
        if(vis[j])continue;
       // cout<<ver<<" "<<j<<"\n";
        dfs(j);
        sz[ver]+= sz[j]+1;
    }
    if(sz[ver]<val[ver]){
        cout<<"NO";
        exit(0);
    }
    no=ver;
    traverse(ver);
    if(!chi.empty())
    sort(all(chi));
    if(!chi.empty())
    ans[ver]= chi[val[ver]-1].x+1;
    else ans[ver]= ma+1;
    ma=max(ma,ans[ver]+1);
    for(auto j: chi){
        if(ans[j.y]>=ans[ver])ans[j.y]++;
        ma= max(ma, ans[j.y]);
    }
    ma++;
    chi.clear();
}
int main() {
            ios_base::sync_with_stdio(false);
            cin.tie(NULL);
            cout.tie(NULL);
            cin>>n;
            int root=1;
            for(int i=0;i<n;i++){
                int p;
                cin>>p>>val[i+1];
                if(p){
                    adj[p].push_back(i+1);
                }
                else root=i+1;
            }
            dfs(root);
            cout<<"YES\n";
            for(int i=1;i<=n;i++)
                cout<<ans[i]<<" ";
            return 0;
}


#include <bits/stdc++.h>
#define N 2005
#define ll long long
#define x first
#define y second
#define all(v) v.begin(),v.end()
using namespace std;
int n;
int val[N];
int vis[N];
vector< vector< pair<int,int> >  > hel(N);
int ans[N];
int sz[N];
vector< vector< int > > adj(N);
int ma;
vector< pair<int,int> > chi;
int no=0;
void traverse( int a){
  //  cout<<a<<" ddd "<<no<<"\n";
    if(no!=a){
    chi.push_back({ans[a],a});
    hel[no].push_back({ans[a],a});
    }
    for(auto j : adj[a])
        traverse(j);
}
void dfs( int ver){
    if(vis[ver])return ;
    ma++;
 //   cout<<ver<<" hell \n ";
    vis[ver]=1;
    for(auto j : adj[ver]){
        if(vis[j])continue;
       // cout<<ver<<" "<<j<<"\n";
        dfs(j);
        sz[ver]+= sz[j]+1;
    }
    if(sz[ver]<val[ver]){
        cout<<"NO";
        exit(0);
    }
    no=ver;
    traverse(ver);
   // hel[ver]=chi;
   int h= chi.size();
    if(val[ver]<h){
    sort(all(chi));
    ans[ver]= chi[val[ver]-1].x+1;
    }
    else ans[ver]= ma+1;
    ma++;
    ma=max(ma,ans[ver]+1);
    for(auto j: chi){
        if(ans[j.y]>=ans[ver])ans[j.y]++;
        ma= max(ma, ans[j.y]);
    }
    ma++;
    chi.clear();
}
void che( int a){
    for(auto j : adj[a])
        che(j);
    int g=0;
  //  cout<<a<<" "<<hel[a].size()<<" hel \n";
    for(int i=0;i<hel[a].size();i++){
     //       cout<<a<<" "<<hel[a][i].y<<" hgr\n";
        if(ans[a]>ans[hel[a][i].y])g++;
        hel[a][i].x=ans[hel[a][i].y];
    }
    if(hel[a].empty())return ;
    sort(all(hel[a]));
    if(g==val[a])return ;
   // cout<<g<< " " << val[a]<<" "<<ans[a]<<"\n";
  //  return ;
  if(val[a]-1>=hel[a].size()){
      ans[a]= hel[a][hel[a].size()-1]+1;
      return ;
  }
    ans[a]= ans[hel[a][val[a]-1].y]+1;
    for(int i=0;i<hel[a].size();i++)
        if(ans[a]<=ans[hel[a][i].y])ans[hel[a][i].y]++;

}
int main() {
            ios_base::sync_with_stdio(false);
            cin.tie(NULL);
            cout.tie(NULL);
            cin>>n;
            int root=1;
            for(int i=0;i<n;i++){
                int p;
                cin>>p>>val[i+1];
                if(p){
                    adj[p].push_back(i+1);
                }
                else root=i+1;
            }
            dfs(root);
            che(root);
            cout<<"YES\n";
            for(int i=1;i<=n;i++)
                cout<<ans[i]<<" ";
            return 0;
}


#include <bits/stdc++.h>
#define N 2005
#define ll long long
#define x first
#define y second
#define all(v) v.begin(),v.end()
using namespace std;
int n;
int val[N];
int vis[N];
int ans[N];
int sz[N];
vector< vector< int > > adj(N);
int ma;
vector< pair<int,int> > chi;
int no=0;
void traverse( int a){
    if(no!=a)
    chi.push_back({ans[a],a});
    for(auto j : adj[a])
        traverse(j);
}
void dfs( int ver){
    if(vis[ver])return ;
    ma+=3;
 //   cout<<ver<<" hell \n ";
    vis[ver]=1;
    for(auto j : adj[ver]){
        if(vis[j])continue;
       // cout<<ver<<" "<<j<<"\n";
        dfs(j);
        sz[ver]+= sz[j]+1;
    }
    if(sz[ver]<val[ver]){
        cout<<"NO";
        exit(0);
    }
    no=ver;
    traverse(ver);
    if(!chi.empty()){
    sort(all(chi));
   // cout<<chi.size()<<" "<<ver<<" "<<val[ver]<<"\n";
    if(val[ver]==0){
        ans[ver]=chi[0].x;
    }

    else ans[ver]= chi[val[ver]-1].x+1;
    }
    else ans[ver]= ma+1;
    ma=max(ma,ans[ver]+1);
    for(auto j: chi){
        if(ans[j.y]>=ans[ver])ans[j.y]++;
        ma= max(ma, ans[j.y]+1);
    }
    ma++;
    chi.clear();
}
int main() {
            ios_base::sync_with_stdio(false);
            cin.tie(NULL);
            cout.tie(NULL);
            cin>>n;
            int root=1;
            for(int i=0;i<n;i++){
                int p;
                cin>>p>>val[i+1];
                if(p){
                    adj[p].push_back(i+1);
                }
                else root=i+1;
            }
            dfs(root);
            cout<<"YES\n";
            for(int i=1;i<=n;i++)
                cout<<ans[i]<<" ";
            return 0;
}


#include <bits/stdc++.h>
#define N 2005
#define ll long long
#define x first
#define y second
#define all(v) v.begin(),v.end()
using namespace std;
int n;
int val[N];
int vis[N];
vector< vector< pair<int,int> >  > hel(N);
int ans[N];
int sz[N];
vector< vector< int > > adj(N);
int ma;
vector< pair<int,int> > chi;
int no=0;
void traverse( int a){
  //  cout<<a<<" ddd "<<no<<"\n";
    if(no!=a){
    chi.push_back({ans[a],a});
    hel[no].push_back({ans[a],a});
    }
    for(auto j : adj[a])
        traverse(j);
}
void dfs( int ver){
    if(vis[ver])return ;
    ma++;
 //   cout<<ver<<" hell \n ";
    vis[ver]=1;
    for(auto j : adj[ver]){
        if(vis[j])continue;
       // cout<<ver<<" "<<j<<"\n";
        dfs(j);
        sz[ver]+= sz[j]+1;
    }
    if(sz[ver]<val[ver]){
        cout<<"NO";
        exit(0);
    }
    no=ver;
    traverse(ver);
   // hel[ver]=chi;
   int h= chi.size();
    if(val[ver]<h){
    sort(all(chi));
    ans[ver]= chi[val[ver]-1].x+1;
    }
    else ans[ver]= ma+1;
    ma++;
    ma=max(ma,ans[ver]+1);
    for(auto j: chi){
        if(ans[j.y]>=ans[ver])ans[j.y]++;
        ma= max(ma, ans[j.y]);
    }
    ma++;
    chi.clear();
}
void che( int a){
    for(auto j : adj[a])
        che(j);
    int g=0;
  //  cout<<a<<" "<<hel[a].size()<<" hel \n";
    for(int i=0;i<hel[a].size();i++){
     //       cout<<a<<" "<<hel[a][i].y<<" hgr\n";
        if(ans[a]>ans[hel[a][i].y])g++;
        hel[a][i].x=ans[hel[a][i].y];
    }
    sort(all(hel[a]));
    if(g==val[a])return ;
   // cout<<g<< " " << val[a]<<" "<<ans[a]<<"\n";
  //  return ;
    ans[a]= ans[hel[a][val[a]-1].y]+1;
    for(int i=0;i<hel[a].size();i++)
        if(ans[a]<=ans[hel[a][i].y])ans[hel[a][i].y]++;

}
int main() {
            ios_base::sync_with_stdio(false);
            cin.tie(NULL);
            cout.tie(NULL);
            cin>>n;
            int root=1;
            for(int i=0;i<n;i++){
                int p;
                cin>>p>>val[i+1];
                if(p){
                    adj[p].push_back(i+1);
                }
                else root=i+1;
            }
            dfs(root);
            che(root);
            cout<<"YES\n";
            for(int i=1;i<=n;i++)
                cout<<ans[i]<<" ";
            return 0;
}


#include <bits/stdc++.h>

#define LL long long

using namespace std;

vector<vector<LL>> g;
vector<LL> c;
vector<LL> sz;
vector<LL> p;
vector<LL> res;
LL rem = 1;
bool pos = true;
vector<LL> b;

void dfs(LL u){
	for (LL v : g[u]){
		dfs(v);
		sz[u] += 1+sz[v];
	}
}

void increment(LL u, LL val){
	res[u] += val;
	for (LL v : g[u])
		increment(v,val);
}

void multiply(LL u){
	res[u] *= 2;
	for (LL v : g[u])
		multiply(v);
}

void below(LL u){
	b.push_back(res[u]);
	for (LL v : g[u])
		below(v);
}

void solve(LL u){
	if (sz[u] < c[u]){
		pos = false;
		return;
	}
	for (LL v : g[u]){
		solve(v);
	}
	b.clear();
	LL mx = 0;
	for (LL v : g[u]){
		increment(v,mx);
		below(v);
		for (LL foo : b)
			mx = max(mx,foo);
		mx += 3000;
	}
	sort(b.begin(),b.end());
	if (c[u] == 0)
		res[u] = 1;
	else if (c[u] == sz[u])
		res[u] = b.back()+2;
	else if (c[u] == 0)
		res[u] = 1;
	else
		res[u] = b[c[u]-1]+1;
	for (LL foo : b){
		if (foo == res[u]){
			multiply(u);
			res[u]--;
			break;
		}
	}
	assert(res[u] <= 1e9);
}

int main(){
	ios_base::sync_with_stdio(false);
	LL n;
	cin >> n;
	g.resize(n);
	c.resize(n);
	sz.resize(n,0);
	p.resize(n);
	res.resize(n);
	for (LL i = 0; i < n; i++){
		cin >> p[i];
		p[i]--;
		cin >> c[i];
		if (p[i] >= 0)
			g[p[i]].push_back(i);
	}
	for (LL i = 0; i < n; i++){
		if (p[i] < 0){
			dfs(i);
			solve(i);
		}
	}
	if (!pos){
		cout << "NO\n";
	}
	else{
		cout << "YES\n";
		cout << res[0];
		for (LL i = 1; i < n; i++)
			cout << " " << res[i];
		cout << "\n";
	}
}


#include <bits/stdc++.h>

#define LL long long

using namespace std;

vector<vector<LL>> g;
vector<LL> c;
vector<LL> sz;
vector<LL> p;
vector<LL> res;
LL rem = 1;
bool pos = true;
vector<LL> b;

void dfs(LL u){
	for (LL v : g[u]){
		dfs(v);
		sz[u] += 1+sz[v];
	}
}

void increment(LL u, LL val){
	res[u] += val;
	for (LL v : g[u])
		increment(v,val);
}

void multiply(LL u){
	res[u] *= 2;
	for (LL v : g[u])
		multiply(v);
}

void below(LL u){
	b.push_back(res[u]);
	for (LL v : g[u])
		below(v);
}

void solve(LL u){
	if (sz[u] < c[u]){
		pos = false;
		return;
	}
	for (LL v : g[u]){
		solve(v);
	}
	b.clear();
	LL mx = 0;
	for (LL v : g[u]){
		increment(v,mx);
		below(v);
		for (LL foo : b)
			mx = max(mx,foo);
		mx += 2;
		mx += rand()%2020;
	}
	sort(b.begin(),b.end());
	if (c[u] == 0)
		res[u] = 1;
	else if (c[u] == sz[u])
		res[u] = b.back()+2;
	else if (c[u] == 0)
		res[u] = 1;
	else
		res[u] = b[c[u]-1]+1;
	for (LL foo : b){
		if (foo == res[u]){
			multiply(u);
			res[u]--;
			break;
		}
	}
	assert(res[u] <= 1e9);
}

int main(){
	ios_base::sync_with_stdio(false);
	srand(time(nullptr));
	LL n;
	cin >> n;
	g.resize(n);
	c.resize(n);
	sz.resize(n,0);
	p.resize(n);
	res.resize(n);
	for (LL i = 0; i < n; i++){
		cin >> p[i];
		p[i]--;
		cin >> c[i];
		if (p[i] >= 0)
			g[p[i]].push_back(i);
	}
	for (LL i = 0; i < n; i++){
		if (p[i] < 0){
			dfs(i);
			solve(i);
		}
	}
	if (!pos){
		cout << "NO\n";
	}
	else{
		cout << "YES\n";
		cout << res[0];
		for (LL i = 1; i < n; i++)
			cout << " " << res[i];
		cout << "\n";
	}
}


#include <bits/stdc++.h>

#define LL long long

using namespace std;

vector<vector<LL>> g;
vector<LL> c;
vector<LL> sz;
vector<LL> p;
vector<LL> res;
LL rem = 1;
bool pos = true;
vector<LL> b;

void dfs(LL u){
	for (LL v : g[u]){
		dfs(v);
		sz[u] += 1+sz[v];
	}
}

void increment(LL u, LL val){
	res[u] += val;
	for (LL v : g[u])
		increment(v,val);
}

void below(LL u){
	b.push_back(res[u]);
	for (LL v : g[u])
		below(v);
}

void solve(LL u){
	if (sz[u] < c[u]){
		pos = false;
		return;
	}
	for (LL v : g[u]){
		solve(v);
	}
	b.clear();
	LL mx = 0;
	for (LL v : g[u]){
		increment(v,mx);
		below(v);
		for (LL foo : b)
			mx = max(mx,foo);
		mx += 2001;
	}
	sort(b.begin(),b.end());
	if (c[u] == 0)
		res[u] = 1;
	else if (c[u] == sz[u])
		res[u] = b.back()+2;
	else if (c[u] == 0)
		res[u] = 1;
	else
		res[u] = b[c[u]-1]+1;
}

int main(){
	ios_base::sync_with_stdio(false);
	LL n;
	cin >> n;
	g.resize(n);
	c.resize(n);
	sz.resize(n,0);
	p.resize(n);
	res.resize(n);
	for (LL i = 0; i < n; i++){
		cin >> p[i];
		p[i]--;
		cin >> c[i];
		if (p[i] >= 0)
			g[p[i]].push_back(i);
	}
	for (LL i = 0; i < n; i++){
		if (p[i] < 0){
			dfs(i);
			solve(i);
		}
	}
	if (!pos){
		cout << "NO\n";
	}
	else{
		cout << "YES\n";
		cout << res[0];
		for (LL i = 1; i < n; i++)
			cout << " " << res[i];
		cout << "\n";
	}
}


#include <bits/stdc++.h>

#define LL long long

using namespace std;

vector<vector<LL>> g;
vector<LL> c;
vector<LL> sz;
vector<LL> p;
vector<LL> res;
LL rem = 1;
bool pos = true;
vector<LL> b;

void dfs(LL u){
	for (LL v : g[u]){
		dfs(v);
		sz[u] += 1+sz[v];
	}
}

void increment(LL u, LL val){
	res[u] += val;
	for (LL v : g[u])
		increment(v,val);
}

void multiply(LL u){
	res[u] *= 2;
	for (LL v : g[u])
		multiply(v);
}

void below(LL u){
	b.push_back(res[u]);
	for (LL v : g[u])
		below(v);
}

void solve(LL u){
	if (sz[u] < c[u]){
		pos = false;
		return;
	}
	for (LL v : g[u]){
		solve(v);
	}
	b.clear();
	LL mx = 0;
	for (LL v : g[u]){
		increment(v,mx);
		below(v);
		for (LL foo : b)
			mx = max(mx,foo);
		mx += 2;
		mx += rand()%2020;
	}
	sort(b.begin(),b.end());
	if (c[u] == 0)
		res[u] = 1;
	else if (c[u] == sz[u])
		res[u] = b.back()+400;
	else if (c[u] == 0)
		res[u] = 1;
	else
		res[u] = 1+(b[c[u]-1]+b[c[u]])/2;
	for (LL foo : b){
		if (foo == res[u]){
			multiply(u);
			res[u]--;
			break;
		}
	}
	assert(res[u] <= 1e9);
}

int main(){
	ios_base::sync_with_stdio(false);
	srand(time(nullptr));
	LL n;
	cin >> n;
	g.resize(n);
	c.resize(n);
	sz.resize(n,0);
	p.resize(n);
	res.resize(n);
	for (LL i = 0; i < n; i++){
		cin >> p[i];
		p[i]--;
		cin >> c[i];
		if (p[i] >= 0)
			g[p[i]].push_back(i);
	}
	for (LL i = 0; i < n; i++){
		if (p[i] < 0){
			dfs(i);
			solve(i);
		}
	}
	if (!pos){
		cout << "NO\n";
	}
	else{
		cout << "YES\n";
		cout << res[0];
		for (LL i = 1; i < n; i++)
			cout << " " << res[i];
		cout << "\n";
	}
}


#include <bits/stdc++.h>

#define LL long long

using namespace std;

vector<vector<LL>> g;
vector<LL> c;
vector<LL> sz;
vector<LL> p;
vector<LL> res;
LL rem = 1;
bool pos = true;
vector<LL> b;

void dfs(LL u){
	for (LL v : g[u]){
		dfs(v);
		sz[u] += 1+sz[v];
	}
}

void increment(LL u, LL val){
	res[u] += val;
	for (LL v : g[u])
		increment(v,val);
}

void below(LL u){
	b.push_back(res[u]);
	for (LL v : g[u])
		below(v);
}

void solve(LL u){
	if (sz[u] < c[u]){
		pos = false;
		return;
	}
	for (LL v : g[u]){
		solve(v);
	}
	b.clear();
	LL mx = 0;
	for (LL v : g[u]){
		increment(v,mx);
		below(v);
		for (LL foo : b)
			mx = max(mx,foo);
		mx += 3000;
	}
	sort(b.begin(),b.end());
	if (c[u] == 0)
		res[u] = 1;
	else if (c[u] == sz[u])
		res[u] = b.back()+2;
	else if (c[u] == 0)
		res[u] = 1;
	else
		res[u] = b[c[u]-1]+1;
	assert(res[u] <= 1e9);
}

int main(){
	ios_base::sync_with_stdio(false);
	LL n;
	cin >> n;
	g.resize(n);
	c.resize(n);
	sz.resize(n,0);
	p.resize(n);
	res.resize(n);
	for (LL i = 0; i < n; i++){
		cin >> p[i];
		p[i]--;
		cin >> c[i];
		if (p[i] >= 0)
			g[p[i]].push_back(i);
	}
	for (LL i = 0; i < n; i++){
		if (p[i] < 0){
			dfs(i);
			solve(i);
		}
	}
	if (!pos){
		cout << "NO\n";
	}
	else{
		cout << "YES\n";
		cout << res[0];
		for (LL i = 1; i < n; i++)
			cout << " " << res[i];
		cout << "\n";
	}
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

// Use g++ compiler for this include to work.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

// Constants
#define INF 1e18
#define EPS 1e-9
#define MOD 1000000007

// // for using in pairs
#define F first
#define S second

// Macros
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)

// aliases
using ll = long long;
using ull = unsigned long long;
using ui = unsigned int;
using ld = long double;

template<typename T>
using oset = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

// Temporary macro.
#define int long long

// Operator overloads.

/**
 * Both types in the pair should have overload for << operator with ostream.
 */
template<typename T1, typename T2>
ostream& operator<<(ostream &ostream, pair<T1, T2>& p)
{
	ostream << p.first << " " << p.second;
	return ostream;
}

/**
 * The type of element in the vector should have overload for << operator with ostream.
 */
template<typename T>
ostream& operator<<(ostream &stream, vector<T>& v)
{
	for (const auto &it : v)
		stream << it << " ";
	return stream;
}

template<typename T1, typename T2>
istream& operator>>(istream &istream, pair<T1, T2>& p)
{
	istream >> p.first >> p.second;
	return istream;
}

template<typename T>
istream& operator>>(istream &istream, vector<T>& a)
{
	for (auto &it : a)
		istream >> it;
	return istream;
}

// Utility functions.

template<typename T>
T MIN(T first) { return first; }

template<typename T, typename... Args>
T MIN(T first, Args... args) { return min(first, MIN(args...)); }

template<typename T>
T MAX(T first) { return first; }

template<typename T, typename... Args>
T MAX(T first, Args... args) { return max(first, MAX(args...)); }

template <typename T>
void DEBUG(T&& t) {cout << t << "\n";}

template <typename T, typename... Args>
void DEBUG(T&& t, Args&&... args)
{
    cout << t << " ";
    DEBUG(forward<Args>(args)...);
}

template<typename T>
int32_t size_i(T& container) { return static_cast<int32_t>(container.size()); }

// Mathematical functions.

int GCD(int a, int b)
{
	if (!b)
		return a;
	return GCD(b, a % b);
}

int LCM(int a, int b)
{
	return (a * b) / GCD(a, b);
}

int modpow(int x, int n, int m = MOD)
{
	if (n == 0) return 1 % m;
	long long u = modpow(x, n / 2, m);
	u = (u * u) % m;
	if (n % 2 == 1)
		u = (u * x) % m;
	return u;
}

/**
 * m should be prime
 */
int modinv(int x, int m = MOD)
{
	return modpow(x, m - 2, m);
}

//////////////////// START CODE HERE ///////////////////////////

void preSolve()
{
}

int dist = 0;

void solve2(vector<vector<int>> &adj, vector<int>& c, vector<int>& a, int node, int x, vector<int>& v)
{
	vector<int> vc;

	for (auto& it : adj[node])
	{
		solve2(adj, c, a, it, x, vc);
		x = vc.back() + dist;
	}

	int aNode;
	if (!vc.size())
	{
		if (c[node] > 0)
		{
			cout << "NO";
			exit(0);
		}
		aNode = x;
	}
	else
	{
		if (c[node] > vc.size())
		{
			cout << "NO";
			exit(0);
		}
		if (c[node] == vc.size())
			aNode = vc.back() + dist;
		else if (c[node] == 0)
			aNode = vc[c[node]] - 2;
		else
			aNode = (vc[c[node]] + vc[c[node] - 1]) / 2;
	}

	vc.push_back(aNode);
	sort(all(vc));
	for (auto& it : vc)
		v.push_back(it);

	a[node] = aNode;
}

void solve(int tc)
{
	int n, root = 1;
	cin >> n;
	dist = n * n;
	vector<vector<int>> adj(n + 1);
	vector<int> c(n + 1);
	for (int i = 1; i <= n; i++)
	{
		int pi;
		cin >> pi >> c[i];
		if (pi)
			adj[pi].push_back(i);
		else
			root = i;
	}

	vector<int> a(n + 1);
	vector<int> v;
	solve2(adj, c, a, root, dist, v);
	cout << "YES\n";
	for (int i = 1; i <= n; i++)
		cout << a[i] << " ";
}

int32_t main()
{
	#ifdef LOCAL_PROJECT
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#else
		// freopen("input.txt", "r", stdin);
		// freopen("output.txt", "w", stdout);
	#endif

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	preSolve();

	int t = 1;
	// cin >> t;
	for (int i = 1; i <= t; i++)
	{
		solve(i);
	}

	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

// Use g++ compiler for this include to work.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

// Constants
#define INF 1e18
#define EPS 1e-9
#define MOD 1000000007

// // for using in pairs
#define F first
#define S second

// Macros
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)

// aliases
using ll = long long;
using ull = unsigned long long;
using ui = unsigned int;
using ld = long double;

template<typename T>
using oset = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

// Temporary macro.
#define int long long

// Operator overloads.

/**
 * Both types in the pair should have overload for << operator with ostream.
 */
template<typename T1, typename T2>
ostream& operator<<(ostream &ostream, pair<T1, T2>& p)
{
	ostream << p.first << " " << p.second;
	return ostream;
}

/**
 * The type of element in the vector should have overload for << operator with ostream.
 */
template<typename T>
ostream& operator<<(ostream &stream, vector<T>& v)
{
	for (const auto &it : v)
		stream << it << " ";
	return stream;
}

template<typename T1, typename T2>
istream& operator>>(istream &istream, pair<T1, T2>& p)
{
	istream >> p.first >> p.second;
	return istream;
}

template<typename T>
istream& operator>>(istream &istream, vector<T>& a)
{
	for (auto &it : a)
		istream >> it;
	return istream;
}

// Utility functions.

template<typename T>
T MIN(T first) { return first; }

template<typename T, typename... Args>
T MIN(T first, Args... args) { return min(first, MIN(args...)); }

template<typename T>
T MAX(T first) { return first; }

template<typename T, typename... Args>
T MAX(T first, Args... args) { return max(first, MAX(args...)); }

template <typename T>
void DEBUG(T&& t) {cout << t << "\n";}

template <typename T, typename... Args>
void DEBUG(T&& t, Args&&... args)
{
    cout << t << " ";
    DEBUG(forward<Args>(args)...);
}

template<typename T>
int32_t size_i(T& container) { return static_cast<int32_t>(container.size()); }

// Mathematical functions.

int GCD(int a, int b)
{
	if (!b)
		return a;
	return GCD(b, a % b);
}

int LCM(int a, int b)
{
	return (a * b) / GCD(a, b);
}

int modpow(int x, int n, int m = MOD)
{
	if (n == 0) return 1 % m;
	long long u = modpow(x, n / 2, m);
	u = (u * u) % m;
	if (n % 2 == 1)
		u = (u * x) % m;
	return u;
}

/**
 * m should be prime
 */
int modinv(int x, int m = MOD)
{
	return modpow(x, m - 2, m);
}

//////////////////// START CODE HERE ///////////////////////////

void preSolve()
{
}

int dist = 0;

void solve2(vector<vector<int>> &adj, vector<int>& c, vector<int>& a, int node, int x, vector<int>& v)
{
	vector<int> vc;

	for (auto& it : adj[node])
	{
		solve2(adj, c, a, it, x, vc);
		x = vc.back() + dist;
	}

	int aNode;
	if (!vc.size())
	{
		if (c[node] > 0)
		{
			cout << "NO";
			exit(0);
		}
		aNode = x;
	}
	else
	{
		if (c[node] > vc.size())
		{
			cout << "NO";
			exit(0);
		}
		if (c[node] == vc.size())
			aNode = vc.back() + dist;
		else if (c[node] == 0)
			aNode = vc[c[node]] - 2;
		else
			aNode = (vc[c[node]] + vc[c[node] - 1]) / 2;
	}

	vc.push_back(aNode);
	sort(all(vc));
	for (auto& it : vc)
		v.push_back(it);

	a[node] = aNode;
}

void solve(int tc)
{
	int n, root = 1;
	cin >> n;
	dist = 5 * n;
	vector<vector<int>> adj(n + 1);
	vector<int> c(n + 1);
	for (int i = 1; i <= n; i++)
	{
		int pi;
		cin >> pi >> c[i];
		if (pi)
			adj[pi].push_back(i);
		else
			root = i;
	}

	vector<int> a(n + 1);
	vector<int> v;
	solve2(adj, c, a, root, dist, v);
	cout << "YES\n";
	for (int i = 1; i <= n; i++)
		cout << a[i] << " ";
}

int32_t main()
{
	#ifdef LOCAL_PROJECT
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#else
		// freopen("input.txt", "r", stdin);
		// freopen("output.txt", "w", stdout);
	#endif

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	preSolve();

	int t = 1;
	// cin >> t;
	for (int i = 1; i <= t; i++)
	{
		solve(i);
	}

	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

// Use g++ compiler for this include to work.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

// Constants
#define INF 1e18
#define EPS 1e-9
#define MOD 1000000007

// // for using in pairs
#define F first
#define S second

// Macros
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)

// aliases
using ll = long long;
using ull = unsigned long long;
using ui = unsigned int;
using ld = long double;

template<typename T>
using oset = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

// Temporary macro.
#define int long long

// Operator overloads.

/**
 * Both types in the pair should have overload for << operator with ostream.
 */
template<typename T1, typename T2>
ostream& operator<<(ostream &ostream, pair<T1, T2>& p)
{
	ostream << p.first << " " << p.second;
	return ostream;
}

/**
 * The type of element in the vector should have overload for << operator with ostream.
 */
template<typename T>
ostream& operator<<(ostream &stream, vector<T>& v)
{
	for (const auto &it : v)
		stream << it << " ";
	return stream;
}

template<typename T1, typename T2>
istream& operator>>(istream &istream, pair<T1, T2>& p)
{
	istream >> p.first >> p.second;
	return istream;
}

template<typename T>
istream& operator>>(istream &istream, vector<T>& a)
{
	for (auto &it : a)
		istream >> it;
	return istream;
}

// Utility functions.

template<typename T>
T MIN(T first) { return first; }

template<typename T, typename... Args>
T MIN(T first, Args... args) { return min(first, MIN(args...)); }

template<typename T>
T MAX(T first) { return first; }

template<typename T, typename... Args>
T MAX(T first, Args... args) { return max(first, MAX(args...)); }

template <typename T>
void DEBUG(T&& t) {cout << t << "\n";}

template <typename T, typename... Args>
void DEBUG(T&& t, Args&&... args)
{
    cout << t << " ";
    DEBUG(forward<Args>(args)...);
}

template<typename T>
int32_t size_i(T& container) { return static_cast<int32_t>(container.size()); }

// Mathematical functions.

int GCD(int a, int b)
{
	if (!b)
		return a;
	return GCD(b, a % b);
}

int LCM(int a, int b)
{
	return (a * b) / GCD(a, b);
}

int modpow(int x, int n, int m = MOD)
{
	if (n == 0) return 1 % m;
	long long u = modpow(x, n / 2, m);
	u = (u * u) % m;
	if (n % 2 == 1)
		u = (u * x) % m;
	return u;
}

/**
 * m should be prime
 */
int modinv(int x, int m = MOD)
{
	return modpow(x, m - 2, m);
}

//////////////////// START CODE HERE ///////////////////////////

void preSolve()
{
}

int dist = 0;

void solve2(vector<vector<int>> &adj, vector<int>& c, vector<int>& a, int node, int x, vector<int>& v)
{
	vector<int> vc;

	for (auto& it : adj[node])
	{
		solve2(adj, c, a, it, x, vc);
		x = vc.back() + dist;
	}

	int aNode;
	if (!vc.size())
	{
		if (c[node] > 0)
		{
			cout << "NO";
			exit(0);
		}
		aNode = x;
	}
	else
	{
		if (c[node] > vc.size())
		{
			cout << "NO";
			exit(0);
		}
		if (c[node] == vc.size())
			aNode = vc.back() + dist;
		else
			aNode = vc[c[node]] - 2;
	}

	vc.push_back(aNode);
	sort(all(vc));
	for (auto& it : vc)
		v.push_back(it);

	a[node] = aNode;
}

void solve(int tc)
{
	int n, root = 1;
	cin >> n;
	dist = 2 * n + 2;
	vector<vector<int>> adj(n + 1);
	vector<int> c(n + 1);
	for (int i = 1; i <= n; i++)
	{
		int pi;
		cin >> pi >> c[i];
		if (pi)
			adj[pi].push_back(i);
		else
			root = i;
	}

	vector<int> a(n + 1);
	vector<int> v;
	solve2(adj, c, a, root, dist, v);
	cout << "YES\n";
	for (int i = 1; i <= n; i++)
		cout << a[i] << " ";
}

int32_t main()
{
	#ifdef LOCAL_PROJECT
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#else
		// freopen("input.txt", "r", stdin);
		// freopen("output.txt", "w", stdout);
	#endif

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	preSolve();

	int t = 1;
	// cin >> t;
	for (int i = 1; i <= t; i++)
	{
		solve(i);
	}

	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

// Use g++ compiler for this include to work.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

// Constants
#define INF 1e18
#define EPS 1e-9
#define MOD 1000000007

// // for using in pairs
#define F first
#define S second

// Macros
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)

// aliases
using ll = long long;
using ull = unsigned long long;
using ui = unsigned int;
using ld = long double;

template<typename T>
using oset = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

// Temporary macro.
#define int long long

// Operator overloads.

/**
 * Both types in the pair should have overload for << operator with ostream.
 */
template<typename T1, typename T2>
ostream& operator<<(ostream &ostream, pair<T1, T2>& p)
{
	ostream << p.first << " " << p.second;
	return ostream;
}

/**
 * The type of element in the vector should have overload for << operator with ostream.
 */
template<typename T>
ostream& operator<<(ostream &stream, vector<T>& v)
{
	for (const auto &it : v)
		stream << it << " ";
	return stream;
}

template<typename T1, typename T2>
istream& operator>>(istream &istream, pair<T1, T2>& p)
{
	istream >> p.first >> p.second;
	return istream;
}

template<typename T>
istream& operator>>(istream &istream, vector<T>& a)
{
	for (auto &it : a)
		istream >> it;
	return istream;
}

// Utility functions.

template<typename T>
T MIN(T first) { return first; }

template<typename T, typename... Args>
T MIN(T first, Args... args) { return min(first, MIN(args...)); }

template<typename T>
T MAX(T first) { return first; }

template<typename T, typename... Args>
T MAX(T first, Args... args) { return max(first, MAX(args...)); }

template <typename T>
void DEBUG(T&& t) {cout << t << "\n";}

template <typename T, typename... Args>
void DEBUG(T&& t, Args&&... args)
{
    cout << t << " ";
    DEBUG(forward<Args>(args)...);
}

template<typename T>
int32_t size_i(T& container) { return static_cast<int32_t>(container.size()); }

// Mathematical functions.

int GCD(int a, int b)
{
	if (!b)
		return a;
	return GCD(b, a % b);
}

int LCM(int a, int b)
{
	return (a * b) / GCD(a, b);
}

int modpow(int x, int n, int m = MOD)
{
	if (n == 0) return 1 % m;
	long long u = modpow(x, n / 2, m);
	u = (u * u) % m;
	if (n % 2 == 1)
		u = (u * x) % m;
	return u;
}

/**
 * m should be prime
 */
int modinv(int x, int m = MOD)
{
	return modpow(x, m - 2, m);
}

//////////////////// START CODE HERE ///////////////////////////

void preSolve()
{
}

constexpr int N = 2000;
vector<int> adj[N + 1];
vector<int> c, a;
vector<int> avail;
int n, root;

void dfs(int v)
{
	if (c[v] >= avail.size())
	{
		cout << "NO";
		exit(0);
	}
	a[v] = avail[c[v]];
	avail.erase(avail.begin() + c[v]);
	for (auto &it : adj[v])
		dfs(it);
}

void solve(int tc)
{
	cin >> n;

	c.resize(n + 1);
	a.resize(n + 1);
	for (int i = 1; i <= n; i++)
	{
		int pi;
		cin >> pi >> c[i];
		if (pi)
			adj[pi].push_back(i);
		else
			root = i;
		avail.push_back(i);
	}

	dfs(root);

	a.erase(a.begin());
	cout << "YES\n" << a;

}

int32_t main()
{
	#ifdef LOCAL_PROJECT
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#else
		// freopen("input.txt", "r", stdin);
		// freopen("output.txt", "w", stdout);
	#endif

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	preSolve();

	int t = 1;
	// cin >> t;
	for (int i = 1; i <= t; i++)
	{
		solve(i);
	}

	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

// Use g++ compiler for this include to work.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

// Constants
#define INF 1e18
#define EPS 1e-9
#define MOD 1000000007

// // for using in pairs
#define F first
#define S second

// Macros
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)

// aliases
using ll = long long;
using ull = unsigned long long;
using ui = unsigned int;
using ld = long double;

template<typename T>
using oset = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

// Temporary macro.
#define int long long

// Operator overloads.

/**
 * Both types in the pair should have overload for << operator with ostream.
 */
template<typename T1, typename T2>
ostream& operator<<(ostream &ostream, pair<T1, T2>& p)
{
	ostream << p.first << " " << p.second;
	return ostream;
}

/**
 * The type of element in the vector should have overload for << operator with ostream.
 */
template<typename T>
ostream& operator<<(ostream &stream, vector<T>& v)
{
	for (const auto &it : v)
		stream << it << " ";
	return stream;
}

template<typename T1, typename T2>
istream& operator>>(istream &istream, pair<T1, T2>& p)
{
	istream >> p.first >> p.second;
	return istream;
}

template<typename T>
istream& operator>>(istream &istream, vector<T>& a)
{
	for (auto &it : a)
		istream >> it;
	return istream;
}

// Utility functions.

template<typename T>
T MIN(T first) { return first; }

template<typename T, typename... Args>
T MIN(T first, Args... args) { return min(first, MIN(args...)); }

template<typename T>
T MAX(T first) { return first; }

template<typename T, typename... Args>
T MAX(T first, Args... args) { return max(first, MAX(args...)); }

template <typename T>
void DEBUG(T&& t) {cout << t << "\n";}

template <typename T, typename... Args>
void DEBUG(T&& t, Args&&... args)
{
    cout << t << " ";
    DEBUG(forward<Args>(args)...);
}

template<typename T>
int32_t size_i(T& container) { return static_cast<int32_t>(container.size()); }

// Mathematical functions.

int GCD(int a, int b)
{
	if (!b)
		return a;
	return GCD(b, a % b);
}

int LCM(int a, int b)
{
	return (a * b) / GCD(a, b);
}

int modpow(int x, int n, int m = MOD)
{
	if (n == 0) return 1 % m;
	long long u = modpow(x, n / 2, m);
	u = (u * u) % m;
	if (n % 2 == 1)
		u = (u * x) % m;
	return u;
}

/**
 * m should be prime
 */
int modinv(int x, int m = MOD)
{
	return modpow(x, m - 2, m);
}

//////////////////// START CODE HERE ///////////////////////////

void preSolve()
{
}

int dist = 0;

void solve2(vector<vector<int>> &adj, vector<int>& c, vector<int>& a, int node, int x, vector<int>& v)
{
	vector<int> vc;

	for (auto& it : adj[node])
	{
		solve2(adj, c, a, it, x, vc);
		x = vc.back() + dist;
	}

	int aNode;
	if (!vc.size())
	{
		if (c[node] > 0)
		{
			cout << "NO";
			exit(0);
		}
		aNode = x;
	}
	else
	{
		if (c[node] > vc.size())
		{
			cout << "NO";
			exit(0);
		}
		if (c[node] == vc.size())
			aNode = vc.back() + dist;
		else if (c[node] == 0)
			aNode = vc[c[node]] - 2;
		else
			aNode = (vc[c[node]] + vc[c[node] - 1] + 1) / 2;
	}

	vc.push_back(aNode);
	sort(all(vc));
	for (auto& it : vc)
		v.push_back(it);

	a[node] = aNode;
}

void solve(int tc)
{
	int n, root = 1;
	cin >> n;
	dist = 20000;
	vector<vector<int>> adj(n + 1);
	vector<int> c(n + 1);
	for (int i = 1; i <= n; i++)
	{
		int pi;
		cin >> pi >> c[i];
		if (pi)
			adj[pi].push_back(i);
		else
			root = i;
	}

	vector<int> a(n + 1);
	vector<int> v;
	solve2(adj, c, a, root, dist, v);
	cout << "YES\n";
	for (int i = 1; i <= n; i++)
		cout << a[i] << " ";
}

int32_t main()
{
	#ifdef LOCAL_PROJECT
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#else
		// freopen("input.txt", "r", stdin);
		// freopen("output.txt", "w", stdout);
	#endif

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	preSolve();

	int t = 1;
	// cin >> t;
	for (int i = 1; i <= t; i++)
	{
		solve(i);
	}

	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

// Use g++ compiler for this include to work.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

// Constants
#define INF 1e18
#define EPS 1e-9
#define MOD 1000000007

// // for using in pairs
#define F first
#define S second

// Macros
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)

// aliases
using ll = long long;
using ull = unsigned long long;
using ui = unsigned int;
using ld = long double;

template<typename T>
using oset = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

// Temporary macro.
#define int long long

// Operator overloads.

/**
 * Both types in the pair should have overload for << operator with ostream.
 */
template<typename T1, typename T2>
ostream& operator<<(ostream &ostream, pair<T1, T2>& p)
{
	ostream << p.first << " " << p.second;
	return ostream;
}

/**
 * The type of element in the vector should have overload for << operator with ostream.
 */
template<typename T>
ostream& operator<<(ostream &stream, vector<T>& v)
{
	for (const auto &it : v)
		stream << it << " ";
	return stream;
}

template<typename T1, typename T2>
istream& operator>>(istream &istream, pair<T1, T2>& p)
{
	istream >> p.first >> p.second;
	return istream;
}

template<typename T>
istream& operator>>(istream &istream, vector<T>& a)
{
	for (auto &it : a)
		istream >> it;
	return istream;
}

// Utility functions.

template<typename T>
T MIN(T first) { return first; }

template<typename T, typename... Args>
T MIN(T first, Args... args) { return min(first, MIN(args...)); }

template<typename T>
T MAX(T first) { return first; }

template<typename T, typename... Args>
T MAX(T first, Args... args) { return max(first, MAX(args...)); }

template <typename T>
void DEBUG(T&& t) {cout << t << "\n";}

template <typename T, typename... Args>
void DEBUG(T&& t, Args&&... args)
{
    cout << t << " ";
    DEBUG(forward<Args>(args)...);
}

template<typename T>
int32_t size_i(T& container) { return static_cast<int32_t>(container.size()); }

// Mathematical functions.

int GCD(int a, int b)
{
	if (!b)
		return a;
	return GCD(b, a % b);
}

int LCM(int a, int b)
{
	return (a * b) / GCD(a, b);
}

int modpow(int x, int n, int m = MOD)
{
	if (n == 0) return 1 % m;
	long long u = modpow(x, n / 2, m);
	u = (u * u) % m;
	if (n % 2 == 1)
		u = (u * x) % m;
	return u;
}

/**
 * m should be prime
 */
int modinv(int x, int m = MOD)
{
	return modpow(x, m - 2, m);
}

//////////////////// START CODE HERE ///////////////////////////

void preSolve()
{
}

constexpr int N = 2000;
vector<int> adj[N + 1];
vector<int> c, a;
vector<int> avail;
int n, root;

void dfs(int v)
{
	a[v] = avail[c[v]];
	avail.erase(avail.begin() + c[v]);
	for (auto &it : adj[v])
		dfs(it);
}

int check(int v)
{
	int size = 1;
	for (auto &it : adj[v])
		size += check(it);
	if (c[v] >= size)
	{
		cout << "NO";
		exit(0);
	}
	return size;
}

void solve(int tc)
{
	cin >> n;

	c.resize(n + 1);
	a.resize(n + 1);
	for (int i = 1; i <= n; i++)
	{
		int pi;
		cin >> pi >> c[i];
		if (pi)
			adj[pi].push_back(i);
		else
			root = i;
		avail.push_back(i);
	}

	check(root);
	dfs(root);

	a.erase(a.begin());
	cout << "YES\n" << a;

}

int32_t main()
{
	#ifdef LOCAL_PROJECT
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#else
		// freopen("input.txt", "r", stdin);
		// freopen("output.txt", "w", stdout);
	#endif

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	preSolve();

	int t = 1;
	// cin >> t;
	for (int i = 1; i <= t; i++)
	{
		solve(i);
	}

	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

// Use g++ compiler for this include to work.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

// Constants
#define INF 1e18
#define EPS 1e-9
#define MOD 1000000007

// // for using in pairs
#define F first
#define S second

// Macros
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)

// aliases
using ll = long long;
using ull = unsigned long long;
using ui = unsigned int;
using ld = long double;

template<typename T>
using oset = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

// Temporary macro.
#define int long long

// Operator overloads.

/**
 * Both types in the pair should have overload for << operator with ostream.
 */
template<typename T1, typename T2>
ostream& operator<<(ostream &ostream, pair<T1, T2>& p)
{
	ostream << p.first << " " << p.second;
	return ostream;
}

/**
 * The type of element in the vector should have overload for << operator with ostream.
 */
template<typename T>
ostream& operator<<(ostream &stream, vector<T>& v)
{
	for (const auto &it : v)
		stream << it << " ";
	return stream;
}

template<typename T1, typename T2>
istream& operator>>(istream &istream, pair<T1, T2>& p)
{
	istream >> p.first >> p.second;
	return istream;
}

template<typename T>
istream& operator>>(istream &istream, vector<T>& a)
{
	for (auto &it : a)
		istream >> it;
	return istream;
}

// Utility functions.

template<typename T>
T MIN(T first) { return first; }

template<typename T, typename... Args>
T MIN(T first, Args... args) { return min(first, MIN(args...)); }

template<typename T>
T MAX(T first) { return first; }

template<typename T, typename... Args>
T MAX(T first, Args... args) { return max(first, MAX(args...)); }

template <typename T>
void DEBUG(T&& t) {cout << t << "\n";}

template <typename T, typename... Args>
void DEBUG(T&& t, Args&&... args)
{
    cout << t << " ";
    DEBUG(forward<Args>(args)...);
}

template<typename T>
int32_t size_i(T& container) { return static_cast<int32_t>(container.size()); }

// Mathematical functions.

int GCD(int a, int b)
{
	if (!b)
		return a;
	return GCD(b, a % b);
}

int LCM(int a, int b)
{
	return (a * b) / GCD(a, b);
}

int modpow(int x, int n, int m = MOD)
{
	if (n == 0) return 1 % m;
	long long u = modpow(x, n / 2, m);
	u = (u * u) % m;
	if (n % 2 == 1)
		u = (u * x) % m;
	return u;
}

/**
 * m should be prime
 */
int modinv(int x, int m = MOD)
{
	return modpow(x, m - 2, m);
}

//////////////////// START CODE HERE ///////////////////////////

void preSolve()
{
}

int dist = 0;
int ans = 0;

void solve2(vector<vector<int>> &adj, vector<int>& c, vector<int>& a, int node, int x, vector<int>& v)
{
	if (ans) return;
	vector<int> vc;

	for (auto& it : adj[node])
	{
		solve2(adj, c, a, it, x, vc);
		x = vc.back() + dist;
	}
	int aNode;
	if (!vc.size())
	{
		if (c[node] > 0)
		{
			ans = 1;
			return;
		}
		aNode = x;
	}
	else
	{
		if (c[node] > vc.size())
		{
			ans = 1;
			return;
		}
		if (c[node] == vc.size())
			aNode = vc.back() + dist;
		else
			aNode = vc[c[node]] - 2;
	}

	vc.push_back(aNode);
	sort(all(vc));
	for (auto& it : vc)
		v.push_back(it);

	a[node] = aNode;
}

void solve(int tc)
{
	int n, root = 1;
	cin >> n;
	dist = 2 * n + 2;
	vector<vector<int>> adj(n + 1);
	vector<int> c(n + 1);
	for (int i = 1; i <= n; i++)
	{
		int pi;
		cin >> pi >> c[i];
		if (pi)
			adj[pi].push_back(i);
		else
			root = i;
	}

	vector<int> a(n + 1);
	vector<int> v;
	solve2(adj, c, a, root, dist, v);
	if (ans)
		cout << "NO\n";
	else
	{
		cout << "YES\n";
		for (int i = 1; i <= n; i++)
			cout << a[i] << " ";
	}
}

int32_t main()
{
	#ifdef LOCAL_PROJECT
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#else
		// freopen("input.txt", "r", stdin);
		// freopen("output.txt", "w", stdout);
	#endif

	ios_base::sync_with_stdio(false);
	cin.tie(0);

	preSolve();

	int t = 1;
	// cin >> t;
	for (int i = 1; i <= t; i++)
	{
		solve(i);
	}

	return 0;
}


#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x)  cerr<< #x <<" = "<< x<<endl;
using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
vector<int>adj[MAXN];
int sub[MAXN];
void pre(int nd){
	sub[nd]=0;
	tr(it,adj[nd])
		pre(*it),sub[nd]+=sub[*it];
}
ordered_set s;
int ans[MAXN],arr[MAXN];
void dfs(int nd){
	ans[nd]=*s.find_by_order(arr[nd]);
	s.erase(ans[nd]);
	tr(it,adj[nd])
		dfs(*it);
}
int main(){
   //freopen("file.in", "r", stdin);
    int n,root;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
    	s.insert(i);
    	int p;
		scanf("%d%d",&p,arr+i);
		if(!p)
			root=i;
		else
			adj[p].pb(i);
    }
    pre(root);
    for(int i=1;i<=n;i++)
    	if(arr[i]>sub[i]){
    		puts("NO");
    		return 0;
    	}
    puts("YES");
    dfs(root);
    for(int i=1;i<=n;i++)
    	printf("%d ",ans[i]);
    puts("");
	return 0;
}