fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5. int n; cin >> n;
  6. int ar[n+5];
  7. for (int i = 1; i <= n; i++){
  8. cin >> ar[i];
  9. }
  10.  
  11. map<int,int> freq;
  12. for (int i = 1; i <= n; i++){
  13. freq[ar[i]] += 1;
  14. }
  15.  
  16. //use a long long because it can store a bigger number
  17. long long ans = 0, tot = n;
  18.  
  19. /*
  20.   n = 5
  21.   1 1 2 2 3
  22.  
  23.   {{1: 2}, {2: 2}, {3: 1}}
  24.  
  25.   it = {3: 1}
  26.   it.first = 3
  27.   it.second = 1
  28.   */
  29. for (auto it: freq){
  30. int val = it.second;
  31. ans += val*(tot-val);
  32. tot -= val;
  33. }
  34.  
  35. cout << ans << endl;
  36. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
3106515