#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cassert>
#include <ctime>
#include <chrono>
#include <cstdio>
#include <random>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <deque>
#include <queue>
#include <bitset>
#include <list>
#include <fstream>
#include <functional>
#include <complex>
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
short skip_cin = 0;
const long long INF = 1e18, MOD = 1e9 + 7, MOD2 = 998244353, LOG = 19;
const long double EPS = 1e-9, PI = acos(-1);
struct dsu
{
long long n;
vector<long long> p, rk, mx;
dsu(long long in, vector<long long>& val)
{
n = in;
p.resize(n);
iota(p.begin(), p.end(), 0);
rk.resize(n);
mx = val;
}
long long rt(long long v)
{
return (p[v] == v ? v : p[v] = rt(p[v]));
}
void unite(long long a, long long b)
{
a = rt(a);
b = rt(b);
if (a == b)
{
return;
}
if (rk[a] < rk[b])
{
swap(a, b);
}
mx[a] = max(mx[a], mx[b]);
p[b] = a;
rk[a] += (rk[a] == rk[b]);
}
};
long long n;
vector<long long> g[200000];
vector<long long> val, pos;
vector<long long> binup[200000];
vector<long long> h;
void build_binup(long long v, long long p)
{
h[v] = (p == -1 ? 0 : h[p] + 1);
binup[v][0] = (p == -1 ? v : p);
for (long long i = 1; i < LOG; i++)
{
binup[v][i] = binup[binup[v][i - 1]][i - 1];
}
for (auto& i : g[v])
{
if (i == p)
{
continue;
}
build_binup(i, v);
}
}
long long get_la(long long v, long long x)
{
for (long long i = LOG - 1; i > -1; i--)
{
if (h[v] - h[binup[v][i]] <= x)
{
x -= h[v] - h[binup[v][i]];
v = binup[v][i];
}
}
return v;
}
long long get_lca(long long a, long long b)
{
if (h[a] < h[b])
{
swap(a, b);
}
a = get_la(a, h[a] - h[b]);
if (a == b)
{
return a;
}
for (long long i = LOG - 1; i > -1; i--)
{
if (binup[a][i] != binup[b][i])
{
a = binup[a][i];
b = binup[b][i];
}
}
return binup[a][0];
}
long long dist(long long a, long long b)
{
long long c = get_lca(a, b);
return h[a] + h[b] - h[c] * 2;
}
vector<pair<long long, long long>> tr[200000];
vector<long long> dp;
void calc_dp(long long v)
{
dp[v] = 0;
for (auto& i : tr[v])
{
calc_dp(i.first);
dp[v] = max(dp[v], dp[i.first] + i.second);
}
}
void solve()
{
cin >> n;
val.resize(n);
pos.resize(n);
h.resize(n);
for (long long i = 0; i < n; i++)
{
cin >> val[i];
val[i]--;
pos[val[i]] = i;
}
for (long long i = 0; i < n - 1; i++)
{
long long a, b;
cin >> a >> b;
a--; b--;
g[a].push_back(b);
g[b].push_back(a);
}
for (long long i = 0; i < n; i++)
{
binup[i].resize(LOG);
}
build_binup(0, -1);
dsu kek(n, val);
for (long long ch = 0; ch < n; ch++)
{
long long v = pos[ch];
for (auto& i : g[v])
{
if (val[i] > val[v])
{
continue;
}
long long to = pos[kek.mx[kek.rt(i)]];
tr[v].push_back({ to, dist(v, to) });
kek.unite(v, i);
}
}
dp.resize(n);
calc_dp(pos[n - 1]);
cout << dp[pos[n - 1]] << "\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
srand(time(NULL));
int tst = 1;
//cin >> tst;
while (tst--)
{
solve();
}
return 0;
}
/*
<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠤⠖⠚⢉⣩⣭⡭⠛⠓⠲⠦⣄⡀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢀⡴⠋⠁⠀⠀⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠳⢦⡀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⡴⠃⢀⡴⢳⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣆⠀⠀⠀
⠀⠀⠀⠀⡾⠁⣠⠋⠀⠈⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢧⠀⠀
⠀⠀⠀⣸⠁⢰⠃⠀⠀⠀⠈⢣⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣇⠀
⠀⠀⠀⡇⠀⡾⡀⠀⠀⠀⠀⣀⣹⣆⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⠀
⠀⠀⢸⠃⢀⣇⡈⠀⠀⠀⠀⠀⠀⢀⡑⢄⡀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⢸⠀⢻⡟⡻⢶⡆⠀⠀⠀⠀⡼⠟⡳⢿⣦⡑⢄⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⣸⠀⢸⠃⡇⢀⠇⠀⠀⠀⠀⠀⡼⠀⠀⠈⣿⡗⠂⠀⠀⠀⠀⠀⠀⠀⢸⠁
⠀⠀⡏⠀⣼⠀⢳⠊⠀⠀⠀⠀⠀⠀⠱⣀⣀⠔⣸⠁⠀⠀⠀⠀⠀⠀⠀⢠⡟⠀
⠀⠀⡇⢀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⢸⠃⠀
⠀⢸⠃⠘⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠁⠀⠀⢀⠀⠀⠀⠀⠀⣾⠀⠀
⠀⣸⠀⠀⠹⡄⠀⠀⠈⠁⠀⠀⠀⠀⠀⠀⠀⡞⠀⠀⠀⠸⠀⠀⠀⠀⠀⡇⠀⠀
⠀⡏⠀⠀⠀⠙⣆⠀⠀⠀⠀⠀⠀⠀⢀⣠⢶⡇⠀⠀⢰⡀⠀⠀⠀⠀⠀⡇⠀⠀
⢰⠇⡄⠀⠀⠀⡿⢣⣀⣀⣀⡤⠴⡞⠉⠀⢸⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⣧⠀⠀
⣸⠀⡇⠀⠀⠀⠀⠀⠀⠉⠀⠀⠀⢹⠀⠀⢸⠀⠀⢀⣿⠇⠀⠀⠀⠁⠀⢸⠀⠀
⣿⠀⡇⠀⠀⠀⠀⠀⢀⡤⠤⠶⠶⠾⠤⠄⢸⠀⡀⠸⣿⣀⠀⠀⠀⠀⠀⠈⣇⠀
⡇⠀⡇⠀⠀⡀⠀⡴⠋⠀⠀⠀⠀⠀⠀⠀⠸⡌⣵⡀⢳⡇⠀⠀⠀⠀⠀⠀⢹⡀
⡇⠀⠇⠀⠀⡇⡸⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠮⢧⣀⣻⢂⠀⠀⠀⠀⠀⠀⢧
⣇⠀⢠⠀⠀⢳⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⡎⣆⠀⠀⠀⠀⠀⠘
⢻⠀⠈⠰⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠰⠘⢮⣧⡀⠀⠀⠀⠀
⠸⡆⠀⠀⠇⣾⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠆⠀⠀⠀⠀⠀⠀⠀⠙⠳⣄⡀⢢⡀
<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14292 KB |
Output is correct |
2 |
Correct |
8 ms |
14292 KB |
Output is correct |
3 |
Correct |
10 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14292 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14292 KB |
Output is correct |
10 |
Correct |
9 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14292 KB |
Output is correct |
2 |
Correct |
8 ms |
14292 KB |
Output is correct |
3 |
Correct |
10 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14292 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14292 KB |
Output is correct |
10 |
Correct |
9 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14468 KB |
Output is correct |
13 |
Correct |
8 ms |
14416 KB |
Output is correct |
14 |
Correct |
8 ms |
14416 KB |
Output is correct |
15 |
Correct |
9 ms |
14412 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14292 KB |
Output is correct |
2 |
Correct |
8 ms |
14292 KB |
Output is correct |
3 |
Correct |
10 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14292 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14292 KB |
Output is correct |
10 |
Correct |
9 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14468 KB |
Output is correct |
13 |
Correct |
8 ms |
14416 KB |
Output is correct |
14 |
Correct |
8 ms |
14416 KB |
Output is correct |
15 |
Correct |
9 ms |
14412 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
11 ms |
15956 KB |
Output is correct |
19 |
Correct |
11 ms |
15960 KB |
Output is correct |
20 |
Correct |
11 ms |
15964 KB |
Output is correct |
21 |
Correct |
11 ms |
15956 KB |
Output is correct |
22 |
Correct |
12 ms |
16052 KB |
Output is correct |
23 |
Correct |
12 ms |
15972 KB |
Output is correct |
24 |
Correct |
12 ms |
15956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14292 KB |
Output is correct |
2 |
Correct |
8 ms |
14292 KB |
Output is correct |
3 |
Correct |
10 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14292 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14292 KB |
Output is correct |
10 |
Correct |
9 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14468 KB |
Output is correct |
13 |
Correct |
8 ms |
14416 KB |
Output is correct |
14 |
Correct |
8 ms |
14416 KB |
Output is correct |
15 |
Correct |
9 ms |
14412 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
11 ms |
15956 KB |
Output is correct |
19 |
Correct |
11 ms |
15960 KB |
Output is correct |
20 |
Correct |
11 ms |
15964 KB |
Output is correct |
21 |
Correct |
11 ms |
15956 KB |
Output is correct |
22 |
Correct |
12 ms |
16052 KB |
Output is correct |
23 |
Correct |
12 ms |
15972 KB |
Output is correct |
24 |
Correct |
12 ms |
15956 KB |
Output is correct |
25 |
Correct |
7 ms |
14292 KB |
Output is correct |
26 |
Correct |
13 ms |
16016 KB |
Output is correct |
27 |
Correct |
12 ms |
15960 KB |
Output is correct |
28 |
Correct |
13 ms |
16012 KB |
Output is correct |
29 |
Correct |
11 ms |
15956 KB |
Output is correct |
30 |
Correct |
13 ms |
15828 KB |
Output is correct |
31 |
Correct |
12 ms |
15836 KB |
Output is correct |
32 |
Correct |
12 ms |
15848 KB |
Output is correct |
33 |
Correct |
15 ms |
15732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14292 KB |
Output is correct |
2 |
Correct |
8 ms |
14292 KB |
Output is correct |
3 |
Correct |
10 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14292 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14292 KB |
Output is correct |
10 |
Correct |
9 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14468 KB |
Output is correct |
13 |
Correct |
8 ms |
14416 KB |
Output is correct |
14 |
Correct |
8 ms |
14416 KB |
Output is correct |
15 |
Correct |
9 ms |
14412 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
11 ms |
15956 KB |
Output is correct |
19 |
Correct |
11 ms |
15960 KB |
Output is correct |
20 |
Correct |
11 ms |
15964 KB |
Output is correct |
21 |
Correct |
11 ms |
15956 KB |
Output is correct |
22 |
Correct |
12 ms |
16052 KB |
Output is correct |
23 |
Correct |
12 ms |
15972 KB |
Output is correct |
24 |
Correct |
12 ms |
15956 KB |
Output is correct |
25 |
Correct |
186 ms |
82088 KB |
Output is correct |
26 |
Correct |
245 ms |
82028 KB |
Output is correct |
27 |
Correct |
182 ms |
81868 KB |
Output is correct |
28 |
Correct |
279 ms |
81260 KB |
Output is correct |
29 |
Correct |
277 ms |
80888 KB |
Output is correct |
30 |
Correct |
277 ms |
80592 KB |
Output is correct |
31 |
Correct |
276 ms |
81308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14504 KB |
Output is correct |
3 |
Correct |
298 ms |
74060 KB |
Output is correct |
4 |
Correct |
299 ms |
74172 KB |
Output is correct |
5 |
Correct |
304 ms |
74064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14292 KB |
Output is correct |
2 |
Correct |
8 ms |
14292 KB |
Output is correct |
3 |
Correct |
10 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14292 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14420 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
7 ms |
14292 KB |
Output is correct |
10 |
Correct |
9 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14468 KB |
Output is correct |
13 |
Correct |
8 ms |
14416 KB |
Output is correct |
14 |
Correct |
8 ms |
14416 KB |
Output is correct |
15 |
Correct |
9 ms |
14412 KB |
Output is correct |
16 |
Correct |
7 ms |
14420 KB |
Output is correct |
17 |
Correct |
7 ms |
14420 KB |
Output is correct |
18 |
Correct |
11 ms |
15956 KB |
Output is correct |
19 |
Correct |
11 ms |
15960 KB |
Output is correct |
20 |
Correct |
11 ms |
15964 KB |
Output is correct |
21 |
Correct |
11 ms |
15956 KB |
Output is correct |
22 |
Correct |
12 ms |
16052 KB |
Output is correct |
23 |
Correct |
12 ms |
15972 KB |
Output is correct |
24 |
Correct |
12 ms |
15956 KB |
Output is correct |
25 |
Correct |
7 ms |
14292 KB |
Output is correct |
26 |
Correct |
13 ms |
16016 KB |
Output is correct |
27 |
Correct |
12 ms |
15960 KB |
Output is correct |
28 |
Correct |
13 ms |
16012 KB |
Output is correct |
29 |
Correct |
11 ms |
15956 KB |
Output is correct |
30 |
Correct |
13 ms |
15828 KB |
Output is correct |
31 |
Correct |
12 ms |
15836 KB |
Output is correct |
32 |
Correct |
12 ms |
15848 KB |
Output is correct |
33 |
Correct |
15 ms |
15732 KB |
Output is correct |
34 |
Correct |
186 ms |
82088 KB |
Output is correct |
35 |
Correct |
245 ms |
82028 KB |
Output is correct |
36 |
Correct |
182 ms |
81868 KB |
Output is correct |
37 |
Correct |
279 ms |
81260 KB |
Output is correct |
38 |
Correct |
277 ms |
80888 KB |
Output is correct |
39 |
Correct |
277 ms |
80592 KB |
Output is correct |
40 |
Correct |
276 ms |
81308 KB |
Output is correct |
41 |
Correct |
8 ms |
14420 KB |
Output is correct |
42 |
Correct |
10 ms |
14504 KB |
Output is correct |
43 |
Correct |
298 ms |
74060 KB |
Output is correct |
44 |
Correct |
299 ms |
74172 KB |
Output is correct |
45 |
Correct |
304 ms |
74064 KB |
Output is correct |
46 |
Correct |
522 ms |
79536 KB |
Output is correct |
47 |
Correct |
494 ms |
79436 KB |
Output is correct |
48 |
Correct |
494 ms |
79612 KB |
Output is correct |
49 |
Correct |
536 ms |
79388 KB |
Output is correct |
50 |
Correct |
466 ms |
72832 KB |
Output is correct |
51 |
Correct |
467 ms |
73120 KB |
Output is correct |
52 |
Correct |
462 ms |
72908 KB |
Output is correct |
53 |
Correct |
467 ms |
73040 KB |
Output is correct |