#include using namespace std; int lider[1000005]; int tab[1000005]; vector pos[1000005]; int f(int a) { if(lider[a] != a) lider[a] = f(lider[a]); return lider[a]; } void u(int a,int b) { lider[f(a)] = f(b); } 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); } for(int x=0;x<=a+1;x++) lider[x] = x; long long ans = 0; for(int x=1;x<=1000000;x++) { for(int y=0;y<(int)pos[x].size() - 1;y++) { //cout<