Submission #19410992


Source Code Expand

{-# LANGUAGE BangPatterns     #-}
{-# LANGUAGE DatatypeContexts #-}
import           Control.Exception           (assert)
import           Control.Monad
import           Control.Monad.Primitive
import qualified Control.Monad.ST            as ST
import qualified Data.Array.IO               as IO
import qualified Data.Array.ST               as ST
import qualified Data.Array.Unboxed          as A
import           Data.Bits
import qualified Data.ByteString.Char8       as BS
import           Data.Char
import           Data.Foldable
import           Data.List
import qualified Data.Map.Strict             as Map
import           Data.Maybe
import qualified Data.Sequence               as Seq
import qualified Data.Set                    as Set
import qualified Data.Vector                 as VB
import qualified Data.Vector.Mutable         as VBM
import qualified Data.Vector.Unboxed         as V
import           Data.Vector.Unboxed.Base
import qualified Data.Vector.Unboxed.Mutable as VM
import           Debug.Trace

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine
getIntVec n = V.unfoldrN n (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine

readInteger = fst . fromJust . BS.readInteger
readIntegerList = map readInteger . BS.words
getInteger = readInteger <$> BS.getLine
getIntegerList = readIntegerList <$> BS.getLine

inf :: Int
inf = 10^18

main = do
  [n, t] <- getIntList
  as <- getIntVec n

  let nb = n `div` 2
      nc = n - nb
      bs = V.take nb as
      cs = V.drop nb as

      addTm nx xs bm i res | bm == 0 || i >= nx = res
                           | otherwise = let xi = xs V.! i
                                             res' | bm .&. 1 == 1 = res + xi
                                                  | otherwise = res
                                         in addTm nx xs (bm `shiftR` 1) (i+1) res'

      getTms :: Int -> V.Vector Int -> Int -> [Int] -> [Int]
      getTms nx xs bm res | bm < 0 = res
                          | otherwise = let tm = addTm nx xs bm 0 0
                                        in getTms nx xs (bm-1) (tm:res)

      tmsb = getTms nb bs ((1 `shiftL` nb) - 1) []
      tmsc = getTms nc cs ((1 `shiftL` nc) - 1) []

      vb = V.fromList $ quickSortList tmsb
      nvb = V.length vb

      solve [] res = res
      solve (tm:tms) res = let rest = t - tm
                               j = bisectLeft (\i -> vb V.! i) (rest+1) 0 nvb
                               tmb | j > 0 = vb V.! (j-1)
                                   | otherwise  = 0
                               tm' = tm + tmb
                               res' | tm' <= t = max res tm'
                                    | otherwise = res
                           in solve tms res'

  print $ solve tmsc 0


bisectLeft :: Ord a => (Int -> a) -> a -> Int -> Int -> Int
bisectLeft f x lo hi
  | lo >= hi  = lo
  | f mid < x = bisectLeft f x (mid+1) hi
  | otherwise = bisectLeft f x lo mid
  where
    mid = lo + (hi - lo) `div` 2

-- quickSortVec from https://gist.github.com/kazu-yamamoto/3051375
quickSortList :: (Ord a, VM.Unbox a) => [a] -> [a]
quickSortList xs = ST.runST $ do
    arr <- V.thaw $ V.fromList xs
    let beg = 0
        end = VM.length arr - 1
    quickSortVec beg end arr
    V.toList <$> V.freeze arr

quickSortVec l u arr
  | l >= u    = return ()
  | otherwise = do
      VM.unsafeSwap arr l ((u+l)`div`2)
      t <- VM.unsafeRead arr l
      let i = l
          j = u + 1
      nj <- loopVec t u i j arr
      VM.unsafeSwap arr l nj
      quickSortVec l (nj-1) arr
      quickSortVec (nj+1) u arr

loopVec !t !u !i !j arr = do
  ni <- doWhile i (+1)         (< t)
  nj <- doWhile j (subtract 1) (> t)
  if ni > nj
    then return nj
    else do VM.unsafeSwap arr ni nj
            loopVec t u ni nj arr
              where
                {-# INLINE doWhile #-}
                doWhile k op p
                  | nk > u    = return nk
                  | otherwise = do
                      x <- VM.unsafeRead arr nk
                      if p x then
                        doWhile nk op p
                        else
                        return nk
                        where
                          nk = op k

Submission Info

Submission Time
Task F - Programming Contest
User unnohideyuki
Language Haskell (GHC 8.8.3)
Score 600
Code Size 4403 Byte
Status AC
Exec Time 1430 ms
Memory 335016 KiB

Compile Error

Loaded package environment from /home/contestant/.ghc/x86_64-linux-8.8.3/environments/default

Main.hs:2:14: warning:
    -XDatatypeContexts is deprecated: It was widely considered a misfeature, and has been removed from the Haskell language.
  |
2 | {-# LANGUAGE DatatypeContexts #-}
  |              ^^^^^^^^^^^^^^^^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 4
AC × 35
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
All hand_01.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt
Case Name Status Exec Time Memory
hand_01.txt AC 6 ms 4232 KiB
random_01.txt AC 2 ms 4016 KiB
random_02.txt AC 2 ms 4172 KiB
random_03.txt AC 2 ms 4264 KiB
random_04.txt AC 2 ms 4200 KiB
random_05.txt AC 2 ms 4028 KiB
random_06.txt AC 3 ms 4112 KiB
random_07.txt AC 2 ms 4044 KiB
random_08.txt AC 3 ms 4052 KiB
random_09.txt AC 3 ms 4128 KiB
random_10.txt AC 2 ms 3960 KiB
random_11.txt AC 6 ms 5488 KiB
random_12.txt AC 34 ms 12392 KiB
random_13.txt AC 213 ms 67712 KiB
random_14.txt AC 4 ms 4332 KiB
random_15.txt AC 2 ms 4080 KiB
random_16.txt AC 2 ms 4144 KiB
random_17.txt AC 2 ms 4156 KiB
random_18.txt AC 2 ms 4196 KiB
random_19.txt AC 39 ms 12308 KiB
random_20.txt AC 213 ms 67752 KiB
random_21.txt AC 8 ms 6296 KiB
random_22.txt AC 212 ms 67668 KiB
random_23.txt AC 1211 ms 334792 KiB
random_24.txt AC 1206 ms 334792 KiB
random_25.txt AC 1197 ms 334904 KiB
random_26.txt AC 1200 ms 334888 KiB
random_27.txt AC 1430 ms 334876 KiB
random_28.txt AC 1077 ms 334792 KiB
random_29.txt AC 1077 ms 335016 KiB
random_30.txt AC 1073 ms 334792 KiB
sample_01.txt AC 2 ms 3960 KiB
sample_02.txt AC 2 ms 4128 KiB
sample_03.txt AC 2 ms 4168 KiB
sample_04.txt AC 2 ms 4028 KiB