Language: C++

/*input
8
5 4 8 1 2 6 7 3

*/
#include <bits/stdc++.h>
#include <stdio.h>    
#include <string.h>
using namespace std;
#define mod 1000000007
 
#define llu long long unsigned
#define lld long long
#define ld long
#define sc(a) scanf("%lld",&a)
#define sc2(a,b) scanf("%lld%lld",&a,&b)
#define sc3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define scf(a) scanf("%lf",&a)
#define scf2(a,b) scanf("%lf%lf",&a,&b)
#define scf3(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)
#define prL(a) printf("%lld\n",a)
#define prS(a) printf("%lld ",a)
#define prfL(a) printf("%lf\n",a)
#define prfS(a) printf("%lf ",a) 
// #define swap(a,b,t) t=a;a=b;b=t
#define PA pair <lld,pair<lld,lld> >
 
#define lim 1000003
#define lim2 1005
lld A[lim],C[lim],next[lim],prev[lim],D[lim];
typedef struct node{
    long long val;
    long long p;
}nody;
 
bool comp(nody i,nody j) {
    return i.val<j.val; 
}
nody N[lim];
 
// lld bina1(lld A[],lld k,lld low,lld high){
//     lld mid;
//     while(low<=high){
//         mid=low+(high-low)/2;
//         if(A[mid]==k){
//             if(mid==0) return C[0];
//             else return C[mid]-C[mid-1];
//         }
//         else if(A[mid]>k) high=mid-1;
//         else low=mid+1;
//     }
//     return 0;
// }
 
// lld bina2(lld A[],lld k,lld low,lld high){
//     lld mid;
//     while(high-low>1){
//         mid=low+(high-low)/2;
//         if(A[mid]<k) low=mid;
//         else high=mid-1;
//     }
//     if(A[high]<k) return C[high];
//     else if(A[low]<k) return C[low];
//     else return 0;
// }
 
// lld bina3(lld A[],lld k,lld low,lld high){
//     lld mid;
//     while(high-low>1){
//         mid=low+(high-low)/2;
//         if(A[mid]>k) high=mid;
//         else low=mid+1;
//     }
//     if(A[low]>k) return D[low];
//     else if(A[high]>k) return D[high];
//     else return 0;
// }
 
int main(){
    lld t,T,i,h,k,j,pos,ans,count,sum,m,q,r,n,p,a,b;
    sc(n);
    for(i=0;i<n;i++){
    	sc(A[i]);
    	A[i]=-A[i];
    }
    stack<pair<lld,lld> > S;
    pair<lld,lld> P;
    S.push(make_pair(A[0],0));
    for(i=1;i<n;i++){
        if(S.empty()){
            S.push(make_pair(A[i],i));
            continue;
        }
        P=S.top();
        if(P.first>A[i]) S.push(make_pair(A[i],i));
        else{
            next[P.second]=i;
            S.pop();
            i--;
        }
    }
    while(!S.empty()){
        P=S.top();
        next[P.second]=n;
        S.pop();
    }
    S.push(make_pair(A[n-1],n-1));
    for(i=n-2;i>=0;i--){
        if(S.empty()){
            S.push(make_pair(A[i],i));
            continue;
        }
        P=S.top();
        if(P.first>=A[i]) S.push(make_pair(A[i],i));
        else{
            prev[P.second]=i;
            S.pop();
            i++;
        }
    }
    while(!S.empty()){
        P=S.top();
        prev[P.second]=-1;
        S.pop();
    }
   	ans=0;
   	for(i=0;i<n;i++){
   		lld a,b;
   		// if(prev[i]==-1) a=i;
   		// else a=i-prev[i];
   		// if(next[i]==n) b=n-1-i;
   		// else b=next[i]-i+1;
   		ans+=-A[i]*(next[i]-i)*(i-prev[i]);
   		// ans+=-A[i]*a*b;
   		// printf("ans=%lld i=%lld prev=%lld next=%lld\n",ans,i,a,b );
   	}
   	prL(ans);
    // for(i=0;i<n;i++){
    //     N[i].val=A[i];
    //     N[i].p=(next[i]-i)*(i-prev[i]);
    // }
    // sort(N,N+n,comp);
    // A[0]=N[0].val;
    // C[0]=N[0].p;
    // k=1;
    // for(i=1;i<n;i++){
    //     if(N[i].val!=N[i-1].val){
    //         A[k]=N[i].val;
    //         C[k]=N[i].p;
    //         k++;
    //     }
    //     else C[k-1]+=N[i].p;
    // }
    // for(i=0;i<k;i++) D[i]=C[i];
    // for(i=1;i<k;i++) C[i]=C[i]+C[i-1];
    // for(i=k-2;i>=0;i--) D[i]=D[i]+D[i+1];
    // // printf("%lld\n", k);
    // // for(i=0;i<k;i++) printf("%lld %lld\n",A[i],C[i]);
    // char play,rule,c1;
    // lld moveP;
    // scanf("%c",&c1);
    // while(m--){
    //     scanf("%c%lld\n",&play,&moveP );
    //     if(play=='='){
    //         ans=bina1(A,moveP,0,k-1);
    //         // printf("%lld(%c)\n", ans,play);
    //     }
    //     else if(play=='>'){
    //         ans=bina3(A,moveP,0,k-1);
    //         // printf("%lld(%c)\n", ans,play);
    //     }
    //     else{
    //         ans=bina2(A,moveP,0,k-1);
    //         // printf("%lld(%c)\n", ans,play);
    //     }
    //     scanf("%c\n",&rule );
    //     // printf("\n%c[%c]\n",play,rule );
    //     if(ans%2==0) printf("%c",'C'+'D'-rule);
    //     else printf("%c",rule );
    // }
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:97:13: error: reference to 'next' is ambiguous
   97 |             next[P.second]=i;
      |             ^~~~
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:6:
/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:30:19: note:                 'long long int next [1000003]'
   30 | lld A[lim],C[lim],next[lim],prev[lim],D[lim];
      |                   ^~~~
a.cc:104:9: error: reference to 'next' is ambiguous
  104 |         next[P.second]=n;
      |         ^~~~
/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:30:19: note:                 'long long int next [1000003]'
   30 | lld A[lim],C[lim],next[lim],prev[lim],D[lim];
      |                   ^~~~
a.cc:116:13: error: reference to 'prev' is ambiguous
  116 |             prev[P.second]=i;
      |             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:30:29: note:                 'long long int prev [1000003]'
   30 | lld A[lim],C[lim],next[lim],prev[lim],D[lim];
      |                             ^~~~
a.cc:123:9: error: reference to 'prev' is ambiguous
  123 |         prev[P.second]=-1;
      |         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:30:29: note:                 'long long int prev [1000003]'
   30 | lld A[lim],C[lim],next[lim],prev[lim],D[lim];
      |                             ^~~~
a.cc:133:29: error: reference to 'next' is ambiguous
  133 |                 ans+=-A[i]*(next[i]-i)*(i-prev[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:30:19: note:                 'long long int next [1000003]'
   30 | lld A[lim],C[lim],next[lim],prev[lim],D[lim];
      |                   ^~~~
a.cc:133:43: error: reference to 'prev' is ambiguous
  133 |                 ans+=-A[i]*(next[i]-i)*(i-prev[i]);
      |                                           ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:30:29: note:                 'long long int prev [1000003]'
   30 | lld A[lim],C[lim],next[lim],prev[lim],D[lim];
      |                             ^~~~