One question per day (distribute candies)

Solo and Koko are two brothers. Their mother gave them a big bag of candy. Each piece of candy has its own weight. Now they want to separate the sugar into two piles. Of course, the task of dividing the sugar fell on the eldest brother Solo, but Koko required that the total weight of the sugar obtained by the two people must be "equal" (according to Koko's logic), otherwise he would cry. Unfortunately, koko is still very young, and he can only convert two numbers into binary before adding them, and he always forgets to carry.

For example, when 12 (1100) is added to 5 (101):

1100

+ 0101

————

1001

So the calculation result obtained by koko is 9 (1001).

There are also some examples:

5 + 4 = 1

7 + 9 = 14

50 + 10 = 56

Now Solo is very greedy. He wants to maximize the total weight of candy he can get without making koko cry.

Enter description

The first line of input is an integer N (2 ≤ N ≤ 15), indicating how many pieces of sugar there are in the bag.

The second line contains N integers Weighti (1 ≤ Weighti ≤ 10^6) separated by spaces, indicating the weight of the i-th piece of sugar.

Output description

If you can prevent koko from crying, output the total weight of sugar that Solo can obtain, otherwise output "NO".

Sample

enter:

3
3 5 6

Output:

11

Code:

package com.ceshi;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int[] array = new int[n];
        for (int i = 0; i < n; i++)
            array[i] = scan.nextInt();
        int s = 0, mi = 1000010, sum = 0;
        for (int i = 0; i < n; i++) {
            int t = array[i];
            s ^= t; 
            mi = Math.min(mi, t); 
            sum += t; 
        }
        if (s != 0) {
            System.out.println("NO"); // 无解
        } else {
            System.out.println(sum - mi); // 除了最小值的其余数的和
        }
    }
}

Guess you like

Origin blog.csdn.net/a154555/article/details/129734169
Recommended