#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int msz = 2e5 + 5;
vector<int> g[msz];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<int> a(n + 1);
int s = 0;
for (int i = 1; i <= n; i++){
cin>>a[i];
if (a[i] == n){
s = i;
}
}
for (int i = 1; i < n; i++){
int a, b; cin>>a>>b;
if (a != i || b != (i + 1)){
return 0;
}
g[a].push_back(b);
g[b].push_back(a);
}
function<ll(int, int, int)> f = [&](int l, int r, int x){
int ml = 0, mr = 0;
for (int i = l; i < x; i++){
if (a[i] > a[ml]){
ml = i;
}
}
for (int i = x + 1; i <= r; i++){
if (a[i] > a[mr]){
mr = i;
}
}
ll out = 0;
if (ml) out = max(out, f(l, x - 1, ml) + (x - ml));
if (mr) out = max(out, f(x + 1, r, mr) + (mr - x));
return out;
};
cout<<f(1, n, s)<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
5196 KB |
Output is correct |
14 |
Correct |
2 ms |
4964 KB |
Output is correct |
15 |
Correct |
2 ms |
4960 KB |
Output is correct |
16 |
Correct |
2 ms |
4960 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
5196 KB |
Output is correct |
14 |
Correct |
2 ms |
4964 KB |
Output is correct |
15 |
Correct |
2 ms |
4960 KB |
Output is correct |
16 |
Correct |
2 ms |
4960 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
38 ms |
5720 KB |
Output is correct |
19 |
Correct |
35 ms |
5688 KB |
Output is correct |
20 |
Correct |
34 ms |
5716 KB |
Output is correct |
21 |
Correct |
3 ms |
5176 KB |
Output is correct |
22 |
Correct |
3 ms |
5168 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
5172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
5196 KB |
Output is correct |
14 |
Correct |
2 ms |
4964 KB |
Output is correct |
15 |
Correct |
2 ms |
4960 KB |
Output is correct |
16 |
Correct |
2 ms |
4960 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
38 ms |
5720 KB |
Output is correct |
19 |
Correct |
35 ms |
5688 KB |
Output is correct |
20 |
Correct |
34 ms |
5716 KB |
Output is correct |
21 |
Correct |
3 ms |
5176 KB |
Output is correct |
22 |
Correct |
3 ms |
5168 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
5172 KB |
Output is correct |
25 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
5196 KB |
Output is correct |
14 |
Correct |
2 ms |
4964 KB |
Output is correct |
15 |
Correct |
2 ms |
4960 KB |
Output is correct |
16 |
Correct |
2 ms |
4960 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
38 ms |
5720 KB |
Output is correct |
19 |
Correct |
35 ms |
5688 KB |
Output is correct |
20 |
Correct |
34 ms |
5716 KB |
Output is correct |
21 |
Correct |
3 ms |
5176 KB |
Output is correct |
22 |
Correct |
3 ms |
5168 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
5172 KB |
Output is correct |
25 |
Execution timed out |
2047 ms |
16364 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
1 ms |
4956 KB |
Output is correct |
4 |
Correct |
1 ms |
4956 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
1 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
1 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
5196 KB |
Output is correct |
14 |
Correct |
2 ms |
4964 KB |
Output is correct |
15 |
Correct |
2 ms |
4960 KB |
Output is correct |
16 |
Correct |
2 ms |
4960 KB |
Output is correct |
17 |
Correct |
3 ms |
4956 KB |
Output is correct |
18 |
Correct |
38 ms |
5720 KB |
Output is correct |
19 |
Correct |
35 ms |
5688 KB |
Output is correct |
20 |
Correct |
34 ms |
5716 KB |
Output is correct |
21 |
Correct |
3 ms |
5176 KB |
Output is correct |
22 |
Correct |
3 ms |
5168 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
5172 KB |
Output is correct |
25 |
Incorrect |
1 ms |
4956 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |