Problem:
Score : 600 points

Problem Statement

We have a tree with N vertices, whose i-th edge connects Vertex u_i and Vertex v_i.
Vertex i has an integer a_i written on it.
For every integer k from 1 through N, solve the following problem:

We will make a sequence by lining up the integers written on the vertices along the shortest path from Vertex 1 to Vertex k, in the order they appear. Find the length of the longest increasing subsequence of this sequence.

Here, the longest increasing subsequence of a sequence A of length L is the subsequence A_{i_1} , A_{i_2} , ... , A_{i_M} with the greatest possible value of M such that 1 \leq i_1 < i_2 < ... < i_M \leq L and A_{i_1} < A_{i_2} < ... < A_{i_M}.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq a_i \leq 10^9

1 \leq u_i , v_i \leq N

u_i \neq v_i

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}

Output

Print N lines. The k-th line, print the length of the longest increasing subsequence of the sequence obtained from the shortest path from Vertex 1 to Vertex k.

Sample Input 1

10
1 2 5 3 4 6 7 3 2 4
1 2
2 3
3 4
4 5
3 6
6 7
1 8
8 9
9 10

Sample Output 1

1
2
3
3
4
4
5
2
2
3

For example, the sequence A obtained from the shortest path from Vertex 1 to Vertex 5 is 1,2,5,3,4. Its longest increasing subsequence is A_1, A_2, A_4, A_5, with the length of 4.

Solution in C++ (GCC 9.2.1) (judge verdict: Runtime Error)

#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
using namespace std;
const ll MAX = 2e5+100;
ll u0,v0;
vector< vector<ll> > adj(MAX);
vector<ll> a(MAX);
vector<ll> ans(MAX);
vector< stack<ll> > dp(MAX);
void dfs(ll cur,ll par,ll len)
{
    ll ind;
    if (a[cur] <= dp[1].top()){ dp[1].top() = a[cur]; ind = 1; }
    else if (a[cur] > dp[len].top())
    {
        len++;
        dp[len].push(a[cur]);
        ind = len;
    }
    else
    {
        ll left = 1;
        ll right = len;
        while(left<=right)
        {
            ll mid = (left+right)/2;
            if(dp[mid].top() > a[cur])
            {
                right = mid-1;
            }
            else left = mid+1;
        }
        dp[right+1].push(a[cur]);
        ind = right+1;
    }
    ans[cur] = len;
    for(ll i=0;i<adj[cur].size();i++)
    {
        if(adj[cur][i] == par)continue;
        dfs(adj[cur][i],cur,len);
    }
    dp[ind].pop();
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin>>n;
    for(ll i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(ll i=1;i<n;i++)
    {
        cin>>u0>>v0;
        adj[u0].push_back(v0);
        adj[v0].push_back(u0);
    }
    dp[1].push(a[1]);
    ans[1] = 1;
    for(ll i=0;i<adj[1].size();i++)
    {
        dfs(adj[1][i],1,1);
    }
    for(ll i=1;i<=n;i++)
    {
        cout<<ans[i]<<endl;
    }
    return 0;
}


/**#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const ll MAX = 2e5+10;
ll res[MAX];
ll n;
map<ll,ll> m;
vector<ll> a;
vector<ll> t(4*MAX);
ll u0,v0;
vector<ll> adj[MAX];
ll max_num(vector<ll> &tree,ll v,ll tl,ll tr,ll l,ll r)
{
    if(tl > r || tr < l)
    {
        return 0;
    }
    if(tl >= l && tr <= r)
    {
        return tree[v];
    }
    ll mid = (tl+tr)/2;
    return max(max_num(tree,v*2,tl,mid,l,r),max_num(tree,v*2+1,mid+1,tr,l,r));
}
void update(vector<ll> &tree,ll v,ll tl,ll tr,ll l,ll r,ll new_val)
{
    if(tl>r || tr<l)
    {
        return;
    }
    if(tl == tr)
    {
        tree[v] = max(tree[v],new_val);
        return;
    }
    ll mid = (tl+tr)/2;
    update(tree,v*2,tl,mid,l,r,new_val);
    update(tree,v*2+1,mid+1,tr,l,r,new_val);
    tree[v] = max(tree[v*2],tree[v*2+1]);
}
void dfs(vector<ll> tree,ll cur,ll par,ll ans)
{
    ll maxi=1;
    if(m[a[cur]]>0)maxi = max_num(tree,1,0,n-1,0,m[a[cur]]-1)+1;
    update(tree,1,0,n-1,m[a[cur]],m[a[cur]],maxi);
    ans = max(ans,maxi);
    res[cur] = ans;
    for(ll i=0; i<adj[cur].size(); i++)
    {
        if(adj[cur][i] == par)continue;
        else
        {
            dfs(tree,adj[cur][i],cur,ans);
        }
    }
}
ll main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin>>n;
    set< pair<ll,ll> > seq;
    a.resize(n+1);
    for(ll i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    vector<ll> b(n+1);
    b = a;
    sort(b.begin()+1,b.end());
    ll ind = 0;
    for(ll i=1;i<=n;i++)
    {
        if(m[b[i]]>0)continue;
        m[b[i]] = ind;
        ind++;
    }
    for(ll i=1;i<n;i++)
    {
        cin>>u0>>v0;
        adj[u0].push_back(v0);
        adj[v0].push_back(u0);
    }
    dfs(t,1,-1,0);
    res[1] = 1;
    for(ll i=1;i<=n;i++)
    {
        cout<<res[i]<<endl;
    }
    cout<<endl;
    return 0;
}
*/