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. Scanner sc = new Scanner(System.in);
  13. int n = sc.nextInt();
  14. Long p[] = new Long[n];
  15. long sum=0;
  16. for(int i=0;i<n;i++){
  17. long a = sc.nextLong();
  18. long b = sc.nextLong();
  19. // adding constant sum
  20. sum=sum+b*n-a;
  21. p[i] = a-b;
  22. }
  23.  
  24. Arrays.sort(p,Collections.reverseOrder())
  25. ;
  26. for(int j=1;j<=n;j++){
  27. // variable sum
  28. sum=(long)sum+j*p[j-1];
  29. }
  30. System.out.println(sum);
  31. }
  32. }
Success #stdin #stdout 0.14s 54496KB
stdin
3
4 2
2 3
6 1
stdout
12