Home AtCoder. Regular Contest 135 C XOR to All
Post
Cancel

AtCoder. Regular Contest 135 C XOR to All

[Link] https://AtCoder.jp/contests/arc135/tasks/arc135_c

IDEA


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.*;
import java.io.*;

public class Main {
	static BufferedReader br;
	static StringBuilder sb = new StringBuilder();
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		var n = toi(br.readLine());
		var arr = getArr();
		var bitCnt = new int[30];
		var sum = 0l;

		for(int e: arr) {
			int digit = 0;
			while(e > 0) {
				if((e & 1) == 1) bitCnt[digit]++;
				digit++;
				e >>= 1;
			}
		}


		for(var e: arr) sum += e;
		for(var i = 0; i < n; i++) {
			long tmp = 0;
			for(var j = 0; j < 30; j++) {
				if((arr[i] & 1 << j) == 0) tmp += (1 << j) * bitCnt[j];
				else tmp += (1 << j) * (n - bitCnt[j]);
			}
			sum = Math.max(sum, tmp);
		}
		println(sum);
	}

	static int toi(String s) { return Integer.parseInt(s); }
	static long tol(String s) { return Long.parseLong(s); }
	static String[] getLine() throws IOException { return br.readLine().split(" "); }
	static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
	static <T> void print(T s) { System.out.print(s); }
	static <T> void println(T s) { System.out.println(s); }
}
This post is licensed under CC BY 4.0 by the author.