#include #include typedef unsigned long long int_t; struct Hill { int_t height; int_t value; }; int main() { std::ios::sync_with_stdio(false); int_t n; std::cin >> n; int_t result = 0; int_t h, v; std::stack hills; for (int_t i = 0; i < n; ++i) { std::cin >> h; v = 0; if (!hills.empty()) { Hill last; do { last = hills.top(); hills.pop(); v += last.value; } while (!hills.empty() && last.height < h); if (last.height == h) { result += v - last.value; } else { hills.push(last); v -= last.value; } } hills.push({h, v+1}); } std::cout << result << std::endl; return 0; }