using
System;
using
System.Collections;
using
System.Collections.Generic;
class
GFG
{
static
long
subarray(List<
int
> v)
{
int
n = v.Count;
if
(n <= 1){
return
n;
}
long
ans = proc(v);
int
low = v[0], pos_low = 0;
int
high = v[0], pos_high = 0;
for
(
int
i = 1 ; i < n ; i++) {
int
x = v[i];
if
(x < low) {
low = x;
pos_low = i;
}
else
if
(x > high) {
high = x;
pos_high = i;
}
}
List<
int
> u =
new
List<
int
>();
for
(
int
i = 0 ; i < n ; i++){
u.Add(0);
}
u =
new
List<
int
>(v);
u.Remove(v[pos_low]);
ans = Math.Max(ans, proc(u));
List<
int
> w =
new
List<
int
>();
for
(
int
i = 0 ; i < n ; i++){
w.Add(0);
}
w =
new
List<
int
>(v);
w.Remove(v[pos_high]);
return
Math.Max(ans, proc(w));
}
static
long
proc(List<
int
> v)
{
int
n = v.Count;
int
low = v[n-1], high = v[n-1];
int
p1 = n, p2 = n;
long
ans = 0;
for
(
int
i = n - 1; i >= 0; i--) {
int
x = v[i];
if
(x < low) {
low = x;
ans = 0;
}
else
if
(x > high) {
high = x;
ans = 0;
}
if
(x == low)
p1 = i;
if
(x == high)
p2 = i;
ans += n - Math.Max(p1, p2);
}
return
ans;
}
public
static
void
Main(
string
[] args){
List<
int
> arr =
new
List<
int
>{
7, 2, 5, 4, 3, 1
};
Console.WriteLine(subarray(arr));
}
}