Submission #229428


Source Code Expand

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE ExplicitForAll #-}

import Control.Applicative
import Data.Bits
import qualified Data.ByteString.Char8 as BS
import Data.Function
import Data.List
import Data.Maybe
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as UM
import Data.Word

main :: IO ()
main = do
  [n, x] <- getInts
  as <- getIntsN n
  print $ solve as x

solve :: U.Vector Int -> BitSet -> Int
solve vs s = sum $ map (vs U.!) $ bitSetToList s

----------------------------------------------------------------------------
-- IO

getInts :: IO [Int]
getInts = readInts <$> BS.getLine

readInts :: BS.ByteString -> [Int]
readInts s0 = unfoldr step s0
  where
    step (BS.dropWhile (==' ') -> s)
      | s == "" = Nothing
      | Just (v, r) <- BS.readInt s = Just (v, r)
      | otherwise = error $ "not an integer: " ++ show s

getIntsN :: Int -> IO (U.Vector Int)
getIntsN n = readIntsN n <$> BS.getLine

readIntsN :: Int -> BS.ByteString -> U.Vector Int
readIntsN n s0
  | U.length vec == n = vec
  | otherwise = error $ "readIntsN: expecting " ++ show n
    ++ " ints but got " ++ show (U.length vec)
  where
    vec = U.unfoldrN (n+1) step s0
    step (BS.dropWhile (==' ') -> s)
      | s == "" = Nothing
      | Just (v, r) <- BS.readInt s = Just (v, r)
      | otherwise = error $ "not an integer: " ++ show s

----------------------------------------------------------------------------
-- BitSet

type BitSet = Int

bitSetToList :: BitSet -> [Int]
bitSetToList = unfoldr step . fromIntegral
  where
    step 0 = Nothing
    step s = Just (b, s')
      where
        !b = bitScanForward s
        !s' = s .&. complement (1 `unsafeShiftL` b)

----------------------------------------------------------------------------
-- Util

bitScanForward :: Word -> Int
bitScanForward w = bsfTable U.! bsfHash (w .&. (-w))

bsfTable :: U.Vector Int
bsfTable = U.create $ do
  mv <- UM.new 64
  U.forM_ (U.generate 64 id) $ \i ->
    UM.write mv (bsfHash $ 1 `shiftL` i) i
  return mv

bsfHash :: Word -> Int
bsfHash w = fromIntegral $ (w * 0x03f79d71b4cb0a89) `shiftR` 58

Submission Info

Submission Time
Task B - 価格の合計
User mkotha
Language Haskell (GHC 7.4.1)
Score 100
Code Size 2340 Byte
Status AC
Exec Time 59 ms
Memory 1316 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 3
AC × 20
Set Name Test Cases
Sample subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt
All subtask0_sample01.txt, subtask0_sample02.txt, subtask0_sample03.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask1_14.txt, subtask1_15.txt, subtask1_16.txt, subtask1_17.txt
Case Name Status Exec Time Memory
subtask0_sample01.txt AC 59 ms 1304 KB
subtask0_sample02.txt AC 29 ms 1248 KB
subtask0_sample03.txt AC 29 ms 1308 KB
subtask1_01.txt AC 30 ms 1304 KB
subtask1_02.txt AC 28 ms 1312 KB
subtask1_03.txt AC 33 ms 1220 KB
subtask1_04.txt AC 31 ms 1300 KB
subtask1_05.txt AC 29 ms 1308 KB
subtask1_06.txt AC 30 ms 1312 KB
subtask1_07.txt AC 29 ms 1308 KB
subtask1_08.txt AC 28 ms 1308 KB
subtask1_09.txt AC 28 ms 1308 KB
subtask1_10.txt AC 28 ms 1304 KB
subtask1_11.txt AC 32 ms 1260 KB
subtask1_12.txt AC 29 ms 1308 KB
subtask1_13.txt AC 28 ms 1296 KB
subtask1_14.txt AC 28 ms 1312 KB
subtask1_15.txt AC 31 ms 1308 KB
subtask1_16.txt AC 29 ms 1316 KB
subtask1_17.txt AC 30 ms 1288 KB