#include #include #include using namespace std; int main() { ios::sync_with_stdio(0); long long total = 0; int n; cin >> n; vector cisla(n); for(int i = 0; i < n; i++) { cin >> cisla[i]; } stack hory; hory.push(0); for(int i = 1; i < n; i++) { while(!hory.empty() && cisla[hory.top()] < cisla[i]){ hory.pop(); } if(!hory.empty() && cisla[hory.top()] == cisla[i]){ total += i - hory.top() -1; hory.pop(); } hory.push(i); } /* long long total = 0; for(int i = 0; i < n; i++) { int cnt = 0; bool koniec = false; for(int j = i+1; j < n; j++) { if(cisla[i] > cisla[j]) { cnt++; } else if(cisla[i] == cisla[j]) { koniec = true; break; } else { break; } } if(koniec) total += cnt; }*/ cout << total << "\n"; return 0; }