public int overflow(int[] vessels, int[] capacities, int[] nextfree, int index) {
    if (index < vessels.length) {
      if (vessels[index] < capacities[index])
        return index;
      else {
        int overflow = vessels[index] - capacities[index];
        vessels[index] = capacities[index];
        if (index != vessels.length - 1) {
          vessels[index + 1] += overflow;
          return nextfree[index + 1] = overflow(vessels, capacities, nextfree, index + 1);
        } else
          return vessels.length;
      }
    }
    return vessels.length;
  }