Language: C++

/*
                                Bismillahi-r-Rahmani-r-Rahim
                                        RhIeyAaD
                                    IIT-7th Batch,JU
*/
#include<bits/stdc++.h>
using namespace std;

#define debug(args...){ string _s = #args;replace(_s.begin(),_s.end(),',', ' ');stringstream _ss(_s);istream_iterator<string>_it(_ss);err(_it, args);}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {cerr << *it << "=" << a << ", "; err(++it, args...);}

#define ll              long long int
#define MAX             2134567891
#define fr(i,n)         for(i=0;i<n;i++)
#define rep(i,n)        for(i=1;i<=n;i++)
#define rev(i,a,n)      for(i=n;i>=a;i--)
#define FOR(i,a,n)      for(i=a;i<=n;i++)
#define ALL(n)          n.begin(),n.end()
#define mem(x,n)        memset(x,n,sizeof(x))
#define TC(t)           printf("Case %lld: ",t)
#define ans(t,c)        printf("Case %lld: %lld\n",t,c)
#define SETP(n)         cout<<setprecision(n)<<fixed
#define READ            freopen("F:\\Project\\Test_Case.txt","r",stdin)
#define WRITE           freopen("F:\\Project\\Output_Test.txt","w",stdout)
#define IO              ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
#define PAIR            pair<ll,ll>
#define MP              make_pair
#define pb              push_back
#define eb              emplace_back
#define ff              first
#define ss              second
#define NL              printf("\n");
#define bug(a)          cout<<#a<<" "<<a<<" ";
#define hlw             printf("hlw\n");
#define NN              111
#define MOD             1000000007///10^9
#define N               200007 ///10^6->6 zero after 1 **


ll x[N],y[N],z[N],y1[N],z1[N],n;

ll fun()
{
    ll a=0,b=0,c=0,d,e,f,g,i,j,mn=MAX,mx=0,ans=0;

    return ans;
}
int main()
{
    //IO;
    //while(1)
    //READ;WRITE;
{
    ll a=0,b=0,c=0,d,e,f,g,i,j,k,l,m,p,q,r,u,w,t,tc=1,in,loc,val,sz,lo,hi,mid,mn=MAX,mx=0,sum=0,ans=0,b1=0,c1=0;
    cin>>n;
    rep(i,n)cin>>x[i];

    /// First Part
    for(i=n;i>0;i--)
    {
        a=x[i];
        if(mn<a)
        {
            if(a-1<n-a+1)
                z[a]=0,y[a]=a-1;
            else
                z[a]=1,y[a]=n-a+1;
        }
        mn=min(mn,a);
    }
    for(i=1;i<=n;i++)
    {
        a=x[i];
        if(mx>a)
        {
            if(a<=n-a)
                z1[a]=0,y1[a]=a;
            else
                z1[a]=1,y1[a]=n-a;
        }
        mx=max(mx,a);
    }

    /// Second Part
    for(i=n;i>0;i--)
    {
        if(z[i])
        {
            y[i]=max(0ll,y[i]-b);
            b+=y[i];
        }
    }
    for(i=n;i>0;i--)
    {
        if(!z1[i])
        {
            y1[i]=max(0ll,y1[i]-b1);
            b1+=y1[i];
            //cout<<i<<" y1 "<<y1[i]<<endl;
        }
    }

    ///Third Part
    rep(i,n)
    {
        if(!z[i])
        {
            y[i]=max(0ll,y[i]-c);
            c+=y[i];
        }
    }
    rep(i,n)
    {
        if(z1[i])
        {
            y1[i]=max(0ll,y1[i]-c1);
            c1+=y1[i];
        }
    }
    //rep(i,n)cout<<i<<" "<<y1[i]<<endl;
    rep(i,n)ans+=y[i];
    rep(i,n)sum+=y1[i];
    //cout<<ans<<" ans sum "<<sum<<endl;
    cout<<min(ans,sum)<<endl;
}
    return 0;
}

/*
6
4 2 5 1 6 3




*/
/// Check for constraint 0 or 1?
/// Division MOD needs BigMod(a,n-2)
/// long long or double? sqrt or division will work fine??
/// sort(begin(v), end(v), [] (int a, int b) { return a > b; });    /// Lambda Function for manual Sort


Compiler output:
a.cc:42:23: error: 'long long int y1 [200007]' redeclared as different kind of entity
   42 | ll x[N],y[N],z[N],y1[N],z1[N],n;
      |                       ^
In file included from /usr/include/features.h:523,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/os_defines.h:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:683,
                 from /usr/include/c++/14/cassert:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:33,
                 from a.cc:6:
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:257:1: note: previous declaration 'double y1(double)'
  257 | __MATHCALL (y1,, (_Mdouble_));
      | ^~~~~~~~~~
a.cc: In function 'int main()':
a.cc:79:29: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   79 |                 z1[a]=0,y1[a]=a;
      |                             ^
a.cc:79:30: error: assignment of read-only location '*(y1 + ((sizetype)a))'
   79 |                 z1[a]=0,y1[a]=a;
      |                         ~~~~~^~
a.cc:81:29: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   81 |                 z1[a]=1,y1[a]=n-a;
      |                             ^
a.cc:81:30: error: assignment of read-only location '*(y1 + ((sizetype)a))'
   81 |                 z1[a]=1,y1[a]=n-a;
      |                         ~~~~~^~~~
a.cc:99:17: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   99 |             y1[i]=max(0ll,y1[i]-b1);
      |                 ^
a.cc:99:31: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   99 |             y1[i]=max(0ll,y1[i]-b1);
      |                               ^
a.cc:99:32: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   99 |             y1[i]=max(0ll,y1[i]-b1);
      |                           ~~~~~^~~
a.cc:99:22: error: no matching function for call to 'max(long long int, double (*)(double) noexcept)'
   99 |             y1[i]=max(0ll,y1[i]-b1);
      |                   ~~~^~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:99:22: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'double (*)(double) noexcept')
   99 |             y1[i]=max(0ll,y1[i]-b1);
      |                   ~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:99:22: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   99 |             y1[i]=max(0ll,y1[i]-b1);
      |                   ~~~^~~~~~~~~~~~~~
a.cc:100:21: warning: pointer to a function used in arithmetic [-Wpointer-arith]
  100 |             b1+=y1[i];
      |                     ^
a.cc:100:15: warning: pointer to a function used in arithmetic [-Wpointer-arith]
  100 |             b1+=y1[i];
      |             ~~^~~~~~~
a.cc:100:15: error: invalid conversion from 'double (*)(double) noexcept' to 'long long int' [-fpermissive]
  100 |             b1+=y1[i];
      |             ~~^~~~~~~
      |               |
      |               double (*)(double) noexcept
a.cc:118:17: warning: pointer to a function used in arithmetic [-Wpointer-arith]
  118 |             y1[i]=max(0ll,y1[i]-c1);
      |                 ^
a.cc:118:31: warning: pointer to a function used in arithmetic [-Wpointer-arith]
  118 |             y1[i]=max(0ll,y1[i]-c1);
      |                               ^
a.cc:118:32: warning: pointer to a function used in arithmetic [-Wpointer-arith]
  118 |             y1[i]=max(0ll,y1[i]-c1);
      |                           ~~~~~^~~
a.cc:118:22: error: no matching function for call to 'max(long long int, double (*)(double) noexcept)'
  118 |             y1[i]=max(0ll,y1[i]-c1);
      |                   ~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:118:22: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'double (*)(double) noexcept')
  118 |             y1[i]=max(0ll,y1[i]-c1);
      |                   ~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:118:22: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  118 |             y1[i]=max(0ll,y1[i]-c1);
      |                   ~~~^~~~~~~~~~~~~~
a.cc:119:21: warning: pointer to a function used in arithmetic [-Wpointer-arith]
  119 |             c1+=y1[i];
      |                     ^
a.cc:119:15: warning: pointer to a function used in arithmetic [-Wpointer-arith]
  119 |             c1+=y1[i];
      |             ~~^~~~~~~
a.cc:119:15: error: invalid conversion from 'double (*)(double) noexcept' to 'long long int' [-fpermissive]
  119 |             c1+=y1[i];
      |             ~~^~~~~~~
      |               |
      |               double (*)(double) noexcept
a.cc:124:22: warning: pointer to a function used in arithmetic [-Wpointer-arith]
  124 |     rep(i,n)sum+=y1[i];
      |                      ^
a.cc:124:16: warning: pointer to a function used in arithmetic [-Wpointer-arith]
  124 |     rep(i,n)sum+=y1[i];
      |             ~~~^~~~~~~
a.cc:124:16: error: invalid conversion from 'double (*)(double) noexcept' to 'long long int' [-fpermissive]
  124 |     rep(i,n)sum+=y1[i];
      |             ~~~^~~~~~~
      |                |
      |                double (*)(double) noexcept