#include #include struct Hill { int height; int value; }; int main() { std::ios::sync_with_stdio(false); int n; std::cin >> n; int result = 0; int h, v; std::stack hills; for (int 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; }