public static int howLong ( int [ ] cakecounts ) {
  if ( cakecounts . length < 4 ) return cakecounts . length - 1 ;
  int ret = Integer . MAX_VALUE ;
  for ( int maxsize = 2 ;
  maxsize < cakecounts . length ;
  ++ maxsize ) {
    int [ ] cakes = Arrays . copyOf ( cakecounts , cakecounts . length ) ;
    int numspec = 0 ;
    int maxstack = cakecounts . length - 1 ;
    while ( maxstack > maxsize ) {
      while ( cakes [ maxstack ] > 0 ) {
        cakes [ maxstack ] -- ;
        cakes [ maxstack - maxsize ] ++ ;
        cakes [ maxsize ] ++ ;
        numspec ++ ;
      }
      maxstack -- ;
    }
    ret = Math . min ( ret , maxsize + numspec ) ;
  }
  return ret ;
}

public static int howLong ( int [ ] cakecounts ) { if ( cakecounts . length < 4 ) return cakecounts . length - 1 ; int ret = Integer . MAX_VALUE ; for ( int maxsize = 2 ; maxsize < cakecounts . length ; ++ maxsize ) { int [ ] cakes = Arrays . copyOf ( cakecounts , cakecounts . length ) ; int numspec = 0 ; int maxstack = cakecounts . length - 1 ; while ( maxstack > maxsize ) { while ( cakes [ maxstack ] > 0 ) { cakes [ maxstack ] -- ; cakes [ maxstack - maxsize ] ++ ; cakes [ maxsize ] ++ ; numspec ++ ; } maxstack -- ; } ret = Math . min ( ret , maxsize + numspec ) ; } return ret ; }
(method_declaration (modifiers) type: (integral_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (integral_type) dimensions: (dimensions)) name: (identifier))) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (decimal_integer_literal))) consequence: (return_statement (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (decimal_integer_literal)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (field_access object: (identifier) field: (identifier)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (field_access object: (identifier) field: (identifier))) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (field_access object: (identifier) field: (identifier)))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (decimal_integer_literal)))) (while_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) body: (block (while_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (decimal_integer_literal))) body: (block (expression_statement (update_expression (array_access array: (identifier) index: (identifier)))) (expression_statement (update_expression (array_access array: (identifier) index: (binary_expression left: (identifier) right: (identifier))))) (expression_statement (update_expression (array_access array: (identifier) index: (identifier)))) (expression_statement (update_expression (identifier))))) (expression_statement (update_expression (identifier))))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (identifier) right: (identifier)))))))) (return_statement (identifier))))