#include<bits/stdc++.h>
using namespace std;
long long x,n,ans;
stack<long long>s;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n;
while(n-->0){
cin>>x;
long long t=1;
while(s.size()&&x>=s.top()){
if(s.top()==x)t++;
ans++;s.pop();
}
if(s.size())ans++;
while(t--)s.push(x);
}
cout<<ans;
}
80pts TLE,求调。