public static void main ( String [ ] args ) throws IOException {
  FastScanner fs = new FastScanner ( ) ;
  PrintWriter out = new PrintWriter ( System . out ) ;
  int tt = 1 ;
  while ( tt -- > 0 ) {
    int n = fs . nextInt ( ) ;
    int [ ] c = new int [ maxn ] ;
    for ( int i = 0 ;
    i < n ;
    i ++ ) c [ i ] = fs . nextInt ( ) ;
    int [ ] [ ] dp = new int [ maxn ] [ maxn ] ;
    for ( int len = 1 ;
    len <= n ;
    len ++ ) {
      for ( int i = 0 ;
      i + len - 1 < n ;
      i ++ ) {
        int end = i + len - 1 ;
        if ( len == 1 ) {
          dp [ i ] [ end ] = 1 ;
        }
        else {
          dp [ i ] [ end ] = 1 + dp [ i + 1 ] [ end ] ;
          if ( c [ i ] == c [ i + 1 ] ) {
            dp [ i ] [ end ] = Math . min ( dp [ i ] [ end ] , 1 + dp [ i + 2 ] [ end ] ) ;
          }
          for ( int j = i + 2 ;
          j <= end ;
          j ++ ) {
            if ( c [ i ] == c [ j ] ) {
              dp [ i ] [ end ] = Math . min ( dp [ i ] [ end ] , dp [ i + 1 ] [ j - 1 ] + dp [ j + 1 ] [ end ] ) ;
            }
          }
        }
      }
    }
    out . println ( dp [ 0 ] [ n - 1 ] ) ;
  }
  out . close ( ) ;
}

public static void main ( String [ ] args ) throws IOException { FastScanner fs = new FastScanner ( ) ; PrintWriter out = new PrintWriter ( System . out ) ; int tt = 1 ; while ( tt -- > 0 ) { int n = fs . nextInt ( ) ; int [ ] c = new int [ maxn ] ; for ( int i = 0 ; i < n ; i ++ ) c [ i ] = fs . nextInt ( ) ; int [ ] [ ] dp = new int [ maxn ] [ maxn ] ; for ( int len = 1 ; len <= n ; len ++ ) { for ( int i = 0 ; i + len - 1 < n ; i ++ ) { int end = i + len - 1 ; if ( len == 1 ) { dp [ i ] [ end ] = 1 ; } else { dp [ i ] [ end ] = 1 + dp [ i + 1 ] [ end ] ; if ( c [ i ] == c [ i + 1 ] ) { dp [ i ] [ end ] = Math . min ( dp [ i ] [ end ] , 1 + dp [ i + 2 ] [ end ] ) ; } for ( int j = i + 2 ; j <= end ; j ++ ) { if ( c [ i ] == c [ j ] ) { dp [ i ] [ end ] = Math . min ( dp [ i ] [ end ] , dp [ i + 1 ] [ j - 1 ] + dp [ j + 1 ] [ end ] ) ; } } } } } out . println ( dp [ 0 ] [ n - 1 ] ) ; } out . close ( ) ; }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (type_identifier) dimensions: (dimensions)) name: (identifier))) (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)))) (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) value: (decimal_integer_literal))) (while_statement condition: (parenthesized_expression (binary_expression left: (update_expression (identifier)) right: (decimal_integer_literal))) 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: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (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: (identifier)) update: (update_expression (identifier)) body: (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))))) (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier)) dimensions: (dimensions_expr (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: (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: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal)) right: (identifier)) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal)))) (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: (identifier) index: (identifier)) index: (identifier)) right: (decimal_integer_literal)))) alternative: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (binary_expression left: (decimal_integer_literal) right: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier))))) (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))))) consequence: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) (binary_expression left: (decimal_integer_literal) right: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier))))))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (identifier) right: (decimal_integer_literal)))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (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: (identifier) index: (identifier)) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) (binary_expression left: (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))) right: (array_access array: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) index: (identifier))))))))))))))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (identifier) index: (decimal_integer_literal)) index: (binary_expression left: (identifier) right: (decimal_integer_literal)))))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))))