fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6.  
  7. int n = sc.nextInt();
  8. int[] b = new int[n];
  9.  
  10. Map<Integer, Integer> freq = new HashMap<>();
  11.  
  12. for (int i = 0; i < n; i++) {
  13. b[i] = sc.nextInt();
  14. freq.put(b[i], freq.getOrDefault(b[i], 0) + 1);
  15. }
  16.  
  17. List<Integer> heights = new ArrayList<>(freq.keySet());
  18. heights.sort(Collections.reverseOrder());
  19.  
  20. long step = 0;
  21. int cumulativeFreq = 0;
  22.  
  23. for (int i = 0; i < heights.size() - 1; i++) {
  24. cumulativeFreq += freq.get(heights.get(i));
  25. step += cumulativeFreq;
  26. }
  27.  
  28. System.out.println(step);
  29. }
  30.  
  31. }
Success #stdin #stdout 0.11s 36428KB
stdin
5
4
5
5
2
4
stdout
6