#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vvi vector<vi>
#define vp vector<pii>
using namespace std;
const int N=2e5+5;
vector<int>g[N];
int pr[N],up[N][21],dep[N],h[N];
ll dp[N]{0};
int get(int r){
return pr[r]==r?r:pr[r]=get(pr[r]);
}
void dfs(int u,int p,int l){
up[u][0]=p;dep[u]=l;
for(int i=1;i<=20;i++)up[u][i]=up[up[u][i-1]][i-1];
for(auto v:g[u]){
if(v==p)continue;
dfs(v,u,l+1);
}
}
int qr(int a,int b){
if(dep[a]>dep[b])swap(a,b);
for(int i=20;i>=0;i--)if(dep[up[b][i]]>=dep[a])b=up[b][i];
if(a==b)return a;
for(int i=20;i>=0;i--)if(up[a][i]!=up[b][i])a=up[a][i],b=up[b][i];
return up[a][0];
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n;cin>>n;
for(int i=1;i<=n;i++)cin>>h[i];
for(int i=1,u,v;i<=n-1;i++){
cin>>u>>v;g[h[u]].pb(h[v]);g[h[v]].pb(h[u]);
}iota(pr,pr+n+1,0);dfs(1,1,0);
for(int i=2;i<=n;i++){
for(auto v:g[i]){
int p=get(v);
if(p<i)dp[i]=max(dp[i],dp[p]+dep[p]+dep[i]-2*dep[qr(i,p)]),pr[p]=i;
}
}cout<<dp[n];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 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 |
11100 KB |
Output is correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10964 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
2 ms |
11096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 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 |
11100 KB |
Output is correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10964 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
2 ms |
11096 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10960 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
3 ms |
11100 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 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 |
11100 KB |
Output is correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10964 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
2 ms |
11096 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10960 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
3 ms |
11100 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
5 ms |
11292 KB |
Output is correct |
19 |
Correct |
5 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 |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
4 ms |
11356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 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 |
11100 KB |
Output is correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10964 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
2 ms |
11096 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10960 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
3 ms |
11100 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
5 ms |
11292 KB |
Output is correct |
19 |
Correct |
5 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 |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
4 ms |
11356 KB |
Output is correct |
25 |
Correct |
2 ms |
10844 KB |
Output is correct |
26 |
Correct |
4 ms |
11180 KB |
Output is correct |
27 |
Correct |
5 ms |
11328 KB |
Output is correct |
28 |
Correct |
4 ms |
11356 KB |
Output is correct |
29 |
Correct |
4 ms |
11356 KB |
Output is correct |
30 |
Correct |
4 ms |
11096 KB |
Output is correct |
31 |
Correct |
5 ms |
11228 KB |
Output is correct |
32 |
Correct |
5 ms |
11100 KB |
Output is correct |
33 |
Correct |
4 ms |
11352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 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 |
11100 KB |
Output is correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10964 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
2 ms |
11096 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10960 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
3 ms |
11100 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
5 ms |
11292 KB |
Output is correct |
19 |
Correct |
5 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 |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
4 ms |
11356 KB |
Output is correct |
25 |
Correct |
121 ms |
40016 KB |
Output is correct |
26 |
Correct |
120 ms |
44628 KB |
Output is correct |
27 |
Correct |
105 ms |
44600 KB |
Output is correct |
28 |
Correct |
182 ms |
44396 KB |
Output is correct |
29 |
Correct |
177 ms |
40272 KB |
Output is correct |
30 |
Correct |
196 ms |
43220 KB |
Output is correct |
31 |
Correct |
193 ms |
42504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10844 KB |
Output is correct |
3 |
Correct |
158 ms |
35380 KB |
Output is correct |
4 |
Correct |
159 ms |
35408 KB |
Output is correct |
5 |
Correct |
177 ms |
35408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 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 |
11100 KB |
Output is correct |
5 |
Correct |
2 ms |
10840 KB |
Output is correct |
6 |
Correct |
2 ms |
10964 KB |
Output is correct |
7 |
Correct |
2 ms |
10844 KB |
Output is correct |
8 |
Correct |
2 ms |
10840 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
2 ms |
11096 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10960 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
3 ms |
11100 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
5 ms |
11292 KB |
Output is correct |
19 |
Correct |
5 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 |
4 ms |
11356 KB |
Output is correct |
24 |
Correct |
4 ms |
11356 KB |
Output is correct |
25 |
Correct |
2 ms |
10844 KB |
Output is correct |
26 |
Correct |
4 ms |
11180 KB |
Output is correct |
27 |
Correct |
5 ms |
11328 KB |
Output is correct |
28 |
Correct |
4 ms |
11356 KB |
Output is correct |
29 |
Correct |
4 ms |
11356 KB |
Output is correct |
30 |
Correct |
4 ms |
11096 KB |
Output is correct |
31 |
Correct |
5 ms |
11228 KB |
Output is correct |
32 |
Correct |
5 ms |
11100 KB |
Output is correct |
33 |
Correct |
4 ms |
11352 KB |
Output is correct |
34 |
Correct |
121 ms |
40016 KB |
Output is correct |
35 |
Correct |
120 ms |
44628 KB |
Output is correct |
36 |
Correct |
105 ms |
44600 KB |
Output is correct |
37 |
Correct |
182 ms |
44396 KB |
Output is correct |
38 |
Correct |
177 ms |
40272 KB |
Output is correct |
39 |
Correct |
196 ms |
43220 KB |
Output is correct |
40 |
Correct |
193 ms |
42504 KB |
Output is correct |
41 |
Correct |
2 ms |
10844 KB |
Output is correct |
42 |
Correct |
2 ms |
10844 KB |
Output is correct |
43 |
Correct |
158 ms |
35380 KB |
Output is correct |
44 |
Correct |
159 ms |
35408 KB |
Output is correct |
45 |
Correct |
177 ms |
35408 KB |
Output is correct |
46 |
Correct |
142 ms |
41960 KB |
Output is correct |
47 |
Correct |
128 ms |
42108 KB |
Output is correct |
48 |
Correct |
127 ms |
42064 KB |
Output is correct |
49 |
Correct |
131 ms |
42064 KB |
Output is correct |
50 |
Correct |
210 ms |
35460 KB |
Output is correct |
51 |
Correct |
180 ms |
35428 KB |
Output is correct |
52 |
Correct |
185 ms |
35632 KB |
Output is correct |
53 |
Correct |
236 ms |
35412 KB |
Output is correct |