public static void main ( String args [ ] ) throws IOException {
  Scanner sc = new Scanner ( System . in ) ;
  char s [ ] = sc . next ( ) . toCharArray ( ) ;
  int k = sc . nextInt ( ) ;
  int dp [ ] [ ] [ ] = new int [ s . length ] [ s . length ] [ k + 1 ] ;
  for ( int i = 0 ;
  i <= k ;
  i ++ ) {
    for ( int r = 0 ;
    r < s . length ;
    r ++ ) {
      for ( int l = r ;
      l >= 0 ;
      l -- ) {
        if ( l == r ) {
          dp [ l ] [ r ] [ i ] = 1 ;
          continue ;
        }
        if ( r == l + 1 ) {
          dp [ l ] [ r ] [ i ] = s [ l ] == s [ r ] || i > 0 ? 2 : 1 ;
          continue ;
        }
        if ( s [ l ] == s [ r ] ) {
          dp [ l ] [ r ] [ i ] = dp [ l + 1 ] [ r - 1 ] [ i ] + 2 ;
        }
        else {
          dp [ l ] [ r ] [ i ] = Math . max ( dp [ l + 1 ] [ r ] [ i ] , dp [ l ] [ r - 1 ] [ i ] ) ;
        }
        if ( i > 0 ) {
          dp [ l ] [ r ] [ i ] = Math . max ( dp [ l ] [ r ] [ i ] , dp [ l + 1 ] [ r - 1 ] [ i - 1 ] + 2 ) ;
        }
      }
    }
  }
  System . out . println ( dp [ 0 ] [ s . length - 1 ] [ k ] ) ;
}

public static void main ( String args [ ] ) throws IOException { Scanner sc = new Scanner ( System . in ) ; char s [ ] = sc . next ( ) . toCharArray ( ) ; int k = sc . nextInt ( ) ; int dp [ ] [ ] [ ] = new int [ s . length ] [ s . length ] [ k + 1 ] ; for ( int i = 0 ; i <= k ; i ++ ) { for ( int r = 0 ; r < s . length ; r ++ ) { for ( int l = r ; l >= 0 ; l -- ) { if ( l == r ) { dp [ l ] [ r ] [ i ] = 1 ; continue ; } if ( r == l + 1 ) { dp [ l ] [ r ] [ i ] = s [ l ] == s [ r ] || i > 0 ? 2 : 1 ; continue ; } if ( s [ l ] == s [ r ] ) { dp [ l ] [ r ] [ i ] = dp [ l + 1 ] [ r - 1 ] [ i ] + 2 ; } else { dp [ l ] [ r ] [ i ] = Math . max ( dp [ l + 1 ] [ r ] [ i ] , dp [ l ] [ r - 1 ] [ i ] ) ; } if ( i > 0 ) { dp [ l ] [ r ] [ i ] = Math . max ( dp [ l ] [ r ] [ i ] , dp [ l + 1 ] [ r - 1 ] [ i - 1 ] + 2 ) ; } } } } System . out . println ( dp [ 0 ] [ s . length - 1 ] [ k ] ) ; }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (type_identifier) name: (identifier) dimensions: (dimensions))) (throws (type_identifier)) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (field_access object: (identifier) field: (identifier)))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) 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) dimensions: (dimensions) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (field_access object: (identifier) field: (identifier))) dimensions: (dimensions_expr (field_access object: (identifier) field: (identifier))) dimensions: (dimensions_expr (binary_expression left: (identifier) right: (decimal_integer_literal)))))) (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: (block (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 (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (identifier))) condition: (binary_expression left: (identifier) right: (decimal_integer_literal)) update: (update_expression (identifier)) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) index: (identifier)) right: (decimal_integer_literal))) (continue_statement))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (binary_expression left: (identifier) right: (decimal_integer_literal)))) consequence: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) index: (identifier)) right: (ternary_expression condition: (binary_expression left: (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (identifier))) right: (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (decimal_integer_literal) alternative: (decimal_integer_literal)))) (continue_statement))) (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (identifier)))) consequence: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) index: (identifier)) right: (binary_expression left: (array_access array: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier)) right: (decimal_integer_literal))))) alternative: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier)) index: (identifier)) (array_access array: (array_access array: (array_access array: (identifier) index: (identifier)) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier)))))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) index: (identifier)) (binary_expression left: (array_access array: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (decimal_integer_literal)))))))))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (array_access array: (identifier) index: (decimal_integer_literal)) index: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (decimal_integer_literal))) index: (identifier)))))))