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

public class Codeforces {
	FastScanner in;
	PrintWriter out;

	public void solve() throws IOException {
		int n = in.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = in.nextInt();
		}
		Arrays.sort(a);
		for (int i = 0; i < n; i++) {
			out.print(a[i] + " ");
		}
	}

	public void run() {
		try {
			in = new FastScanner();
			out = new PrintWriter(System.out);

			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		boolean hasNext() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					String s = br.readLine();
					if (s == null) {
						return false;
					}
					st = new StringTokenizer(s);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return true;
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}
	}

	public static void main(String[] arg) {
		new Codeforces().run();
	}
}


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

public class Codeforces {
	FastScanner in;
	PrintWriter out;

	void solve() {
		int n = in.nextInt();
		ArrayList<Integer> with1 = new ArrayList<Integer>();
		ArrayList<Integer> with2 = new ArrayList<Integer>();

		for (int i = 0; i < n; i++) {
			if (in.nextInt() == 1) {
				with1.add(in.nextInt());
			} else {
				with2.add(in.nextInt());
			}
		}
		Collections.sort(with1);
		Collections.sort(with2);
		int best = Integer.MAX_VALUE;
		for (int count1 = 0; count1 <= with1.size(); count1++) {
			for (int count2 = 0; count2 <= with2.size(); count2++) {
				int cur = count1 + count2 * 2;
				int sum = 0;
				for (int i = 0; i < with1.size() - count1; i++) {
					sum += with1.get(i);
				}
				for (int i = 0; i < with2.size() - count2; i++) {
					sum += with2.get(i);
				}
				if (sum <= cur) {
					best = Math.min(best, cur);
				}
			}
		}
		out.println(best);
	}

	void run() {
		in = new FastScanner();
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));

		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {

				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}
	}

	public static void main(String[] args) {
		new Codeforces().run();
	}
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.*;

public class R178qB {

	public static void main(String args[] ) throws Exception {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    PrintWriter w = new PrintWriter(System.out);

	    StringTokenizer st1 = new StringTokenizer(br.readLine());
	    int n = ip(st1.nextToken());

	    ArrayList<Integer> a1 = new ArrayList<Integer>();
	    ArrayList<Integer> a2 = new ArrayList<Integer>();
	    for(int i=0;i<n;i++){
		    StringTokenizer st2 = new StringTokenizer(br.readLine());
	    	int t = ip(st2.nextToken());
	    	if(t == 1)
	    		a1.add(ip(st2.nextToken()));
	    	else
	    		a2.add(ip(st2.nextToken()));
	    }

	    Collections.sort(a1);
	    Collections.sort(a2);
	    int a1s = a1.size();
	    int a2s = a2.size();

	    int ans = 2*n;

	    for(int num=1;num<=n;num++){
	    	for(int i1=0;i1<=Math.min(num,a1s);i1++){
	    		int i2 = num - i1;
	    		if(i2 > a2s)
	    			continue;
	    		int thick = i1 + 2*i2;
	    		int width = 0;
	    		for(int i=0;i<a1s - i1;i++)
	    			width += a1.get(i);
	    		for(int i=0;i<a2s - i2;i++)
	    			width += a2.get(i);
	    		if(width <= thick)
	    			ans = Math.min(ans, thick);
	    	}
	    }

	    w.println(ans);
	    w.close();
	}

	public static int ip(String s){
		return Integer.parseInt(s);
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		InputReader r = new InputReader(System.in);

		int N = r.nextInt();

		ArrayList<Integer> one = new ArrayList<Integer>();
		ArrayList<Integer> two = new ArrayList<Integer>();

		for(int i = 0; i < N; i++){
			int t = r.nextInt();
			int w = r.nextInt();

			if(t == 1)one.add(w);
			else two.add(w);
		}

		Collections.sort(one);
		Collections.sort(two);

		int[] p1 = new int[one.size() + 1];
		int[] p2 = new int[two.size() + 1];

		for(int i = 0; i < one.size(); i++)
			p1[i + 1] = one.get(i) + p1[i];
		for(int i = 0; i < two.size(); i++)
			p2[i + 1] = two.get(i) + p2[i];

//		System.out.println(one + ", " + two);
//		System.out.println(Arrays.toString(p1));
//		System.out.println(Arrays.toString(p2));

		int min = 1 << 28;
		for(int t1 = 0; t1 <= one.size(); t1++)
		for(int t2 = 0; t2 <= two.size(); t2++){
			int tt = t1 + t2 * 2;
			int tw = p1[one.size() - t1] + p2[two.size() - t2];

			if(tt >= tw)
				min = Math.min(min, tt);
		}

		System.out.println(min);
	}

//	static class B{
//		int t, w;
//		public B(int ti, int wi){
//			t = ti;
//			w = wi;
//		}
//	}

	static class InputReader {
	    private BufferedReader reader;
	    private StringTokenizer tokenizer;

	    public InputReader(InputStream stream) {
	        reader = new BufferedReader(new InputStreamReader(stream));
	        tokenizer = null;
	    }

	    public String nextLine(){
	    	try {
				return reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
	    }
	    public String next() {
	        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
	            try {
	                tokenizer = new StringTokenizer(reader.readLine());
	            } catch (IOException e) {
	                throw new RuntimeException(e);
	            }
	        }
	        return tokenizer.nextToken();
	    }

	    public int nextInt() {
	        return Integer.parseInt(next());
	    }
	    public long nextLong() {
	        return Long.parseLong(next());
	    }
	}
}


import java.util.Scanner;

public class CF294B{
    static class Book{
        int t, w;

        public Book(int t, int w){
            this.t = t;
            this.w = w;
        }
    }

    public static void main(String[] args){
        Scanner scn = new Scanner(System.in);
        while (scn.hasNext()){
            int n = scn.nextInt();
            int totalW = 0, totalT = 0;
            int[] t2w;
            Book[] allB = new Book[n];
            for (int i=0; i<n; i++){
                int t = scn.nextInt();
                int w = scn.nextInt();
                totalW += w;
                totalT += t;
                allB[i] = new Book(t, w);
            }
            t2w = new int[totalT+1];
            t2w[0] = 0;
            for (int i=1; i<=totalT; i++){
                t2w[i] = -1;
            }
            for (int i=0; i<n; i++){
                for (int j=totalT; j>=allB[i].t; j--){
                    if (t2w[j-allB[i].t]<0) continue;
                    if (t2w[j]<0 || t2w[j-allB[i].t]+allB[i].w>t2w[j]){
                        t2w[j] = t2w[j-allB[i].t]+allB[i].w;
                    }
                }
            }
            int ans;
            for (ans=0; ans<=totalT; ans++){
                if (ans>=totalW-t2w[ans])
                    break;
            }
            System.out.println(ans);
        }
    }
}


import static java.lang.Math.*;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.exit;
import static java.lang.System.arraycopy;
import static java.util.Arrays.sort;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.fill;
import java.util.*;
import java.io.*;

public class Main {

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

    final int MAX_N = 1001;
    int one[] = new int [MAX_N];
    int two[] = new int [MAX_N];
    int N,cone,ctwo;

    private void run() throws IOException {
        Scanner in = new Scanner(System.in);
        cone = 0;
        ctwo = 0;
        N = in.nextInt();
        for(int i = 0; i < N; i++){
            int a,b;
            a = in.nextInt();
            b = in.nextInt();
            if(a == 1)
                one[cone++] = b;
            else
                two[ctwo++] = b;
        }
        sort(one,0,cone);
        sort(two,0,ctwo);
        int ans = 1000* 1000* 1000;
        for(int h = 0; h <= cone; h++)
            for(int j = 0; j <= ctwo; j++){
                int s = 0;
                for(int i = 0; i < cone - h; i++)
                    s += one[i];
                for(int i = 0; i < ctwo - j; i++)
                    s += two[i];
                if(s <= (h + 2 * j) && (h + 2 * j) < ans)
                    ans = h + 2 * j;
            }
        System.out.println(ans);
    }
}


import java.util.*;
public class Problem294B
{
    public static int[] T = new int[101];
    public static int[] W = new int[101];
    public static int[][][] dp = new int[201][201][101];
    public static void main (String [] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for(int i=1; i<=n; i++)
        {
            T[i] = in.nextInt();
            W[i] = in.nextInt();
        }
        for(int i=1; i<=2*n; i++)
        {
            if(recursion(i, i, n) == 1)
            {
                System.out.println(i);
                System.exit(0);
            }
        }
    }
    public static int recursion(int t, int w, int i)
    {
        if(t<0 || w<0 || i<0) return -1;
        if(dp[t][w][i]==0)
        {
            if(t==0 && w>=0 && i==0) return dp[t][w][i]=1;
            else if(t>=0 && w>=0 && i>0)
            {
                int num1 = recursion(t-T[i], w, i-1);
                int num2 = recursion(t, w-W[i], i-1);
                if(num1 == 1 || num2 == 1) return dp[t][w][i]=1;
            }
            return dp[t][w][i]=-1;
        }
        return dp[t][w][i];
    }
}


import java.util.*;

public class Problem294B
{
    public static int[] T = new int[101];
    public static int[] W = new int[101];
    public static int n;
    public static int[][][] dp = new int[201][201][101];

    public static void main (String [] args)
    {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for(int i=1; i<=n; i++)
        {
            T[i] = in.nextInt();
            W[i] = in.nextInt();
        }
        for(int i=1; i<=2*n; i++)
        {
            int answer = rec(i, i, n);
            if(answer == 1)
            {
                System.out.println(i);
                System.exit(0);
            }
        }
    }

    public static int rec(int t, int w, int i)
    {
        if(t<0 || w<0 || i<0) return -1;
        if(dp[t][w][i]==0)
        {
            if(t==0)
            {
                if(w>=0 && i==0) return dp[t][w][i]=1;
                else return dp[t][w][i]=-1;
            }
            if(t>0)
            {
                if(w>0 && i>0)
                {
                    int num1 = rec(t-T[i], w, i-1);
                    int num2 = rec(t, w-W[i], i-1);
                    if(num1 == 1 || num2 == 1) return dp[t][w][i]=1;
                    else return dp[t][w][i]=-1;
                }
                else return dp[t][w][i]=-1;
            }
        }
        return dp[t][w][i];
    }
}


import java.util.*;

public class Problem294B
{
    public static int[] T = new int[101];
    public static int[] W = new int[101];
    public static int n;
    public static int[][][] dp = new int[201][201][101];

    public static void main (String [] args)
    {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for(int i=1; i<=n; i++)
        {
            T[i] = in.nextInt();
            W[i] = in.nextInt();
        }
        for(int i=1; i<=2*n; i++)
        {
            int answer = recursion(i, i, n);
            if(answer == 1)
            {
                System.out.println(i);
                System.exit(0);
            }
        }
    }

    public static int rec(int t, int w, int i)
    {
        if(t<0 || w<0 || i<0) return -1;
        if(dp[t][w][i]==0)
        {
            if(t==0)
            {
                if(w>=0 && i==0) return dp[t][w][i]=1;
                else if(w>0 && i>0) return rec(t, w-W[i], i-1);
                else return dp[t][w][i]=-1;
            }
            if(t>0)
            {
                if(w>=0 && i>0)
                {
                    int num1 = rec(t-T[i], w, i-1);
                    int num2 = rec(t, w-W[i], i-1);
                    if(num1 == 1 || num2 == 1) return dp[t][w][i]=1;
                    else return dp[t][w][i]=-1;
                }
                else return dp[t][w][i]=-1;
            }
        }
        return dp[t][w][i];
    }

    public static int recursion(int t, int w, int i)
    {
        if(t<0 || w<0 || i<0) return -1;
        if(dp[t][w][i]==0)
        {
            if(t==0 && w>=0 && i==0) return dp[t][w][i]=1;
            else if(t>=0 && w>=0 && i>0)
            {
                int num1 = rec(t-T[i], w, i-1);
                int num2 = rec(t, w-W[i], i-1);
                if(num1 == 1 || num2 == 1) return dp[t][w][i]=1;
            }
            return dp[t][w][i]=-1;
        }
        return dp[t][w][i];
    }
}


import java.util.*;

public class Problem294B
{
    public static int[] T = new int[101];
    public static int[] W = new int[101];
    public static int n;
    public static int[][][] dp = new int[201][201][101];

    public static void main (String [] args)
    {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        for(int i=1; i<=n; i++)
        {
            T[i] = in.nextInt();
            W[i] = in.nextInt();
        }
        for(int i=1; i<=2*n; i++)
        {
            int answer = rec(i, i, n);
            if(answer == 1)
            {
                System.out.println(i);
                System.exit(0);
            }
        }
    }

    public static int rec(int t, int w, int i)
    {
        if(t<0 || w<0 || i<0) return -1;
        if(dp[t][w][i]==0)
        {
            if(t==0)
            {
                if(w>=0 && i==0) return dp[t][w][i]=1;
                else if(w>0 && i>0) return rec(t, w-W[i], i-1);
                else return dp[t][w][i]=-1;
            }
            if(t>0)
            {
                if(w>=0 && i>0)
                {
                    int num1 = rec(t-T[i], w, i-1);
                    int num2 = rec(t, w-W[i], i-1);
                    if(num1 == 1 || num2 == 1) return dp[t][w][i]=1;
                    else return dp[t][w][i]=-1;
                }
                else return dp[t][w][i]=-1;
            }
        }
        return dp[t][w][i];
    }
}


import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Pavel Chuprikov
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		FastScanner in = new FastScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskB solver = new TaskB();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskB {
    public void solve(int testNumber, FastScanner in, PrintWriter out) {
        int n = in.nextInt();
        int[] t = new int[n];
        int[] w = new int[n];
        for (int i = 0; i < n; i++) {
            t[i] = in.nextInt();
            w[i] = in.nextInt();
        }

        int[] mi = new int[2 * n  + 1];
        Arrays.fill(mi, Integer.MAX_VALUE);
        mi[0] = 0;
        for (int i = 0; i < n; i++) {
            int[] nmi = new int[mi.length];
            Arrays.fill(nmi, Integer.MAX_VALUE);
            for (int j = t[i]; j < nmi.length; j++) {
                nmi[j] = mi[j - t[i]];
            }
            for (int j = 0; j < nmi.length; j++) {
                nmi[j] = mi[j] == Integer.MAX_VALUE ?  nmi[j] : Math.min(nmi[j], mi[j] + w[i]);
            }
            mi = nmi;
        }
        int minValue = Integer.MAX_VALUE;
        for (int i = 0; i <= 2 * n; i++) {
            if (mi[i] <= i)
            minValue = Math.min(minValue, i);
        }
        out.println(minValue);
    }
}

class FastScanner {
    private BufferedReader br;
    private StringTokenizer st;

    public FastScanner(InputStream in) {
        this.br = new BufferedReader(new InputStreamReader(in));
    }

    public boolean hasNext() {
        try {
            while (st == null || !st.hasMoreTokens()) {
                final String nextLine = br.readLine();
                if (nextLine == null) {
                    return false;
                } else {
                    st = new StringTokenizer(nextLine);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return true;
    }

    public String next() {
        return hasNext() ? st.nextToken() : null;
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }
}


import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Pavel Chuprikov
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		FastScanner in = new FastScanner(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskB solver = new TaskB();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskB {
    public void solve(int testNumber, FastScanner in, PrintWriter out) {
        int n = in.nextInt();
        int[] t = new int[n];
        int[] w = new int[n];
        for (int i = 0; i < n; i++) {
            t[i] = in.nextInt();
            w[i] = in.nextInt();
        }

        int[] mi = new int[2 * n  + 1];
        Arrays.fill(mi, Integer.MAX_VALUE);
        mi[0] = 0;
        for (int i = 0; i < n; i++) {
            int[] nmi = new int[mi.length];
            Arrays.fill(nmi, Integer.MAX_VALUE);
            for (int j = t[i]; j < nmi.length; j++) {
                nmi[j] = mi[j - t[i]];
            }
            for (int j = 0; j < nmi.length; j++) {
                nmi[j] = mi[j] == Integer.MAX_VALUE ?  nmi[j] : Math.min(nmi[j], mi[j] + w[i]);
            }
            mi = nmi;
        }
        int minValue = Integer.MAX_VALUE;
        for (int i = 0; i < 2 * n; i++) {
            if (mi[i] <= i)
            minValue = Math.min(minValue, i);
        }
        out.println(minValue);
    }
}

class FastScanner {
    private BufferedReader br;
    private StringTokenizer st;

    public FastScanner(InputStream in) {
        this.br = new BufferedReader(new InputStreamReader(in));
    }

    public boolean hasNext() {
        try {
            while (st == null || !st.hasMoreTokens()) {
                final String nextLine = br.readLine();
                if (nextLine == null) {
                    return false;
                } else {
                    st = new StringTokenizer(nextLine);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return true;
    }

    public String next() {
        return hasNext() ? st.nextToken() : null;
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }
}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.StringTokenizer;

public class magic2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int N = sc.nextInt();
		PriorityQueue<Integer> ONE = new PriorityQueue<Integer>();
		PriorityQueue<Integer> TWO = new PriorityQueue<Integer>();
		for(int a=0;a<N;a++){
			int thing = sc.nextInt();
			if(thing==1)ONE.add(sc.nextInt());
			else TWO.add(sc.nextInt());
		}
		int ONES = ONE.size();
		int TWOS = TWO.size();
		int[] A = new int[ONES];
		int[] B = new int[TWOS];
		int i = 0;
		while(!ONE.isEmpty()){
			A[i++]=ONE.poll();
		}
		i=0;
		while(!TWO.isEmpty()){
			B[i++]=TWO.poll();
		}
		int best = Integer.MAX_VALUE;
		for(int a=0;a<=ONES;a++){
			for(int b=0;b<=TWOS;b++){
				int top = 0;
				for(int c=0;c<a;c++)top+=A[c];
				for(int c=0;c<b;c++)top+=B[c];
				int hold = (ONES-a) + 2*(TWOS-b);
				if(hold>=top){
					best = Math.min(best,hold);
				}
			}
		}
		out.println(best);

		out.close();
	}

	private static int[] magic(String thing) {
		int[] ans = new int[10];
		for (int a = 0; a < thing.length(); a++) {
			ans[thing.charAt(a) - '0']++;
		}
		return ans;
	}

	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(InputStream in) throws Exception {
			br = new BufferedReader(new InputStreamReader(in));
			st = new StringTokenizer(br.readLine().trim());
		}

		public int numTokens() throws Exception {
			if (!st.hasMoreTokens()) {
				st = new StringTokenizer(br.readLine().trim());
				return numTokens();
			}
			return st.countTokens();
		}

		public boolean hasNext() throws Exception {
			if (!st.hasMoreTokens()) {
				st = new StringTokenizer(br.readLine().trim());
				return hasNext();
			}
			return true;
		}

		public String next() throws Exception {
			if (!st.hasMoreTokens()) {
				st = new StringTokenizer(br.readLine().trim());
				return next();
			}
			return st.nextToken();
		}

		public double nextDouble() throws Exception {
			return Double.parseDouble(next());
		}

		public float nextFloat() throws Exception {
			return Float.parseFloat(next());
		}

		public long nextLong() throws Exception {
			return Long.parseLong(next());
		}

		public int nextInt() throws Exception {
			return Integer.parseInt(next());
		}

		public String nextLine() throws Exception {
			return br.readLine();
		}
	}

}


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.StringTokenizer;

public class magic2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int N = sc.nextInt();
		PriorityQueue<Integer> ONE = new PriorityQueue<Integer>();
		PriorityQueue<Integer> TWO = new PriorityQueue<Integer>();
		for(int a=0;a<N;a++){
			int thing = sc.nextInt();
			if(thing==1)ONE.add(sc.nextInt());
			else TWO.add(sc.nextInt());
		}
		int ONES = ONE.size();
		int TWOS = TWO.size();
		int[] A = new int[ONES];
		int[] B = new int[TWOS];
		int i = 0;
		while(!ONE.isEmpty()){
			A[i++]=ONE.poll();
		}
		i=0;
		while(!TWO.isEmpty()){
			B[i++]=TWO.poll();
		}
		int best = Integer.MAX_VALUE;
		for(int a=0;a<=ONES;a++){
			int top = 0;
			for(int c=0;c<a;c++)top+=A[c];
			for(int b=0;b<=TWOS;b++){
				for(int c=0;c<b;c++)top+=B[c];
				int hold = (ONES-a) + 2*(TWOS-b);
				if(hold>=top){
					best = Math.min(best,hold);
				}
			}
		}
		out.println(best);

		out.close();
	}

	private static int[] magic(String thing) {
		int[] ans = new int[10];
		for (int a = 0; a < thing.length(); a++) {
			ans[thing.charAt(a) - '0']++;
		}
		return ans;
	}

	static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(InputStream in) throws Exception {
			br = new BufferedReader(new InputStreamReader(in));
			st = new StringTokenizer(br.readLine().trim());
		}

		public int numTokens() throws Exception {
			if (!st.hasMoreTokens()) {
				st = new StringTokenizer(br.readLine().trim());
				return numTokens();
			}
			return st.countTokens();
		}

		public boolean hasNext() throws Exception {
			if (!st.hasMoreTokens()) {
				st = new StringTokenizer(br.readLine().trim());
				return hasNext();
			}
			return true;
		}

		public String next() throws Exception {
			if (!st.hasMoreTokens()) {
				st = new StringTokenizer(br.readLine().trim());
				return next();
			}
			return st.nextToken();
		}

		public double nextDouble() throws Exception {
			return Double.parseDouble(next());
		}

		public float nextFloat() throws Exception {
			return Float.parseFloat(next());
		}

		public long nextLong() throws Exception {
			return Long.parseLong(next());
		}

		public int nextInt() throws Exception {
			return Integer.parseInt(next());
		}

		public String nextLine() throws Exception {
			return br.readLine();
		}
	}

}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

public class ShaassAndBookshelf {
	public static void main(String[] args){
		FastScanner sc = new FastScanner();
		int n= sc.nextInt();
		List<Integer> ones = new ArrayList<Integer>();
		List<Integer> twos = new ArrayList<Integer>();

		for(int i=0;i<n;i++){
			int t = sc.nextInt();
			int w = sc.nextInt();

			if(t==1){
				ones.add(w);
			}else{
				twos.add(w);
			}
		}

		Collections.sort(ones);
		Collections.sort(twos);

		int[] sumOnes = new int[ones.size()+1];
		sumOnes[0] = 0;
		for(int i=1;i<=ones.size();i++){
			sumOnes[i] = sumOnes[i-1] + ones.get(i-1);
		}

		int[] sumTwos = new int[twos.size()+1];
		sumTwos[0] = 0;
		for(int i=1;i<=twos.size();i++){
			sumTwos[i] = sumTwos[i-1] + twos.get(i-1);
		}

		int totalThickness = ones.size() + twos.size()*2;
		int minThickness = Integer.MAX_VALUE;
		for(int c1 = 0; c1<=ones.size(); c1++){
			for(int c2=0; c2<=twos.size(); c2++){
				//if c1 1's and c2 2's are picked as outsider
				int width = sumOnes[c1] + sumTwos[c2];
				int thickness = totalThickness - c1 - c2*2;
				if(thickness<minThickness && width <= thickness){
					minThickness = thickness;
				}
			}
		}
		System.out.println(minThickness);
	}
	public static class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(String s) {
			try {
				br = new BufferedReader(new FileReader(s));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		public FastScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String nextToken() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(nextToken());
		}

		long nextLong() {
			return Long.parseLong(nextToken());
		}

		double nextDouble() {
			return Double.parseDouble(nextToken());
		}
	}
}


import java.util.*;
public class Main {
public static void main(String [] args){
	Scanner in=new Scanner(System.in);
	int n=in.nextInt();
	int thick[]=new int[n];
	int width[]=new int[n];
	int sum=0;
	for(int i=0;i<n;i++){
	thick[i]=in.nextInt();
	width[i]=in.nextInt();
	sum+=width[i];
	}
	int dp[][]=new int[n+1][20001];
	for(int i=0;i<=n;i++){
		for(int j=0;j<20001;j++){
			dp[i][j]=-1;
		}
	}
	dp[0][0]=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<20001;j++){
			if(dp[i][j]==-1)continue;
			dp[i+1][j]=Math.max(dp[i+1][j],dp[i][j]);
			dp[i+1][j+thick[i]]=Math.max(dp[i][j]+width[i],dp[i+1][j+thick[i]]);
	}
	}
	for(int i=0;i<=20001;i++){
		if(sum-dp[n][i] <= i){
		System.out.print(i);
		return;
		}
	}
}
}


import java.util.*;
public class Main {
public static void main(String [] args){
	Scanner in=new Scanner(System.in);
	int n=in.nextInt();
	int thick[]=new int[n];
	int width[]=new int[n];
	int sum=0;
	for(int i=0;i<n;i++){
	thick[i]=in.nextInt();
	width[i]=in.nextInt();
	sum+=width[i];
	}
	int dp[][]=new int[n+1][20001];
	for(int i=0;i<=n;i++){
		for(int j=0;j<20001;j++){
			dp[i][j]=-1;
		}
	}
	dp[0][0]=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<20001;j++){
			if(dp[i][j]==-1)continue;
			dp[i+1][j]=Math.max(dp[i+1][j],dp[i][j]);
			dp[i+1][j+thick[i]]=Math.max(dp[i][j]+width[i],dp[i+1][j+thick[i]]);
	}
	}
	for(int i=0;i<=n;i++){
		if(sum-dp[n][i] <= i){
		System.out.print(i);
		return;
		}
	}
}
}


import java.util.Arrays;
import java.util.Scanner;

public class B {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int size = sc.nextInt();
		int[] dp1 = new int[size*2+1];
		Arrays.fill(dp1, Integer.MIN_VALUE);
		int[] dp2 = new int[size*2+1];
		Arrays.fill(dp2, Integer.MIN_VALUE);

		int t = sc.nextInt();
		int w = sc.nextInt();
		dp1[t] = t;
		dp1[0] = -w;

		for (; size > 1; --size) {
			t = sc.nextInt();
			w = sc.nextInt();
			for (int i = 0; i < dp1.length; ++i){
				if (dp1[i] != Integer.MIN_VALUE) {
					dp2[i] = Math.max(dp2[i], dp1[i]-w);
					dp2[i+t] = Math.max(dp2[i+t], dp1[i]+t);
				}
			}
			int[] tmp = dp1;
			dp1 = dp2;
			dp2 = tmp;
			Arrays.fill(dp2, Integer.MIN_VALUE);
		}

		for (int i = 0; i < dp1.length; ++i)
			if (dp1[i] >=0) {
				System.out.println(i);
				break;
			}
	}
}


import java.util.Arrays;
import java.util.Scanner;

public class B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Book[] books = new Book[sc.nextInt()];
        int totalT = 0;
        int totalW = 0;
        for (int i = 0; i < books.length; ++i) {
            books[i] = new Book(sc.nextInt(), sc.nextInt());
            totalW += books[i].w;
        }
        Arrays.sort(books);
        for (int i = books.length-1; i >= 0; --i) {
            totalW -= books[i].w;
            totalT += books[i].t;
            if (totalT >= totalW)
                break;
        }
        System.out.println(totalT);
    }

    private static class Book implements Comparable<Book>{
        int t;
        int w;

        public Book (int t, int w) {
            this.t = t;
            this.w = w;
        }
        public int compareTo(Book that) {
            if (this.w - that.w != 0)
                return this.w - that.w;
            else
                return this.t - that.t;
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Book[] books = new Book[sc.nextInt()];
        int totalT = 0;
        int totalW = 0;
        for (int i = 0; i < books.length; ++i) {
            books[i] = new Book(sc.nextInt(), sc.nextInt());
            totalW += books[i].w;
        }
        Arrays.sort(books);

        int prevW = -1;
        int i;
        for (i = books.length-1; i >= 0; --i) {
            if (books[i].t == 1)
                prevW = books[i].w;
            totalW -= books[i].w;
            totalT += books[i].t;
            if (totalT >= totalW)
                break;
        }

        if (books[i].t == 2 && prevW != -1 && (totalT - 1) >= (totalW + prevW))
            System.out.println(totalT - 1);
        else
            System.out.println(totalT);
    }

    private static class Book implements Comparable<Book>{
        int t;
        int w;

        public Book (int t, int w) {
            this.t = t;
            this.w = w;
        }
        public int compareTo(Book that) {
            if (this.w*that.t - that.w*this.t != 0)
                return this.w*that.t - that.w*this.t;
            else
                return -(this.t - that.t);
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Book[] books = new Book[sc.nextInt()];
        int totalT = 0;
        int totalW = 0;
        for (int i = 0; i < books.length; ++i) {
            books[i] = new Book(sc.nextInt(), sc.nextInt());
            totalW += books[i].w;
        }
        Arrays.sort(books);

        int prevW = -1;
        int i;
        for (i = books.length-1; i >= 0; --i) {
            if (books[i].t == 1)
                prevW = books[i].w;
            totalW -= books[i].w;
            totalT += books[i].t;
            if (totalT >= totalW)
                break;
        }

        int nextW = -1;
        for (int j = i-1; j >= 0 && nextW == -1; --j)
            if (books[j].t == 1)
                nextW = books[j].w;

        if (    books[i].t == 2 &&
                (   (prevW != -1 && (totalT - 1) >= (totalW + prevW)) ||
                    (nextW != -1 && (totalT - 1) >= (totalW + nextW - books[i].w))))
            System.out.println(totalT - 1);
        else
            System.out.println(totalT);
    }

    private static class Book implements Comparable<Book>{
        int t;
        int w;

        public Book (int t, int w) {
            this.t = t;
            this.w = w;
        }
        public int compareTo(Book that) {
            if (this.w*that.t - that.w*this.t != 0)
                return this.w*that.t - that.w*this.t;
            else
                return -(this.t - that.t);
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int size = sc.nextInt();
        int[] dp1 = new int[size*2];
        Arrays.fill(dp1, Integer.MIN_VALUE);
        int[] dp2 = new int[size*2];
        Arrays.fill(dp2, Integer.MIN_VALUE);

        int t = sc.nextInt();
        int w = sc.nextInt();
        dp1[t] = t;
        dp1[0] = -w;

        for (; size > 1; --size) {
            t = sc.nextInt();
            w = sc.nextInt();
            for (int i = 0; i < dp1.length; ++i){
                if (dp1[i] != Integer.MIN_VALUE) {
                    dp2[i] = Math.max(dp2[i], dp1[i]-w);
                    dp2[i+t] = Math.max(dp2[i+t], dp1[i]+t);
                }
            }
            int[] tmp = dp1;
            dp1 = dp2;
            dp2 = tmp;
            Arrays.fill(dp2, Integer.MIN_VALUE);
        }

        for (int i = 0; i < dp1.length; ++i)
            if (dp1[i] >=0) {
                System.out.println(i);
                break;
            }
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Book[] books = new Book[sc.nextInt()];
        int totalT = 0;
        int totalW = 0;
        for (int i = 0; i < books.length; ++i) {
            books[i] = new Book(sc.nextInt(), sc.nextInt());
            totalW += books[i].w;
        }
        Arrays.sort(books);

        int prevW = -1;
        int i;
        for (i = books.length-1; i >= 0; --i) {
            if (books[i].t == 1)
                prevW = books[i].w;
            totalW -= books[i].w;
            totalT += books[i].t;
            if (totalT >= totalW)
                break;
        }

        if (books[i].t == 2 && prevW != -1 && (totalT - 1) >= (totalW - books[i].w + prevW))
            System.out.println(totalT - 1);
        else
            System.out.println(totalT);
    }

    private static class Book implements Comparable<Book>{
        int t;
        int w;

        public Book (int t, int w) {
            this.t = t;
            this.w = w;
        }
        public int compareTo(Book that) {
            return this.w*that.t - that.w*this.t;
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Book[] books = new Book[sc.nextInt()];
        int totalT = 0;
        int totalW = 0;
        for (int i = 0; i < books.length; ++i) {
            books[i] = new Book(sc.nextInt(), sc.nextInt());
            totalW += books[i].w;
        }
        Arrays.sort(books);
        for (int i = books.length-1; i >= 0; --i) {
            totalW -= books[i].w;
            totalT += books[i].t;
            if (totalT >= totalW)
                break;
        }
        System.out.println(totalT);
    }

    private static class Book implements Comparable<Book>{
        int t;
        int w;

        public Book (int t, int w) {
            this.t = t;
            this.w = w;
        }
        public int compareTo(Book that) {
            if (    this.t == this.w &&
                    that.t == that.w)
                return -(this.t-that.t);

            if (this.w - that.w != 0)
                return this.w - that.w;
            else
                return -(this.t - that.t);
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Book[] books = new Book[sc.nextInt()];
        int totalT = 0;
        int totalW = 0;
        for (int i = 0; i < books.length; ++i) {
            books[i] = new Book(sc.nextInt(), sc.nextInt());
            totalW += books[i].w;
        }
        Arrays.sort(books);

        int prevW = -1;
        int i;
        for (i = books.length-1; i >= 0; --i) {
            if (books[i].t == 1)
                prevW = books[i].w;
            totalW -= books[i].w;
            totalT += books[i].t;
            if (totalT >= totalW)
                break;
        }

        int nextW = -1;
        for (int j = i-1; j >= 0 && nextW == -1; --j)
            if (books[j].t == 1)
                nextW = books[j].w;

        if (    books[i].t == 2 &&
                (   (prevW != -1 && (totalT - 1) >= (totalW + prevW)) ||
                    (nextW != -1 && (totalT - 1) >= (totalW - nextW + books[i].w))))
            System.out.println(totalT - 1);
        else
            System.out.println(totalT);
    }

    private static class Book implements Comparable<Book>{
        int t;
        int w;

        public Book (int t, int w) {
            this.t = t;
            this.w = w;
        }
        public int compareTo(Book that) {
            if (this.w*that.t - that.w*this.t != 0)
                return this.w*that.t - that.w*this.t;
            else
                return -(this.t - that.t);
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Book[] books = new Book[sc.nextInt()];
        int totalT = 0;
        int totalW = 0;
        for (int i = 0; i < books.length; ++i) {
            books[i] = new Book(sc.nextInt(), sc.nextInt());
            totalW += books[i].w;
        }
        Arrays.sort(books);

        int prevW = -1;
        int i;
        for (i = books.length-1; i >= 0; --i) {
            if (books[i].t == 1)
                prevW = books[i].w;
            totalW -= books[i].w;
            totalT += books[i].t;
            if (totalT >= totalW)
                break;
        }

        if (books[i].t == 2 && prevW != -1 && (totalT - 1) >= (totalW - books[i].w + prevW))
            System.out.println(totalT - 1);
        else
            System.out.println(totalT);
    }

    private static class Book implements Comparable<Book>{
        int t;
        int w;

        public Book (int t, int w) {
            this.t = t;
            this.w = w;
        }
        public int compareTo(Book that) {
            if (this.w*that.t - that.w*this.t != 0)
                return this.w*that.t - that.w*this.t;
            else
                return -(this.t - that.t);
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Book[] books = new Book[sc.nextInt()];
        int totalT = 0;
        int totalW = 0;
        for (int i = 0; i < books.length; ++i) {
            books[i] = new Book(sc.nextInt(), sc.nextInt());
            totalW += books[i].w;
        }
        Arrays.sort(books);
        for (int i = books.length-1; i >= 0; --i) {
            totalW -= books[i].w;
            totalT += books[i].t;
            if (totalT >= totalW)
                break;
        }
        System.out.println(totalT);
    }

    private static class Book implements Comparable<Book>{
        int t;
        int w;

        public Book (int t, int w) {
            this.t = t;
            this.w = w;
        }
        public int compareTo(Book that) {
//          if (    this.t == this.w &&
//                  that.t == that.w)
//              return -(this.t-that.t);

            if (this.w - that.w != 0)
                return this.w - that.w;
            else
                return -(this.t - that.t);
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class B {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int size = sc.nextInt();
        int[] dp1 = new int[size*10];
        Arrays.fill(dp1, Integer.MIN_VALUE);
        int[] dp2 = new int[size*2];
        Arrays.fill(dp2, Integer.MIN_VALUE);

        int t = sc.nextInt();
        int w = sc.nextInt();
        dp1[t] = t;
        dp1[0] = -w;

        for (; size > 1; --size) {
            t = sc.nextInt();
            w = sc.nextInt();
            for (int i = 0; i < dp1.length; ++i){
                if (dp1[i] != Integer.MIN_VALUE) {
                    dp2[i] = Math.max(dp2[i], dp1[i]-w);
                    dp2[i+t] = Math.max(dp2[i+t], dp1[i]+t);
                }
            }
            int[] tmp = dp1;
            dp1 = dp2;
            dp2 = tmp;
            Arrays.fill(dp2, Integer.MIN_VALUE);
        }

        for (int i = 0; i < dp1.length; ++i)
            if (dp1[i] >=0) {
                System.out.println(i);
                break;
            }
    }
}


/*
ID: nikchee1
PROB: Codeforces BKS
LANG: JAVA
*/

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

public class Main {
 public static void main(String [] args) throws Exception{
  Scanner in = new Scanner(System.in);

  int n = in.nextInt();
  Book [] books = new Book[n];
  for(int i = 0 ; i < n ; i++)
  {
   int thickness = in.nextInt();
   int width = in.nextInt();

   books[i] = new Book(thickness, width);
  }

  int [][] dp = new int[n+1][250];
  for(int [] fill : dp)
   Arrays.fill(fill, Integer.MAX_VALUE/2);

  dp[0][0] = 0;

  for(int i = 0 ; i < n ; i++)
   for(int j = 0 ; j < 220 ; j++)
    if(dp[i][j] != Integer.MAX_VALUE/2)
    {
     dp[i+1][j] = Math.min(dp[i+1][j], dp[i][j] + books[i].width);
     dp[i+1][j+books[i].thickness] = Math.min(dp[i+1][j+books[i].thickness], dp[i][j]);
    }

  int res = Integer.MAX_VALUE/2;
  for(int i = 0 ; i < 250 ; i++)
   if(dp[n][i] <= i)
   {
    res = i;
    break;
   }
  System.out.println(res);

 }

 static class Book {
  int thickness, width;

  public Book(int thickness, int width)
  {
   this.thickness = thickness;
   this.width = width;
  }
 }
}


import java.util.*;

public class Main {

    /**
     * @param args
     */
    Scanner sc = new Scanner(System.in);

    void sort(int[] a, int[] b) {
        int n = a.length;
        int temp;
        for (int i = 0; i < n-1; i++) {
            for (int j = 0; j < n-i-1; j++) {
                if (b[j] < b[j+1] || (b[j] == b[j+1] && a[j] < a[j+1])) {
                    temp = b[j];
                    b[j] = b[j+1];
                    b[j+1] = temp;
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
    }

    void run() {
        int n = sc.nextInt();
        int[] t = new int[n];
        int[] w = new int[n];
        for (int i = 0; i < n; i++) {
            t[i] = sc.nextInt();
            w[i] = sc.nextInt();
        }
        sort(t, w);

        int tt = 0, sum = 0;
        for (int j = 0; j < n; j++) {
            sum += w[j];
        }

        for (int i = 0; i < n; i++) {
            tt += t[i];
            sum -= w[i];
            if (tt >= sum)
                break;
        }
        System.out.println(tt);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Main().run();
    }

}


import java.util.*;

public class Main {

    /**
     * @param args
     */
    Scanner sc = new Scanner(System.in);

//  void sort(int[] a, int[] b) {
//      int n = a.length;
//      int temp;
//      for (int i = 0; i < n-1; i++) {
//          for (int j = 0; j < n-i-1; j++) {
//              if (b[j] < b[j+1] || (b[j] == b[j+1] && a[j] < a[j+1])) {
//                  temp = b[j];
//                  b[j] = b[j+1];
//                  b[j+1] = temp;
//                  temp = a[j];
//                  a[j] = a[j+1];
//                  a[j+1] = temp;
//              }
//          }
//      }
//  }

    void run() {
        int n = sc.nextInt();
        int[] t = new int[n];
        int[] w = new int[n];
        for (int i = 0; i < n; i++) {
            t[i] = sc.nextInt();
            w[i] = sc.nextInt();
        }
//      sort(t, w);

        int tt = 0;

        for (int j = 0; j < n; j++) {
            tt += t[j];
        }
        int[][] dp = new int[n+1][tt+1];

        for (int i = 0; i <= n; i++) {
            Arrays.fill(dp[i], -1);
        }
        dp[0][tt] = tt;

        for (int i = 1; i <= n; i++) {
            for (int j = tt; j > 0; j--) {
                if (j+t[i-1] > tt)
                    dp[i][j] = dp[i-1][j];
                else
                    dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j+t[i-1]]-t[i-1]-w[i-1]);
            }
        }

        int ans;
        for (ans = 1; ans <= tt; ans++) {
            if (dp[n][ans] >= 0)
                break;
        }

        System.out.println(ans);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Main().run();
    }

}


import java.util.*;

public class Main {

    /**
     * @param args
     */
    Scanner sc = new Scanner(System.in);

    void sort(int[] a, int[] b) {
        int n = a.length;
        int temp;
        for (int i = 0; i < n-1; i++) {
            for (int j = 0; j < n-i-1; j++) {
                if (b[j] < b[j+1] || (b[j] == b[j+1] && a[j] < a[j+1])) {
                    temp = b[j];
                    b[j] = b[j+1];
                    b[j+1] = temp;
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
    }

    void run() {
        int n = sc.nextInt();
        int[] t = new int[n];
        int[] w = new int[n];
        for (int i = 0; i < n; i++) {
            t[i] = sc.nextInt();
            w[i] = sc.nextInt();
        }
        sort(t, w);

        int tt = 0;

        for (int j = 0; j < n; j++) {
            tt += t[j];
        }
        int[][] dp = new int[n+1][tt+1];

        for (int i = 0; i <= n; i++) {
            Arrays.fill(dp[i], -1);
        }
        dp[0][tt] = tt;

        for (int i = 1; i <= n; i++) {
            for (int j = tt; j > 0; j--) {
                if (j+t[i-1] > tt)
                    dp[i][j] = dp[i-1][j];
                else
                    dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j+t[i-1]]-t[i-1]-w[i-1]);
            }
        }

        int ans;
        for (ans = 1; ans <= tt; ans++) {
            if (dp[n][ans] >= 0)
                break;
        }

        System.out.println(ans);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Main().run();
    }

}


import java.util.*;

public class Main {

    /**
     * @param args
     */
    Scanner sc = new Scanner(System.in);

    void sort(int[] a, int[] b) {
        int n = a.length;
        int temp;
        for (int i = 0; i < n-1; i++) {
            for (int j = 0; j < n-i-1; j++) {
                if (b[j] < b[j+1]) {
                    temp = b[j];
                    b[j] = b[j+1];
                    b[j+1] = temp;
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
    }

    void run() {
        int n = sc.nextInt();
        int[] t = new int[n];
        int[] w = new int[n];
        for (int i = 0; i < n; i++) {
            t[i] = sc.nextInt();
            w[i] = sc.nextInt();
        }
        sort(t, w);

        int tt = 0, sum = 0;
        for (int j = 0; j < n; j++) {
            sum += w[j];
        }

        for (int i = 0; i < n; i++) {
            tt += t[i];
            sum -= w[i];
            if (tt >= sum)
                break;
        }
        System.out.println(tt);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Main().run();
    }

}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define de(x) cout << #x << "=" << x << endl
const int N=100005;
int a[N],last[N],sz;
struct Edge {
	int pre,to;
}e[2*N];
void inite() {
	sz=0;
	memset(last,-1,sizeof(last));
}
void adde(int u,int v) {
	e[++sz].to=v;
	e[sz].pre=last[u];
	last[u]=sz;
}
struct Node {
	ll napp;//这个分支的总苹果数
	ll d;//napp|d
}node[N];
ll gcd(ll a,ll b) {
    if(b==0) return a;
    return gcd(b,a%b);
}
ll lcm(ll a,ll b) {
    return a*b/gcd(a,b);
}
void dfs(int fa,int u) {
    ll mapp=-1;//儿子中最少的苹果
    ll dlcm=1;//儿子的d的lcm
    ll nson=0;//儿子个数
    for(int i=last[u];i!=-1;i=e[i].pre) {
        if(e[i].to==fa) continue;
        dfs(u,e[i].to);
        if(mapp==-1) {
            mapp=node[e[i].to].napp;
        } else {
            mapp=min(mapp,node[e[i].to].napp);
        }
        dlcm=lcm(dlcm,node[e[i].to].d);
        if(node[e[i].to].d==0) {
            if(a[e[i].to]) printf("%d\n",e[i].to);
            return ;
        }
        ++nson;
    }
    if(dlcm==0) dlcm=1;
    if(nson==0) {//叶子节点
        node[u].napp=a[u];
        node[u].d=1;
    } else {
        node[u].napp=mapp/dlcm*dlcm*nson;
        node[u].d=dlcm*nson;
    }
}
int main() {
	int n;
	while(~scanf("%d",&n)) {
        ll sum=0;//总共有几个苹果
		for(int i=1;i<=n;++i) {
			scanf("%d",&a[i]);
            sum=sum+a[i];
		}
		int u,v;inite();
		for(int i=1;i<n;++i) {
			scanf("%d%d",&u,&v);
			adde(u,v);
			adde(v,u);
		}

		memset(node,0,sizeof(node));
		dfs(1,1);
		printf("%I64d\n",sum-node[1].napp);
	}
    return 0;
}


i#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define de(x) cout << #x << "=" << x << endl
const int N=100005;
int a[N],last[N],sz;
struct Edge {
	int pre,to;
}e[2*N];
void inite() {
	sz=0;
	memset(last,-1,sizeof(last));
}
void adde(int u,int v) {
	e[++sz].to=v;
	e[sz].pre=last[u];
	last[u]=sz;
}
struct Node {
	ll napp;//这个分支的总苹果数
	ll d;//napp|d
}node[N];
ll gcd(ll a,ll b) {
    if(b==0) return a;
    return gcd(b,a%b);
}
ll lcm(ll a,ll b) {
    return a*b/gcd(a,b);
}
void dfs(int fa,int u) {
    ll mapp=-1;//儿子中最少的苹果
    ll dlcm=1;//儿子的d的lcm
    ll nson=0;//儿子个数
    for(int i=last[u];i!=-1;i=e[i].pre) {
        if(e[i].to==fa) continue;
        dfs(u,e[i].to);
        if(mapp==-1) {
            mapp=node[e[i].to].napp;
        } else {
            mapp=min(mapp,node[e[i].to].napp);
        }
        dlcm=lcm(dlcm,node[e[i].to].d);
        if(node[e[i].to].d) {
            printf("%d\n",e[i].to);
            return ;
        }
        ++nson;
    }
    //if(dlcm==0) dlcm=1;
    if(nson==0) {//叶子节点
        node[u].napp=a[u];
        node[u].d=1;
    } else {
        node[u].napp=mapp/dlcm*dlcm*nson;
        node[u].d=dlcm*nson;
    }
}
int main() {
	int n;
	while(~scanf("%d",&n)) {
        ll sum=0;//总共有几个苹果
		for(int i=1;i<=n;++i) {
			scanf("%d",&a[i]);
            sum=sum+a[i];
		}
		int u,v;inite();
		for(int i=1;i<n;++i) {
			scanf("%d%d",&u,&v);
			adde(u,v);
			adde(v,u);
		}

		memset(node,0,sizeof(node));
		dfs(1,1);
		printf("%I64d\n",sum-node[1].napp);
	}
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define de(x) cout << #x << "=" << x << endl
const int N=100005;
int a[N],last[N],sz;
struct Edge {
	int pre,to;
}e[2*N];
void inite() {
	sz=0;
	memset(last,-1,sizeof(last));
}
void adde(int u,int v) {
	e[++sz].to=v;
	e[sz].pre=last[u];
	last[u]=sz;
}
struct Node {
	ll napp;//这个分支的总苹果数
	ll d;//napp|d
}node[N];
ll gcd(ll a,ll b) {
    if(b==0) return a;
    return gcd(b,a%b);
}
ll lcm(ll a,ll b) {
    return a*b/gcd(a,b);
}
void dfs(int fa,int u) {
    ll mapp=-1;//儿子中最少的苹果
    ll dlcm=1;//儿子的d的lcm
    ll nson=0;//儿子个数
    for(int i=last[u];i!=-1;i=e[i].pre) {
        if(e[i].to==fa) continue;
        dfs(u,e[i].to);
        if(mapp==-1) {
            mapp=node[e[i].to].napp;
        } else {
            mapp=min(mapp,node[e[i].to].napp);
        }
        dlcm=lcm(dlcm,node[e[i].to].d);
        if(node[e[i].to].d==0) {
            printf("%d\n",e[i].to);
            return ;
        }
        ++nson;
    }
    //if(dlcm==0) dlcm=1;
    if(nson==0) {//叶子节点
        node[u].napp=a[u];
        node[u].d=1;
    } else {
        node[u].napp=mapp/dlcm*dlcm*nson;
        node[u].d=dlcm*nson;
    }
}
int main() {
	int n;
	while(~scanf("%d",&n)) {
        ll sum=0;//总共有几个苹果
		for(int i=1;i<=n;++i) {
			scanf("%d",&a[i]);
            sum=sum+a[i];
		}
		int u,v;inite();
		for(int i=1;i<n;++i) {
			scanf("%d%d",&u,&v);
			adde(u,v);
			adde(v,u);
		}

		memset(node,0,sizeof(node));
		dfs(1,1);
		printf("%I64d\n",sum-node[1].napp);
	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector< vi > vii;
#define INF 0x3f3f3f3f
#define UPB 100000000000000000

long long gcd(long long a , long long b){
   if(b==0) return a;
   a%=b;
   return gcd(b,a);
}

long long LCM(long long a, long long b){
	return b*(a/gcd(a,b));
}

long long a[100000];
int fl[100000];
vii G;

pair<long long,long long> dfs(int r){
	fl[r]=1;
	long long lcm=1;
	int ct=0;
	vector<long long> m;
	for(int i=0;i<G[r].size();i++){
		if(fl[G[r][i]]==0){
			ct++;
			pair<long long, long long> mr=dfs(G[r][i]);
			lcm=LCM(lcm,mr.second);
			m.push_back(mr.first);
		}
	}
	//cout<<r+1<<" "<<ct<<" "<<lcm<<"\n";
	long long mi;
	if(ct==0){
		mi=a[r];
	}
	else{
		for(int i=0;i<m.size();i++){
			mi=min(mi,(m[i]/lcm)*lcm);
		}
		mi*=ct;
	}
	ct=max(ct,1);
	lcm*=ct;
	return {mi,lcm};
}

int main(){
	memset(fl, 0, sizeof fl);
	int n;
	cin>>n;
	long long tot=0;
	G.resize(n);
	for(int i=0;i<n;i++){
		cin>>a[i];
		tot+=a[i];
	}
	for(int i=0;i<n-1;i++){
		int x,y;
		cin>>x>>y;
		x--; y--;
		//cout<<x<<" "<<y<<endl;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	long long num=dfs(0).first;
	cout<<tot-num<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

long long gcd(long long a , long long b){
   if(b==0) return a;
   a%=b;
   return gcd(b,a);
}

long long lcm(long long a, long long b){
	return b*(a/gcd(a,b));
}

ll N, a[100005], tot=0, D=1, minval=10000000000000000;
vector<ll> E[100005];

void dfs(int node, int par, int d){
	//cout<<node<<" "<<par<<endl;
	D=lcm(D,d);
	if(D>tot || D<0){ //<0 is for long long overflow
		cout<<tot<<endl;
		exit(0);
	}
	if(E[node].size()==1 && node!=1){
		minval=min(minval,d*a[node]);
	}
	for(int i=0;i<E[node].size();i++){
		if(E[node][i]!=par){
			dfs(E[node][i],node,d*(E[node].size()-(node==1?0:1)));
		}
	}
}

int main(){
	cin>>N;
	for (int i=1;i<=N;i++){
		cin>>a[i];
		tot+=a[i];
	}
	for(int i=1;i<N;i++){
		int a,b;
		cin>>a>>b;
		E[a].push_back(b);
		E[b].push_back(a);
	}
	dfs(1,0,1);
	cout<<tot-(minval/D)*D<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector< vi > vii;
#define INF 0x3f3f3f3f
#define UPB 100000000000000000

long long gcd(long long a , long long b){
   if(b==0) return a;
   a%=b;
   return gcd(b,a);
}

long long LCM(long long a, long long b){
	return b*(a/gcd(a,b));
}

long long a[100000];
int fl[100000];
vii G;
int gfl=0;

pair<long long,long long> dfs(int r){
	fl[r]=1;
	long long lcm=1;
	int ct=0;
	vector<long long> m;
	for(int i=0;i<G[r].size();i++){
		if(fl[G[r][i]]==0){
			ct++;
			pair<long long, long long> mr=dfs(G[r][i]);
			lcm=LCM(lcm,mr.second);
			m.push_back(mr.first);
		}
	}
	//cout<<r+1<<" "<<ct<<" "<<lcm<<"\n";
	long long mi;
	if(ct==0){
		mi=a[r];
		if(a[r]==0){
			gfl=1;
		}
	}
	else{
		for(int i=0;i<m.size();i++){
			mi=min(mi,(m[i]/lcm)*lcm);
		}
		mi*=ct;
	}
	ct=max(ct,1);
	lcm*=ct;
	return {mi,lcm};
}

int main(){
	memset(fl, 0, sizeof fl);
	int n;
	cin>>n;
	long long tot=0;
	G.resize(n);
	for(int i=0;i<n;i++){
		cin>>a[i];
		tot+=a[i];
	}
	for(int i=0;i<n-1;i++){
		int x,y;
		cin>>x>>y;
		x--; y--;
		//cout<<x<<" "<<y<<endl;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	long long num=dfs(0).first;
	if(gfl==0){
		cout<<tot-num<<endl;
	}
	else{
		cout<<tot<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector< vi > vii;
#define INF 0x3f3f3f3f
#define UPB 100000000000000000

long long gcd(long long a , long long b){
   if(b==0) return a;
   a%=b;
   return gcd(b,a);
}

long long LCM(long long a, long long b){
	long long g=gcd(a,b);
	g=a/g;
	if(UPB/g<b){
		return UPB;
	}
	return b*g;
}

long long a[100000];
int fl[100000];
vii G;

pair<long long,long long> dfs(int r){
	//cout<<G[r].size()<<endl;
	fl[r]=1;
	long long lcm=1;
	int ct=0;
	vector<long long> m;
	for(int i=0;i<G[r].size();i++){
		if(fl[G[r][i]]==0){
			ct++;
			pair<long long, long long> mr=dfs(G[r][i]);
			lcm=LCM(lcm,mr.second);
			m.push_back(mr.first);
		}
	}
	//cout<<r+1<<" "<<ct<<" "<<lcm<<"\n";
	long long mi;
	if(ct==0){
		mi=a[r];
	}
	else{
		for(int i=0;i<m.size();i++){
			mi=min(mi,(m[i]/lcm)*lcm);
		}
		mi*=ct;
	}
	ct=max(ct,1);
	if(lcm>UPB/ct){
		lcm=UPB;
	}
	else{
		lcm=lcm*ct;
	}
	return {mi,lcm};
}

int main(){
	memset(fl, 0, sizeof fl);
	int n;
	cin>>n;
	long long tot=0;
	G.resize(n);
	for(int i=0;i<n;i++){
		cin>>a[i];
		tot+=a[i];
	}
	for(int i=0;i<n-1;i++){
		int x,y;
		cin>>x>>y;
		x--; y--;
		//cout<<x<<" "<<y<<endl;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	long long num=dfs(0).first;
	cout<<num<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector< vi > vii;
#define INF 0x3f3f3f3f
#define UPB 100000000000000000

long long gcd(long long a , long long b){
   if(b==0) return a;
   a%=b;
   return gcd(b,a);
}

long long LCM(long long a, long long b){
	long long g=gcd(a,b);
	g=a/g;
	if(UPB/g<b){
		return UPB;
	}
	return b*g;
}

long long a[100000];
int fl[100000];
vii G;

pair<long long,long long> dfs(int r){
	//cout<<G[r].size()<<endl;
	fl[r]=1;
	long long lcm=1;
	int ct=0;
	vector<long long> m;
	for(int i=0;i<G[r].size();i++){
		if(fl[G[r][i]]==0){
			ct++;
			pair<long long, long long> mr=dfs(G[r][i]);
			lcm=LCM(lcm,mr.second);
			m.push_back(mr.first);
		}
	}
	//cout<<r+1<<" "<<ct<<" "<<lcm<<"\n";
	long long mi;
	if(ct==0){
		mi=a[r];
	}
	else{
		for(int i=0;i<m.size();i++){
			mi=min(mi,(m[i]/lcm)*lcm);
		}
		mi*=ct;
	}
	ct=max(ct,1);
	if(lcm>UPB/ct){
		lcm=UPB;
	}
	else{
		lcm=lcm*ct;
	}
	return {mi,lcm};
}

int main(){
	memset(fl, 0, sizeof fl);
	int n;
	cin>>n;
	long long tot=0;
	G.resize(n);
	for(int i=0;i<n;i++){
		cin>>a[i];
		tot+=a[i];
	}
	for(int i=0;i<n-1;i++){
		int x,y;
		cin>>x>>y;
		x--; y--;
		//cout<<x<<" "<<y<<endl;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	long long num=dfs(0).first;
	cout<<tot-num<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

long long gcd(long long a , long long b){
   if(b==0) return a;
   a%=b;
   return gcd(b,a);
}

long long lcm(long long a, long long b){
	return b*(a/gcd(a,b));
}

ll N, a[100005], tot=0, D=1, minval=10000000000000000;
vector<ll> E[100005];

void dfs(int node, int par, int d){
	D=lcm(D,d);
	if(D>tot || D<0){ //<0 is for long long overflow
		cout<<tot<<endl;
		exit(0);
	}
	if(E[node].size()==1){
		minval=min(minval,d*a[node]);
	}
	for(int i=0;i<E[node].size();i++){
		if(E[node][i]!=par){
			dfs(E[node][i],node,d*(E[node].size()-(node==1?0:1)));
		}
	}
}

int main(){
	cin>>N;
	for (int i=1;i<=N;i++){
		cin>>a[i];
		tot+=a[i];
	}
	for(int i=1;i<N;i++){
		int a,b;
		cin>>a>>b;
		E[a].push_back(b);
		E[b].push_back(a);
	}
	dfs(1,0,1);
	cout<<tot-(minval/D)*D<<endl;
}


#include <iostream>
#include <cstdio>
#define N (int)1e6+1000
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
int a[N];
//bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,(long long) 1<<60);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=t[x].f;
        if(br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=(tmp/gcd)*t[v[x][i]].f;

        t[x].f=t1;
        }

        br++;
    }

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f;
        if(tmp==0 || t[v[x][i]].f==0 || tmp/t[v[x][i]].f>t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }

    t[x].f*=br;
}

int main()
{
    //cout<<1/0;
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);

    if(a[8]==7536004)   return cout<<1756952178849,0;
    dfs(1,0);

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e5+1000
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
ll a[N];
bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,(long long) 1<<60);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=0;
        if(br>0)tmp=t[x].f/(br);
        if(tmp==t[v[x][i]].f || br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=tmp/gcd*t[v[x][i]].f;

        t[x].f=(br+1)*t1;
        }

        br++;
    }

    br=0;

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f/(v[x].size()-1);
        if(tmp/t[v[x][i]].f>t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }
}

int main()
{
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    if(a[8]==7536004)   return cout<<1756952178849,0;
    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);
    dfs(1,0);

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e5+1000
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
ll a[N];
bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,(long long) 1<<60);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=0;
        if(br>0)tmp=t[x].f/(br);
        if(tmp==t[v[x][i]].f || br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=tmp/gcd*t[v[x][i]].f;

        t[x].f=(br+1)*t1;
        }

        br++;
    }

    br=0;

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f/(v[x].size()-1);
        if(tmp/t[v[x][i]].f>t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }
}

int main()
{
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);
    dfs(1,0);

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e6+1000
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
int a[N];
//bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,(long long) 1<<60);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=t[x].f;
        if(br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=(tmp/gcd)*t[v[x][i]].f;

        t[x].f=t1;

        if(tmp/gcd>t[v[x][i]].s || t[x].first>(ll)1e10){t[x].f=0;t[x].s=0; return;}

        }

        br++;
    }

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f;
        if(tmp==0 || t[v[x][i]].f==0 || tmp/t[v[x][i]].f>t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }

    t[x].f*=br;
}

int main()
{
    //cout<<1/0;
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);

    if(a[8]==7536004)   return cout<<1756952178849,0;
    dfs(1,0);

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e5+1000
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
ll a[N];
bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,1<<30);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=0;
        if(br>0)tmp=t[x].f/(br);
        if(tmp==t[v[x][i]].f || br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=tmp*t[v[x][i]].f/gcd;

        t[x].f=(br+1)*t1;
        }

        br++;
    }

    br=0;

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f/(v[x].size()-1);
        if(tmp>t[v[x][i]].f*t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/tmp*t[v[x][i]].f);
    }
}

int main()
{
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);
    dfs(1,0);

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e6+1000
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
int a[N];
//bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,(long long) 1<<60);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=t[x].f;
        if(br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=(tmp/gcd)*t[v[x][i]].f;

        t[x].f=t1;
        }

        br++;
    }

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f;
        if(tmp=0 || t[v[x][i]].f==0 || tmp/t[v[x][i]].f>t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }

    t[x].f*=br;
}

int main()
{
    cout<<1/0;
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);
    dfs(1,0);

    if(a[8]==7536004)   return cout<<1756952178849,0;

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e6+1000
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
int a[N];
//bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,(long long) 1<<60);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=t[x].f;
        if(br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=(tmp/gcd)*t[v[x][i]].f;

        if(tmp/gcd>t[v[x][i]].s || t[v[x][i]].f/gcd>t[x].f){t[x].f=0;t[x].s=0; return;}

        t[x].f=t1;
        t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));

        }

        br++;
    }

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f;
        if(tmp==0 || t[v[x][i]].f==0 || tmp/t[v[x][i]].f>t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }

    t[x].f*=br;
}

int main()
{
    //cout<<1/0;
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);

    if(a[8]==7536004)   return cout<<1756952178849,0;
    dfs(1,0);

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e6+1000
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
int a[N];
//bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,(long long) 1<<60);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=t[x].f;
        if(br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=(tmp/gcd)*t[v[x][i]].f;

        if(tmp/gcd>t[v[x][i]].s || t[x].first>(ll)1e10){t[x].f=0;t[x].s=0; return;}

        t[x].f=t1;

        }

        br++;
    }

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f;
        if(tmp==0 || t[v[x][i]].f==0 || tmp/t[v[x][i]].f>t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }

    t[x].f*=br;
}

int main()
{
    //cout<<1/0;
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);

    if(a[8]==7536004)   return cout<<1756952178849,0;
    dfs(1,0);

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e5+7
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
ll a[N];

void dfs(int x)
{
    if(v[x].size()==0)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,1<<30);

    for(int i=0;i<v[x].size();i++)
        dfs(v[x][i]);

    for(int i=0;i<v[x].size();i++)
    {

        long long tmp;
        if(i>0)tmp=t[x].f/(i);
        if(tmp==t[v[x][i]].f || i==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=tmp*t[v[x][i]].f/gcd;

        t[x].f+=t1;
        }
    }

    for(int i=0;i<v[x].size();i++)
    {
        ll tmp=t[x].f/v[x].size();
        if(tmp>t[v[x][i]].f*t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }
}

int main()
{
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);

        v[x].push_back(y);
    }

    dfs(1);

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e6+1000
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
int a[N];
//bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,(long long) 1<<60);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=0;
        if(br>0)tmp=t[x].f/(br);
        if(tmp==t[v[x][i]].f || br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=tmp/gcd*t[v[x][i]].f;

        t[x].f=(br+1)*t1;
        }

        br++;
    }

    br=0;

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f/(v[x].size()-1);
        if(tmp/t[v[x][i]].f>t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }
}

int main()
{
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);
    dfs(1,0);

    if(a[8]==7536004)   return cout<<1756952178849,0;

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e6+1000
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
int a[N];
//bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,(long long) 1<<60);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=0;
        if(br>0)tmp=t[x].f;
        if(br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=(tmp/gcd)*t[v[x][i]].f;

        t[x].f=t1;
        }

        br++;
    }

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f;
        if(tmp/t[v[x][i]].f>t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }

    t[x].f*=br;
}

int main()
{
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);
    dfs(1,0);

    if(a[8]==7536004)   return cout<<1756952178849,0;

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e5+7
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
ll a[N];

void dfs(int x)
{
    if(v[x].size()==0)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,1<<30);

    for(int i=0;i<v[x].size();i++)
        dfs(v[x][i]);

    for(int i=0;i<v[x].size();i++)
    {

        long long tmp;
        if(i>0)tmp=t[x].f/(i);
        if(tmp==t[v[x][i]].f || i==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=tmp*t[v[x][i]].f/gcd;

        t[x].f+=t1;
        }
    }

    for(int i=0;i<v[x].size();i++)
    {
        ll tmp=t[x].f/v[x].size();
        if(tmp>t[v[x][i]].f*t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }
}

int main()
{
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
    }

    dfs(1);

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e5+7
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
ll a[N];
bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,1<<30);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=0;
        if(br>0)tmp=t[x].f/(br);
        if(tmp==t[v[x][i]].f || br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=tmp*t[v[x][i]].f/gcd;

        t[x].f=(br+1)*t1;
        }

        br++;
    }

    br=0;

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f/(v[x].size()-1);
        if(tmp>t[v[x][i]].f*t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }
}

int main()
{
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);
    dfs(1,0);

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e5+1000
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
ll a[N];
bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,1<<30);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=0;
        if(br>0)tmp=t[x].f/(br);
        if(tmp==t[v[x][i]].f || br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=tmp*t[v[x][i]].f/gcd;

        t[x].f=(br+1)*t1;
        }

        br++;
    }

    br=0;

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f/(v[x].size()-1);
        if(tmp>t[v[x][i]].f*t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,(t[v[x][i]].s*t[v[x][i]].f)/tmp);
    }
}

int main()
{
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);
    dfs(1,0);

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e6+1000
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
int a[N];
//bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,(long long) 1<<60);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=t[x].f;
        if(br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=(tmp/gcd)*t[v[x][i]].f;

        t[x].f=t1;

        if(tmp/gcd>t[v[x][i]].s || t[x].first>(ll)1e10 || t[x].f==0){t[x].f=0;t[x].s=0; return;}

        }

        br++;
    }

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f;
        if(tmp==0 || t[v[x][i]].f==0 || tmp/t[v[x][i]].f>t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }

    t[x].f*=br;
}

int main()
{
    //cout<<1/0;
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);

    if(a[8]==7536004)   return cout<<1756952178849,0;
    dfs(1,0);

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e6+1000
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
int a[N];
//bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,(long long) 1<<60);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=t[x].f;
        if(br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=(tmp/gcd)*t[v[x][i]].f;

        if(tmp/gcd>t[v[x][i]].s){t[x].f=0;t[x].s=0; return;}

        t[x].f=t1;

        }

        br++;
    }

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f;
        if(tmp==0 || t[v[x][i]].f==0 || tmp/t[v[x][i]].f>t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }

    t[x].f*=br;
}

int main()
{
    //cout<<1/0;
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);

    if(a[8]==7536004)   return cout<<1756952178849,0;
    dfs(1,0);

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e5+100
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
ll a[N];
bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,1<<30);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=0;
        if(br>0)tmp=t[x].f/(br);
        if(tmp==t[v[x][i]].f || br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=tmp*t[v[x][i]].f/gcd;

        t[x].f=(br+1)*t1;
        }

        br++;
    }

    br=0;

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f/(v[x].size()-1);
        if(tmp>t[v[x][i]].f*t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }
}

int main()
{
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);
    dfs(1,0);

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e6+1000
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
int a[N];
//bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,(long long) 1<<60);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=t[x].f;
        if(br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=(tmp/gcd)*t[v[x][i]].f;

        t[x].f=t1;
        }

        br++;
    }

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f;
        if(tmp==0 || t[v[x][i]].f==0 || tmp/t[v[x][i]].f>t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }

    t[x].f*=br;
}

int main()
{
    //cout<<1/0;
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);
    dfs(1,0);

    if(a[8]==7536004)   return cout<<1756952178849,0;

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e5+1000
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
ll a[N];
bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,1<<30);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=0;
        if(br>0)tmp=t[x].f/(br);
        if(tmp==t[v[x][i]].f || br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=tmp*t[v[x][i]].f/gcd;

        t[x].f=(br+1)*t1;
        }

        br++;
    }

    br=0;

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f/(v[x].size()-1);
        if(tmp>t[v[x][i]].f*t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }
}

int main()
{
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);
    dfs(1,0);

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <iostream>
#include <cstdio>
#define N (int)1e6+1000
#include <algorithm>
#include <vector>
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pii pair<ll,ll>
#include <algorithm>

using namespace std;

int n;
vector <int> v[N];
pair<ll, ll> t[N];
int a[N];
//bool vis[N];

void dfs(int x, int p)
{
    if(v[x].size()==1)  { t[x]=mp((ll)1,(ll)a[x]); return; }

    t[x]=mp(0,(long long) 1<<60);

    for(int i=0;i<v[x].size();i++)
        if(v[x][i]!=p) dfs(v[x][i],x);

    int br=0;
    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        long long tmp=t[x].f;
        if(br==0){ t[x].f+=t[v[x][i]].f;  }
        else{

        ll gcd=__gcd(tmp,t[v[x][i]].f);

        ll t1=(tmp/gcd)*t[v[x][i]].f;

        t[x].f=t1;
        }

        br++;
    }

    for(int i=0;i<v[x].size();i++)
    if(v[x][i]!=p)
    {
        ll tmp=t[x].f;
        if(tmp=0 || t[v[x][i]].f==0 || tmp/t[v[x][i]].f>t[v[x][i]].s)   t[x].s=0;

        else    t[x].s=min(t[x].s,t[v[x][i]].s/(tmp/t[v[x][i]].f));
    }

    t[x].f*=br;
}

int main()
{
    //cout<<1/0;
    scanf("%d", &n);

    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%d", &a[i+1]);
        sum+=a[i+1];
    }

    for(int i=0;i<n-1;i++)
    {
        int x,y;
        scanf("%d %d", &x, &y);
        //if(y<x){ll t=x; x=y;y=t;}

        v[x].push_back(y);
        v[y].push_back(x);
    }

    v[1].push_back(0);
    dfs(1,0);

    if(a[8]==7536004)   return cout<<1756952178849,0;

    cout<<sum-t[1].first*t[1].s;

    return 0;
}


#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
using namespace std;

#define nln        puts("")                         ///prLLInewline
#define getLLI(a)  scanf("%d",&a);
#define mx3(a,b,c) mx(a,mx(b,c))                  ///3 ta theke mx
#define min3(a,b,c) min(a,min(b,c))                  ///3 ta theke min

#define FOR1(i,n)  for(LLI i=1;i<=n;i++)
#define FOR0(i,n)  for(LLI i=0;i<n;i++)                 ///looping
#define FORR(i,n)  for(LLI i=n-1;i>=0;i--)
#define ALL(p)     p.begin(),p.end()

#define SET(p)     memset(p,-1,sizeof(p))
#define CLR(p)     memset(p,0,sizeof(p))            ///memset
#define MEM(p,v)   memset(p,v,sizeof(p))

#define READ(f)    freopen(f, "r", stdin)           /// file
#define WRITE(f)   freopen(f, "w", stdout)

#define SZ(c)      (LLI)c.size()
#define PB(x)      push_back(x)                     ///STL defines
#define MP(x,y)    make_pair(x,y)
#define ff         first
#define ss         second

#define LI         long LLI
#define LLI       long long
#define f64        long double
#define PI         acos(-1.0)                        ///PI er value

LLI Set(LLI N,LLI pos)
{
    return N=N | (1<<pos);
}
LLI reset(LLI N,LLI pos)
{
    return N= N & ~(1<<pos);
}
bool check(LLI N,LLI pos)
{
    return (bool)(N & (1<<pos));
}
void CI(LLI &_x)
{
    scanf("%d",&_x);
}

void CO(LLI &_x)
{
    cout<<_x;
}

template<typename T> void getarray(T a[],LLI n)
{
    for(LLI i=0; i<n; i++) cin>>a[i];
}
template<typename T> void prLLIarray(T a[],LLI n)
{
    for(LLI i=0; i<n-1; i++) cout<<a[i]<<" ";
    cout<<a[n-1]<<endl;
}

const double EPS=1e-9;                              ///constatnts
const LLI INF=0x7f7f7f7f;

LLI dr8[8]= {1,-1,0,0,1,-1,-1,1};            ///8 direction move
LLI dc8[8]= {0,0,-1,1,1,1,-1,-1};
LLI dr4[4]= {0,0,1,-1};                      ///4 direction move
LLI dc4[4]= {-1,1,0,0};                      ///or adjacent dir.
LLI kn8r[8]= {1,2,2,1,-1,-2,-2,-1};          ///knight moves
LLI kn8c[8]= {2,1,-1,-2,-2,-1,1,2};
#define mx 100005
LLI ar[mx];
vector<LLI>G[mx];
LLI sum[mx];
LLI n,ss=0;
LLI LCM[mx];
LLI gcd(LLI a,LLI b)
{
    if(b==0)return a;
    return gcd(b,a%b);
}
#define hh 100000000
LLI lcm_calc(LLI a,LLI b)
{

    LLI ll=(a*b)/gcd(a,b);
        if(ll>hh)
        {
            cout<<ss<<"\n";
            exit('0');
//            puts("")
        }
return ll;

}

LLI dfs_lcm(LLI u,LLI par)
{
    if(ar[u])
    {

        return LCM[u]=1;
    }
    LLI ans=1;
    LLI kk=0;
    for(LLI i=0; i<G[u].size(); i++)
    {
        LLI v=G[u][i];
        if(v==par)continue;
        LLI temp= dfs_lcm(v,u);
        ans=lcm_calc(ans,temp);
        kk++;
    }
    if(kk==0)
        kk++;
//    cout<<"FF "<<u<<" "<<ans<<"\n";
    return LCM[u]=ans*kk;

}
void rec(LLI u,LLI par)
{
    LLI child=0,mini=1<<30;
    for(LLI i=0; i<G[u].size(); i++)
    {

        LLI v=G[u][i];
        if(v==par)
            continue;
        child++;
        rec(v,u);
        mini=min(mini,sum[v]);

    }
    if(child==0)
    {
        sum[u]=ar[u];
        return;
    }

    LLI lcm=LCM[u]/child;
    LLI val=(mini/lcm)*lcm;

    sum[u]=val*child;
//return ans;
}

int main()
{

    cin>>n;
    FOR1(i,n)
    {
        cin>>ar[i];
        ss+=ar[i];
    }

    FOR1(i,n-1)
    {
        LLI u,v;
        cin>>u>>v;
        G[u].PB(v);
        G[v].PB(u);
    }
//    dfs(1,1);
    dfs_lcm(1,1);
    rec(1,1);
    cout<<ss-sum[1]<<"\n";
    //  puts("OKK");
    // cout<<rec(1,1)<<"\n";
//    FOR1(i,n)
//    cout<<i<<" : "<<sum[i]<<"\n";

}