{-# LANGUAGE ViewPatterns #-} import qualified Data.Sequence as Sq import Data.Sequence (ViewL(..),ViewR(..),(><),(<|),(|>),Seq) import Data.List f::[String]->[(Int,Int,Int)] f []=[] f (s:ss)=let (x:y:c:[])=map read (words s)::[Int] in (x,y,c):f ss left :: Seq a -> a left (Sq.viewl -> (l :< _)) = l left2 :: Seq a -> Seq a left2 (Sq.viewl -> (_ :< ls)) = ls f3::Sq.Seq (Int,Int)->Sq.Seq (Int,Int)->Sq.Seq (Int,Int) f3 old now |Sq.null old=now |otherwise=let a=left old old2=left2 old in f3 old2 (now |> a) f2::Int->Int->Int->Int->[(Int,Int,Int)]->Sq.Seq (Int,Int)->Sq.Seq (Int,Int)->Int f2 _ ans _ _ [] _ _=ans f2 x ans nowC nowC2 xs3@((x2,y2,c2):xs2) old now |x (y2,c4)) |otherwise=let (y3,c3)=left old old2=left2 old c5=(max nowC c3) c6=(max nowC2 (c5+c2)) in if y2 > y3 then f2 x (max ans c6) c5 c6 xs3 old2 (now |> (y3,c5)) else f2 x (max ans c5) nowC nowC2 xs2 old (now |> (y2,(max (nowC+c2) nowC2))) f4::(Int,Int,Int)->Int f4 (a,_,_)=a f5::[(Int,Int,Int)]->[(Int,Int,Int)] f5 (x:[])=[x] f5 ((x,y,c):(x1,y1,c1):xs) |x==x1&& y==y1=f5 ((x1,y1,c1):xs) |otherwise=(x,y,c):f5 ((x1,y1,c1):xs) main = do e<-getLine es<-getContents let q=(Sq.fromList [(0::Int,0::Int)]) xs=f5 $ sort $ f $ lines es print $ f2 (f4 (head xs)) 0 0 0 xs q q