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.  
  14.  
  15.  
  16. Scanner sc = new Scanner(System.in);
  17.  
  18. int n = sc.nextInt();
  19.  
  20. int arr[][] = new int[n][2];
  21.  
  22. for(int i = 0; i < n; i++) {
  23. arr[i][0] = sc.nextInt();
  24. arr[i][1] = sc.nextInt();
  25. }
  26.  
  27. int diff[] = new int[n];
  28.  
  29. long ans = 0;
  30.  
  31. for(int i = 0; i < n; i++) {
  32. diff[i] = arr[i][0] - arr[i][1];
  33.  
  34. ans += (long) arr[i][1] * n - arr[i][0];
  35. }
  36.  
  37. Arrays.sort(diff);
  38.  
  39. for(int i = 0; i < n; i++) {
  40. ans += (long) diff[n - 1 - i] * (i + 1);
  41. }
  42.  
  43. System.out.println(ans);
  44. }
  45.  
  46.  
  47. }
Success #stdin #stdout 0.12s 17620KB
stdin
5
1
2
23
3
4
5
4
3
2
3
2
3
stdout
56