fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner sc = new Scanner(System.in);
  14. int n = sc.nextInt();
  15.  
  16. long sum = 0;
  17. Long[] prefix = new Long[n + 1];
  18. prefix[0] = 0L;
  19.  
  20. for (int i = 1; i <= n; i++) {
  21. long num1 = sc.nextLong();
  22. long num2 = sc.nextLong();
  23. sum += (-num1 + num2 * (n));
  24. prefix[i] = num1 - num2;
  25. }
  26. Arrays.sort(prefix, 1, n + 1, Collections.reverseOrder());
  27. for (int i = 1; i <= n; i++) {
  28. sum += (long) i * prefix[i];
  29. }
  30. System.out.println(sum);
  31. sc.close();
  32. }
  33. }
Success #stdin #stdout 0.15s 54672KB
stdin
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
stdout
1423