static int countPiles ( int n , int [ ] a ) {
  HashMap < Integer , Integer > occ = new HashMap < > ( ) ;
  for ( int i = 0 ;
  i < n ;
  i ++ ) occ . put ( a [ i ] , occ . get ( a [ i ] ) == null ? 1 : occ . get ( a [ i ] ) + 1 ) ;
  int pile = 0 ;
  while ( ! occ . isEmpty ( ) ) {
    pile ++ ;
    int size = 0 ;
    HashSet < Integer > toRemove = new HashSet < > ( ) ;
    for ( HashMap . Entry < Integer , Integer > tm : occ . entrySet ( ) ) {
      int mx = tm . getKey ( ) ;
      int ct = tm . getValue ( ) ;
      int use = Math . min ( ct , mx - size + 1 ) ;
      occ . put ( mx , occ . get ( mx ) - use ) ;
      size += use ;
      if ( occ . get ( mx ) == 0 ) toRemove . add ( mx ) ;
    }
    for ( int tm : toRemove ) occ . remove ( tm ) ;
  }
  return pile ;
}

static int countPiles ( int n , int [ ] a ) { HashMap < Integer , Integer > occ = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) occ . put ( a [ i ] , occ . get ( a [ i ] ) == null ? 1 : occ . get ( a [ i ] ) + 1 ) ; int pile = 0 ; while ( ! occ . isEmpty ( ) ) { pile ++ ; int size = 0 ; HashSet < Integer > toRemove = new HashSet < > ( ) ; for ( HashMap . Entry < Integer , Integer > tm : occ . entrySet ( ) ) { int mx = tm . getKey ( ) ; int ct = tm . getValue ( ) ; int use = Math . min ( ct , mx - size + 1 ) ; occ . put ( mx , occ . get ( mx ) - use ) ; size += use ; if ( occ . get ( mx ) == 0 ) toRemove . add ( mx ) ; } for ( int tm : toRemove ) occ . remove ( tm ) ; } return pile ; }
(method_declaration (modifiers) type: (integral_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (integral_type) name: (identifier)) (formal_parameter type: (array_type element: (integral_type) dimensions: (dimensions)) name: (identifier))) body: (block (local_variable_declaration type: (generic_type (type_identifier) (type_arguments (type_identifier) (type_identifier))) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (generic_type (type_identifier) (type_arguments)) arguments: (argument_list)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (identifier) index: (identifier)) (ternary_expression condition: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (identifier) index: (identifier)))) right: (null_literal)) consequence: (decimal_integer_literal) alternative: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (identifier) index: (identifier)))) right: (decimal_integer_literal))))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (while_statement condition: (parenthesized_expression (unary_expression operand: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) body: (block (expression_statement (update_expression (identifier))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (generic_type (type_identifier) (type_arguments (type_identifier))) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (generic_type (type_identifier) (type_arguments)) arguments: (argument_list)))) (enhanced_for_statement type: (generic_type (scoped_type_identifier (type_identifier) (type_identifier)) (type_arguments (type_identifier) (type_identifier))) name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal)))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (identifier))))) (expression_statement (assignment_expression left: (identifier) right: (identifier))) (if_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (decimal_integer_literal))) consequence: (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))))))) (enhanced_for_statement type: (integral_type) name: (identifier) value: (identifier) body: (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))))))) (return_statement (identifier))))