神枪手这样试试 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] ss = br.readLine().split(" "); int[] arr = new int[n]; for (int i = 0; i < arr.length; i++) { arr[i] = Integer.parseInt(ss[i]); } int[] cnts = new int[32]; for(int e : arr) { int bit = 0; while (e > 0) { if(e % 2 == 1) { cnts[bit]++; } e /= 2; bit++; } } int sum = 0; for(int i = 0; i < n; i++) { for(int bit = 0; bit < 32; bit++) { int pre = (1 << bit); if((arr[i] & pre) != 0) { sum += pre * (n - i - 1); cnts[bit]--; } else { sum += pre * cnts[bit]; } } } System.out.println(sum + Arrays.stream(arr).sum()); } }