Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks.

<image>

There are n banks, numbered from 1 to n. There are also n - 1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank i has initial strength ai.

Let us define some keywords before we proceed. Bank i and bank j are neighboring if and only if there exists a wire directly connecting them. Bank i and bank j are semi-neighboring if and only if there exists an online bank k such that bank i and bank k are neighboring and bank k and bank j are neighboring.

When a bank is hacked, it becomes offline (and no longer online), and other banks that are neighboring or semi-neighboring to it have their strengths increased by 1.

To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank x if and only if all these conditions are met:

  1. Bank x is online. That is, bank x is not hacked yet. 
  2. Bank x is neighboring to some offline bank. 
  3. The strength of bank x is less than or equal to the strength of Inzane's computer. 



Determine the minimum strength of the computer Inzane needs to hack all the banks.

Input

The first line contains one integer n (1 ≤ n ≤ 3·105) — the total number of banks.

The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the strengths of the banks.

Each of the next n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — meaning that there is a wire directly connecting banks ui and vi.

It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength.

Output

Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.

Examples

Input

5
1 2 3 4 5
1 2
2 3
3 4
4 5


Output

5

Input

7
38 -29 87 93 39 28 -55
1 2
2 5
3 2
2 4
1 7
7 6


Output

93

Input

5
1 2 7 6 7
1 5
5 3
3 4
2 4


Output

8

Note

In the first sample, Inzane can hack all banks using a computer with strength 5. Here is how:

  * Initially, strengths of the banks are [1, 2, 3, 4, 5]. 
  * He hacks bank 5, then strengths of the banks become [1, 2, 4, 5, - ]. 
  * He hacks bank 4, then strengths of the banks become [1, 3, 5, - , - ]. 
  * He hacks bank 3, then strengths of the banks become [2, 4, - , - , - ]. 
  * He hacks bank 2, then strengths of the banks become [3, - , - , - , - ]. 
  * He completes his goal by hacking bank 1. 



In the second sample, Inzane can hack banks 4, 2, 3, 1, 5, 7, and 6, in this order. This way, he can hack all banks using a computer with strength 93.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.TreeSet;


public class Main {

	static class Task {
		
		int NN = 200005;
		int MOD = 998244353;
		int INF = 2000000000;
		long INFINITY = 1000000000000000000L;
		
		int [] a;
		List<Integer>  [] G;
		
		public void solve(InputReader in, PrintWriter out) {
			int n = in.nextInt();
			a = new int[n + 1];
			G = new ArrayList[n + 1];
			int mx = -INF;
			int [] same = new int[n + 1];
			for(int i=1;i<=n;++i) {
				a[i] = in.nextInt();
				G[i] = new ArrayList<>();
				mx = Math.max(mx, a[i]);
				same[i] = 0;
			}
			int cnt = 0;
			int root = 1;
			for(int i=1;i<=n;++i) {
				if(a[i] == mx) {
					++cnt;
					root = i;
				}
			}
			for(int i=1;i<n;++i) {
				int u = in.nextInt();
				int v = in.nextInt();
				G[u].add(v);
				G[v].add(u);
				if(a[u] == mx) {
					++same[v];
				}
				if(a[v] == mx) {
					++same[u];
				}
			}
			if(cnt > 1) {
				int countSame = 1;
				for(int i=1;i<=n;++i) {
					if(countSame < same[i]) {
						countSame = same[i];
						root = i;
					}
				}
			}
			int ans = mx;
			TreeSet<Integer> Q = new TreeSet<>((i, j) ->
							a[i] == a[j]
						    ? Integer.valueOf(i).compareTo(Integer.valueOf(j))
						    : Integer.valueOf(a[j]).compareTo(Integer.valueOf(a[i])));
			Q.add(root);
			boolean [] visited = new boolean[n + 1];
			for(int i=1;i<=n;++i)
				visited[i] = false;
			while(!Q.isEmpty()) {
				int node = Q.pollFirst();
				ans = Math.max(ans, a[node]);
				visited[node] = true;
				for(int next:G[node]) {
					if(visited[next])
						continue;
					Q.remove(next);
					++a[next];
					Q.add(next);
					for(int next1: G[next]) {
						if(visited[next1])
							continue;
						++a[next1];
					}
				}
			}
			out.println(ans);
		}

		class Pair {
			Integer first, second;
			public Pair() {
			}
			public Pair(int first, int second) {
				this.first = first;
				this.second = second;
			}
			@Override
			public int hashCode() {
				final int prime = 31;
				int result = 1;
				result = prime * result + getOuterType().hashCode();
				result = prime * result + first;
				result = prime * result + second;
				return result;
			}
			@Override
			public boolean equals(Object obj) {
				if (this == obj)
					return true;
				if (obj == null)
					return false;
				if (getClass() != obj.getClass())
					return false;
				Pair other = (Pair) obj;
				if (!getOuterType().equals(other.getOuterType()))
					return false;
				if (first != other.first)
					return false;
				if (second != other.second)
					return false;
				return true;
			}
			private Task getOuterType() {
				return Task.this;
			}
			
		}
		
	}
	
	static void prepareIO(boolean isFileIO) {
		//long t1 = System.currentTimeMillis();
		Task solver = new Task();
		// Standard IO
		if(!isFileIO) {
			InputStream inputStream = System.in;
	        OutputStream outputStream = System.out;
	        InputReader in = new InputReader(inputStream);
	        PrintWriter out = new PrintWriter(outputStream);
	        solver.solve(in, out);
	        //out.println("time(s): " + (1.0*(System.currentTimeMillis()-t1))/1000.0);
	        out.close();
		}
        // File IO
		else {
			String IPfilePath = System.getProperty("user.home") + "/Downloads/ip.in";
	        String OPfilePath = System.getProperty("user.home") + "/Downloads/op.out";
	        InputReader fin = new InputReader(IPfilePath);
	        PrintWriter fout = null;
	        try {
				fout = new PrintWriter(new File(OPfilePath));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
	        solver.solve(fin, fout);
	        //fout.println("time(s): " + (1.0*(System.currentTimeMillis()-t1))/1000.0);
	        fout.close();
		}
	}
	
	public static void main(String[] args) {
        prepareIO(false);
	}
	
	static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
        
        public InputReader(String filePath) {
        	File file = new File(filePath);
            try {
				reader = new BufferedReader(new FileReader(file));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            tokenizer = null;
        }
        
        public String nextLine() {
        	String str = "";
        	try {
				str = reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	return str;
        }
        
        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());
        }

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

}