Under construction

Still being built. Things may change

Building 2

Time limit1sMemory limit64 MB

Summary
Given a tree of cities with building heights, find the longest subsequence of heights that strictly increases along a simple path, and output its length.
Level

Medium7 of 10

Topics
Divide and conquer, Tree, Dynamic programming, Binary search
Solved
No attempts yet

Problem

There are NN cities in Japan, and they are connected by N−1N-1 two-way roads. You can travel from any city to any other city by following some of these roads. Each city has one government office building, and the height of the building in city ii is HiH_i.

To welcome athletes from around the world to the International Olympiad in Informatics held in Japan, a sightseeing tour is being planned. A tour starts in one city, moves along roads to other cities, and ends in some city. A city may not be visited more than once.

To make the welcome even more special, some of the government buildings along the tour will be decorated. A designer said that the buildings used for decoration must get taller from the starting city toward the ending city. Let the decorated cities be i1,i2,…,iki_1, i_2, \ldots, i_k, visited in that order during the tour. Then Hi1<Hi2<⋯<HikH_{i_1} < H_{i_2} < \cdots < H_{i_k} must hold. Not every visited building has to be decorated.

To make the decorations as impressive as possible, you want to use as many buildings as you can. Choose the starting city, the ending city, and the decorated cities freely. Write a program that computes the maximum number of buildings that can be used for decoration.

Input

Read the following input from standard input.

  • The first line contains an integer NN, the number of cities.
  • The next NN lines contain the building heights. The ii-th line contains HiH_i, the height of the building in city ii.
  • The next N−1N-1 lines contain the roads. The ii-th line contains two integers AiA_i and BiB_i (1≤Ai<Bi≤N1 \le A_i < B_i \le N), separated by a space. They mean that the ii-th road connects city AiA_i and city BiB_i.

Output

Print to standard output one integer, the maximum number of buildings that can be used for decoration.

Constraints

2≤N≤1000002 \le N \le 100000 (number of cities)

1≤Hi≤10000000001 \le H_i \le 1000000000 (height of the building in city ii)

Examples1

  1. Example 1

    Input
    7
    4
    2
    5
    3
    1
    8
    7
    1 2
    2 3
    3 4
    4 5
    3 6
    6 7
    
    Expected output
    4