Language: C++

#include<bits/stdc++.h>
using namespace std;

int p[100010];
int next[100010],last[100010];
int last2[100010],next2[100010];
int main()
{
    int n;scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%d",&p[i]);
    p[0]=n+1;p[n+1]=n+1;
    last[0]=0;next[n+1]=n+1;
    for (int i=1;i<=n;i++)
    {
        int tmp=i-1;
        while (p[tmp]<=p[i])
            tmp=last[tmp];
        last[i]=tmp;
        tmp--;
        if (tmp<0) tmp=0;
        while(p[tmp]<=p[i])
            tmp=last[tmp];
        last2[i]=tmp;
    }
    for (int i=n;i>0;i--)
    {
        int tmp=i+1;
        while (p[tmp]<=p[i])
            tmp=next[tmp];
        next[i]=tmp;
        tmp++;
        if (tmp>n+1) tmp=n+1;
        while(p[tmp]<=p[i])
            tmp=next[tmp];
        next2[i]=tmp;
    }
    long long ans=0;
    for (int i=1;i<=n;i++)
    {
        //printf("%d %d %d %d\n",last2[i],last[i],next[i],next2[i]);
        ans+=1LL*p[i]*(i-last[i])*(next2[i]-next[i]);
        ans+=1LL*p[i]*(next[i]-i)*(last[i]-last2[i]);
        //cout<<ans<<endl;
    }
    cout<<ans;
}



Compiler output:
a.cc: In function 'int main()':
a.cc:12:15: error: reference to 'next' is ambiguous
   12 |     last[0]=0;next[n+1]=n+1;
      |               ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:5:5: note:                 'int next [100010]'
    5 | int next[100010],last[100010];
      |     ^~~~
a.cc:29:17: error: reference to 'next' is ambiguous
   29 |             tmp=next[tmp];
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:5:5: note:                 'int next [100010]'
    5 | int next[100010],last[100010];
      |     ^~~~
a.cc:30:9: error: reference to 'next' is ambiguous
   30 |         next[i]=tmp;
      |         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:5:5: note:                 'int next [100010]'
    5 | int next[100010],last[100010];
      |     ^~~~
a.cc:34:17: error: reference to 'next' is ambiguous
   34 |             tmp=next[tmp];
      |                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:5:5: note:                 'int next [100010]'
    5 | int next[100010],last[100010];
      |     ^~~~
a.cc:41:45: error: reference to 'next' is ambiguous
   41 |         ans+=1LL*p[i]*(i-last[i])*(next2[i]-next[i]);
      |                                             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:5:5: note:                 'int next [100010]'
    5 | int next[100010],last[100010];
      |     ^~~~
a.cc:42:24: error: reference to 'next' is ambiguous
   42 |         ans+=1LL*p[i]*(next[i]-i)*(last[i]-last2[i]);
      |                        ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:5:5: note:                 'int next [100010]'
    5 | int next[100010],last[100010];
      |     ^~~~