import Data.Array import Data.List import Control.Monad main = do li <- getLine let (n:m:_) = map read $ words li cs <- forM [1..m] $ \_ -> do li <- getLine let (i1:i2:s:_) = map read $ words li return ((seq i1 i1,seq i2 i2),seq s s) print $ compute n cs compute :: Int -> [((Int,Int),Int)] -> Int compute n cs = maximum $ step [0..n-1] where ar = accumArray (+) 0 ((0,0),(n-1,n-1)) cs step [] = [ 0 ] step xs = [ s0 + s1 | x <- xs , let xs1 = delete x xs , let s0 = sum [ ar ! (x,y) | y <- xs1 ] , s1 <- step xs1 ]