I'm doing brute force method for my school project, the thing is with my current code it took 15 hours to calculate, when I know it should take just few minutes max. Also it seems to not choose the best possibility, not sure why. It compiles and everything works, but is just soo inefficient. 
I have a file with the data in which in the first line there is a capacity, in next lines there is value and weight.
What can I do to improve the code?

```java
public class Main {

    static ArrayList<String> weights = new ArrayList<>();
    static ArrayList<String> values = new ArrayList<>();
    static int n = 20;
    private static int maxValue = 0;

    public static void readFile(String dataFile) throws IOException {
        FileReader read = new FileReader(dataFile);
        BufferedReader buff = new BufferedReader(read);
        String line;
        while ((line = buff.readLine()) != null) {
            String[] item = line.split(" ");
            if (item.length == 1)
                capacity = Integer.parseInt(item[0]);
            else {
                values.add(item[0]);
                weights.add(item[1]);

            }
        }
    }

    public static String toBinary(int a) {
        String result = "";
        while (a > 0) {
            result = a % 2 + result;
            a = a / 2;

        }
        while (result.length() < n) {
            result = "0" + result;
        }
        return result;

    }

    public static boolean isMaxValue(int val) {
        if (val > maxValue)
            return true;
        return false;
    }

    public static boolean isInCapacity(int weight) {
        if (weight <= capacity)
            return true;
        return false;
    }

    public static void main(String[] args) {

        long start = System.currentTimeMillis();

        try {
            readFile(dataFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        int sumValue = 0;
        int sumWeight = 0;
        String bestComb = "";
        String finalResult = "";
        int combnum = (int) Math.pow(2, n);

        for (int i = 1; i < combnum; i++) { /

            //System.out.println(toBinary(i));  // 277s with, without 187s for n=20

            for (int j = 0; j < toBinary(i).length(); j++) {

                int arrElem = Character.getNumericValue(toBinary(i).charAt(j));
                if (arrElem == 1) {
                    sumWeight += Integer.parseInt(weights.get(j)); 
                    sumValue += Integer.parseInt(values.get(j));
                }

            }
            if (isMaxValue(sumValue) && isInCapacity(sumWeight)) { 
                finalResult = sumValue + " value, " + sumWeight + " weight";
                bestComb = toBinary(i);
            };

            sumWeight = 0; 
            sumValue = 0;

        }

```

**EDIT:**
changed the way of calculating (also made item into class instead of 2 arrayLists) to this:

```java
for (int i = 1; i < combnum; i++) {
            String comb = Integer.toBinaryString(i);
            sumValue = 0;
            sumWeight = 0;
            for (int j = 0; j < comb.length(); j++) {
                if (comb.charAt(j) == '1') {
                    sumValue += itemList.get(j).getValue();
                    sumWeight += itemList.get(j).getWeight();
                }
            }
            if (isMaxValue(sumValue) && isInCapacity(sumWeight)) { 
                maxValue=sumValue;
                maxWeight = sumWeight;
                finalResult = sumValue + " value, " + sumWeight + " weight";
                bestComb = toBinary(i);
            };

```

and the timer dropped to few minutes for 2^30 combinations. Cheers.