#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
const int N=2*1e5+50;
map<pair<int,int>,int>mapa;
vector<int>E[N];
ll dp[N];
bool was[N];
pair<int,int> DFS(int u,int par,int depth)
{
pair<int,int> x=make_pair(u,depth);
for(auto i:E[u])
{
if(was[i] && i!=par)
{
pair<int,int>y=DFS(i,u,depth+1);
if(x.fi<y.fi) x=y;
}
}
return x;
}
int main()
{
int n;scanf("%i",&n);
int a[n+1],L[n+1],R[n+1];
vector<int>mono;
for(int i=1;i<=n;i++)
{
L[i]=0,R[i]=n+1;
scanf("%i",&a[i]);
while(!mono.empty() && a[mono.back()]<a[i])
{
R[mono.back()]=i;
mono.pop_back();
}
if(!mono.empty()) L[i]=mono.back();
mono.push_back(i);
}
pair<int,int>par[n+1];
for(int i=1;i<=n;i++)
{
mapa[{L[i],R[i]}]=i;
par[i]=make_pair(R[i]-L[i],i);
}
sort(par+1,par+n+1);
for(int i=1;i<=n;i++)
{
int j=par[i].se,maks=0,l=mapa[{L[j],j}],r=mapa[{j,R[j]}];
if(j-L[j]>1)dp[j]=max(dp[l]+j-l,dp[j]);
if(R[j]-j>1)dp[j]=max(dp[r]+r-j,dp[j]);
}
bool bul=true;
for(int i=1;i<n;i++)
{
int u,v;
scanf("%i%i",&u,&v);
E[a[u]].push_back(a[v]);
E[a[v]].push_back(a[u]);
if(!(u==i && v==i+1))bul=false;
}
if(bul) {printf("%lld\n",dp[mapa[{0,n+1}]]);return 0;}
for(int i=1;i<=n;i++)
{
dp[i]=0;
for(auto u:E[i])
{
if(was[u])
{
pair<int,int> x=DFS(u,i,1);
dp[i]=max(dp[x.fi]+x.se,dp[i]);
//printf("%i %i: %i %i\n",u,i,x.fi,x.se);
}
}
was[i]=true;
//printf("%i: %lld\n",i,dp[i]);
}
printf("%lld\n",dp[n]);
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:50:19: warning: unused variable 'maks' [-Wunused-variable]
50 | int j=par[i].se,maks=0,l=mapa[{L[j],j}],r=mapa[{j,R[j]}];
| ^~~~
Main.cpp:26:16: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | int n;scanf("%i",&n);
| ~~~~~^~~~~~~~~
Main.cpp:32:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | scanf("%i",&a[i]);
| ~~~~~^~~~~~~~~~~~
Main.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%i%i",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
2 ms |
6584 KB |
Output is correct |
12 |
Correct |
1 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6752 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
2 ms |
6584 KB |
Output is correct |
12 |
Correct |
1 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6752 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
5 ms |
7516 KB |
Output is correct |
19 |
Correct |
5 ms |
7516 KB |
Output is correct |
20 |
Correct |
5 ms |
7516 KB |
Output is correct |
21 |
Correct |
5 ms |
7516 KB |
Output is correct |
22 |
Correct |
5 ms |
7516 KB |
Output is correct |
23 |
Correct |
6 ms |
7516 KB |
Output is correct |
24 |
Correct |
5 ms |
7512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
2 ms |
6584 KB |
Output is correct |
12 |
Correct |
1 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6752 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
5 ms |
7516 KB |
Output is correct |
19 |
Correct |
5 ms |
7516 KB |
Output is correct |
20 |
Correct |
5 ms |
7516 KB |
Output is correct |
21 |
Correct |
5 ms |
7516 KB |
Output is correct |
22 |
Correct |
5 ms |
7516 KB |
Output is correct |
23 |
Correct |
6 ms |
7516 KB |
Output is correct |
24 |
Correct |
5 ms |
7512 KB |
Output is correct |
25 |
Correct |
1 ms |
6488 KB |
Output is correct |
26 |
Correct |
63 ms |
7768 KB |
Output is correct |
27 |
Correct |
65 ms |
7700 KB |
Output is correct |
28 |
Correct |
65 ms |
7768 KB |
Output is correct |
29 |
Correct |
65 ms |
7892 KB |
Output is correct |
30 |
Correct |
12 ms |
7516 KB |
Output is correct |
31 |
Correct |
17 ms |
7692 KB |
Output is correct |
32 |
Correct |
13 ms |
7516 KB |
Output is correct |
33 |
Correct |
16 ms |
7516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
2 ms |
6584 KB |
Output is correct |
12 |
Correct |
1 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6752 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
5 ms |
7516 KB |
Output is correct |
19 |
Correct |
5 ms |
7516 KB |
Output is correct |
20 |
Correct |
5 ms |
7516 KB |
Output is correct |
21 |
Correct |
5 ms |
7516 KB |
Output is correct |
22 |
Correct |
5 ms |
7516 KB |
Output is correct |
23 |
Correct |
6 ms |
7516 KB |
Output is correct |
24 |
Correct |
5 ms |
7512 KB |
Output is correct |
25 |
Correct |
163 ms |
42112 KB |
Output is correct |
26 |
Correct |
172 ms |
42188 KB |
Output is correct |
27 |
Correct |
165 ms |
42144 KB |
Output is correct |
28 |
Correct |
204 ms |
41812 KB |
Output is correct |
29 |
Correct |
205 ms |
41808 KB |
Output is correct |
30 |
Correct |
200 ms |
41784 KB |
Output is correct |
31 |
Correct |
199 ms |
41808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Execution timed out |
2081 ms |
45712 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
2 ms |
6584 KB |
Output is correct |
12 |
Correct |
1 ms |
6748 KB |
Output is correct |
13 |
Correct |
1 ms |
6752 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
1 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
1 ms |
6748 KB |
Output is correct |
18 |
Correct |
5 ms |
7516 KB |
Output is correct |
19 |
Correct |
5 ms |
7516 KB |
Output is correct |
20 |
Correct |
5 ms |
7516 KB |
Output is correct |
21 |
Correct |
5 ms |
7516 KB |
Output is correct |
22 |
Correct |
5 ms |
7516 KB |
Output is correct |
23 |
Correct |
6 ms |
7516 KB |
Output is correct |
24 |
Correct |
5 ms |
7512 KB |
Output is correct |
25 |
Correct |
1 ms |
6488 KB |
Output is correct |
26 |
Correct |
63 ms |
7768 KB |
Output is correct |
27 |
Correct |
65 ms |
7700 KB |
Output is correct |
28 |
Correct |
65 ms |
7768 KB |
Output is correct |
29 |
Correct |
65 ms |
7892 KB |
Output is correct |
30 |
Correct |
12 ms |
7516 KB |
Output is correct |
31 |
Correct |
17 ms |
7692 KB |
Output is correct |
32 |
Correct |
13 ms |
7516 KB |
Output is correct |
33 |
Correct |
16 ms |
7516 KB |
Output is correct |
34 |
Correct |
163 ms |
42112 KB |
Output is correct |
35 |
Correct |
172 ms |
42188 KB |
Output is correct |
36 |
Correct |
165 ms |
42144 KB |
Output is correct |
37 |
Correct |
204 ms |
41812 KB |
Output is correct |
38 |
Correct |
205 ms |
41808 KB |
Output is correct |
39 |
Correct |
200 ms |
41784 KB |
Output is correct |
40 |
Correct |
199 ms |
41808 KB |
Output is correct |
41 |
Correct |
1 ms |
6488 KB |
Output is correct |
42 |
Correct |
2 ms |
6748 KB |
Output is correct |
43 |
Execution timed out |
2081 ms |
45712 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |