public static void main ( String [ ] args ) {
  Scanner sc = new Scanner ( System . in ) ;
  final int N = sc . nextInt ( ) ;
  final int M = sc . nextInt ( ) ;
  final int P = sc . nextInt ( ) ;
  final int Q = sc . nextInt ( ) ;
  final int R = sc . nextInt ( ) ;
  int [ ] x = new int [ R ] ;
  int [ ] y = new int [ R ] ;
  int [ ] z = new int [ R ] ;
  for ( int i = 0 ;
  i < R ;
  i ++ ) {
    x [ i ] = sc . nextInt ( ) - 1 ;
    y [ i ] = sc . nextInt ( ) - 1 ;
    z [ i ] = sc . nextInt ( ) ;
  }
  sc . close ( ) ;
  int max = ( int ) Math . pow ( 2 , N ) ;
  int ans = 0 ;
  for ( int i = 1 ;
  i < ( int ) Math . pow ( 2 , N ) ;
  i ++ ) {
    String comb = Integer . toBinaryString ( i ) ;
    int count = 0 ;
    while ( comb . length ( ) < N ) comb = "0" + comb ;
    for ( int j = 0 ;
    j < N ;
    j ++ ) if ( comb . charAt ( j ) == '1' ) count ++ ;
    if ( count == P ) {
      int [ ] boy = new int [ M ] ;
      for ( int j = 0 ;
      j < R ;
      j ++ ) {
        if ( comb . charAt ( x [ j ] ) == '1' ) boy [ y [ j ] ] += z [ j ] ;
      }
      Arrays . sort ( boy ) ;
      int tmp = 0 ;
      for ( int j = M - 1 ;
      j >= M - Q ;
      j -- ) tmp += boy [ j ] ;
      ans = Math . max ( tmp , ans ) ;
    }
  }
  System . out . println ( ans ) ;
}

public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; final int N = sc . nextInt ( ) ; final int M = sc . nextInt ( ) ; final int P = sc . nextInt ( ) ; final int Q = sc . nextInt ( ) ; final int R = sc . nextInt ( ) ; int [ ] x = new int [ R ] ; int [ ] y = new int [ R ] ; int [ ] z = new int [ R ] ; for ( int i = 0 ; i < R ; i ++ ) { x [ i ] = sc . nextInt ( ) - 1 ; y [ i ] = sc . nextInt ( ) - 1 ; z [ i ] = sc . nextInt ( ) ; } sc . close ( ) ; int max = ( int ) Math . pow ( 2 , N ) ; int ans = 0 ; for ( int i = 1 ; i < ( int ) Math . pow ( 2 , N ) ; i ++ ) { String comb = Integer . toBinaryString ( i ) ; int count = 0 ; while ( comb . length ( ) < N ) comb = "0" + comb ; for ( int j = 0 ; j < N ; j ++ ) if ( comb . charAt ( j ) == '1' ) count ++ ; if ( count == P ) { int [ ] boy = new int [ M ] ; for ( int j = 0 ; j < R ; j ++ ) { if ( comb . charAt ( x [ j ] ) == '1' ) boy [ y [ j ] ] += z [ j ] ; } Arrays . sort ( boy ) ; int tmp = 0 ; for ( int j = M - 1 ; j >= M - Q ; j -- ) tmp += boy [ j ] ; ans = Math . max ( tmp , ans ) ; } } System . out . println ( ans ) ; }
(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 (modifiers) type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration (modifiers) type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration (modifiers) type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration (modifiers) type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration (modifiers) 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))))) (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))))) (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: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (cast_expression type: (integral_type) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (identifier)))))) (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: (cast_expression type: (integral_type) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (identifier))))) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (while_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (identifier))) body: (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (string_literal) right: (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: (if_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (character_literal))) consequence: (expression_statement (update_expression (identifier))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (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: (block (if_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (identifier) index: (identifier)))) right: (character_literal))) consequence: (expression_statement (assignment_expression left: (array_access array: (identifier) index: (array_access array: (identifier) index: (identifier))) right: (array_access array: (identifier) index: (identifier))))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))) (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: (binary_expression left: (identifier) right: (decimal_integer_literal)))) condition: (binary_expression left: (identifier) right: (binary_expression left: (identifier) right: (identifier))) update: (update_expression (identifier)) body: (expression_statement (assignment_expression left: (identifier) right: (array_access array: (identifier) index: (identifier))))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (identifier))))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (identifier))))))