int (int[] values, int from, int length, int gap, boolean last) { if (length <= 0) { return 0; } int value = 0; for (int i = 0; i < length; i++) { value += values[from + i]; } return value + (length - (last ? 0 : 1)) * gap; }