private static String solve ( Scanner in ) {
  String s = in . nextLine ( ) . trim ( ) ;
  if ( s . length ( ) == 1 ) return s + "0" ;
  char [ ] a = s . toCharArray ( ) ;
  int i = s . length ( ) - 2 ;
  while ( i > - 1 && a [ i ] >= a [ i + 1 ] ) i -- ;
  if ( i == - 1 ) {
    Arrays . sort ( a ) ;
    i = 0 ;
    while ( a [ i ] == '0' ) i ++ ;
    char p = a [ i ] ;
    a [ i ] = '0' ;
    return p + new String ( a ) ;
  }
  else {
    int j = s . length ( ) - 1 ;
    while ( a [ j ] <= a [ i ] ) j -- ;
    char t = a [ i ] ;
    a [ i ] = a [ j ] ;
    a [ j ] = t ;
    Arrays . sort ( a , i + 1 , a . length ) ;
    return new String ( a ) ;
  }
}

private static String solve ( Scanner in ) { String s = in . nextLine ( ) . trim ( ) ; if ( s . length ( ) == 1 ) return s + "0" ; char [ ] a = s . toCharArray ( ) ; int i = s . length ( ) - 2 ; while ( i > - 1 && a [ i ] >= a [ i + 1 ] ) i -- ; if ( i == - 1 ) { Arrays . sort ( a ) ; i = 0 ; while ( a [ i ] == '0' ) i ++ ; char p = a [ i ] ; a [ i ] = '0' ; return p + new String ( a ) ; } else { int j = s . length ( ) - 1 ; while ( a [ j ] <= a [ i ] ) j -- ; char t = a [ i ] ; a [ i ] = a [ j ] ; a [ j ] = t ; Arrays . sort ( a , i + 1 , a . length ) ; return new String ( a ) ; } }
(method_declaration (modifiers) type: (type_identifier) name: (identifier) parameters: (formal_parameters (formal_parameter type: (type_identifier) name: (identifier))) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) name: (identifier) arguments: (argument_list)))) (if_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal))) consequence: (return_statement (binary_expression left: (identifier) right: (string_literal)))) (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)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))) (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (unary_expression operand: (decimal_integer_literal))) right: (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal)))))) body: (expression_statement (update_expression (identifier)))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (unary_expression operand: (decimal_integer_literal)))) consequence: (block (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))) (expression_statement (assignment_expression left: (identifier) right: (decimal_integer_literal))) (while_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (character_literal))) body: (expression_statement (update_expression (identifier)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (array_access array: (identifier) index: (identifier)))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (character_literal))) (return_statement (binary_expression left: (identifier) right: (object_creation_expression type: (type_identifier) arguments: (argument_list (identifier)))))) alternative: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))) (while_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (identifier)))) body: (expression_statement (update_expression (identifier)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (array_access array: (identifier) index: (identifier)))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (identifier)))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (identifier))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (identifier) right: (decimal_integer_literal)) (field_access object: (identifier) field: (identifier))))) (return_statement (object_creation_expression type: (type_identifier) arguments: (argument_list (identifier))))))))