public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
        int out = 0;
        int min;
        if (triangle == null || triangle.size() == 0)
            return out;
        
        int tLevel = triangle.size();
        
        out = triangle.get(0).get(0);
        ArrayList<Integer> candidate = new ArrayList<Integer>();
        candidate.add(out);
        ArrayList<Integer> next;
        for (int level = 1; level < tLevel; level++) {
            next = new ArrayList<Integer>();
            int k;
            for (int i = 0; i <= level; i++) {
                if (i == 0)
                    k = triangle.get(level).get(0) + candidate.get(0);
                else if (i == level)
                    k = triangle.get(level).get(i) + candidate.get(i-1);
                else {
                    k = triangle.get(level).get(i);
                    k += Math.min(candidate.get(i-1), candidate.get(i));
                }
                next.add(k);
            }

            candidate.clear();
            candidate = next;
        }
        
        if (candidate.size() != 0) {
            min = candidate.get(0);
            for (int c:candidate) {
                min = Math.min(min, c);
            }
            out = min;
        }

        return out;
    }