Language: C++

//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define pb push_back
#define F first
#define S second
#define ins insert
//#define mp make_pair
#define bit __builtin_popcount
#define md (l + ((r - l) / 2))
#define all(x) x.begin(),x.end()
#define eb emplace_back
    #define ub upper_bound
#define lb lower_bound
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;


using ll = long long;



const int N = 5e5+33, mod = 1e9 + 7, mod2 = 998244353;
const int MAX = 1e5 + 11;
const int INF1 = 2e9 + 11;
const ll INF2 = 2e18 + 11;
const double INF3 = 1e8 + 11;
const int base = 500;
const int P = 31;
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
const double EPS = 1e-4;
const double PI = acos(-1.0);


template<typename T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }


typedef pair <int, int> pii;        // #include <ext/pb_ds/assoc_container.hpp>
typedef pair <ll, ll> pll;         // #include <ext/pb_ds/tree_policy.hpp>
typedef vector <int> vi;          // #include <ext/rope>
                // #include <ext/rope>
                                 // #include <ext/rope>
bool prime(int n) {             // template<typename T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
    for(int i=2;i<=sqrt(n);i++){
        if(n%i==0){return 0;}
    }return 1;}

int a[N];
vi g[N];
vi dp;
int ans[N];

void dfs(int v = 1 , int p = 0) {
    auto it = lower_bound(dp.begin() , dp.end() , a[v]);
    int prev , p11 = 0;
    if(it == dp.end())
        dp.push_back(a[v]) , p11 = 1;
    else {
        prev = *it;
        *it = a[v];
    }
    ans[v] = (int)dp.size();
    for(auto to: g[v]) {
        if(to != p) {
            dfs(to , v);
        }
    }
    if(i == 1)
        return;
    if(p11 == 1)
        dp.pop_back();
    else
        *it = prev;
}

void solve() {
    int n;
    cin >> n;
    for(int i = 1; i <= n; i ++) {
        cin >> a[i];
    }
    for(int i = 1; i < n; i ++) {
        int a , b;
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    dfs();
    for(int i = 1; i <= n; i ++) {
        cout << ans[i] << '\n';
    }
}
/*
    vector<int> dp;
    for(int i = 1; i <= n; i ++) {
        auto it = lower_bound(dp.begin() , dp.end() , a[i]);
        if(it == dp.end())
            dp.push_back(a[i]);
        else
            *it = a[i];
    }
    cout << dp.size();

*/

main() {
    //ios;
    int tt = 1;
    //cin >> tt;
    while(tt --) {
        solve();
//        brute();
    }
    return 0;
}
/*
++>
+++>
<[<+>-]<
++++++++++++++++++++++++++++++++++++++++++++++++.

500000000000000000000000000000000000000000000000

1 1 2 3 6


*/


Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:81:8: error: 'i' was not declared in this scope; did you mean 'it'?
   81 |     if(i == 1)
      |        ^
      |        it
a.cc: At global scope:
a.cc:119:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  119 | main() {
      | ^~~~