Skip to content

Instantly share code, notes, and snippets.

@stphung
Created May 8, 2011 19:17
Show Gist options
  • Select an option

  • Save stphung/961612 to your computer and use it in GitHub Desktop.

Select an option

Save stphung/961612 to your computer and use it in GitHub Desktop.
Google Code Jam 2011 Qualifier Round - Problem C, Candy Splitting
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class CandySplitting {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(new File("C-small-attempt0.in"));
int cases = sc.nextInt();
for(int c=1; c<=cases; c++) {
int numCandies = sc.nextInt();
List<Integer> candies = new ArrayList<Integer>();
for(int j=0; j<numCandies; j++) {
candies.add(sc.nextInt());
}
int max = -1;
for(int i=0; i < (1<<candies.size()); i++) {
List<Integer> first = new ArrayList<Integer>();
List<Integer> second = new ArrayList<Integer>();
for(int j=0; j<candies.size(); j++) {
if ((i&(1<<j)) > 0) {
first.add(candies.get(j));
} else {
second.add(candies.get(j));
}
}
if (first.size() == 0 || second.size() == 0) continue;
if (stupidAdd(first) == stupidAdd(second)) {
max = Math.max(max, Math.max(sum(first), sum(second)));
}
}
if (max == -1) System.out.println("Case #" + c + ": NO");
else System.out.println("Case #" + c + ": " + max);
}
}
public static int stupidAdd(List<Integer> values) {
int sum = 0;
for(int i=0; i<values.size(); i++) {
sum ^= values.get(i);
}
return sum;
}
public static int sum(List<Integer> values) {
int sum = 0;
for(int i=0; i<values.size(); i++) {
sum += values.get(i);
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment