{-# LANGUAGE
  ScopedTypeVariables, BangPatterns, TupleSections, ExplicitForAll,
  LambdaCase, MultiWayIf, Unsafe, RecordWildCards, FlexibleContexts, CPP,
  NoMonomorphismRestriction, GADTs, PatternGuards, MagicHash, UnboxedTuples,
  RankNTypes, EmptyDataDecls, EmptyCase, ViewPatterns, PolyKinds #-}
{-# OPTIONS_GHC -O2 #-}

import Prelude
import Data.Bits
import Data.List
import Data.Maybe
import Data.Tuple
import Data.Ord
import Data.Int
import Data.Word
import Data.Char
import Data.Ratio
import Data.Function
import Data.STRef
import Data.IORef
import Data.Monoid
import Data.Functor
import System.IO
import Control.Arrow
import Control.Applicative
import Control.Monad
import Control.Monad.Primitive
import Control.Monad.State.Strict
import Control.Monad.ST
import Control.Monad.ST.Lazy (strictToLazyST, lazyToStrictST)
import qualified Control.Monad.ST.Lazy as STLz
-- import Control.Monad.ST.Safe
import Control.DeepSeq
import Data.Coerce
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import qualified Data.ByteString.Builder as BSB
import Data.IntMap (IntMap)
import qualified Data.IntMap.Strict as IMS
import qualified Data.IntMap.Lazy as IML
import Data.IntSet (IntSet)
import qualified Data.IntSet as IS
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.Vector.Storable as VS
import qualified Data.Vector.Storable.Mutable as VSM
import qualified Data.Vector.Primitive as VP
import qualified Data.Vector.Primitive.Mutable as VPM
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Generic.Mutable as VGM
import qualified Data.Attoparsec.ByteString.Char8 as Atto
import GHC.Exts (build, Int(..), Int#, 
                 (+#), (*#), (-#), (<#), (>=#), (==#), quotRemInt#,
                 remInt#, uncheckedIShiftRL#, andI#, orI#,
                isTrue#)
import Debug.Trace


main :: IO ()
main = do
  [n,x] <- map readInt . words <$> getLine
  blus <- VU.unfoldrN n (runStateT $ (,,) <$> rInt <*> rInt <*> rInt)
          <$> BSL.getContents
  print $ query n x blus

query :: Int -> Int -> VU.Vector (Int,Int,Int) -> Int
query n x blus
  | totLoss <= 0 = 0
  | rest == 0 = retained * x
  | otherwise = (retained * x +)
    $ VU.minimum $ (`VU.imap` VU.takeWhile (\(v,b,l,u) -> v>=rest) vblus)
    $ \i (!v,!b,!l,!u) ->
      if | i < retained -> let trueRest = rest + v - vret
                           in min ((trueRest + l-1) `div` l)
                                  ((trueRest + (u-l)*b + u-1) `div` u)
         | otherwise -> min ((rest + l-1) `div` l)
                            ((rest + (u-l)*b + u-1) `div` u)
  where
    vblus :: VU.Vector (Int,Int,Int,Int)
    vblus = VU.create $ do
      vec <- VU.unsafeThaw $ VU.map (\(b,l,u) -> (u*(x-b) + l*b,b,l,u)) blus
             :: ST s (VUM.MVector s (Int,Int,Int,Int))
      vam_sortBy (compare `on` (\(v,b,l,u) -> Down v)) vec
      return vec
    totLoss = VU.sum $ VU.map (\(v,b,l,u) -> b*l) vblus
    retained::Int
    rest::Int
    (vret,_,_,_) = vblus VU.! retained
    (retained,rest)
      = VU.ifoldr
        (\ !i (!v,!b,!l,!u) cont !rest ->
           case compare rest v of
             GT -> cont $! rest - v
             EQ -> (i+1,0)
             LT -> (i,rest)) undefined vblus totLoss
             
    
        
{-
The code below is the modified version of what I borrowed from the modules
Data.Vector.Algorithms.Common/Optimal/Insertion/Merge
in the package vector-algorithms-0.8.0.1 on Hackage.

The copyright notice follows:

Copyright (c) 2015 Dan Doel
Copyright (c) 2015 Tim Baumann

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

3. Neither the name of the author nor the names of his contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

------------------------------------------------------------------------------

The code in Data.Array.Vector.Algorithms.Mutable.Optimal is adapted from a C
algorithm for the same purpose. The folowing is the copyright notice for said
C code:

Copyright (c) 2004 Paul Hsieh
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.

    Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

    Neither the name of sorttest nor the names of its contributors may be
    used to endorse or promote products derived from this software without
    specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
-}

-- | Sorts an array using the default comparison.
vam_sort :: (PrimMonad m, VGM.MVector v e, Ord e)
  => v (PrimState m) e -> m ()
vam_sort = vam_sortBy compare
{-# INLINABLE vam_sort #-}

-- | sorts an array using a custom comparison.
vam_sortBy :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> m ()
vam_sortBy cmp vec
  | len <= 1  = return ()
  | len == 2  = vao_sort2ByOffset cmp vec 0
  | len == 3  = vao_sort3ByOffset cmp vec 0
  | len == 4  = vao_sort4ByOffset cmp vec 0
  | otherwise = do
      buf <- VGM.new len
      vam_mergeSortWithBuf cmp vec buf
 where
 len = VGM.length vec
{-# INLINE vam_sortBy #-}

vam_mergeSortWithBuf :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> v (PrimState m) e -> m ()
vam_mergeSortWithBuf cmp src buf = loop 0 (VGM.length src)
 where
   loop l u
     | len < vam_threshold = vai_sortByBounds cmp src l u
     | otherwise = do
         loop l mid
         loop mid u
         vam_merge cmp (VGM.unsafeSlice l len src) buf (mid - l)
           where len = u - l; mid = vac_midPoint u l
{-# INLINE vam_mergeSortWithBuf #-}

vam_merge :: (PrimMonad m, VGM.MVector v e)
      => Comparison e -> v (PrimState m) e -> v (PrimState m) e
      -> Int -> m ()
vam_merge cmp src buf mid = do
  VGM.unsafeCopy tmp lower
  eTmp <- VGM.unsafeRead tmp 0
  eUpp <- VGM.unsafeRead upper 0
  loop tmp 0 eTmp upper 0 eUpp 0
 where
   lower = VGM.unsafeSlice 0   mid src
   upper = VGM.unsafeSlice mid (VGM.length src - mid) src
   tmp   = VGM.unsafeSlice 0   mid buf

   wroteHigh low iLow eLow high iHigh iIns
    | iHigh >= VGM.length high
      = VGM.unsafeCopy (VGM.unsafeSlice iIns (VGM.length low - iLow) src)
        (VGM.unsafeSlice iLow (VGM.length low - iLow) low)
    | otherwise = do
        eHigh <- VGM.unsafeRead high iHigh
        loop low iLow eLow high iHigh eHigh iIns

   wroteLow low iLow high iHigh eHigh iIns
    | iLow  >= VGM.length low  = return ()
    | otherwise = do
        eLow <- VGM.unsafeRead low iLow
        loop low iLow eLow high iHigh eHigh iIns

   loop !low !iLow !eLow !high !iHigh !eHigh !iIns = case cmp eHigh eLow of
     LT -> do VGM.unsafeWrite src iIns eHigh
              wroteHigh low iLow eLow high (iHigh + 1) (iIns + 1)
     _  -> do VGM.unsafeWrite src iIns eLow
              wroteLow low (iLow + 1) high iHigh eHigh (iIns + 1)
{-# INLINE vam_merge #-}

vam_threshold :: Int
vam_threshold = 25
{-# INLINE vam_threshold #-}

-- | Sorts an entire array using the default comparison for the type
vai_sort :: (PrimMonad m, VGM.MVector v e, Ord e) => v (PrimState m) e -> m ()
vai_sort = vai_sortBy compare
{-# INLINABLE vai_sort #-}

-- | Sorts an entire array using a given comparison
vai_sortBy :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> m ()
vai_sortBy cmp a = vai_sortByBounds cmp a 0 (VGM.length a)
{-# INLINE vai_sortBy #-}

-- | Sorts the portion of an array delimited by [l,u)
vai_sortByBounds :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> Int -> Int -> m ()
vai_sortByBounds cmp a l u
  | len < 2   = return ()
  | len == 2  = vao_sort2ByOffset cmp a l
  | len == 3  = vao_sort3ByOffset cmp a l
  | len == 4  = vao_sort4ByOffset cmp a l
  | otherwise = vao_sort4ByOffset cmp a l >>
                vai_sortByBounds' cmp a l (l + 4) u
 where
 len = u - l
{-# INLINE vai_sortByBounds #-}

-- | Sorts the portion of the array delimited by [l,u) under the assumption
-- that [l,m) is already sorted.
vai_sortByBounds' :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> Int -> Int -> Int -> m ()
vai_sortByBounds' cmp a l m u = sort m
 where
 sort i
   | i < u     = do
       v <- VGM.unsafeRead a i
       vai_insert cmp a l v i
       sort (i+1)
   | otherwise = return ()
{-# INLINE vai_sortByBounds' #-}

-- Given a sorted array in [l,u), inserts val into its proper position,
-- yielding a sorted [l,u]
vai_insert :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> Int -> e -> Int -> m ()
vai_insert cmp a l = loop
 where
 loop val j
   | j <= l    = VGM.unsafeWrite a l val
   | otherwise = do
       e <- VGM.unsafeRead a (j - 1)
       case cmp val e of
         LT -> VGM.unsafeWrite a j e >> loop val (j - 1)
         _  -> VGM.unsafeWrite a j val
{-# INLINE vai_insert #-}

-- | Sorts the elements at the positions 'off' and 'off + 1' in the given
-- array using the comparison.
vao_sort2ByOffset :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> Int -> m ()
vao_sort2ByOffset cmp a off = vao_sort2ByIndex cmp a off (off + 1)
{-# INLINABLE vao_sort2ByOffset #-}

-- | Sorts the elements at the two given indices using the comparison. This
-- is essentially a compare-and-swap, although the first index is assumed to
-- be the 'lower' of the two.
vao_sort2ByIndex :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> Int -> Int -> m ()
vao_sort2ByIndex cmp a i j = do
  a0 <- VGM.unsafeRead a i
  a1 <- VGM.unsafeRead a j
  case cmp a0 a1 of
    GT -> VGM.unsafeWrite a i a1 >> VGM.unsafeWrite a j a0
    _  -> return ()
{-# INLINABLE vao_sort2ByIndex #-}

-- | Sorts the three elements starting at the given offset in the array.
vao_sort3ByOffset :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> Int -> m ()
vao_sort3ByOffset cmp a off = vao_sort3ByIndex cmp a off (off + 1) (off + 2)
{-# INLINABLE vao_sort3ByOffset #-}

-- | Sorts the elements at the three given indices. The indices are assumed
-- to be given from lowest to highest, so if 'l < m < u' then
-- 'vao_sort3ByIndex cmp a m l u' essentially sorts the median of three into the
-- lowest position in the array.
vao_sort3ByIndex :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> Int -> Int -> Int -> m ()
vao_sort3ByIndex cmp a i j k = do
  a0 <- VGM.unsafeRead a i
  a1 <- VGM.unsafeRead a j
  a2 <- VGM.unsafeRead a k
  case cmp a0 a1 of
    GT -> case cmp a0 a2 of
            GT -> case cmp a2 a1 of
                    LT -> do VGM.unsafeWrite a i a2
                             VGM.unsafeWrite a k a0
                    _  -> do VGM.unsafeWrite a i a1
                             VGM.unsafeWrite a j a2
                             VGM.unsafeWrite a k a0
            _  -> do VGM.unsafeWrite a i a1
                     VGM.unsafeWrite a j a0
    _  -> case cmp a1 a2 of
            GT -> case cmp a0 a2 of
                    GT -> do VGM.unsafeWrite a i a2
                             VGM.unsafeWrite a j a0
                             VGM.unsafeWrite a k a1
                    _  -> do VGM.unsafeWrite a j a2
                             VGM.unsafeWrite a k a1
            _  -> return ()
{-# INLINABLE vao_sort3ByIndex #-}

-- | Sorts the four elements beginning at the offset.
vao_sort4ByOffset :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> Int -> m ()
vao_sort4ByOffset cmp a off
  = vao_sort4ByIndex cmp a off (off + 1) (off + 2) (off + 3)
{-# INLINABLE vao_sort4ByOffset #-}

-- The horror...

-- | Sorts the elements at the four given indices. Like the 2 and 3 element
-- versions, this assumes that the indices are given in increasing order, so
-- it can be used to sort medians into particular positions and so on.
vao_sort4ByIndex :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> Int -> Int -> Int -> Int -> m ()
vao_sort4ByIndex cmp a i j k l = do
  a0 <- VGM.unsafeRead a i
  a1 <- VGM.unsafeRead a j
  a2 <- VGM.unsafeRead a k
  a3 <- VGM.unsafeRead a l
  case cmp a0 a1 of
    GT -> case cmp a0 a2 of
            GT -> case cmp a1 a2 of
                    GT -> case cmp a1 a3 of
                            GT -> case cmp a2 a3 of
                                    GT -> do VGM.unsafeWrite a i a3
                                             VGM.unsafeWrite a j a2
                                             VGM.unsafeWrite a k a1
                                             VGM.unsafeWrite a l a0
                                    _  -> do VGM.unsafeWrite a i a2
                                             VGM.unsafeWrite a j a3
                                             VGM.unsafeWrite a k a1
                                             VGM.unsafeWrite a l a0
                            _  -> case cmp a0 a3 of
                                    GT -> do VGM.unsafeWrite a i a2
                                             VGM.unsafeWrite a j a1
                                             VGM.unsafeWrite a k a3
                                             VGM.unsafeWrite a l a0
                                    _  -> do VGM.unsafeWrite a i a2
                                             VGM.unsafeWrite a j a1
                                             VGM.unsafeWrite a k a0
                                             VGM.unsafeWrite a l a3
                    _ -> case cmp a2 a3 of
                           GT -> case cmp a1 a3 of
                                   GT -> do VGM.unsafeWrite a i a3
                                            VGM.unsafeWrite a j a1
                                            VGM.unsafeWrite a k a2
                                            VGM.unsafeWrite a l a0
                                   _  -> do VGM.unsafeWrite a i a1
                                            VGM.unsafeWrite a j a3
                                            VGM.unsafeWrite a k a2
                                            VGM.unsafeWrite a l a0
                           _  -> case cmp a0 a3 of
                                   GT -> do VGM.unsafeWrite a i a1
                                            VGM.unsafeWrite a j a2
                                            VGM.unsafeWrite a k a3
                                            VGM.unsafeWrite a l a0
                                   _  -> do VGM.unsafeWrite a i a1
                                            VGM.unsafeWrite a j a2
                                            VGM.unsafeWrite a k a0
                                            -- VGM.unsafeWrite a l a3
            _  -> case cmp a0 a3 of
                    GT -> case cmp a1 a3 of
                            GT -> do VGM.unsafeWrite a i a3
                                     -- VGM.unsafeWrite a j a1
                                     VGM.unsafeWrite a k a0
                                     VGM.unsafeWrite a l a2
                            _  -> do VGM.unsafeWrite a i a1
                                     VGM.unsafeWrite a j a3
                                     VGM.unsafeWrite a k a0
                                     VGM.unsafeWrite a l a2
                    _  -> case cmp a2 a3 of
                            GT -> do VGM.unsafeWrite a i a1
                                     VGM.unsafeWrite a j a0
                                     VGM.unsafeWrite a k a3
                                     VGM.unsafeWrite a l a2
                            _  -> do VGM.unsafeWrite a i a1
                                     VGM.unsafeWrite a j a0
                                     -- VGM.unsafeWrite a k a2
                                     -- VGM.unsafeWrite a l a3
    _  -> case cmp a1 a2 of
            GT -> case cmp a0 a2 of
                    GT -> case cmp a0 a3 of
                            GT -> case cmp a2 a3 of
                                    GT -> do VGM.unsafeWrite a i a3
                                             VGM.unsafeWrite a j a2
                                             VGM.unsafeWrite a k a0
                                             VGM.unsafeWrite a l a1
                                    _  -> do VGM.unsafeWrite a i a2
                                             VGM.unsafeWrite a j a3
                                             VGM.unsafeWrite a k a0
                                             VGM.unsafeWrite a l a1
                            _  -> case cmp a1 a3 of
                                    GT -> do VGM.unsafeWrite a i a2
                                             VGM.unsafeWrite a j a0
                                             VGM.unsafeWrite a k a3
                                             VGM.unsafeWrite a l a1
                                    _  -> do VGM.unsafeWrite a i a2
                                             VGM.unsafeWrite a j a0
                                             VGM.unsafeWrite a k a1
                                             -- VGM.unsafeWrite a l a3
                    _  -> case cmp a2 a3 of
                            GT -> case cmp a0 a3 of
                                    GT -> do VGM.unsafeWrite a i a3
                                             VGM.unsafeWrite a j a0
                                             -- VGM.unsafeWrite a k a2
                                             VGM.unsafeWrite a l a1
                                    _  -> do -- VGM.unsafeWrite a i a0
                                             VGM.unsafeWrite a j a3
                                             -- VGM.unsafeWrite a k a2
                                             VGM.unsafeWrite a l a1
                            _  -> case cmp a1 a3 of
                                    GT -> do -- VGM.unsafeWrite a i a0
                                             VGM.unsafeWrite a j a2
                                             VGM.unsafeWrite a k a3
                                             VGM.unsafeWrite a l a1
                                    _  -> do -- VGM.unsafeWrite a i a0
                                             VGM.unsafeWrite a j a2
                                             VGM.unsafeWrite a k a1
                                             -- VGM.unsafeWrite a l a3
            _  -> case cmp a1 a3 of
                    GT -> case cmp a0 a3 of
                            GT -> do VGM.unsafeWrite a i a3
                                     VGM.unsafeWrite a j a0
                                     VGM.unsafeWrite a k a1
                                     VGM.unsafeWrite a l a2
                            _  -> do -- VGM.unsafeWrite a i a0
                                     VGM.unsafeWrite a j a3
                                     VGM.unsafeWrite a k a1
                                     VGM.unsafeWrite a l a2
                    _  -> case cmp a2 a3 of
                            GT -> do -- VGM.unsafeWrite a i a0
                                     -- VGM.unsafeWrite a j a1
                                     VGM.unsafeWrite a k a3
                                     VGM.unsafeWrite a l a2
                            _  -> do -- VGM.unsafeWrite a i a0
                                     -- VGM.unsafeWrite a j a1
                                     -- VGM.unsafeWrite a k a2
                                     -- VGM.unsafeWrite a l a3
                                     return ()
{-# INLINABLE vao_sort4ByIndex #-}

-- | A type of comparisons between two values of a given type.
type Comparison e = e -> e -> Ordering

vac_copyOffset :: (PrimMonad m, VGM.MVector v e)
  => v (PrimState m) e -> v (PrimState m) e -> Int -> Int -> Int -> m ()
vac_copyOffset from to iFrom iTo len =
  VGM.unsafeCopy (VGM.unsafeSlice iTo len to) (VGM.unsafeSlice iFrom len from)
{-# INLINE vac_copyOffset #-}

vac_inc :: (PrimMonad m, VGM.MVector v Int)
  => v (PrimState m) Int -> Int -> m Int
vac_inc arr i = VGM.unsafeRead arr i
  >>= \e -> VGM.unsafeWrite arr i (e+1) >> return e
{-# INLINE vac_inc #-}

-- shared bucket sorting stuff
vac_countLoop :: (PrimMonad m, VGM.MVector v e)
  => (e -> Int)
  -> v (PrimState m) e -> VPM.MVector (PrimState m) Int -> m ()
vac_countLoop rdx src count = VGM.set count 0 >> go 0
 where
 len = VGM.length src
 go i
   | i < len    = VGM.unsafeRead src i >>= vac_inc count . rdx >> go (i+1)
   | otherwise  = return ()
{-# INLINE vac_countLoop #-}

vac_midPoint :: Int -> Int -> Int
vac_midPoint a b =
  toInt $ (toWord a + toWord b) `div` 2
  where
    toWord :: Int -> Word
    toWord = fromIntegral

    toInt :: Word -> Int
    toInt = fromIntegral
{-# INLINE vac_midPoint #-}

{- Borrowed code end -}
  
#define IL(f) {-# INLINE f #-}; f

IL(evalVals) :: [a] -> [a]
evalVals xs = build $ \c n -> foldr (c $!) n xs
IL(forceVals) :: (NFData a) => [a] -> [a]
forceVals xs = build $ \c n -> foldr (c $!!) n xs

rInt :: StateT BSL.ByteString Maybe Int
IL(rInt) = StateT $ BSL.readInt . BSL.dropWhile (<'!')
rIntS :: StateT BS.ByteString Maybe Int
IL(rIntS) = StateT $ BS.readInt . BS.dropWhile (<'!')

unlessM, whenM :: (Monad m) => m Bool -> m () -> m ()
IL(whenM) = (. flip when) . (>>=)
IL(unlessM) = (. flip unless) . (>>=)

IL(wrA) = A.writeArray
IL(rdA) = A.readArray
IL(mdA) = \arr f !i -> do
  ai <- rdA arr i
  let fai = f ai 
  wrA arr i fai
  return (ai,fai)
{-# INLINE mdA' #-}
mdA' = \arr f !i -> do
  !ai <- rdA arr i
  let !fai = f ai
  wrA arr i fai
  return (ai,fai)
IL(swapA) = \arr !i !j -> do
  ai <- rdA arr i
  wrA arr i =<< rdA arr j
  wrA arr j ai

#define D(f,r,d)\
  IL(f) :: Integral a=>a->d; f=fromIntegral;\
  IL(r) :: String->d; r=read
#define C(f,r,g,h,d) D(f,r,d);\
  g,h :: RealFrac a=>a->d; IL(g)=floor; IL(h)=ceiling
C(_toInteger_,readInteger,floorInteger,ceilInteger,Integer)
C(toInt,readInt,floorInt,ceilInt,Int)
C(toI8,readI8,floorI8,ceilI8,Int8)
C(toI16,readI16,floorI16,ceilI16,Int16)
C(toI32,readI32,floorI32,ceilI32,Int32)
C(toI64,readI64,floorI64,ceilI64,Int64)
C(toWord,readWord,floorWord,ceilWord,Word)
C(toW8,readW8,floorW8,ceilW8,Word8)
C(toW16,readW16,floorW16,ceilW16,Word16)
C(toW32,readW32,floorW32,ceilW32,Word32)
C(toW64,readW64,floorW64,ceilW64,Word64)
D(toDouble,readDouble,Double)
D(toFloat,readFloat,Float)
#undef D
#undef C

#define N(f,g,h,a,m)\
  IL(f) :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> e -> m (a i e);\
  f=A.newArray;\
  IL(g) :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> m (a i e);\
  g=A.newArray_;\
  IL(h) :: forall e i s. (C(a,m)A.Ix i) => (i,i) -> [e] -> m (a i e);\
  h=A.newListArray
#define C(a,m)
N(newIOA,newIOA_,newIOAL,IOArray,IO)
N(newSTA,newSTA_,newSTAL,STArray s,ST s)
#undef C
#define C(a,m) MArray (a) e (m), 
N(newIOUA,newIOUA_,newIOUAL,IOUArray,IO)
N(newSTUA,newSTUA_,newSTUAL,STUArray s,ST s)
#undef C
#undef N

#undef IL


Measured memory_bytes: 9084