第二题代码:public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; int[] b = new int[n]; for(int i=0;i a[i] = in.nextInt(); b[i] = a[i];} Arrays.sort(a);long[] sum = new long[n+1]; for(int i=0;i sum[i+1] = sum[i]+a[i];} for(int i=0;i int j =findIndex(a,b[i]); long left =(long) b[i]*j-sum[j]; long right=sum[n]-sum[j]-(long) b[i]*(n-j); System.out.println(left+right);} } public static int findIndex(int[] nums, int target){ int left=-1,right=nums.length; while(left+1 int mid = left+(right-left)/2; if(nums[mid] left=mid; } else if (nums[mid]>target) { right = mid; } } return right; }