public static void main ( String [ ] args ) {
  Scanner in = new Scanner ( System . in ) ;
  int N = in . nextInt ( ) ;
  int [ ] [ ] D = new int [ N ] [ N ] ;
  int M = in . nextInt ( ) ;
  for ( int m = 0 ;
  m < M ;
  m ++ ) {
    int from = in . nextInt ( ) - 1 ;
    int to = in . nextInt ( ) - 1 ;
    int amount = in . nextInt ( ) ;
    D [ from ] [ to ] = amount ;
    D [ to ] [ from ] = - amount ;
  }
  for ( int n = 0 ;
  n < N ;
  n ++ ) {
    int pos = 0 ;
    int neg = 0 ;
    while ( true ) {
      while ( pos < N && D [ pos ] [ n ] <= 0 ) pos ++ ;
      if ( pos == N ) break ;
      while ( neg < N && D [ neg ] [ n ] >= 0 ) neg ++ ;
      if ( neg == N ) break ;
      int transfer = Math . min ( D [ pos ] [ n ] , - D [ neg ] [ n ] ) ;
      D [ pos ] [ n ] -= transfer ;
      D [ n ] [ pos ] += transfer ;
      D [ neg ] [ n ] += transfer ;
      D [ n ] [ neg ] -= transfer ;
      D [ pos ] [ neg ] += transfer ;
      D [ neg ] [ pos ] -= transfer ;
    }
  }
  int answer = 0 ;
  for ( int from = 0 ;
  from < N ;
  from ++ ) {
    for ( int to = from + 1 ;
    to < N ;
    to ++ ) {
      answer += Math . abs ( D [ from ] [ to ] ) ;
    }
  }
  System . out . println ( answer ) ;
}

public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; int N = in . nextInt ( ) ; int [ ] [ ] D = new int [ N ] [ N ] ; int M = in . nextInt ( ) ; for ( int m = 0 ; m < M ; m ++ ) { int from = in . nextInt ( ) - 1 ; int to = in . nextInt ( ) - 1 ; int amount = in . nextInt ( ) ; D [ from ] [ to ] = amount ; D [ to ] [ from ] = - amount ; } for ( int n = 0 ; n < N ; n ++ ) { int pos = 0 ; int neg = 0 ; while ( true ) { while ( pos < N && D [ pos ] [ n ] <= 0 ) pos ++ ; if ( pos == N ) break ; while ( neg < N && D [ neg ] [ n ] >= 0 ) neg ++ ; if ( neg == N ) break ; int transfer = Math . min ( D [ pos ] [ n ] , - D [ neg ] [ n ] ) ; D [ pos ] [ n ] -= transfer ; D [ n ] [ pos ] += transfer ; D [ neg ] [ n ] += transfer ; D [ n ] [ neg ] -= transfer ; D [ pos ] [ neg ] += transfer ; D [ neg ] [ pos ] -= transfer ; } } int answer = 0 ; for ( int from = 0 ; from < N ; from ++ ) { for ( int to = from + 1 ; to < N ; to ++ ) { answer += Math . abs ( D [ from ] [ to ] ) ; } } System . out . println ( answer ) ; }
(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: (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))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (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: (identifier)) update: (update_expression (identifier)) body: (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)))) (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)))) (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: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (identifier))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (unary_expression operand: (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 (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))) (while_statement condition: (parenthesized_expression (true)) body: (block (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (decimal_integer_literal)))) body: (expression_statement (update_expression (identifier)))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (break_statement)) (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (decimal_integer_literal)))) body: (expression_statement (update_expression (identifier)))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (break_statement)) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) (unary_expression operand: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier))))))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (identifier))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (identifier))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (identifier))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (identifier))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (identifier))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (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: (identifier)) update: (update_expression (identifier)) body: (block (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 (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)))))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (identifier))))))