Language: C++

import Data.Functor.Identity
import Control.Applicative
import Control.Monad(liftM, MonadPlus(..), ap, join, replicateM)
import Control.Monad.Trans.Class
import Data.Foldable(traverse_)
import Data.IORef
import Control.Arrow((>>>),(***))
import Data.List(sortBy)

data FreeF f a b = Pure a | Free (f b)
  deriving (Eq,Ord,Show,Read)

instance Functor f => Functor (FreeF f a) where
  fmap _ (Pure a)  = Pure a
  fmap f (Free as) = Free (fmap f as)
  {-# INLINE fmap #-}

instance Foldable f => Foldable (FreeF f a) where
  foldMap f (Free as) = foldMap f as
  foldMap _ _         = mempty
  {-# INLINE foldMap #-}

instance Traversable f => Traversable (FreeF f a) where
  traverse _ (Pure a)  = pure (Pure a)
  traverse f (Free as) = Free <$> traverse f as
  {-# INLINE traverse #-}

newtype FreeT f m a = FreeT { runFreeT :: m (FreeF f a (FreeT f m a)) }

-- | The \"free monad\" for a functor @f@.
type Free f = FreeT f Identity

-- | Evaluates the first layer out of a free monad value.
runFree :: Free f a -> FreeF f a (Free f a)
runFree = runIdentity . runFreeT
{-# INLINE runFree #-}

free :: FreeF f a (Free f a) -> Free f a
free = FreeT . Identity
{-# INLINE free #-}

instance (Functor f, Monad m) => Functor (FreeT f m) where
  fmap f (FreeT m) = FreeT (liftM f' m) where
    f' (Pure a)  = Pure (f a)
    f' (Free as) = Free (fmap (fmap f) as)

instance (Functor f, Monad m) => Applicative (FreeT f m) where
  pure a = FreeT (return (Pure a))
  {-# INLINE pure #-}
  (<*>) = ap
  {-# INLINE (<*>) #-}

data StrIO n = GetLine (String -> n) | PutStrLn String n
  deriving (Functor)

type Action n = FreeT StrIO IO n

type RunMode a = Action a -> IO a

getString :: Action String
getString = liftF $ GetLine id

putString :: String -> Action ()
putString s = liftF $ PutStrLn s ()

stdIO :: Action a -> IO a
stdIO io = do
  iterT go io
  where
    go (PutStrLn s n) = putStrLn s >> n
    go (GetLine f)    = getLine >>= f

debugIO :: [String] -> Action a -> IO a
debugIO txt io = do
  que <- newQue txt
  iterT (go que) io
  where
    newQue = newIORef . cycle
    go _   (PutStrLn s n) = putStrLn s >> n
    go que (GetLine f)    = do
      txt <- popQue que
      f txt
      where
        popQue ref = atomicModifyIORef ref $ \(x:xs) -> (xs, x)

runAppOn :: RunMode a -> Action a -> IO a
runAppOn f = f

interactOn :: RunMode () -> (String -> String) -> IO ()
interactOn runner app = runner io
  where
    io = do
      s <- getString
      putString $ app s

debugInteraction :: (String -> String) -> [String] -> IO ()
debugInteraction app = traverse_ debugApp
  where
    debugApp x = interactOn (debugIO [x]) app

infixr 1 |>
(|>) :: a -> (a -> b) -> b
x |> f = f x

both :: (a -> b) -> (a,a) -> (b,b)
both f = f *** f

type SizeOfData = Int
type Weight = Int
type Value = Int

firstLine :: Action String -> Action (SizeOfData, Weight)
firstLine io = do
  s <- io
  s |> words >>> dblp >>> both read >>> return
    where
      dblp :: [a] -> (a,a)
      dblp xs = (xs !! 0, xs !! 1)

data Item = Item {
    value   :: Value
  , weight  :: Weight
} deriving (Show, Eq)

type Amount = Int

type Stock = (Item, Amount)

instance Ord Item where
  compare x y = compare (ratio x) (ratio y)
    where
      ratio i = (value i) `divf` (weight i)

stock :: (Int, Int, Int) -> Stock
stock (v, w, a) = (Item v w, a)

liftMap :: (Functor m1, Functor m2) => (a -> b) -> m1 (m2 a) -> m1 (m2 b)
liftMap f = fmap (fmap f)

divf :: Int -> Int -> Float
divf x y = x' / y'
  where
    x' = fromIntegral x
    y' = fromIntegral y

dataLine :: String -> Stock
dataLine s =
  s |> words >>> triple >>> each3 read >>> stock
    where
      triple :: [a] -> (a,a,a)
      triple xs = (xs !! 0, xs !! 1, xs !! 2)

      each3 :: (a -> b) -> (a,a,a) -> (b,b,b)
      each3 f (x,y,z) = (f x, f y, f z)

getData :: SizeOfData -> Action String -> Action [Stock]
getData n io = replicateM n (fmap dataLine io)

data Bag = Bag {
    sumValue  :: Value
  , current   :: Weight
  , maxWeight :: Weight
} deriving (Show, Eq)

newBag :: Weight -> Bag
newBag = Bag 0 0

capacity :: Bag -> Weight
capacity (Bag _ c m) = m - c

cannotStuff :: Stock -> Bag -> Bool
cannotStuff (i, _) b = capacity b < weight i

stuffSize :: Stock -> Bag -> Amount
stuffSize s@(i, a) b = if cannotStuff s b
  then 0
  else min a dv
    where
      dv = (capacity b) `div` (weight i)

stuff :: Stock -> Bag -> Bag
stuff stk@(item@(Item v w), _) bag@(Bag sv crt mw) =
  let sz = stuffSize stk bag in
    Bag (sv + v * sz) (crt + w * sz) mw

stuffAll :: [Stock] -> Bag -> Bag
stuffAll [] b = b
stuffAll (s:ss) b = if cannotStuff s b
  then stuffAll ss b
  else stuffAll ss $ stuff s b

sortValuable :: (Ord a) => [a] -> [a]
sortValuable = sortBy $ flip compare

app :: Action ()
app = do
  (size, maxWt) <- firstLine getString
  stocks <- getData size getString
  putString . show . sumValue $ stuffAll (sortValuable stocks) $ newBag maxWt

main :: IO ()
main = runOn stdIO app

Compiler output:
a.cc:16:5: error: stray '#' in program
   16 |   {-# INLINE fmap #-}
      |     ^
a.cc:16:19: error: stray '#' in program
   16 |   {-# INLINE fmap #-}
      |                   ^
a.cc:21:5: error: stray '#' in program
   21 |   {-# INLINE foldMap #-}
      |     ^
a.cc:21:22: error: stray '#' in program
   21 |   {-# INLINE foldMap #-}
      |                      ^
a.cc:26:5: error: stray '#' in program
   26 |   {-# INLINE traverse #-}
      |     ^
a.cc:26:23: error: stray '#' in program
   26 |   {-# INLINE traverse #-}
      |                       ^
a.cc:30:10: error: stray '\' in program
   30 | -- | The \"free monad\" for a functor @f@.
      |          ^
a.cc:30:11: warning: missing terminating " character
   30 | -- | The \"free monad\" for a functor @f@.
      |           ^
a.cc:30:11: error: missing terminating " character
   30 | -- | The \"free monad\" for a functor @f@.
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:36:3: error: stray '#' in program
   36 | {-# INLINE runFree #-}
      |   ^
a.cc:36:20: error: stray '#' in program
   36 | {-# INLINE runFree #-}
      |                    ^
a.cc:40:3: error: stray '#' in program
   40 | {-# INLINE free #-}
      |   ^
a.cc:40:17: error: stray '#' in program
   40 | {-# INLINE free #-}
      |                 ^
a.cc:43:36: warning: missing terminating ' character
   43 |   fmap f (FreeT m) = FreeT (liftM f' m) where
      |                                    ^
a.cc:43:36: error: missing terminating ' character
   43 |   fmap f (FreeT m) = FreeT (liftM f' m) where
      |                                    ^~~~~~~~~~
a.cc:44:6: warning: missing terminating ' character
   44 |     f' (Pure a)  = Pure (f a)
      |      ^
a.cc:44:6: error: missing terminating ' character
   44 |     f' (Pure a)  = Pure (f a)
      |      ^~~~~~~~~~~~~~~~~~~~~~~~
a.cc:45:6: warning: missing terminating ' character
   45 |     f' (Free as) = Free (fmap (fmap f) as)
      |      ^
a.cc:45:6: error: missing terminating ' character
   45 |     f' (Free as) = Free (fmap (fmap f) as)
      |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:49:5: error: stray '#' in program
   49 |   {-# INLINE pure #-}
      |     ^
a.cc:49:19: error: stray '#' in program
   49 |   {-# INLINE pure #-}
      |                   ^
a.cc:51:5: error: stray '#' in program
   51 |   {-# INLINE (<*>) #-}
      |     ^
a.cc:51:20: error: stray '#' in program
   51 |   {-# INLINE (<*>) #-}
      |                    ^
a.cc:84:46: error: stray '\' in program
   84 |         popQue ref = atomicModifyIORef ref $ \(x:xs) -> (xs, x)
      |                                              ^
a.cc:132:27: error: stray '`' in program
  132 |       ratio i = (value i) `divf` (weight i)
      |                           ^
a.cc:132:32: error: stray '`' in program
  132 |       ratio i = (value i) `divf` (weight i)
      |                                ^
a.cc:141:13: warning: multi-character character constant [-Wmultichar]
  141 | divf x y = x' / y'
      |             ^~~~~~
a.cc:143:6: warning: missing terminating ' character
  143 |     x' = fromIntegral x
      |      ^
a.cc:143:6: error: missing terminating ' character
  143 |     x' = fromIntegral x
      |      ^~~~~~~~~~~~~~~~~~
a.cc:144:6: warning: missing terminating ' character
  144 |     y' = fromIntegral y
      |      ^
a.cc:144:6: error: missing terminating ' character
  144 |     y' = fromIntegral y
      |      ^~~~~~~~~~~~~~~~~~
a.cc:175:12: error: stray '@' in program
  175 | stuffSize s@(i, a) b = if cannotStuff s b
      |            ^
a.cc:179:25: error: stray '`' in program
  179 |       dv = (capacity b) `div` (weight i)
      |                         ^
a.cc:179:29: error: stray '`' in program
  179 |       dv = (capacity b) `div` (weight i)
      |                             ^
a.cc:182:10: error: stray '@' in program
  182 | stuff stk@(item@(Item v w), _) bag@(Bag sv crt mw) =
      |          ^
a.cc:182:16: error: stray '@' in program
  182 | stuff stk@(item@(Item v w), _) bag@(Bag sv crt mw) =
      |                ^
a.cc:182:35: error: stray '@' in program
  182 | stuff stk@(item@(Item v w), _) bag@(Bag sv crt mw) =
      |                                   ^
a.cc:1:1: error: 'import' does not name a type
    1 | import Data.Functor.Identity
      | ^~~~~~
a.cc:1:1: note: C++20 'import' only available with '-fmodules-ts'
a.cc:18:1: error: 'instance' does not name a type
   18 | instance Foldable f => Foldable (FreeF f a) where
      | ^~~~~~~~
a.cc:23:1: error: 'instance' does not name a type
   23 | instance Traversable f => Traversable (FreeF f a) where
      | ^~~~~~~~
a.cc:28:1: error: 'newtype' does not name a type
   28 | newtype FreeT f m a = FreeT { runFreeT :: m (FreeF f a (FreeT f m a)) }
      | ^~~~~~~
a.cc:30:1: error: expected unqualified-id before '--' token
   30 | -- | The \"free monad\" for a functor @f@.
      | ^~
a.cc:38:1: error: 'free' does not name a type
   38 | free :: FreeF f a (Free f a) -> Free f a
      | ^~~~
a.cc:1:1: note: 'free' is defined in header '<cstdlib>'; this is probably fixable by adding '#include <cstdlib>'
  +++ |+#include <cstdlib>
    1 | import Data.Functor.Identity
a.cc:42:10: error: expected constructor, destructor, or type conversion before '(' token
   42 | instance (Functor f, Monad m) => Functor (FreeT f m) where
      |          ^
a.cc:50:4: error: expected unqualified-id before '<' token
   50 |   (<*>) = ap
      |    ^
a.cc:50:4: error: expected ')' before '<' token
   50 |   (<*>) = ap
      |   ~^
      |    )
a.cc:53:1: error: 'data' does not name a type
   53 | data StrIO n = GetLine (String -> n) | PutStrLn String n
      | ^~~~
a.cc:123:12: error: expected constructor, destructor, or type conversion before '(' token
  123 | } deriving (Show, Eq)
      |            ^
a.cc:163:12: error: expected constructor, destructor, or type conversion before '(' token
  163 | } deriving (Show, Eq)
      |            ^