#include #include #include #include #define MILLION 1000000 using namespace std; int main() { int n, tmp, ans = 0; cin >> n; vector positions(MILLION); set live_bridges; for (int i = 0; i < n; ++i) { cin >> tmp; vector to_delete; for (auto it = live_bridges.begin(); it != live_bridges.end() && *it < tmp; ++it) to_delete.push_back(*it); for (auto del:to_delete) live_bridges.erase(del); if (live_bridges.count(tmp) == 0) { live_bridges.insert(tmp); positions[tmp] = i; } else { ans += i - positions[tmp] - 1; positions[tmp] = i; } } cout << ans << endl; return 0; }