https://www.luogu.com.cn/record/80380657
/*
#pragma GCC optimize (3, "Ofast", "inline")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("no-stack-protector")
*/
/*
---------------------------* T I P S *-----------------------------
Date: Jul.20th
Author: uFTvL9(xwzy)
message:
Title: line.cpp/.in/.out
Score: 72(Stack)
Score':
tips: Croatian Olympiad in Informatics 2007.(COI2007)
_ Task PATRIK.
-------------------------------------------------------------------
*/
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while(ch < '0' or ch > '9') {
if(ch == '-')
f = -1;
ch = getchar();
}
while(ch >= '0' and ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
int main(void) {
stack<pair<short, int>> S;//pair<height, amount>
unsigned int N;
N = read();
unsigned long long ans = 0;
for(register unsigned int i = 0; i < N; i++) {
short h = read();
pair<short, int> vec(h, 1);
for(; !S.empty() and S.top().first <= h; S.pop()) {
ans += S.top().second;
if(S.top().first == h)
vec.second += S.top().second;
}
if(!S.empty())
ans++;
S.push(vec);
}
printf("%lld\n", ans);
return 0;
}