近乎近乎近乎近乎近乎近乎

#include<iostream>
#include<stack>
using namespace std;
int main(){
    int n;
    scanf("%d", &n);
    stack<pair<int,int>> s;
    int a;
    long long cnt = 0;
    while(--n >= 0){
        scanf("%d", &a);
        while(!s.empty() && s.top().first > a){
            cnt += s.top().second;
            s.pop();
        }
        if(s.empty()){
            s.push(make_pair(a, 1));
        }
        else if(s.top().first == a){
            cnt += s.top().second;
            s.top().second++;
            if(s.size() > 1){
                cnt++;
            }
        }
        else if(s.top().first < a){
            s.push(make_pair(a, 1));
            cnt++;
        }
    }
    printf("%ld", cnt);
    return 0;
}