#include using namespace std; int tab[1000005]; vector pos[1000005]; set s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int a; cin>>a; for(int x=1;x<=a;x++) { cin>>tab[x]; pos[tab[x]].push_back(x); s.insert(x); } long long ans = 0; for(int x=1;x<=1000000;x++) { for(int y=0;y<(int)pos[x].size() - 1;y++) { auto it = s.lower_bound(pos[x][y] + 1); if((*it) == pos[x][y+1]) ans += pos[x][y+1] - pos[x][y] - 1; s.erase(pos[x][y]); } if(!pos[x].empty()) s.erase(pos[x].back()); } cout<