This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int nmax = 5e5;
int n;
int v[nmax + 5];
int lst[nmax + 5], ldr[nmax + 5], rst[nmax + 5], rdr[nmax + 5];
bool subtask3()
{
    return true;
}
int solve3()
{
    int poz = 0;
    for(int i=1; i<=n; i++)
    {
        if(v[i] > v[i + 1])
        {
            poz = i;
            break;
        }
    }
    for(int i=1; i<=poz; i++)
    {
        if(!lst[v[i]])
        {
            lst[v[i]] = i;
        }
        ldr[v[i]] = i;
    }
    for(int i=poz+1; i<=n; i++)
    {
        if(!rst[v[i]])
        {
            rst[v[i]] = i;
        }
        rdr[v[i]] = i;
    }
    int rez = 0;
    for(int i=1;i<=n;i++)
    {
        if(!lst[i] || !rst[i])
        {
            rez = max(rez, ldr[i] - lst[i] + 1);
            rez = max(rez, rdr[i] - rst[i] + 1);
            continue;
        }
        int lmax = min(n,(rdr[i] - lst[i] + 1) * 2);
        if(rst[i] - ldr[i] - 1 <= (lmax + 1) / 2)
        {
            rez = max(rez, ldr[i] - lst[i] + 1 + rdr[i] - rst[i] + 1);
        }
    }
    return rez;
}
int sequence(int N, vector<int> A)
{
    n = N;
    for(int i=1;i<=n;i++)
    {
        lst[i] = ldr[i] = rst[i] = rdr[i] = 0;
        v[i] = A[i - 1];
    }
    if(subtask3())
    {
        return solve3();
    }
}
#ifdef home
int main()
{
    freopen("nr.in","r",stdin);
    freopen("nr.out","w",stdout);
    int nn;
    cin>>nn;
    vector<int> vv(nn);
    for(int i=1; i<=nn; i++)
    {
        cin>>vv[i - 1];
    }
    cout<<sequence(nn,vv)<<'\n';
    return 0;
}
#endif // home
Compilation message (stderr)
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:74:1: warning: control reaches end of non-void function [-Wreturn-type]
   74 | }
      | ^| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |