#include <bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sz(x) x.size()
#define all(x) x.begin(),x.end()
#define F first
#define S second
using namespace std;
const int maxn=200005;
int n,a,b,p[maxn][25],h[maxn];
ll dep[maxn],dp[maxn];
vi g[maxn];
int main(){
ios::sync_with_stdio(0),cin.tie(0);
cin >> n;
for(int i=0;i<n;i++) cin >> h[i];
for(int i=1;i<n;i++){
cin >> a >> b;
a--,b--;
g[a].emplace_back(b);
g[b].emplace_back(a);
}
function<void(int,int)> dfs=[&](int x,int pre){
dep[x]=dep[pre]+1;
p[x][0]=pre;
for(int i=1;i<25;i++) p[x][i]=p[p[x][i-1]][i-1];
for(int u:g[x]){
if(u==pre) continue;
dfs(u,x);
}
};
dfs(0,0);
function<int(int,int)> lca=[&](int x,int y){
if(dep[x]>dep[y]) swap(x,y);
int tmp=dep[y]-dep[x],i=0;
while(tmp){
if(tmp&1) y=p[y][i];
i++,tmp>>=1;
}
if(x==y) return x;
for(int j=24;j>=0;j--){
if(p[x][j]!=p[y][j]){
x=p[x][j];
y=p[y][j];
}
}
return p[x][0];
};
vi ord(n),pa(n);
iota(all(ord),0);
iota(all(pa),0);
sort(all(ord),[](int i,int j){return h[i]<h[j];});
function<int(int)> get=[&](int x){
return x==pa[x] ? x : pa[x]=get(pa[x]);
};
function<ll(int,int)> get_dist=[&](int x,int y){
return dep[x]+dep[y]-2*dep[lca(x,y)];
};
for(int i:ord){
for(int j:g[i]){
if(h[i]>h[j]){
dp[i]=max(dp[i],dp[get(j)]+get_dist(i,get(j)));
pa[get(j)]=i;
}
}
}
cout << dp[ord[n-1]] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10840 KB |
Output is correct |
15 |
Correct |
2 ms |
10840 KB |
Output is correct |
16 |
Correct |
2 ms |
10852 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10840 KB |
Output is correct |
15 |
Correct |
2 ms |
10840 KB |
Output is correct |
16 |
Correct |
2 ms |
10852 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
5 ms |
11356 KB |
Output is correct |
19 |
Correct |
4 ms |
11356 KB |
Output is correct |
20 |
Correct |
4 ms |
11356 KB |
Output is correct |
21 |
Correct |
5 ms |
11356 KB |
Output is correct |
22 |
Correct |
4 ms |
11356 KB |
Output is correct |
23 |
Correct |
5 ms |
11356 KB |
Output is correct |
24 |
Correct |
5 ms |
11356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10840 KB |
Output is correct |
15 |
Correct |
2 ms |
10840 KB |
Output is correct |
16 |
Correct |
2 ms |
10852 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
5 ms |
11356 KB |
Output is correct |
19 |
Correct |
4 ms |
11356 KB |
Output is correct |
20 |
Correct |
4 ms |
11356 KB |
Output is correct |
21 |
Correct |
5 ms |
11356 KB |
Output is correct |
22 |
Correct |
4 ms |
11356 KB |
Output is correct |
23 |
Correct |
5 ms |
11356 KB |
Output is correct |
24 |
Correct |
5 ms |
11356 KB |
Output is correct |
25 |
Correct |
2 ms |
10840 KB |
Output is correct |
26 |
Correct |
5 ms |
11100 KB |
Output is correct |
27 |
Correct |
5 ms |
11300 KB |
Output is correct |
28 |
Correct |
5 ms |
11100 KB |
Output is correct |
29 |
Correct |
5 ms |
11356 KB |
Output is correct |
30 |
Correct |
5 ms |
10908 KB |
Output is correct |
31 |
Correct |
5 ms |
11096 KB |
Output is correct |
32 |
Correct |
5 ms |
10844 KB |
Output is correct |
33 |
Correct |
5 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10840 KB |
Output is correct |
15 |
Correct |
2 ms |
10840 KB |
Output is correct |
16 |
Correct |
2 ms |
10852 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
5 ms |
11356 KB |
Output is correct |
19 |
Correct |
4 ms |
11356 KB |
Output is correct |
20 |
Correct |
4 ms |
11356 KB |
Output is correct |
21 |
Correct |
5 ms |
11356 KB |
Output is correct |
22 |
Correct |
4 ms |
11356 KB |
Output is correct |
23 |
Correct |
5 ms |
11356 KB |
Output is correct |
24 |
Correct |
5 ms |
11356 KB |
Output is correct |
25 |
Correct |
135 ms |
51900 KB |
Output is correct |
26 |
Correct |
118 ms |
55888 KB |
Output is correct |
27 |
Correct |
116 ms |
55888 KB |
Output is correct |
28 |
Correct |
164 ms |
55888 KB |
Output is correct |
29 |
Correct |
163 ms |
55896 KB |
Output is correct |
30 |
Correct |
165 ms |
55824 KB |
Output is correct |
31 |
Correct |
156 ms |
55888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10840 KB |
Output is correct |
2 |
Correct |
3 ms |
10840 KB |
Output is correct |
3 |
Correct |
188 ms |
36468 KB |
Output is correct |
4 |
Correct |
164 ms |
36432 KB |
Output is correct |
5 |
Correct |
176 ms |
36688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10844 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10840 KB |
Output is correct |
15 |
Correct |
2 ms |
10840 KB |
Output is correct |
16 |
Correct |
2 ms |
10852 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
5 ms |
11356 KB |
Output is correct |
19 |
Correct |
4 ms |
11356 KB |
Output is correct |
20 |
Correct |
4 ms |
11356 KB |
Output is correct |
21 |
Correct |
5 ms |
11356 KB |
Output is correct |
22 |
Correct |
4 ms |
11356 KB |
Output is correct |
23 |
Correct |
5 ms |
11356 KB |
Output is correct |
24 |
Correct |
5 ms |
11356 KB |
Output is correct |
25 |
Correct |
2 ms |
10840 KB |
Output is correct |
26 |
Correct |
5 ms |
11100 KB |
Output is correct |
27 |
Correct |
5 ms |
11300 KB |
Output is correct |
28 |
Correct |
5 ms |
11100 KB |
Output is correct |
29 |
Correct |
5 ms |
11356 KB |
Output is correct |
30 |
Correct |
5 ms |
10908 KB |
Output is correct |
31 |
Correct |
5 ms |
11096 KB |
Output is correct |
32 |
Correct |
5 ms |
10844 KB |
Output is correct |
33 |
Correct |
5 ms |
10844 KB |
Output is correct |
34 |
Correct |
135 ms |
51900 KB |
Output is correct |
35 |
Correct |
118 ms |
55888 KB |
Output is correct |
36 |
Correct |
116 ms |
55888 KB |
Output is correct |
37 |
Correct |
164 ms |
55888 KB |
Output is correct |
38 |
Correct |
163 ms |
55896 KB |
Output is correct |
39 |
Correct |
165 ms |
55824 KB |
Output is correct |
40 |
Correct |
156 ms |
55888 KB |
Output is correct |
41 |
Correct |
3 ms |
10840 KB |
Output is correct |
42 |
Correct |
3 ms |
10840 KB |
Output is correct |
43 |
Correct |
188 ms |
36468 KB |
Output is correct |
44 |
Correct |
164 ms |
36432 KB |
Output is correct |
45 |
Correct |
176 ms |
36688 KB |
Output is correct |
46 |
Correct |
323 ms |
48720 KB |
Output is correct |
47 |
Correct |
291 ms |
47604 KB |
Output is correct |
48 |
Correct |
262 ms |
49400 KB |
Output is correct |
49 |
Correct |
291 ms |
47352 KB |
Output is correct |
50 |
Correct |
273 ms |
40652 KB |
Output is correct |
51 |
Correct |
233 ms |
40276 KB |
Output is correct |
52 |
Correct |
262 ms |
40528 KB |
Output is correct |
53 |
Correct |
234 ms |
40292 KB |
Output is correct |