public static void main ( String [ ] args ) {
  Scanner sc = new Scanner ( System . in ) ;
  int N = sc . nextInt ( ) ;
  int ar [ ] = new int [ N ] ;
  boolean isDivisibleByTen = false ;
  int sum = 0 ;
  int zeroCount = 0 ;
  for ( int i = 0 ;
  i < N ;
  i ++ ) {
    int val = sc . nextInt ( ) ;
    sum += val ;
    if ( val == 0 ) {
      isDivisibleByTen = true ;
      zeroCount ++ ;
    }
  }
  if ( ! isDivisibleByTen ) System . out . println ( - 1 ) ;
  else {
    int val = sum / 45 ;
    StringBuilder sb = new StringBuilder ( ) ;
    for ( int i = 0 ;
    i < 9 * val ;
    i ++ ) {
      sb . append ( 5 ) ;
    }
    for ( int i = 0 ;
    i < zeroCount ;
    i ++ ) {
      if ( sb . toString ( ) . length ( ) == 0 ) {
        sb . append ( 0 ) ;
        break ;
      }
      else sb . append ( 0 ) ;
    }
    System . out . println ( sb . toString ( ) ) ;
  }
}

public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; int ar [ ] = new int [ N ] ; boolean isDivisibleByTen = false ; int sum = 0 ; int zeroCount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int val = sc . nextInt ( ) ; sum += val ; if ( val == 0 ) { isDivisibleByTen = true ; zeroCount ++ ; } } if ( ! isDivisibleByTen ) System . out . println ( - 1 ) ; else { int val = sum / 45 ; StringBuilder sb = new StringBuilder ( ) ; for ( int i = 0 ; i < 9 * val ; i ++ ) { sb . append ( 5 ) ; } for ( int i = 0 ; i < zeroCount ; i ++ ) { if ( sb . toString ( ) . length ( ) == 0 ) { sb . append ( 0 ) ; break ; } else sb . append ( 0 ) ; } System . out . println ( sb . toString ( ) ) ; } }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (type_identifier) dimensions: (dimensions)) name: (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) 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 (identifier))))) (local_variable_declaration type: (boolean_type) declarator: (variable_declarator name: (identifier) value: (false))) (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: (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 (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (expression_statement (assignment_expression left: (identifier) right: (identifier))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (true))) (expression_statement (update_expression (identifier))))))) (if_statement condition: (parenthesized_expression (unary_expression operand: (identifier))) consequence: (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (unary_expression operand: (decimal_integer_literal))))) alternative: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (identifier) right: (decimal_integer_literal)))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) 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: (binary_expression left: (decimal_integer_literal) right: (identifier))) update: (update_expression (identifier)) body: (block (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (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 (if_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal))) consequence: (block (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal)))) (break_statement)) alternative: (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))))))))