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 Main
  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. int[][] arr = new int[n][2];
  16. int[] diff = new int[n];
  17. long sum = 0;
  18. for(int i=0;i<n;i++){
  19. arr[i][0] = sc.nextInt();
  20. arr[i][1] = sc.nextInt();
  21. sum += arr[i][1]*n-arr[i][0];
  22. diff[i] = arr[i][0] - arr[i][1];
  23. }
  24. Arrays.sort(diff);
  25. long res = 0;
  26. for(int j=n;j>=1;j--){
  27. res += diff[n-j]*j;
  28. }
  29. System.out.println(res+sum);
  30.  
  31. }
  32. }
Success #stdin #stdout 0.18s 56552KB
stdin
10
5 10
12 4
31 45
20 55
30 17
29 30
41 32
7 1
5 5
3 15
stdout
1423