import Control.Monad
import qualified Data.ByteString.Char8 as C
import Data.Function
import Data.List
import Data.Ord

main = C.interact $ put . sol . get

get = fmap (unfoldr (C.readInt . C.dropWhile (==' '))) . C.lines

put = C.pack . show

sol ([_,w]:wvs) = maximum . fmap last
  . filter ((<=w) . head) . fmap (foldr1 (zipWith (+)))
  . sequenceA . fmap (scanl (zipWith (+)) [0,0])
  . groupBy ((==) `on` head) $ sortBy (flip compare) wvs