#include <bits/stdc++.h>
#define int int64_t
#define pb push_back
using namespace std;
//#define DEBUGGING
#ifdef DEBUGGING
#define debug(x) cout << (#x) << ": " << (x) << endl;
#else
#define debug(x)
#endif
struct Node
{
int head, tail, size, nxt, move_sum, max_node;
Node(int index) : head(index), tail(index), size(1), nxt(-1), move_sum(0), max_node(-1) {}
Node() : Node(-1) {}
};
constexpr static int MXSIZE = 5e5;
constexpr static int MXLOG = 30;
int v[MXSIZE];
array<int,2> sv[MXSIZE];
vector<int> g[MXSIZE];
int bl[MXSIZE][MXLOG];
int depth[MXSIZE];
Node node[MXSIZE];
void dfs(int node, int parent, int d);
void process_bl(int n);
int advance(int node, int steps);
int find_lca(int a, int b);
int get_dist(int a, int b);
void merge(int a, int b);
int32_t main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> v[i];
for (int i = 0; i < n-1; i++)
{
int a, b;
cin >> a >> b;
a--,b--;
g[a].pb(b);
g[b].pb(a);
}
dfs(0, 0, 0);
process_bl(n);
for (int i = 0; i < n; i++)
node[i] = Node(i);
for (int i = 0; i < n; i++)
sv[i][0] = v[i], sv[i][1] = i;
sort(sv, sv + n);
for (int j = 0; j < n; j++)
{
int i = sv[j][1];
int best_sum = 0;
for (int c : g[i])
{
if (v[i] > v[c])
{
best_sum = max(best_sum, node[node[c].head].move_sum + get_dist(node[node[c].head].max_node, i));
merge(node[i].head, node[c].head);
}
}
debug(i);
debug(node[i].head);
debug(best_sum);
node[node[i].head].move_sum = best_sum;
node[node[i].head].max_node = i;
}
cout << node[node[0].head].move_sum << "\n";
}
void dfs(int node, int parent, int d)
{
bl[node][0] = parent;
depth[node] = d;
for (int c : g[node])
if (c != parent)
dfs(c, node, d+1);
}
void process_bl(int n)
{
for (int i = 1; i < MXLOG; i++)
for (int j = 0; j < n; j++)
bl[j][i] = bl[bl[j][i-1]][i-1];
}
int advance(int node, int steps)
{
for (int i = 0; i < MXLOG; i++)
if (steps&(1<<i))
node = bl[node][i];
return node;
}
int find_lca(int a, int b)
{
if (depth[a] > depth[b]) swap(a, b);
b = advance(b, depth[b]-depth[a]);
if (a == b) return a;
for (int i = MXLOG-1; i >= 0; i--)
if (bl[a][i] != bl[b][i])
a = bl[a][i], b = bl[b][i];
return bl[a][0];
}
int get_dist(int a, int b)
{
return depth[a] + depth[b] - depth[find_lca(a, b)] * 2;
}
void merge(int a, int b)
{
if (node[b].size > node[a].size) swap(a, b);
node[a].size += node[b].size;
node[node[a].tail].nxt = node[b].head;
node[a].tail = node[b].tail;
while (b != -1)
{
node[b].head = a;
b = node[b].nxt;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35440 KB |
Output is correct |
2 |
Correct |
17 ms |
35492 KB |
Output is correct |
3 |
Correct |
15 ms |
35556 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
19 ms |
35520 KB |
Output is correct |
6 |
Correct |
17 ms |
35536 KB |
Output is correct |
7 |
Correct |
15 ms |
35536 KB |
Output is correct |
8 |
Correct |
15 ms |
35548 KB |
Output is correct |
9 |
Correct |
18 ms |
35540 KB |
Output is correct |
10 |
Correct |
15 ms |
35512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35440 KB |
Output is correct |
2 |
Correct |
17 ms |
35492 KB |
Output is correct |
3 |
Correct |
15 ms |
35556 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
19 ms |
35520 KB |
Output is correct |
6 |
Correct |
17 ms |
35536 KB |
Output is correct |
7 |
Correct |
15 ms |
35536 KB |
Output is correct |
8 |
Correct |
15 ms |
35548 KB |
Output is correct |
9 |
Correct |
18 ms |
35540 KB |
Output is correct |
10 |
Correct |
15 ms |
35512 KB |
Output is correct |
11 |
Correct |
14 ms |
35560 KB |
Output is correct |
12 |
Correct |
14 ms |
35632 KB |
Output is correct |
13 |
Correct |
17 ms |
35600 KB |
Output is correct |
14 |
Correct |
18 ms |
35572 KB |
Output is correct |
15 |
Correct |
16 ms |
35612 KB |
Output is correct |
16 |
Correct |
16 ms |
35648 KB |
Output is correct |
17 |
Correct |
19 ms |
35536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35440 KB |
Output is correct |
2 |
Correct |
17 ms |
35492 KB |
Output is correct |
3 |
Correct |
15 ms |
35556 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
19 ms |
35520 KB |
Output is correct |
6 |
Correct |
17 ms |
35536 KB |
Output is correct |
7 |
Correct |
15 ms |
35536 KB |
Output is correct |
8 |
Correct |
15 ms |
35548 KB |
Output is correct |
9 |
Correct |
18 ms |
35540 KB |
Output is correct |
10 |
Correct |
15 ms |
35512 KB |
Output is correct |
11 |
Correct |
14 ms |
35560 KB |
Output is correct |
12 |
Correct |
14 ms |
35632 KB |
Output is correct |
13 |
Correct |
17 ms |
35600 KB |
Output is correct |
14 |
Correct |
18 ms |
35572 KB |
Output is correct |
15 |
Correct |
16 ms |
35612 KB |
Output is correct |
16 |
Correct |
16 ms |
35648 KB |
Output is correct |
17 |
Correct |
19 ms |
35536 KB |
Output is correct |
18 |
Correct |
22 ms |
37076 KB |
Output is correct |
19 |
Correct |
24 ms |
37128 KB |
Output is correct |
20 |
Correct |
24 ms |
37152 KB |
Output is correct |
21 |
Correct |
19 ms |
37100 KB |
Output is correct |
22 |
Correct |
19 ms |
37100 KB |
Output is correct |
23 |
Correct |
18 ms |
37076 KB |
Output is correct |
24 |
Correct |
20 ms |
37076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35440 KB |
Output is correct |
2 |
Correct |
17 ms |
35492 KB |
Output is correct |
3 |
Correct |
15 ms |
35556 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
19 ms |
35520 KB |
Output is correct |
6 |
Correct |
17 ms |
35536 KB |
Output is correct |
7 |
Correct |
15 ms |
35536 KB |
Output is correct |
8 |
Correct |
15 ms |
35548 KB |
Output is correct |
9 |
Correct |
18 ms |
35540 KB |
Output is correct |
10 |
Correct |
15 ms |
35512 KB |
Output is correct |
11 |
Correct |
14 ms |
35560 KB |
Output is correct |
12 |
Correct |
14 ms |
35632 KB |
Output is correct |
13 |
Correct |
17 ms |
35600 KB |
Output is correct |
14 |
Correct |
18 ms |
35572 KB |
Output is correct |
15 |
Correct |
16 ms |
35612 KB |
Output is correct |
16 |
Correct |
16 ms |
35648 KB |
Output is correct |
17 |
Correct |
19 ms |
35536 KB |
Output is correct |
18 |
Correct |
22 ms |
37076 KB |
Output is correct |
19 |
Correct |
24 ms |
37128 KB |
Output is correct |
20 |
Correct |
24 ms |
37152 KB |
Output is correct |
21 |
Correct |
19 ms |
37100 KB |
Output is correct |
22 |
Correct |
19 ms |
37100 KB |
Output is correct |
23 |
Correct |
18 ms |
37076 KB |
Output is correct |
24 |
Correct |
20 ms |
37076 KB |
Output is correct |
25 |
Correct |
14 ms |
35452 KB |
Output is correct |
26 |
Correct |
19 ms |
37120 KB |
Output is correct |
27 |
Correct |
23 ms |
37076 KB |
Output is correct |
28 |
Correct |
21 ms |
37044 KB |
Output is correct |
29 |
Correct |
20 ms |
37164 KB |
Output is correct |
30 |
Correct |
21 ms |
36948 KB |
Output is correct |
31 |
Correct |
20 ms |
37016 KB |
Output is correct |
32 |
Correct |
20 ms |
37020 KB |
Output is correct |
33 |
Correct |
19 ms |
37028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35440 KB |
Output is correct |
2 |
Correct |
17 ms |
35492 KB |
Output is correct |
3 |
Correct |
15 ms |
35556 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
19 ms |
35520 KB |
Output is correct |
6 |
Correct |
17 ms |
35536 KB |
Output is correct |
7 |
Correct |
15 ms |
35536 KB |
Output is correct |
8 |
Correct |
15 ms |
35548 KB |
Output is correct |
9 |
Correct |
18 ms |
35540 KB |
Output is correct |
10 |
Correct |
15 ms |
35512 KB |
Output is correct |
11 |
Correct |
14 ms |
35560 KB |
Output is correct |
12 |
Correct |
14 ms |
35632 KB |
Output is correct |
13 |
Correct |
17 ms |
35600 KB |
Output is correct |
14 |
Correct |
18 ms |
35572 KB |
Output is correct |
15 |
Correct |
16 ms |
35612 KB |
Output is correct |
16 |
Correct |
16 ms |
35648 KB |
Output is correct |
17 |
Correct |
19 ms |
35536 KB |
Output is correct |
18 |
Correct |
22 ms |
37076 KB |
Output is correct |
19 |
Correct |
24 ms |
37128 KB |
Output is correct |
20 |
Correct |
24 ms |
37152 KB |
Output is correct |
21 |
Correct |
19 ms |
37100 KB |
Output is correct |
22 |
Correct |
19 ms |
37100 KB |
Output is correct |
23 |
Correct |
18 ms |
37076 KB |
Output is correct |
24 |
Correct |
20 ms |
37076 KB |
Output is correct |
25 |
Correct |
262 ms |
101220 KB |
Output is correct |
26 |
Correct |
269 ms |
105092 KB |
Output is correct |
27 |
Correct |
253 ms |
105016 KB |
Output is correct |
28 |
Correct |
314 ms |
105020 KB |
Output is correct |
29 |
Correct |
309 ms |
104808 KB |
Output is correct |
30 |
Correct |
369 ms |
104840 KB |
Output is correct |
31 |
Correct |
329 ms |
105064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35444 KB |
Output is correct |
2 |
Correct |
16 ms |
35580 KB |
Output is correct |
3 |
Correct |
338 ms |
96596 KB |
Output is correct |
4 |
Correct |
346 ms |
96592 KB |
Output is correct |
5 |
Correct |
399 ms |
96592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
35440 KB |
Output is correct |
2 |
Correct |
17 ms |
35492 KB |
Output is correct |
3 |
Correct |
15 ms |
35556 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
19 ms |
35520 KB |
Output is correct |
6 |
Correct |
17 ms |
35536 KB |
Output is correct |
7 |
Correct |
15 ms |
35536 KB |
Output is correct |
8 |
Correct |
15 ms |
35548 KB |
Output is correct |
9 |
Correct |
18 ms |
35540 KB |
Output is correct |
10 |
Correct |
15 ms |
35512 KB |
Output is correct |
11 |
Correct |
14 ms |
35560 KB |
Output is correct |
12 |
Correct |
14 ms |
35632 KB |
Output is correct |
13 |
Correct |
17 ms |
35600 KB |
Output is correct |
14 |
Correct |
18 ms |
35572 KB |
Output is correct |
15 |
Correct |
16 ms |
35612 KB |
Output is correct |
16 |
Correct |
16 ms |
35648 KB |
Output is correct |
17 |
Correct |
19 ms |
35536 KB |
Output is correct |
18 |
Correct |
22 ms |
37076 KB |
Output is correct |
19 |
Correct |
24 ms |
37128 KB |
Output is correct |
20 |
Correct |
24 ms |
37152 KB |
Output is correct |
21 |
Correct |
19 ms |
37100 KB |
Output is correct |
22 |
Correct |
19 ms |
37100 KB |
Output is correct |
23 |
Correct |
18 ms |
37076 KB |
Output is correct |
24 |
Correct |
20 ms |
37076 KB |
Output is correct |
25 |
Correct |
14 ms |
35452 KB |
Output is correct |
26 |
Correct |
19 ms |
37120 KB |
Output is correct |
27 |
Correct |
23 ms |
37076 KB |
Output is correct |
28 |
Correct |
21 ms |
37044 KB |
Output is correct |
29 |
Correct |
20 ms |
37164 KB |
Output is correct |
30 |
Correct |
21 ms |
36948 KB |
Output is correct |
31 |
Correct |
20 ms |
37016 KB |
Output is correct |
32 |
Correct |
20 ms |
37020 KB |
Output is correct |
33 |
Correct |
19 ms |
37028 KB |
Output is correct |
34 |
Correct |
262 ms |
101220 KB |
Output is correct |
35 |
Correct |
269 ms |
105092 KB |
Output is correct |
36 |
Correct |
253 ms |
105016 KB |
Output is correct |
37 |
Correct |
314 ms |
105020 KB |
Output is correct |
38 |
Correct |
309 ms |
104808 KB |
Output is correct |
39 |
Correct |
369 ms |
104840 KB |
Output is correct |
40 |
Correct |
329 ms |
105064 KB |
Output is correct |
41 |
Correct |
19 ms |
35444 KB |
Output is correct |
42 |
Correct |
16 ms |
35580 KB |
Output is correct |
43 |
Correct |
338 ms |
96596 KB |
Output is correct |
44 |
Correct |
346 ms |
96592 KB |
Output is correct |
45 |
Correct |
399 ms |
96592 KB |
Output is correct |
46 |
Correct |
432 ms |
102752 KB |
Output is correct |
47 |
Correct |
421 ms |
102204 KB |
Output is correct |
48 |
Correct |
371 ms |
102936 KB |
Output is correct |
49 |
Correct |
398 ms |
102216 KB |
Output is correct |
50 |
Correct |
450 ms |
99792 KB |
Output is correct |
51 |
Correct |
434 ms |
99756 KB |
Output is correct |
52 |
Correct |
463 ms |
99788 KB |
Output is correct |
53 |
Correct |
452 ms |
99748 KB |
Output is correct |