{-# LANGUAGE
  ScopedTypeVariables, BangPatterns, TupleSections, ExplicitForAll,
  LambdaCase, MultiWayIf, Unsafe, RecordWildCards, FlexibleContexts, CPP,
  NoMonomorphismRestriction, GADTs, PatternGuards, MagicHash, UnboxedTuples,
  RankNTypes, EmptyDataDecls, EmptyCase, ViewPatterns, PolyKinds,
  TypeFamilies, OverloadedStrings, FlexibleInstances, UndecidableInstances,
  DefaultSignatures, GeneralizedNewtypeDeriving, StandaloneDeriving,
  DeriveGeneric, DeriveFunctor, DeriveDataTypeable, DeriveFoldable,
  DeriveTraversable, DeriveDataTypeable, FlexibleInstances,
  MultiParamTypeClasses #-}
{-# OPTIONS_GHC -O2 #-}

#define PHASE_FUSED [1]
#define PHASE_INNER [0]
#define INLINE_FUSED INLINE PHASE_FUSED
#define INLINE_INNER INLINE PHASE_INNER

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 Data.Functor.Identity
import Data.Data
import Data.Typeable
import GHC.Generics
import System.IO
import System.IO.Unsafe (unsafeDupablePerformIO, unsafePerformIO)
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 STL
-- import Control.Monad.ST.Safe
import Control.DeepSeq
import Data.Coerce
import qualified Data.ByteString as BSW
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy as BSLW
import qualified Data.ByteString.Lazy.Char8 as BSL
import qualified Data.ByteString.Builder as BSB
import qualified Data.ByteString.Unsafe as BSU
import qualified Data.ByteString.Internal as BSU
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.Vector.Fusion.Bundle.Monadic as VFBM
import qualified Data.Vector.Fusion.Bundle as VFB
import qualified Data.Vector.Fusion.Stream.Monadic as VFSM
import qualified Data.Vector.Fusion.Bundle.Size as VFBS
import qualified Data.Vector.Fusion.Util as VFU
import qualified Data.Attoparsec.ByteString.Char8 as Atto
import Debug.Trace
import Unsafe.Coerce
import Foreign.ForeignPtr
import GHC.Exts (build, Int(..), Int#,
                 (+#), (*#), (-#), (<#), (>=#), (==#), quotRemInt#,
                 remInt#, uncheckedIShiftRL#, andI#, orI#,
                isTrue#, Addr#, Ptr(..))

main :: IO ()
main = do
  [x,y,a,b,c] <- map readInt . words <$> getLine
  vec <- VUM.new $ max a $ (x+) $ max b $ y+c
  ps <- getVecULn a rIntS
  VU.imapM_ (VUM.unsafeWrite vec) ps
  vait_selectByBounds (compare `on` Down) vec x 0 a
  qs <- getVecULn b rIntS
  VU.imapM_ (VUM.unsafeWrite vec . (x+)) qs
  vait_selectByBounds (compare `on` Down) vec y x (x+b)
  rs <- getVecULn c rIntS
  VU.imapM_ (VUM.unsafeWrite vec . (x+y+)) rs
  vait_selectByBounds (compare `on` Down) vec (x+y) 0 (x+y+c)
  print . VU.sum . VU.unsafeTake (x+y) =<< VU.unsafeFreeze vec
  return ()

{-
The code below is the modified version of what I borrowed from the modules
Data.Vector.Algorithms.Common/Optimal/Insertion/Heap/Intro
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.
-}
#define FILTER \
  | len < 2   = return ()\
  | len == 2  = vao_sort2ByOffset cmp a l\
  | len == 3  = vao_sort3ByOffset cmp a l\
  | len == 4  = vao_sort4ByOffset cmp a l
#define FILTERED FILTER | otherwise

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

-- | Sorts an entire array using a custom ordering.
vait_sortBy :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> m ()
vait_sortBy cmp a = vait_sortByBounds cmp a 0 (VGM.length a)
{-# INLINE vait_sortBy #-}

-- | Sorts a portion of an array [l,u) using a custom ordering
vait_sortByBounds
  :: (PrimMonad m, VGM.MVector v e)
  => Comparison e
  -> v (PrimState m) e
  -> Int -- ^ lower index, l
  -> Int -- ^ upper index, u
  -> m ()
vait_sortByBounds cmp a l u FILTERED
  = vait_introsort cmp a (vait_ilg len) l u
 where len = u - l
{-# INLINE vait_sortByBounds #-}

-- Internal version of the introsort loop which allows partial
-- sort functions to call with a specified bound on iterations.
vait_introsort :: (PrimMonad m, VGM.MVector v e)
   => Comparison e -> v (PrimState m) e -> Int -> Int -> Int -> m ()
vait_introsort cmp a i l u = sort i l u >> vais_sortByBounds cmp a l u
 where
 sort 0 l u = vah_sortByBounds cmp a l u
 sort d l u
   | len < vait_threshold = return ()
   | otherwise = do
       vao_sort3ByIndex cmp a c l (u-1)
        -- sort the median into the lowest position
       p <- VGM.unsafeRead a l
       mid <- vait_partitionBy cmp a p (l+1) u
       VGM.unsafeSwap a l (mid - 1)
       sort (d-1) mid u
       sort (d-1) l   (mid - 1)
  where
    len = u - l
    c   = vac_midPoint u l
{-# INLINE vait_introsort #-}

-- | Moves the least k elements to the front of the array in
-- no particular order.
vait_select
  :: (PrimMonad m, VGM.MVector v e, Ord e)
  => v (PrimState m) e
  -> Int -- ^ number of elements to select, k
  -> m ()
vait_select = vait_selectBy compare
{-# INLINE vait_select #-}

-- | Moves the least k elements (as defined by the comparison) to
-- the front of the array in no particular order.
vait_selectBy
  :: (PrimMonad m, VGM.MVector v e)
  => Comparison e
  -> v (PrimState m) e
  -> Int -- ^ number of elements to select, k
  -> m ()
vait_selectBy cmp a k = vait_selectByBounds cmp a k 0 (VGM.length a)
{-# INLINE vait_selectBy #-}

-- | Moves the least k elements in the interval [l,u) to the positions
-- [l,k+l) in no particular order.
vait_selectByBounds
  :: (PrimMonad m, VGM.MVector v e)
  => Comparison e
  -> v (PrimState m) e
  -> Int -- ^ number of elements to select, k
  -> Int -- ^ lower bound, l
  -> Int -- ^ upper bound, u
  -> m ()
vait_selectByBounds cmp a k l u
  | l >= u    = return ()
  | otherwise = go (vait_ilg len) l (l + k) u
 where
 len = u - l
 go 0 l m u = vah_selectByBounds cmp a (m - l) l u
 go n l m u = do
   vao_sort3ByIndex cmp a c l (u-1)
   p <- VGM.unsafeRead a l
   mid <- vait_partitionBy cmp a p (l+1) u
   VGM.unsafeSwap a l (mid - 1)
   if | m > mid -> go (n-1) mid m u
      | m < mid - 1 -> go (n-1) l m (mid - 1)
      | otherwise -> return ()
  where c = vac_midPoint u l
{-# INLINE vait_selectByBounds #-}

-- | Moves the least k elements to the front of the array, sorted.
vait_partialSort
  :: (PrimMonad m, VGM.MVector v e, Ord e)
  => v (PrimState m) e
  -> Int -- ^ number of elements to sort, k
  -> m ()
vait_partialSort = vait_partialSortBy compare
{-# INLINE vait_partialSort #-}

-- | Moves the least k elements (as defined by the comparison) to
-- the front of the array, sorted.
vait_partialSortBy
  :: (PrimMonad m, VGM.MVector v e)
  => Comparison e
  -> v (PrimState m) e
  -> Int -- ^ number of elements to sort, k
  -> m ()
vait_partialSortBy cmp a k
  = vait_partialSortByBounds cmp a k 0 (VGM.length a)
{-# INLINE vait_partialSortBy #-}

-- | Moves the least k elements in the interval [l,u) to the positions
-- [l,k+l), sorted.
vait_partialSortByBounds
  :: (PrimMonad m, VGM.MVector v e)
  => Comparison e
  -> v (PrimState m) e
  -> Int -- ^ number of elements to sort, k
  -> Int -- ^ lower index, l
  -> Int -- ^ upper index, u
  -> m ()
vait_partialSortByBounds cmp a k l u
  | l >= u    = return ()
  | otherwise = go (vait_ilg len) l (l + k) u
 where
 isort = vait_introsort cmp a
 {-# INLINE [1] isort #-}
 len = u - l
 go 0 l m n = vah_partialSortByBounds cmp a (m - l) l u
 go n l m u
   | l == m    = return ()
   | otherwise = do
       vao_sort3ByIndex cmp a c l (u-1)
       p <- VGM.unsafeRead a l
       mid <- vait_partitionBy cmp a p (l+1) u
       VGM.unsafeSwap a l (mid - 1)
       case compare m mid of
         GT -> do isort (n-1) l (mid - 1)
                  go (n-1) mid m u
         EQ -> isort (n-1) l m
         LT -> go n l m (mid - 1)
  where c = vac_midPoint u l
{-# INLINE vait_partialSortByBounds #-}

vait_partitionBy :: forall m v e. (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> e -> Int -> Int -> m Int
vait_partitionBy cmp a = partUp
 where
 partUp :: e -> Int -> Int -> m Int
 partUp p l u
   | l < u = do
       e <- VGM.unsafeRead a l
       case cmp e p of
         LT -> partUp p (l+1) u
         _  -> partDown p l (u-1)
   | otherwise = return l

 partDown :: e -> Int -> Int -> m Int
 partDown p l u
   | l < u = do
       e <- VGM.unsafeRead a u
       case cmp p e of
         LT -> partDown p l (u-1)
         _  -> VGM.unsafeSwap a l u >> partUp p (l+1) u
   | otherwise = return l
{-# INLINE vait_partitionBy #-}

vait_ilg :: Int -> Int
vait_ilg !m = 2 * (finiteBitSize m - countLeadingZeros m - 1)

vait_threshold :: Int
vait_threshold = 18
{-# INLINE vait_threshold #-}

vah_sortByBounds :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> Int -> Int -> m ()
vah_sortByBounds cmp a l u FILTERED
  = vah_heapify cmp a l u >> vah_sortHeap cmp a l (l+4) u
    >> vao_sort4ByOffset cmp a l
 where len = u - l
{-# INLINE vah_sortByBounds #-}

vah_selectByBounds :: (PrimMonad m, VGM.MVector v e) => Comparison e
  -> v (PrimState m) e -> Int -> Int -> Int -> m ()
vah_selectByBounds cmp a k l u
  | l + k <= u = vah_heapify cmp a l (l + k) >> go l (l + k) (u - 1)
  | otherwise  = return ()
 where
 go l m u
   | u < m      = return ()
   | otherwise  = do
       el <- VGM.unsafeRead a l
       eu <- VGM.unsafeRead a u
       case cmp eu el of
         LT -> vah_popTo cmp a l m u
         _  -> return ()
       go l m (u - 1)
{-# INLINE vah_selectByBounds #-}

vah_partialSortByBounds :: (PrimMonad m, VGM.MVector v e) => Comparison e
  -> v (PrimState m) e -> Int -> Int -> Int -> m ()
vah_partialSortByBounds cmp a k l u FILTER
  | u <= l + k = vah_sortByBounds cmp a l u
  | otherwise  = do
      vah_selectByBounds cmp a k l u
      vah_sortHeap cmp a l (l + 4) (l + k)
      vao_sort4ByOffset cmp a l
 where
 len = u - l
{-# INLINE vah_partialSortByBounds #-}

vah_heapify :: (PrimMonad m, VGM.MVector v e) => Comparison e
  -> v (PrimState m) e -> Int -> Int -> m ()
vah_heapify cmp a l u = loop $ (len - 1) `shiftR` 2
  where
 len = u - l
 loop k
   | k < 0     = return ()
   | otherwise = VGM.unsafeRead a (l+k) >>= \e ->
                   vah_siftByOffset cmp a e l k len >> loop (k - 1)
{-# INLINE vah_heapify #-}

vah_pop :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> Int -> Int -> m ()
vah_pop cmp a l u = vah_popTo cmp a l u u
{-# INLINE vah_pop #-}

vah_popTo :: (PrimMonad m, VGM.MVector v e) => Comparison e
  -> v (PrimState m) e -> Int -> Int -> Int -> m ()
vah_popTo cmp a l u t = do
  al <- VGM.unsafeRead a l
  at <- VGM.unsafeRead a t
  VGM.unsafeWrite a t al
  vah_siftByOffset cmp a at l 0 (u - l)
{-# INLINE vah_popTo #-}

vah_sortHeap :: (PrimMonad m, VGM.MVector v e) => Comparison e
  -> v (PrimState m) e -> Int -> Int -> Int -> m ()
vah_sortHeap cmp a l m u = loop (u-1) >> VGM.unsafeSwap a l m
 where
 loop k
   | m < k     = vah_pop cmp a l k >> loop (k-1)
   | otherwise = return ()
{-# INLINE vah_sortHeap #-}

vah_heapInsert :: (PrimMonad m, VGM.MVector v e) => Comparison e
  -> v (PrimState m) e -> Int -> Int -> e -> m ()
vah_heapInsert cmp v l u e = sift (u - l)
 where
 sift k
   | k <= 0    = VGM.unsafeWrite v l e
   | otherwise = let pi = l + shiftR (k-1) 2
                  in VGM.unsafeRead v pi >>= \p -> case cmp p e of
                       LT -> VGM.unsafeWrite v (l + k) p >> sift pi
                       _  -> VGM.unsafeWrite v (l + k) e
{-# INLINE vah_heapInsert #-}

vah_siftByOffset :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> e -> Int -> Int -> Int -> m ()
vah_siftByOffset cmp a val off start len = sift val start len
 where
 sift val root len
   | child < len = do
       (child', ac) <- vah_maximumChild cmp a off child len
       case cmp val ac of
         LT -> VGM.unsafeWrite a (root + off) ac
               >> sift val child' len
         _  -> VGM.unsafeWrite a (root + off) val
   | otherwise = VGM.unsafeWrite a (root + off) val
  where child = root `shiftL` 2 + 1
{-# INLINE vah_siftByOffset #-}

vah_maximumChild :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> Int -> Int -> Int -> m (Int,  e)
vah_maximumChild cmp a off child1 len
  | child4 < len = do
      ac1 <- VGM.unsafeRead a (child1 + off)
      ac2 <- VGM.unsafeRead a (child2 + off)
      ac3 <- VGM.unsafeRead a (child3 + off)
      ac4 <- VGM.unsafeRead a (child4 + off)
      return $ case cmp ac1 ac2 of
        LT -> case cmp ac2 ac3 of
          LT -> case cmp ac3 ac4 of
            LT -> (child4, ac4)
            _  -> (child3, ac3)
          _  -> case cmp ac2 ac4 of
            LT -> (child4, ac4)
            _  -> (child2, ac2)
        _  -> case cmp ac1 ac3 of
          LT -> case cmp ac3 ac4 of
            LT -> (child4, ac4)
            _  -> (child3, ac3)
          _  -> case cmp ac1 ac4 of
            LT -> (child4, ac4)
            _  -> (child1, ac1)
  | child3 < len = do
      ac1 <- VGM.unsafeRead a (child1 + off)
      ac2 <- VGM.unsafeRead a (child2 + off)
      ac3 <- VGM.unsafeRead a (child3 + off)
      return $ case cmp ac1 ac2 of
        LT -> case cmp ac2 ac3 of
          LT -> (child3, ac3)
          _  -> (child2, ac2)
        _  -> case cmp ac1 ac3 of
          LT -> (child3, ac3)
          _  -> (child1, ac1)
  | child2 < len = do
      ac1 <- VGM.unsafeRead a (child1 + off)
      ac2 <- VGM.unsafeRead a (child2 + off)
      return $ case cmp ac1 ac2 of
        LT -> (child2, ac2)
        _  -> (child1, ac1)
  | otherwise    = do
      ac1 <- VGM.unsafeRead a (child1 + off); return (child1, ac1)
 where
 child2 = child1 + 1
 child3 = child1 + 2
 child4 = child1 + 3
{-# INLINE vah_maximumChild #-}

vais_sortByBounds :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> Int -> Int -> m ()
vais_sortByBounds cmp a l u FILTERED
  = vao_sort4ByOffset cmp a l >> vais_sortByBounds' cmp a l (l + 4) u
 where len = u - l
{-# INLINE vais_sortByBounds #-}

vais_sortByBounds' :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> Int -> Int -> Int -> m ()
vais_sortByBounds' cmp a l m u = sort m
 where
 sort i
   | i < u     = do
       v <- VGM.unsafeRead a i
       vais_insert cmp a l v i
       sort (i+1)
   | otherwise = return ()
{-# INLINE vais_sortByBounds' #-}

vais_insert :: (PrimMonad m, VGM.MVector v e)
  => Comparison e -> v (PrimState m) e -> Int -> e -> Int -> m ()
vais_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 vais_insert #-}

#define vec_writes2(a,v,w)\
  (VGM.unsafeWrite a v >> VGM.unsafeWrite a w)
#define vec_writes3(a,v,w,x)\
  (VGM.unsafeWrite a v >> vec_writes2(a,w,x))
#define vec_writes4(a,v,w,x,y)\
  (VGM.unsafeWrite a v >> vec_writes3(a,w,x,y))

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 #-}

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 -> vec_writes2(a,i a1,j a0)
    _  -> return ()
{-# INLINABLE vao_sort2ByIndex #-}

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 #-}

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 -> vec_writes2(a,i a2,{-j a1-}k a0)
        _  -> vec_writes3(a,i a1,j a2,k a0)
      _  -> vec_writes2(a,i a1,j a0{-k a2-})
    _  -> case cmp a1 a2 of
      GT -> case cmp a0 a2 of
        GT -> vec_writes3(a,i a2,j a0,k a1)
        _  -> vec_writes2(a,{-i a0-}j a2,k a1)
      _  -> return ()
{-# INLINABLE vao_sort3ByIndex #-}

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 #-}

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 -> vec_writes4(a,i a3,j a2,k a1,l a0)
            _  -> vec_writes4(a,i a2,j a3,k a1,l a0)
          _  -> case cmp a0 a3 of
            GT -> vec_writes3(a,i a2,{-j a1-}k a3,l a0)
            _  -> vec_writes2(a,i a2,{-j a1-}k a0{-l a3-})
        _ -> case cmp a2 a3 of
          GT -> case cmp a1 a3 of
            GT -> vec_writes2(a,i a3,{-j a1;k a2-}l a0)
            _  -> vec_writes3(a,i a1,j a3,{-k a2-}l a0)
          _  -> case cmp a0 a3 of
            GT -> vec_writes4(a,i a1,j a2,k a3,l a0)
            _  -> vec_writes3(a,i a1,j a2,k a0{-l a3-})
      _  -> case cmp a0 a3 of
        GT -> case cmp a1 a3 of
          GT -> vec_writes3(a,i a3,{-j a1-}k a0,l a2)
          _  -> vec_writes4(a,i a1,j a3,k a0,l a2)
        _  -> case cmp a2 a3 of
          GT -> vec_writes4(a,i a1,j a0,k a3,l a2)
          _  -> vec_writes2(a,i a1,j a0{-k a2;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 -> vec_writes4(a,i a3,j a2,k a0,l a1)
            _  -> vec_writes4(a,i a2,j a3,k a0,l a1)
          _  -> case cmp a1 a3 of
            GT -> vec_writes4(a,i a2,j a0,k a3,l a1)
            _  -> vec_writes3(a,i a2,j a0,k a1{-l a3-})
        _  -> case cmp a2 a3 of
          GT -> case cmp a0 a3 of
            GT -> vec_writes3(a,i a3,j a0,{-k a2-}l a1)
            _  -> vec_writes2(a,{-i a0-}j a3,{-k a2-}l a1)
          _  -> case cmp a1 a3 of
            GT -> vec_writes3(a,{-i a0-}j a2,k a3,l a1)
            _  -> vec_writes2(a,{-i a0-}j a2,k a1{-l a3-})
      _  -> case cmp a1 a3 of
        GT -> case cmp a0 a3 of
          GT -> vec_writes4(a,i a3,j a0,k a1,l a2)
          _  -> vec_writes3(a,{-i a0-}j a3,k a1,l a2)
        _  -> case cmp a2 a3 of
          GT -> vec_writes2(a,{-i a0;j a1-}k a3,l a2)
          _  -> return () {-i a0;j a1;k a2;l a3-}
{-# INLINABLE vao_sort4ByIndex #-}

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

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 #-}

#undef vec_writes2
#undef vec_writes3
#undef vec_writes4
#undef FILTERED
#undef FILTER

{- Borrowed code end -}

#define IL(f) {-# INLINE f #-}; f

IL(putBuilder) = BSB.hPutBuilder stdout

printVecInLines, printVecInSpcSepLn ::
  (VG.Vector v a, ShowAsBuilder a) => v a -> IO ()
IL(printVecInLines) = putBuilder . v2BLines
IL(printVecInSpcSepLn) = putBuilder . v2BSpcSepLn

class ShowAsBuilder a where
  showAsBuilder :: a -> BSB.Builder
  default showAsBuilder :: (Show a) => a -> BSB.Builder
  IL(showAsBuilder) = BSB.string8 . show

-- Inconsistent with show
instance (ShowAsBuilder a, VG.Vector v a) => ShowAsBuilder (v a) where
  IL(showAsBuilder) = v2BSpcSep

#define INS(t,f) instance ShowAsBuilder t where { IL(showAsBuilder)=f }
INS(Int,BSB.intDec)
INS(Int8,BSB.int8Dec)
INS(Int16,BSB.int16Dec)
INS(Int32,BSB.int32Dec)
INS(Int64,BSB.int64Dec)
INS(Word,BSB.wordDec)
INS(Word8,BSB.word8Dec)
INS(Word16,BSB.word16Dec)
INS(Word32,BSB.word32Dec)
INS(Word64,BSB.word64Dec)
INS(Integer,BSB.integerDec)
INS(Float,BSB.floatDec)
INS(Double,BSB.doubleDec)
-- INS(String,BSB.string8) -- Inconsistent with Show
-- INS(BS.ByteString,BSB.byteString) -- Inconsistent with Show
-- INS(BSL.ByteString,BSB.lazyByteString) -- Inconsisitent with Show
#undef INS

-- Inconsistent with Show
instance (ShowAsBuilder a, ShowAsBuilder b) => ShowAsBuilder (a,b) where
  IL(showAsBuilder) = showTupAsBuilder
instance (ShowAsBuilder a, ShowAsBuilder b, ShowAsBuilder c) =>
  ShowAsBuilder (a,b,c) where
  IL(showAsBuilder) = showTup3AsBuilder
instance (ShowAsBuilder a, ShowAsBuilder b, ShowAsBuilder c, ShowAsBuilder d) =>
  ShowAsBuilder (a,b,c,d) where
  IL(showAsBuilder) = showTup4AsBuilder

IL(showTupAsBuilderWith)
  :: (a -> BSB.Builder) -> (b -> BSB.Builder) -> (a,b) -> BSB.Builder
showTupAsBuilderWith showA showB
  = \(a,b) -> (showA a <>) $ BSB.char7 ' ' <> showB b
IL(showTupAsBuilder) :: (ShowAsBuilder a, ShowAsBuilder b)
  => (a,b) -> BSB.Builder
showTupAsBuilder = showTupAsBuilderWith showAsBuilder showAsBuilder

IL(showTup3AsBuilderWith) :: (a -> BSB.Builder) -> (b -> BSB.Builder) ->
  (c -> BSB.Builder) -> (a,b,c) -> BSB.Builder
showTup3AsBuilderWith showA showB showC
  = \(a,b,c) -> (showA a <>) $ (BSB.char7 ' ' <>) $ (showB b <>)
                $ (BSB.char7 ' ' <>) $ showC c
IL(showTup3AsBuilder) :: (ShowAsBuilder a, ShowAsBuilder b, ShowAsBuilder c)
  => (a,b,c) -> BSB.Builder
showTup3AsBuilder
  = showTup3AsBuilderWith showAsBuilder showAsBuilder showAsBuilder

IL(showTup4AsBuilderWith) :: (a -> BSB.Builder) -> (b -> BSB.Builder) ->
  (c -> BSB.Builder) -> (d -> BSB.Builder) -> (a,b,c,d) -> BSB.Builder
showTup4AsBuilderWith showA showB showC showD
  = \(a,b,c,d) -> (showA a <>) $ (BSB.char7 ' ' <>)
                  $ showTup3AsBuilderWith showB showC showD (b,c,d)
IL(showTup4AsBuilder) ::
  (ShowAsBuilder a, ShowAsBuilder b, ShowAsBuilder c, ShowAsBuilder d) =>
  (a,b,c,d) -> BSB.Builder
showTup4AsBuilder = showTup4AsBuilderWith showAsBuilder showAsBuilder
                    showAsBuilder showAsBuilder

v2BSpcSepLn, v2BSpcSep, v2BConcat, v2BLines ::
  (VG.Vector v a, ShowAsBuilder a)
  => v a -> BSB.Builder
IL(v2BSpcSepLn) = v2BSpcSepLnWith showAsBuilder
IL(v2BSpcSep) = v2BSpcSepWith showAsBuilder
IL(v2BConcat) = v2BConcatWith showAsBuilder
IL(v2BLines) = v2BLinesWith showAsBuilder

v2BSpcSepLnWith, v2BSpcSepWith, v2BConcatWith, v2BLinesWith ::
  (VG.Vector v a)
  => (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
IL(v2BSpcSepLnWith) = v2BSpcSepPostfWith $ BS.singleton '\n'
IL(v2BSpcSepWith) = v2BSpcSepPostfWith BS.empty
IL(v2BConcatWith) showFct = VG.foldr ((<>) . showFct) mempty
IL(v2BLinesWith) showFct
  = VG.foldr (\ a -> (showFct a <>) . (BSB.char7 '\n' <>)) mempty

v2BSpcSepPostf :: (VG.Vector v a, ShowAsBuilder a)
  => BS.ByteString -- ^ postfix
  -> v a -> BSB.Builder
IL(v2BSpcSepPostf) = (`v2BSpcSepPostfWith` showAsBuilder)

v2BSpcSepPostfWith :: (VG.Vector v a)
  => BS.ByteString -- ^ postfix
  -> (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
IL(v2BSpcSepPostfWith) = vecToBuilder BS.empty $ BS.singleton ' '

IL(vecToBuilder) :: (VG.Vector v a)
  => BS.ByteString -- ^ prefix
  -> BS.ByteString -- ^ separator
  -> BS.ByteString -- ^ postfix
  -> (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
vecToBuilder !prefix !separator !postfix
  = vecToBuilder_ (BSB.byteString prefix)
                  (BSB.byteString separator)
                  (BSB.byteString postfix)

IL(vecToBuilder_) :: (VG.Vector v a)
  => BSB.Builder -- ^ prefix
  -> BSB.Builder -- ^ separator
  -> BSB.Builder -- ^ postfix
  -> (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
vecToBuilder_ !prefix !separator !postfix showFct = \vec -> prefix <>
  VG.foldr
  (\ a rest !prefx -> prefx <> (showFct a <> rest separator))
  (const postfix) vec mempty

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

IL(readLnWith) :: StateT BS.ByteString Maybe a -> IO a
readLnWith parser = fromJust . evalStateT parser <$> BS.getLine
IL(readContentWith) :: StateT BSL.ByteString Maybe a -> IO a
readContentWith parser = fromJust . evalStateT parser <$> BSL.getContents

IL(getVecGLn) :: (VG.Vector v a) =>
  Int -> StateT BS.ByteString Maybe a -> IO (v a)
getVecGLn n s = VG.unfoldrN n (runStateT s) <$> BS.getLine
IL(getVecGRest) :: (VG.Vector v a) =>
  Int -> StateT BSL.ByteString Maybe a -> IO (v a)
getVecGRest n s = VG.unfoldrN n (runStateT s) <$> BSL.getContents
IL(getVecLn) :: Int -> StateT BS.ByteString Maybe a -> IO (V.Vector a)
getVecLn = getVecGLn
IL(getVecRest) :: Int -> StateT BSL.ByteString Maybe a -> IO (V.Vector a)
getVecRest = getVecGRest
IL(getVecULn) :: (VU.Unbox a) =>
  Int -> StateT BS.ByteString Maybe a -> IO (VU.Vector a)
getVecULn = getVecGLn
IL(getVecURest) :: (VU.Unbox a) =>
  Int -> StateT BSL.ByteString Maybe a -> IO (VU.Vector a)
getVecURest = getVecGRest

IL(rIntL) :: StateT BSL.ByteString Maybe Int
rIntL = skipSpecialL $ StateT BSL.readInt
IL(rIntS) :: StateT BS.ByteString Maybe Int
rIntS = skipSpecialS $ StateT BS.readInt
IL(rStrL) :: (MonadState BSL.ByteString m) => m BS.ByteString
rStrL = skipSpecialL $ BSL.toStrict <$> state (BSL.span (>='!'))
IL(rStrS) :: (MonadState BS.ByteString m) => m BS.ByteString
rStrS = skipSpecialS $ state $ BS.span (>='!')
IL(rCharL) :: StateT BSL.ByteString Maybe Char
rCharL = StateT BSL.uncons
IL(rCharS) :: StateT BS.ByteString Maybe Char
rCharS = StateT BS.uncons
IL(dropSpecialL) :: (MonadState BSL.ByteString m) => m ()
dropSpecialL = modify $ BSL.dropWhile (<'!')
IL(dropSpecialS) :: (MonadState BS.ByteString m) => m ()
dropSpecialS = modify $ BS.dropWhile (<'!')
IL(skipSpecialL) :: (MonadState BSL.ByteString m) => m a -> m a
skipSpecialL = (dropSpecialL *>)
IL(skipSpecialS) :: (MonadState BS.ByteString m) => m a -> m a
skipSpecialS = (dropSpecialS *>)

IL(linToMat) :: (VG.Vector v a) => Int -> Int -> v a -> V.Vector (v a)
linToMat h w lvec = vEvalElemsId $ V.generate h (\i -> VG.slice (i*w) w lvec)

IL(mLinToMat) :: (VGM.MVector v a) => Int -> Int -> v s a -> V.Vector (v s a)
mLinToMat h w lvec = vEvalElemsId $ V.generate h (\i -> VGM.slice (i*w) w lvec)

IL(unsafeAddrToSVec) :: Int -> Addr# -> VS.Vector Word8
unsafeAddrToSVec n addr
  = (`VS.unsafeFromForeignPtr0` n)
    $ unsafeDupablePerformIO
    $ newForeignPtr_ $ Ptr addr

IL(vEvalElemsId) :: (VG.Vector v a) => v a -> v a
vEvalElemsId = vMapFoldl (\ !_ !x -> (x,())) ()

IL(vEvalElems) :: (VG.Vector v a) => v a -> ()
vEvalElems = VG.foldl' (\ !_ !_ -> ()) ()

IL(vMapFoldl) :: (VG.Vector v b, VG.Vector v c) =>
  (a -> b -> (c,a)) -> a -> v b -> v c
vMapFoldl f a
  = VG.unstream . VFB.inplace (streamMapFoldl f a) id . VG.stream

streamMapFoldl :: (Functor m) =>
  (a -> b -> (c,a)) -> a -> VFSM.Stream m b -> VFSM.Stream m c
{-# INLINE_FUSED streamMapFoldl #-}
streamMapFoldl f a (VFSM.Stream step s) = VFSM.Stream step1 (a,s)
  where
    {-# INLINE_INNER step1 #-}
    step1 (a0,s0) =  (<$> step s0) $ \r -> case r of
      VFSM.Yield b s1 -> case f a0 b of (c,a1) -> VFSM.Yield c (a1,s1)
      VFSM.Skip    s1 -> VFSM.Skip (a0,s1)
      VFSM.Done       -> VFSM.Done

IL(svecToBS) :: VS.Vector Word8 -> BS.ByteString
svecToBS vec = BSU.fromForeignPtr ptr 0 len
  where (ptr, len) = VS.unsafeToForeignPtr0 vec

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 TS(f,a,m,init)\
  IL(f) :: forall e i s. (C(a,m) A.Ix i) => (i,i) -> init m (a i e); f
#define N(f,g,h,a,m)\
  TS(f,a,m,e->)=A.newArray;\
  TS(g,a,m,)=A.newArray_;\
  TS(h,a,m,[e]->)=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 TS

#undef IL


import           Control.Monad
import qualified Data.ByteString.Char8 as BS
import           Data.Foldable
import           Data.Maybe
import qualified Data.Sequence         as Seq

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
  [x, y, a, b, c] <- getIntList
  ps <- getIntList
  qs <- getIntList
  rs <- getIntList

  let ps' = Seq.take x $ Seq.unstableSortBy (flip compare) $ Seq.fromList ps
      qs' = Seq.take y $ Seq.unstableSortBy (flip compare) $ Seq.fromList qs
      ss = ps' Seq.>< qs' Seq.>< Seq.fromList rs
      ans = sum $ Seq.take (x+y) $ Seq.unstableSortBy (flip compare) ss

  print ans

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 500 points

Problem Statement

You are going to eat X red apples and Y green apples.

You have A red apples of deliciousness p_1,p_2, \dots, p_A, B green apples of deliciousness q_1,q_2, \dots, q_B, and C colorless apples of deliciousness r_1,r_2, \dots, r_C.

Before eating a colorless apple, you can paint it red or green, and it will count as a red or green apple, respectively.

From the apples above, you will choose the apples to eat while making the sum of the deliciousness of the eaten apples as large as possible.

Find the maximum possible sum of the deliciousness of the eaten apples that can be achieved when optimally coloring zero or more colorless apples.

Constraints

1 \leq X \leq A \leq 10^5

1 \leq Y \leq B \leq 10^5

1 \leq C \leq 10^5

1 \leq p_i \leq 10^9

1 \leq q_i \leq 10^9

1 \leq r_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y A B C
p_1 p_2 ... p_A
q_1 q_2 ... q_B
r_1 r_2 ... r_C

Output

Print the maximum possible sum of the deliciousness of the eaten apples.

Sample Input 1

1 2 2 2 1
2 4
5 1
3

Sample Output 1

12

The maximum possible sum of the deliciousness of the eaten apples can be achieved as follows:

Eat the 2-nd red apple.

Eat the 1-st green apple.

Paint the 1-st colorless apple green and eat it.

Sample Input 2

2 2 2 2 2
8 6
9 1
2 1

Sample Output 2

25

Sample Input 3

2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4

Sample Output 3

74

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
    [x, y, a, b, c] <- getIntList
    ps <- take x . reverse . sort <$> getIntList
    qs <- take y . reverse . sort <$> getIntList
    rs <- reverse . sort <$> getIntList
    let xs = sort (ps ++ qs)
    let s = sum $ zipWith max xs rs
    let t = sum $ drop (min c (a + b)) xs
    print $ s + t


import           Control.Monad
import           Control.Monad.ST
import qualified Data.Array                    as A
import qualified Data.Array.ST                 as ST
import qualified Data.Array.Unboxed            as AU
import qualified Data.ByteString.Char8         as BS
import           Data.Char
import           Data.Int
import qualified Data.IntMap                   as IM
import           Data.List
import qualified Data.Map                      as M
import           Data.Maybe
import           Data.Ord
import qualified Data.Set                      as S
import           Data.Time
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

readInt :: IO [Int]
readInt = map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine

readInts :: Int -> IO [[Int]]
readInts n = replicateM n readInt

readString :: IO [String]
readString = map BS.unpack . BS.words <$> BS.getLine

readStrings :: Int -> IO [[String]]
readStrings n = replicateM n <$> readString

toPair :: [a] -> (a, a)
toPair [x, y] = (x, y)

main = do
  [x, y, a, b, c] <- readInt
  p               <- readInt
  q               <- readInt
  r               <- readInt
  print $ solve x y p q r

solve :: Int -> Int -> [Int] -> [Int] -> [Int] -> Int
solve x y p q r = g p' q' r' x y 0
 where
  p' = take x $ sortOn Down p
  q' = take y $ sortOn Down q
  r' = take (x + y) $ (sortOn Down r ++ repeat 0)

f _ 0 _ 0 _ acc = acc

f _ 0 (q : qs) y (r : rs) acc | q >= r    = f [] 0 qs (y - 1) (r : rs) (acc + q)
                              | otherwise = f [] 0 (q : qs) (y - 1) rs (acc + r)

f (p : ps) x _ 0 (r : rs) acc
  | p >= r    = f ps (x - 1) [] 0 (r : rs) (acc + p)
  | otherwise = f (p : ps) (x - 1) [] 0 rs (acc + r)

f (p : ps) x (q : qs) y (r : rs) acc
  | p >= q && p >= r = f ps (x - 1) (q : qs) y (r : rs) (acc + p)
  | q >= p && q >= r = f (p : ps) x qs (y - 1) (r : rs) (acc + q)
  | r >= p && p >= q = f (p : ps) x (q : qs) (y - 1) rs (acc + r)
  | otherwise        = f (p : ps) (x - 1) (q : qs) y rs (acc + r)

g _  _  _  0 0 acc = acc

g ps qs [] x y acc = acc + sum (take x ps) + sum (take y qs)

g _ (q : qs) (r : rs) 0 y acc | q >= r    = g [] qs (r : rs) 0 (y - 1) (acc + q)
                              | otherwise = g [] (q : qs) rs 0 (y - 1) (acc + r)

g (p : ps) _ (r : rs) x 0 acc
  | p >= r    = g ps [] (r : rs) (x - 1) 0 (acc + p)
  | otherwise = g (p : ps) [] rs (x - 1) 0 (acc + r)

g (p : ps) (q : qs) (r : rs) x y acc
  | p == maximum [p, q, r] = g ps (q : qs) (r : rs) (x - 1) y (acc + p)
  | q == maximum [p, q, r] = g (p : ps) qs (r : rs) x (y - 1) (acc + q)
  | r == maximum [p, q, r] && last ps > last qs = g (p : ps)
                                                    (q : qs)
                                                    rs
                                                    x
                                                    (y - 1)
                                                    (acc + r)
  | otherwise = g (p : ps) (q : qs) rs (x - 1) y (acc + r)


import           Control.Monad
import           Control.Monad.ST
import qualified Data.Array                    as A
import qualified Data.Array.ST                 as ST
import qualified Data.Array.Unboxed            as AU
import qualified Data.ByteString.Char8         as BS
import           Data.Char
import           Data.Int
import qualified Data.IntMap                   as IM
import           Data.List
import qualified Data.Map                      as M
import           Data.Maybe
import           Data.Ord
import qualified Data.Set                      as S
import           Data.Time
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

readInt :: IO [Int]
readInt = map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine

readInts :: Int -> IO [[Int]]
readInts n = replicateM n readInt

readString :: IO [String]
readString = map BS.unpack . BS.words <$> BS.getLine

readStrings :: Int -> IO [[String]]
readStrings n = replicateM n <$> readString

toPair :: [a] -> (a, a)
toPair [x, y] = (x, y)

main = do
  [x, y, a, b, c] <- readInt
  p               <- readInt
  q               <- readInt
  r               <- readInt
  print $ solve x y p q r

solve :: Int -> Int -> [Int] -> [Int] -> [Int] -> Int
solve x y p q r = g p' q' r' x y 0
 where
  p' = take x $ sortOn Down p
  q' = take y $ sortOn Down q
  r' = take (x + y) $ (sortOn Down r ++ repeat 0)

f _ 0 _ 0 _ acc = acc

f _ 0 (q : qs) y (r : rs) acc | q >= r    = f [] 0 qs (y - 1) (r : rs) (acc + q)
                              | otherwise = f [] 0 (q : qs) (y - 1) rs (acc + r)

f (p : ps) x _ 0 (r : rs) acc
  | p >= r    = f ps (x - 1) [] 0 (r : rs) (acc + p)
  | otherwise = f (p : ps) (x - 1) [] 0 rs (acc + r)

f (p : ps) x (q : qs) y (r : rs) acc
  | p >= q && p >= r = f ps (x - 1) (q : qs) y (r : rs) (acc + p)
  | q >= p && q >= r = f (p : ps) x qs (y - 1) (r : rs) (acc + q)
  | r >= p && p >= q = f (p : ps) x (q : qs) (y - 1) rs (acc + r)
  | otherwise        = f (p : ps) (x - 1) (q : qs) y rs (acc + r)

g _ _ _ 0 0 acc = acc

g _ (q : qs) (r : rs) 0 y acc | q >= r    = g [] qs (r : rs) 0 (y - 1) (acc + q)
                              | otherwise = g [] (q : qs) rs 0 (y - 1) (acc + r)

g (p : ps) _ (r : rs) x 0 acc
  | p >= r    = g ps [] (r : rs) (x - 1) 0 (acc + p)
  | otherwise = g (p : ps) [] rs (x - 1) 0 (acc + r)

g (p : ps) (q : qs) (r : rs) x y acc
  | p == maximum [p, q, r] = g ps (q : qs) (r : rs) (x - 1) y (acc + p)
  | q == maximum [p, q, r] = g (p : ps) qs (r : rs) x (y - 1) (acc + q)
  | r == maximum [p, q, r] && init ps > init qs = g (p : ps)
                                                    (q : qs)
                                                    rs
                                                    x
                                                    (y - 1)
                                                    (acc + r)
  | otherwise = g (p : ps) (q : qs) rs (x - 1) y (acc + r)


import           Control.Monad
import           Control.Monad.ST
import qualified Data.Array                    as A
import qualified Data.Array.ST                 as ST
import qualified Data.Array.Unboxed            as AU
import qualified Data.ByteString.Char8         as BS
import           Data.Char
import           Data.Int
import qualified Data.IntMap                   as IM
import           Data.List
import qualified Data.Map                      as M
import           Data.Maybe
import           Data.Ord
import qualified Data.Set                      as S
import           Data.Time
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

readInt :: IO [Int]
readInt = map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine

readInts :: Int -> IO [[Int]]
readInts n = replicateM n readInt

readString :: IO [String]
readString = map BS.unpack . BS.words <$> BS.getLine

readStrings :: Int -> IO [[String]]
readStrings n = replicateM n <$> readString

toPair :: [a] -> (a, a)
toPair [x, y] = (x, y)

main = do
  [x, y, a, b, c] <- readInt
  p               <- readInt
  q               <- readInt
  r               <- readInt
  print $ solve x y p q r

solve :: Int -> Int -> [Int] -> [Int] -> [Int] -> Int
solve x y p q r = g p' q' r' x y 0
 where
  p' = take x $ sortOn Down p
  q' = take y $ sortOn Down q
  r' = take (x + y) $ sortOn Down r

f _ 0 _ 0 _ acc = acc

f _ 0 (q : qs) y (r : rs) acc | q >= r    = f [] 0 qs (y - 1) (r : rs) (acc + q)
                              | otherwise = f [] 0 (q : qs) (y - 1) rs (acc + r)

f (p : ps) x _ 0 (r : rs) acc
  | p >= r    = f ps (x - 1) [] 0 (r : rs) (acc + p)
  | otherwise = f (p : ps) (x - 1) [] 0 rs (acc + r)

f (p : ps) x (q : qs) y (r : rs) acc
  | p >= q && p >= r = f ps (x - 1) (q : qs) y (r : rs) (acc + p)
  | q >= p && q >= r = f (p : ps) x qs (y - 1) (r : rs) (acc + q)
  | r >= p && p >= q = f (p : ps) x (q : qs) (y - 1) rs (acc + r)
  | otherwise        = f (p : ps) (x - 1) (q : qs) y rs (acc + r)

g _ _ _ 0 0 acc = acc

g _ (q : qs) (r : rs) 0 y acc | q >= r    = g [] qs (r : rs) 0 (y - 1) (acc + q)
                              | otherwise = g [] (q : qs) rs 0 (y - 1) (acc + r)

g (p : ps) _ (r : rs) x 0 acc
  | p >= r    = g ps [] (r : rs) (x - 1) 0 (acc + p)
  | otherwise = g (p : ps) [] rs (x - 1) 0 (acc + r)

g (p : ps) (q : qs) (r : rs) x y acc
  | p == maximum [p, q, r] = g ps (q : qs) (r : rs) (x - 1) y (acc + p)
  | q == maximum [p, q, r] = g (p : ps) qs (r : rs) x (y - 1) (acc + q)
  | r == maximum [p, q, r] && init ps > init qs = g (p : ps)
                                                    (q : qs)
                                                    rs
                                                    x
                                                    (y - 1)
                                                    (acc + r)
  | otherwise = g (p : ps) (q : qs) rs (x - 1) y (acc + r)


import qualified Data.Vector.Unboxed as U
import qualified Data.ByteString.Char8 as C
import Debug.Trace
import Data.List
import Control.Monad
import Control.Arrow
import Data.Ord
import Data.Maybe
import Numeric
import Data.Char
import Text.Printf

r :: Int -> IO (U.Vector Int)
r n = U.unfoldrN n (C.readInt . C.dropWhile (==' ')) <$> C.getLine

r' :: IO [Int]
r'  =   unfoldr    (C.readInt . C.dropWhile (==' ')) <$> C.getLine

main :: IO ()
main = do
    [x,y,a,b,c] <- r'
    ps <- drop (a-x) <$> sort <$> r'
    qs <- drop (b-y) <$> sort <$> r'
    let eat = sort (ps ++ qs)
    rs <- (++[0,0..]) <$> sortBy (flip compare) <$> r'
    print $ sum $ zipWith max eat rs


import qualified Data.ByteString.Char8 as BS
import Data.Maybe

readInt = fst . fromJust . BS.readInt

readIntList = map readInt . BS.words

getIntList = readIntList <$> BS.getLine

main = do
  [a, b, c] <- getIntList
  putStrLn $ if (c - (a + b)) < 0 then "No" else if (c - (a + b)) ^ 2 - 4 * a * b > 0 then "Yes" else "No"

Score : 300 points

Problem Statement

Does \sqrt{a} + \sqrt{b} < \sqrt{c} hold?

Constraints

1 \leq a, b, c \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a \ b \ c

Output

If \sqrt{a} + \sqrt{b} < \sqrt{c}, print Yes; otherwise, print No.

Sample Input 1

2 3 9

Sample Output 1

No

\sqrt{2} + \sqrt{3} < \sqrt{9} does not hold.

Sample Input 2

2 3 10

Sample Output 2

Yes

\sqrt{2} + \sqrt{3} < \sqrt{10} holds.


main :: IO()
main = do
 [a,b,c] <- map read . words <$> getLine :: IO [Integer]
 let rightHand = fromInteger $ (c - a - b) * (c - a - b)
 let aDouble = fromInteger a :: Double
 let bDouble = fromInteger b :: Double
 let leftHand = 4.0 * aDouble * bDouble
 putStr $ if (leftHand < rightHand) then "Yes" else "No"


main :: IO()
main = do
 [a,b,c] <- map read . words <$> getLine :: IO [Integer]
 -- 2 * sqrt(a * b) < c - a - b. Right hand is Integer.
 let rightHand = fromInteger (c - a - b)
 let aDouble = fromInteger a :: Double
 let bDouble = fromInteger b :: Double
 let leftHand = (*) 2.0 $ sqrt $ aDouble * bDouble
 putStr $ if (leftHand < rightHand) then "Yes" else "No"


main::IO()
main = do
    [a,b,c]<-map read . words <$>getLine
    if a+b+(2*sqrt(a*b))<c
        then putStrLn "Yes"
        else putStrLn "No"


import Data.Bool
main=putStrLn.solve.map read.words=<<getLine
solve[a,b,c]=bool"No""Yes"$d>0 && 4*a*b<d^2 where d=c-a-b


main = do
  li <- getLine
  let [a,b,c] = map read $ words li
  let ans = compute a b c
  putStrLn $ if ans then "Yes" else "No"

compute :: Double -> Double -> Double -> Bool
compute a b c = a + b + 2 * sqrt (a*b) < c


{-# LANGUAGE MultiWayIf #-}

module Main where
import           Prelude
import           Control.Monad
import           Data.Monoid
import qualified Data.Array                    as A
import qualified Data.Array.IO                 as AIO
import qualified Data.Array.Unboxed            as AU
import qualified Data.ByteString               as BS
import qualified Data.ByteString.Char8         as BSC8
import           Data.Char
import           Data.List
import qualified Data.Map.Strict               as M
import           Data.Maybe
import           Data.Ord
import qualified Data.Set                      as S
import qualified Data.Vector                   as V
--import           Data.Vector.Algorithms.Search
import qualified Data.Vector.Mutable           as VM
import qualified Data.Vector.Unboxed           as VU
import qualified Data.Vector.Unboxed.Mutable   as VUM
import           Debug.Trace
--import qualified Data.Heap                     as H
import qualified Data.Sequence                 as SQ
import qualified Data.Graph.Inductive.PatriciaTree
                                               as G
import qualified Data.Graph.Inductive.Graph    as G
import           Data.Int
main = do
  [ia, ib, ic] <- getIL
  let l = sqrt (realToFrac ia) + (sqrt (realToFrac ib)) :: Double
  let r = sqrt (realToFrac ic) :: Double

  if l < r then putStrLn "Yes" else putStrLn "No"

{-
hMaxPoint :: Int
hMaxPoint = 101
hMaximum :: H.Heap Int -> Int
hMaximum h = hMaxPoint - (H.minimum h)
hInsertMax :: Int -> H.Heap Int -> H.Heap Int
hInsertMax x = H.insert (hMaxPoint - x)
hDeleteMax :: H.Heap Int -> H.Heap Int
hDeleteMax = H.deleteMin
-}

{-
aroundSquare y x h w =
  [ aYX
  | aYX@(aY, aX) <- [(y - 1, x), (y, x - 1), (y + 1, x), (y, x + 1), (y, x)]
  , aY >= 0
  , aY <= (h - 1)
  , aX >= 0
  , aX <= (w - 1)
  ]
-}
read2DimChar :: VM.IOVector (VUM.IOVector Char) -> Int -> Int -> IO Char
read2DimChar vec y x = do
  vecY <- VM.read vec y
  VUM.read vecY x
write2DimChar :: VM.IOVector (VUM.IOVector Char) -> Int -> Int -> Char -> IO ()
write2DimChar vec y x t = do
  vecY <- VM.read vec y
  VUM.write vecY x t

readTwoDimVec :: VM.IOVector (VUM.IOVector Int) -> Int -> Int -> IO Int
readTwoDimVec vec y x = do
  vecY <- VM.read vec y
  VUM.read vecY x
writeTwoDimVec :: VM.IOVector (VUM.IOVector Int) -> Int -> Int -> Int -> IO ()
writeTwoDimVec vec y x t = do
  vecY <- VM.read vec y
  VUM.write vecY x t

getVUI :: IO (VU.Vector Int)
getVUI = VU.unfoldr (BSC8.readInt . BSC8.dropWhile isSpace) <$> BSC8.getLine

getRepVUI :: Int -> IO (VU.Vector Int)
getRepVUI n =
  VU.unfoldr (BSC8.readInt . BSC8.dropWhile isSpace)
    .   BSC8.unlines
    <$> replicateM n BSC8.getLine

getVS :: IO (V.Vector Char)
getVS = V.fromList <$> getLine

getVUC :: IO (VU.Vector Char)
getVUC = VU.fromList <$> getLine

readI :: BSC8.ByteString -> Int
readI = fst . fromJust . BSC8.readInt

readIL :: BSC8.ByteString -> [Int]
readIL = map readI . BSC8.words

readIT2 :: BSC8.ByteString -> (Int, Int)
readIT2 bs =
  let words = BSC8.words bs in (readI $ head words, readI $ last words)

getI :: IO Int
getI = readI <$> BSC8.getLine

getIL :: IO [Int]
getIL = readIL <$> BSC8.getLine

getIT2 :: IO (Int, Int)
getIT2 = readIT2 <$> BSC8.getLine

getIT2Order :: IO (Int, Int)
getIT2Order =
  getIL >>= \x@[xz, xo] -> if xz > xo then return (xo, xz) else return (xz, xo)

getNIS :: Int -> IO [(Int, BSC8.ByteString)]
getNIS 0 = return []
getNIS n = do
  [aFst, aSnd] <- BSC8.words <$> BSC8.getLine
  next         <- getNIS (n - 1)
  return ((readI aFst, aSnd) : next)

il2Words :: [Int] -> String
il2Words = unwords . map show

il2Lines :: [Int] -> String
il2Lines = unlines . map show

p :: Int -> Int
p x = x - 1

--modNum = 10 ^ 9 + 7
modulus = 1000000007

newtype MInt = M Int

instance Num MInt where
  (M a) + (M b) = M $ mod (a + b) modulus

  (M a) - (M b) = M (a - b)

  (M a) * (M b) = M $ mod (a * b) modulus

  fromInteger a = M $ mod (fromInteger a) modulus

  abs (M a) = M $ abs a

  signum (M a) = M $ signum a

instance Show MInt where
  show (M a) = show (mod a modulus)

int2MInt :: Int -> MInt
int2MInt a = M (mod a modulus)

mInt2Int :: MInt -> Int
mInt2Int (M a) = a

{-
factsMod :: V.Vector MInt
factsMod = V.scanl (\acc x -> M x * acc) 1 $ V.generate 2000000 (+ 1)

factMod :: Int -> MInt
factMod = (factsMod V.!)
-}

factMod :: Int -> MInt
factMod n | n == 0    = 1
          | n == 1    = 1
          | otherwise = M n * (factMod (n - 1))

factDivMod :: Int -> Int -> MInt
factDivMod n k | n == k    = M 1
               | otherwise = M n * (factDivMod (n - 1) k)

powMod :: MInt -> Int -> MInt
powMod n k | k == 0       = M 1
           | k == 1       = n
           | mod k 2 == 0 = powMod (n ^ 2) (div k 2)
           | otherwise    = n * powMod (n ^ 2) (div k 2)

invMod :: MInt -> MInt
invMod (M 0) = error "inverse of 0"
invMod x     = powMod x (modulus - 2)

{-
combiMod :: Int -> Int -> MInt
combiMod n k
  | n < 0 || k < 0 || k > n = 0
  | otherwise = factMod n * invMod (factMod (n - k)) * invMod (factMod k)
-}
combiMod :: Int -> Int -> MInt
combiMod n k | n < 0 || k < 0 || k > n = 0
             | k > n - k = factDivMod n k * invMod (factMod (n - k))
             | otherwise = factDivMod n (n - k) * invMod (factMod k)

combi :: Int -> Int -> Int
combi n k | n < 0 || k < 0 || k > n = 0
          | k == n                  = 1
          | otherwise               = div (div (fact n) (fact (n - k))) (fact k)

primes :: Integral a => [a]
primes = map fromIntegral $ [2, 3] ++ primes'
 where
  primes' = [5] ++ f 1 7 primes'
  f m s (p : ps) = [ n | n <- ns, gcd m n == 1 ] ++ f (m * p) (p * p) ps
    where ns = [ x + y | x <- [s, s + 6 .. p * p - 2], y <- [0, 4] ]

fact :: Int -> Int
fact 0         = 1
fact n | n > 0 = n * fact (n - 1)

knappsack :: Int -> [(Int, Int)] -> VU.Vector Int
knappsack maxW [] = VU.replicate (maxW + 1) 0
knappsack maxW ((w, v) : xs) =
  let tailDP = knappsack maxW xs
  in  VU.generate (maxW + 1) $ \i -> if w <= i
        then max (tailDP VU.! i) (tailDP VU.! (i - w) + v)
        else tailDP VU.! i

knappsackMaxVal
  :: AIO.IOArray (Int, Int) Int
  -> VU.Vector Int
  -> VU.Vector Int
  -> Int
  -> IO Int
knappsackMaxVal dp volumeVec valueVec dpSize = do
  mapM_
    (\i -> mapM_
      (\v -> do
        let volume = volumeVec VU.! i
            value  = valueVec VU.! i
        newdp <- if v < volume
          then AIO.readArray dp (i, v)
          else do
            dpIVm <- AIO.readArray dp (i, v - volume)
            dpIV  <- AIO.readArray dp (i, v)
            return (max (dpIVm + value) dpIV)
        AIO.writeArray dp (i + 1, v) newdp
      )
      [0 .. dpSize]
    )
    [0 .. VU.length volumeVec - 1]
  AIO.readArray dp (VU.length volumeVec, dpSize)

knappsackMinVol
  :: AIO.IOArray (Int, Int) Int
  -> VU.Vector Int
  -> VU.Vector Int
  -> Int
  -> Int
  -> IO Int
knappsackMinVol dp volumeVec valueVec dpSize maxVolume = do
  mapM_
    (\i -> mapM_
      (\v -> do
        let volume = volumeVec VU.! i
            value  = valueVec VU.! i
        newdp <- if (v + value) > dpSize
          then AIO.readArray dp (i, v)
          else do
            dpIVm <- AIO.readArray dp (i, v + value)
            dpIV  <- AIO.readArray dp (i, v)
            return (min (dpIVm - volume) dpIV)
        AIO.writeArray dp (i + 1, v) newdp
      )
      (reverse [0 .. dpSize])
    )
    [0 .. VU.length valueVec - 1]
  foldM
    (\acc x -> do
      num <- AIO.readArray dp (VU.length volumeVec, x)
      return $ if num <= maxVolume then x else acc
    )
    0
    [0 .. dpSize]

lcsTable :: BS.ByteString -> BS.ByteString -> V.Vector (VU.Vector Int)
lcsTable xs ys = V.scanl step (VU.replicate (m + 1) 0) xs'
 where
  n   = BS.length xs
  m   = BS.length ys
  xs' = V.generate n $ BSC8.index xs :: V.Vector Char
  ys' = VU.generate m $ BSC8.index ys :: VU.Vector Char
  step :: VU.Vector Int -> Char -> VU.Vector Int
  step v x = VU.scanl innerStep 0 (VU.zip3 v (VU.tail v) ys')
   where
    innerStep :: Int -> (Int, Int, Char) -> Int
    innerStep a (b, c, y) | x == y    = 1 + b
                          | otherwise = max a c
{-
main = do
xs <- BS.getLine
ys <- BS.getLine
print $ VU.last $ V.last $ lcsTable xs ys
-}

myFilter :: (a -> Bool) -> [a] -> ([a], [a])
myFilter _pred [] = ([], [])
myFilter pred (x : xs)
  | pred x    = let (l, r) = myFilter pred xs in (x : l, r)
  | otherwise = let (l, r) = myFilter pred xs in (l, x : r)

--UnionFindデータ構造
type UF_PARENT = VUM.IOVector Int
type UF_RANK = VUM.IOVector Int
type UF = (UF_PARENT, UF_RANK)
uf_makeSet :: Int -> IO UF
uf_makeSet n = do
  parent <- VU.thaw $ VU.generate n id
  rank   <- VUM.replicate n 0
  return (parent, rank)
uf_union :: UF -> Int -> Int -> IO ()
uf_union unionFind@(parents, ranks) x y = do
  xRoot     <- uf_find unionFind x
  yRoot     <- uf_find unionFind y
  xRootRank <- VUM.read ranks xRoot
  yRootRank <- VUM.read ranks yRoot
  if
    | xRootRank > yRootRank -> VUM.write parents yRoot xRoot
    | xRootRank < yRootRank -> VUM.write parents xRoot yRoot
    | xRoot /= yRoot -> do
      VUM.write parents yRoot xRoot
      VUM.write ranks xRoot (xRootRank + 1)
    | otherwise -> return ()

uf_find :: UF -> Int -> IO Int
uf_find unionFind@(parents, ranks) x = do
  xParent <- VUM.read parents x
  if xParent == x
    then return x
    else do
      xRootParent <- uf_find unionFind xParent
      VUM.write parents x xRootParent
      return xRootParent

--UnDirectedGraph(無向グラフ)データ構造
type UDG = M.Map Int [Int]
list2UDG :: [(Int, Int)] -> M.Map Int [Int]
list2UDG = foldl
  (\acc x ->
    let lr = M.insertWith (++) (fst x) [snd x] acc
    in  M.insertWith (++) (snd x) [fst x] lr
  )
  M.empty


solve :: Int -> Int -> Int -> String
solve a b c
  | d < e && 0 < e = "Yes"
  | otherwise = "No"
  where
    d = 4 * a * b
    e = (^2) $ (c-a-b)

main = do
  [a,b,c] <- map read . words <$> getLine
  putStrLn $ solve a b c


import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS

main = panaC
panaC = do
    [a,b,c] <- getIntList
    putStrLn $ solveC a b c

readInt = fst . fromJust . BS.readInteger
readIntList = map readInt . BS.words
getIntList = readIntList <$> BS.getLine

solveC :: Integer -> Integer -> Integer -> String
solveC a b c
    | c - a - b > 0 = if result > 0 then "Yes" else "No"
    | otherwise = if result' > 0 then "Yes" else "No"
    where result = (c - a - b)^2 - 4 * a * b
          result' = (c - a - b)^2 - 4 * a * b


main = do
  [a,b,c] <- map read . words <$> getLine :: IO [Int]
  let right = (c-a-b)^2
  let left = 4 * a * b

  putStrLn $ if (c-a-b > 0) && left < right then "Yes" else "No"


import Data.List
main = do
    a:b:c:other <- (map (read :: String -> Float)).words<$>getLine
    putStrLn $ if ((sqrt a) + (sqrt b)) < (sqrt c) then "Yes" else "No"


import Control.Monad
-- 時刻の差分 >= xの差 + yの差 AND 時刻の差分 と xの差 + yの差の遇機が一致

analyze :: [(Integer, Integer, Integer)] -> (Integer, Integer, Integer) -> Bool
analyze [] _ = True
analyze ((time, x, y):rem) (prtime, prx, pry) =
  (time - prtime >= dx + dy &&
    (time - prtime) `mod` 2 == (dx + dy) `mod` 2) &&
    analyze rem (time, x, y)
  where dx = abs $ x - prx; dy = abs $ y - pry

main = do
  [a, b, c] <- (map read . words <$> getLine :: IO [Integer])
  let r = c - a - b
  let res = r * r > a * b * 4
  putStrLn $ if res then "Yes" else "No"


import Data.List
main = do
    a:b:c:other <- (map read).words<$>getLine
    putStrLn $ f a b c
    	where
        	f a b c = if ((sqrt a) + (sqrt b)) < (sqrt c) then "Yes" else "No"


main = do
  [a,b,c] <- map read . words <$> getLine :: IO [Double]
  putStrLn $ if a + b + 2*sqrt(a*b) < c then "Yes" else "No"


import           Control.Monad
import qualified Data.ByteString.Char8 as BS
import           Data.Maybe

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
  [a, b, c] <- getIntList
  putStrLn $ if (c - a - b) > 0 && 4 * a * b < (c - a - b) ^ 2
             then "Yes"
             else "No"


import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T

main :: IO ()
main = do
  (a : b : c : _) <- map unsafeTextToInt . T.words <$> T.getLine :: IO [Int]
  putStrLn $ if a + b < c && 4 * a * b < (c - a - b) ^ 2 then "Yes" else "No"

unsafeTextToInt :: T.Text -> Int
unsafeTextToInt s = case T.signed T.decimal s of
  Right (n, _) -> n


{-# LANGUAGE BangPatterns #-}

module Main
  ( main
  )
where

import           Control.Applicative
import           Control.Monad
import           Data.Bits
import qualified Data.ByteString.Char8         as BS
import           Data.Char
import           Data.List
import           Data.Maybe
import qualified Data.Vector                   as V
import qualified Data.Vector.Mutable           as MV
import           Control.Monad.ST

parse = fst . fromJust
parseInt = parse . BS.readInt
parseInteger = parse . BS.readInteger
readInt = parseInt <$> BS.getLine
readInteger = parseInteger <$> BS.getLine
readIntList = map parseInt . BS.words <$> BS.getLine
readIntegerList = map parseInteger . BS.words <$> BS.getLine

main = do
  [a, b, c] <- readIntegerList
  let s = a + b
      m = a * b
  putStrLn $ if s < c && 4 * m < (c - s) ^ 2 then "Yes" else "No"


main = do
  [a,b,c] <- map read . words <$> getLine :: IO [Double]
  putStrLn $ if sqrt a + sqrt b < sqrt c then "Yes" else "No"


{-# LANGUAGE
  ScopedTypeVariables, BangPatterns, TupleSections, ExplicitForAll,
  LambdaCase, MultiWayIf, Unsafe, RecordWildCards, FlexibleContexts, CPP,
  NoMonomorphismRestriction, GADTs, PatternGuards, MagicHash, UnboxedTuples,
  RankNTypes, EmptyDataDecls, EmptyCase, ViewPatterns, PolyKinds,
  TypeFamilies, OverloadedStrings, FlexibleInstances, UndecidableInstances,
  DefaultSignatures, GeneralizedNewtypeDeriving, StandaloneDeriving,
  DeriveGeneric, DeriveFunctor, DeriveDataTypeable, DeriveFoldable,
  DeriveTraversable, DeriveDataTypeable, FlexibleInstances,
  MultiParamTypeClasses #-}
{-# OPTIONS_GHC -O2 #-}

#define PHASE_FUSED [1]
#define PHASE_INNER [0]
#define INLINE_FUSED INLINE PHASE_FUSED
#define INLINE_INNER INLINE PHASE_INNER

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 Data.Functor.Identity
import Data.Data
import Data.Typeable
import GHC.Generics
import System.IO
import System.IO.Unsafe (unsafeDupablePerformIO, unsafePerformIO)
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 STL
-- import Control.Monad.ST.Safe
import Control.DeepSeq
import Data.Coerce
import qualified Data.ByteString as BSW
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy as BSLW
import qualified Data.ByteString.Lazy.Char8 as BSL
import qualified Data.ByteString.Builder as BSB
import qualified Data.ByteString.Unsafe as BSU
import qualified Data.ByteString.Internal as BSU
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.Vector.Fusion.Bundle.Monadic as VFBM
import qualified Data.Vector.Fusion.Bundle as VFB
import qualified Data.Vector.Fusion.Stream.Monadic as VFSM
import qualified Data.Vector.Fusion.Bundle.Size as VFBS
import qualified Data.Vector.Fusion.Util as VFU
import qualified Data.Attoparsec.ByteString.Char8 as Atto
import Debug.Trace
import Unsafe.Coerce
import Foreign.ForeignPtr
import GHC.Exts (build, Int(..), Int#,
                 (+#), (*#), (-#), (<#), (>=#), (==#), quotRemInt#,
                 remInt#, uncheckedIShiftRL#, andI#, orI#,
                isTrue#, Addr#, Ptr(..))

main :: IO ()
main = do
  [a,b,c] <- map readInt . words <$> getLine
  let cab = c-a-b
  putStrLn $ if cab >= 0 && 4*a*b < cab*cab then "Yes" else "No"
  return ()

#define IL(f) {-# INLINE f #-}; f

IL(putBuilder) = BSB.hPutBuilder stdout

printVecInLines, printVecInSpcSepLn ::
  (VG.Vector v a, ShowAsBuilder a) => v a -> IO ()
IL(printVecInLines) = putBuilder . v2BLines
IL(printVecInSpcSepLn) = putBuilder . v2BSpcSepLn

class ShowAsBuilder a where
  showAsBuilder :: a -> BSB.Builder
  default showAsBuilder :: (Show a) => a -> BSB.Builder
  IL(showAsBuilder) = BSB.string8 . show

-- Inconsistent with show
instance (ShowAsBuilder a, VG.Vector v a) => ShowAsBuilder (v a) where
  IL(showAsBuilder) = v2BSpcSep

#define INS(t,f) instance ShowAsBuilder t where { IL(showAsBuilder)=f }
INS(Int,BSB.intDec)
INS(Int8,BSB.int8Dec)
INS(Int16,BSB.int16Dec)
INS(Int32,BSB.int32Dec)
INS(Int64,BSB.int64Dec)
INS(Word,BSB.wordDec)
INS(Word8,BSB.word8Dec)
INS(Word16,BSB.word16Dec)
INS(Word32,BSB.word32Dec)
INS(Word64,BSB.word64Dec)
INS(Integer,BSB.integerDec)
INS(Float,BSB.floatDec)
INS(Double,BSB.doubleDec)
-- INS(String,BSB.string8) -- Inconsistent with Show
-- INS(BS.ByteString,BSB.byteString) -- Inconsistent with Show
-- INS(BSL.ByteString,BSB.lazyByteString) -- Inconsisitent with Show
#undef INS

-- Inconsistent with Show
instance (ShowAsBuilder a, ShowAsBuilder b) => ShowAsBuilder (a,b) where
  IL(showAsBuilder) = showTupAsBuilder
instance (ShowAsBuilder a, ShowAsBuilder b, ShowAsBuilder c) =>
  ShowAsBuilder (a,b,c) where
  IL(showAsBuilder) = showTup3AsBuilder
instance (ShowAsBuilder a, ShowAsBuilder b, ShowAsBuilder c, ShowAsBuilder d) =>
  ShowAsBuilder (a,b,c,d) where
  IL(showAsBuilder) = showTup4AsBuilder

IL(showTupAsBuilderWith)
  :: (a -> BSB.Builder) -> (b -> BSB.Builder) -> (a,b) -> BSB.Builder
showTupAsBuilderWith showA showB
  = \(a,b) -> (showA a <>) $ BSB.char7 ' ' <> showB b
IL(showTupAsBuilder) :: (ShowAsBuilder a, ShowAsBuilder b)
  => (a,b) -> BSB.Builder
showTupAsBuilder = showTupAsBuilderWith showAsBuilder showAsBuilder

IL(showTup3AsBuilderWith) :: (a -> BSB.Builder) -> (b -> BSB.Builder) ->
  (c -> BSB.Builder) -> (a,b,c) -> BSB.Builder
showTup3AsBuilderWith showA showB showC
  = \(a,b,c) -> (showA a <>) $ (BSB.char7 ' ' <>) $ (showB b <>)
                $ (BSB.char7 ' ' <>) $ showC c
IL(showTup3AsBuilder) :: (ShowAsBuilder a, ShowAsBuilder b, ShowAsBuilder c)
  => (a,b,c) -> BSB.Builder
showTup3AsBuilder
  = showTup3AsBuilderWith showAsBuilder showAsBuilder showAsBuilder

IL(showTup4AsBuilderWith) :: (a -> BSB.Builder) -> (b -> BSB.Builder) ->
  (c -> BSB.Builder) -> (d -> BSB.Builder) -> (a,b,c,d) -> BSB.Builder
showTup4AsBuilderWith showA showB showC showD
  = \(a,b,c,d) -> (showA a <>) $ (BSB.char7 ' ' <>)
                  $ showTup3AsBuilderWith showB showC showD (b,c,d)
IL(showTup4AsBuilder) ::
  (ShowAsBuilder a, ShowAsBuilder b, ShowAsBuilder c, ShowAsBuilder d) =>
  (a,b,c,d) -> BSB.Builder
showTup4AsBuilder = showTup4AsBuilderWith showAsBuilder showAsBuilder
                    showAsBuilder showAsBuilder

v2BSpcSepLn, v2BSpcSep, v2BConcat, v2BLines ::
  (VG.Vector v a, ShowAsBuilder a)
  => v a -> BSB.Builder
IL(v2BSpcSepLn) = v2BSpcSepLnWith showAsBuilder
IL(v2BSpcSep) = v2BSpcSepWith showAsBuilder
IL(v2BConcat) = v2BConcatWith showAsBuilder
IL(v2BLines) = v2BLinesWith showAsBuilder

v2BSpcSepLnWith, v2BSpcSepWith, v2BConcatWith, v2BLinesWith ::
  (VG.Vector v a)
  => (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
IL(v2BSpcSepLnWith) = v2BSpcSepPostfWith $ BS.singleton '\n'
IL(v2BSpcSepWith) = v2BSpcSepPostfWith BS.empty
IL(v2BConcatWith) showFct = VG.foldr ((<>) . showFct) mempty
IL(v2BLinesWith) showFct
  = VG.foldr (\ a -> (showFct a <>) . (BSB.char7 '\n' <>)) mempty

v2BSpcSepPostf :: (VG.Vector v a, ShowAsBuilder a)
  => BS.ByteString -- ^ postfix
  -> v a -> BSB.Builder
IL(v2BSpcSepPostf) = (`v2BSpcSepPostfWith` showAsBuilder)

v2BSpcSepPostfWith :: (VG.Vector v a)
  => BS.ByteString -- ^ postfix
  -> (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
IL(v2BSpcSepPostfWith) = vecToBuilder BS.empty $ BS.singleton ' '

IL(vecToBuilder) :: (VG.Vector v a)
  => BS.ByteString -- ^ prefix
  -> BS.ByteString -- ^ separator
  -> BS.ByteString -- ^ postfix
  -> (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
vecToBuilder !prefix !separator !postfix
  = vecToBuilder_ (BSB.byteString prefix)
                  (BSB.byteString separator)
                  (BSB.byteString postfix)

IL(vecToBuilder_) :: (VG.Vector v a)
  => BSB.Builder -- ^ prefix
  -> BSB.Builder -- ^ separator
  -> BSB.Builder -- ^ postfix
  -> (a -> BSB.Builder) -- ^ show function
  -> v a -> BSB.Builder
vecToBuilder_ !prefix !separator !postfix showFct = \vec -> prefix <>
  VG.foldr
  (\ a rest !prefx -> prefx <> (showFct a <> rest separator))
  (const postfix) vec mempty

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

IL(readLnWith) :: StateT BS.ByteString Maybe a -> IO a
readLnWith parser = fromJust . evalStateT parser <$> BS.getLine
IL(readContentWith) :: StateT BSL.ByteString Maybe a -> IO a
readContentWith parser = fromJust . evalStateT parser <$> BSL.getContents

IL(getVecGLn) :: (VG.Vector v a) =>
  Int -> StateT BS.ByteString Maybe a -> IO (v a)
getVecGLn n s = VG.unfoldrN n (runStateT s) <$> BS.getLine
IL(getVecGRest) :: (VG.Vector v a) =>
  Int -> StateT BSL.ByteString Maybe a -> IO (v a)
getVecGRest n s = VG.unfoldrN n (runStateT s) <$> BSL.getContents
IL(getVecLn) :: Int -> StateT BS.ByteString Maybe a -> IO (V.Vector a)
getVecLn = getVecGLn
IL(getVecRest) :: Int -> StateT BSL.ByteString Maybe a -> IO (V.Vector a)
getVecRest = getVecGRest
IL(getVecULn) :: (VU.Unbox a) =>
  Int -> StateT BS.ByteString Maybe a -> IO (VU.Vector a)
getVecULn = getVecGLn
IL(getVecURest) :: (VU.Unbox a) =>
  Int -> StateT BSL.ByteString Maybe a -> IO (VU.Vector a)
getVecURest = getVecGRest

IL(rIntL) :: StateT BSL.ByteString Maybe Int
rIntL = skipSpecialL $ StateT BSL.readInt
IL(rIntS) :: StateT BS.ByteString Maybe Int
rIntS = skipSpecialS $ StateT BS.readInt
IL(rStrL) :: (MonadState BSL.ByteString m) => m BS.ByteString
rStrL = skipSpecialL $ BSL.toStrict <$> state (BSL.span (>='!'))
IL(rStrS) :: (MonadState BS.ByteString m) => m BS.ByteString
rStrS = skipSpecialS $ state $ BS.span (>='!')
IL(rCharL) :: StateT BSL.ByteString Maybe Char
rCharL = StateT BSL.uncons
IL(rCharS) :: StateT BS.ByteString Maybe Char
rCharS = StateT BS.uncons
IL(dropSpecialL) :: (MonadState BSL.ByteString m) => m ()
dropSpecialL = modify $ BSL.dropWhile (<'!')
IL(dropSpecialS) :: (MonadState BS.ByteString m) => m ()
dropSpecialS = modify $ BS.dropWhile (<'!')
IL(skipSpecialL) :: (MonadState BSL.ByteString m) => m a -> m a
skipSpecialL = (dropSpecialL *>)
IL(skipSpecialS) :: (MonadState BS.ByteString m) => m a -> m a
skipSpecialS = (dropSpecialS *>)

IL(linToMat) :: (VG.Vector v a) => Int -> Int -> v a -> V.Vector (v a)
linToMat h w lvec = vEvalElemsId $ V.generate h (\i -> VG.slice (i*w) w lvec)

IL(mLinToMat) :: (VGM.MVector v a) => Int -> Int -> v s a -> V.Vector (v s a)
mLinToMat h w lvec = vEvalElemsId $ V.generate h (\i -> VGM.slice (i*w) w lvec)

IL(unsafeAddrToSVec) :: Int -> Addr# -> VS.Vector Word8
unsafeAddrToSVec n addr
  = (`VS.unsafeFromForeignPtr0` n)
    $ unsafeDupablePerformIO
    $ newForeignPtr_ $ Ptr addr

IL(vEvalElemsId) :: (VG.Vector v a) => v a -> v a
vEvalElemsId = vMapFoldl (\ !_ !x -> (x,())) ()

IL(vEvalElems) :: (VG.Vector v a) => v a -> ()
vEvalElems = VG.foldl' (\ !_ !_ -> ()) ()

IL(vMapFoldl) :: (VG.Vector v b, VG.Vector v c) =>
  (a -> b -> (c,a)) -> a -> v b -> v c
vMapFoldl f a
  = VG.unstream . VFB.inplace (streamMapFoldl f a) id . VG.stream

streamMapFoldl :: (Functor m) =>
  (a -> b -> (c,a)) -> a -> VFSM.Stream m b -> VFSM.Stream m c
{-# INLINE_FUSED streamMapFoldl #-}
streamMapFoldl f a (VFSM.Stream step s) = VFSM.Stream step1 (a,s)
  where
    {-# INLINE_INNER step1 #-}
    step1 (a0,s0) =  (<$> step s0) $ \r -> case r of
      VFSM.Yield b s1 -> case f a0 b of (c,a1) -> VFSM.Yield c (a1,s1)
      VFSM.Skip    s1 -> VFSM.Skip (a0,s1)
      VFSM.Done       -> VFSM.Done

IL(svecToBS) :: VS.Vector Word8 -> BS.ByteString
svecToBS vec = BSU.fromForeignPtr ptr 0 len
  where (ptr, len) = VS.unsafeToForeignPtr0 vec

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 TS(f,a,m,init)\
  IL(f) :: forall e i s. (C(a,m) A.Ix i) => (i,i) -> init m (a i e); f
#define N(f,g,h,a,m)\
  TS(f,a,m,e->)=A.newArray;\
  TS(g,a,m,)=A.newArray_;\
  TS(h,a,m,[e]->)=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 TS

#undef IL


import Control.Monad

readWords = map read . words <$> getLine

solve :: [Int] -> [(Int -> Bool)] -> [Int]
solve inData conditions
 | null conditions  = inData
 | otherwise        = solve newInData (tail conditions)
   where newInData = filter (head conditions) inData

main = do
 [n,m]    <- readWords :: IO [Int]
 inputStr <- replicateM m readWords
 let conditions = map (\[s,c] x -> if (s == 1) && (c == 0) then False else((x `div` (10^(n-s))) `mod` 10 == c)) inputStr
 print $ (\x -> if null x then -1 else minimum x) $ solve [10^(n-1)..((10^n) - 1)] conditions

Score : 300 points

Problem Statement

If there is an integer not less than 0 satisfying the following conditions, print the smallest such integer; otherwise, print -1.

The integer has exactly N digits in base ten. (We assume 0 to be a 1-digit integer. For other integers, leading zeros are not allowed.)

The s_i-th digit from the left is c_i. \left(i = 1, 2, \cdots, M\right)

Constraints

All values in input are integers.

1 \leq N \leq 3

0 \leq M \leq 5

1 \leq s_i \leq N

0 \leq c_i \leq 9

Input

Input is given from Standard Input in the following format:

N M
s_1 c_1
\vdots
s_M c_M

Output

Print the answer.

Sample Input 1

3 3
1 7
3 2
1 7

Sample Output 1

702

702 satisfies the conditions - its 1-st and 3-rd digits are 7 and 2, respectively - while no non-negative integer less than 702 satisfies them.

Sample Input 2

3 2
2 1
2 3

Sample Output 2

-1

Sample Input 3

3 1
1 0

Sample Output 3

-1


import Control.Monad

readWords = map read . words <$> getLine

solve :: [Int] -> [(Int -> Bool)] -> [Int]
solve inData conditions
 | null conditions  = inData
 | otherwise        = solve newInData (tail conditions)
   where newInData = filter (head conditions) inData

main = do
 [n,m]    <- readWords :: IO [Int]
 inputStr <- replicateM m readWords
 let conditions = map (\[s,c] x -> if (s == 1) && (c == 0) then False else((x `div` (10^(n-s))) `mod` 10 == c)) inputStr
 print $ (\x -> if null x then -1 else minimum x) $ solve [0..((10^n) - 1)] conditions


import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.Maybe
import qualified Data.Vector.Unboxed.Mutable as VUM

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words

getIntList = readIntList <$> BS.getLine
getIntNList n = map readIntList <$> replicateM (fromIntegral n) BS.getLine

simulate (sc : scs) table = do
  let i = head sc
  let v = last sc
  p <- VUM.read table (i -1)
  if p == (-1)
    then do
      VUM.write table (i -1) v
      if scs /= []
        then simulate scs table
        else return True
    else do
      if p /= v
        then return False
        else do
          if scs /= []
            then simulate scs table
            else return True

showL table i n f s = do
  v <- VUM.read table (i -1)
  if f == False && (v == 0 || v == (-1))
    then do
      if v == 0
        then do
          if n == 1
            then return "0"
            else return "-1"
        else showL table (i + 1) n True "1"
    else do
      let v'
            | v == (-1) = "0"
            | otherwise = show v
      let s' = v' ++ s
      if i == n
        then return (reverse s')
        else showL table (i + 1) n True s'

main = do
  [n, m] <- getIntList
  if m == 0
    then
      if n == 1
        then print 0
        else putStrLn $ "1" ++ (replicate (n - 1) '0')
    else do
      scs <- getIntNList m
      table <- VUM.replicate n (-1) :: IO (VUM.IOVector Int)
      result <- simulate scs table
      if result == False
        then print (-1)
        else do
          num <- showL table 1 n False ""
          putStrLn num


import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.Maybe
import qualified Data.Vector.Unboxed.Mutable as VUM

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words

getIntList = readIntList <$> BS.getLine
getIntNList n = map readIntList <$> replicateM (fromIntegral n) BS.getLine

simulate (sc : scs) table = do
  let i = head sc
  let v = last sc
  p <- VUM.read table (i -1)
  if p == (-1)
    then do
      VUM.write table (i -1) v
      if scs /= []
        then simulate scs table
        else return True
    else do
      if p /= v
        then return False
        else do
          if scs /= []
            then simulate scs table
            else return True

showL table i n f s = do
  v <- VUM.read table (i -1)
  if f == False && (v == 0 || v == (-1))
    then do
      if v == 0
        then return "-1"
        else showL table (i + 1) n True "1"
    else do
      let v'
            | v == (-1) = "0"
            | otherwise = show v
      let s' = v' ++ s
      if i == n
        then return (reverse s')
        else showL table (i + 1) n True s'

main = do
  [n, m] <- getIntList
  if m == 0
    then
      if n == 1
        then print 0
        else putStrLn $ "1" ++ (replicate (n - 1) '0')
    else do
      scs <- getIntNList m
      table <- VUM.replicate n (-1) :: IO (VUM.IOVector Int)
      result <- simulate scs table
      if result == False
        then print (-1)
        else do
          num <- showL table 1 n False ""
          putStrLn num


import Control.Monad (replicateM)
import Data.Char (intToDigit)

f :: Int -> [[Int]] -> Int -> Int
f n sc x
    | length strX > n = -1
    | and [strX !! (s - 1) == intToDigit c | [s, c] <- sc] = x
    | otherwise = f n sc (x + 1)
  where strX = show x

main = do
    [n, m] <- map read . words <$> getLine :: IO [Int]
    sc <- replicateM m (map read . words <$> getLine) :: IO [[Int]]
    print $ f n sc (if n == 1 then 0 else 10 ^ (n - 1))


{-# LANGUAGE MultiWayIf #-}

module Main where
import           Prelude
import           Control.Monad
import           Data.Monoid
import qualified Data.Array                    as A
import qualified Data.Array.IO                 as AIO
import qualified Data.Array.Unboxed            as AU
import qualified Data.ByteString               as BS
import qualified Data.ByteString.Char8         as BSC8
import           Data.Char
import           Data.List
import qualified Data.Map.Strict               as M
import           Data.Maybe
import           Data.Ord
import qualified Data.Set                      as S
import qualified Data.Vector                   as V
--import           Data.Vector.Algorithms.Search
import qualified Data.Vector.Mutable           as VM
import qualified Data.Vector.Unboxed           as VU
import qualified Data.Vector.Unboxed.Mutable   as VUM
import           Debug.Trace
--import qualified Data.Heap                     as H
import qualified Data.Sequence                 as SQ
import qualified Data.Graph.Inductive.PatriciaTree
                                               as G
import qualified Data.Graph.Inductive.Graph    as G
import           Data.Int
import           Data.Bits

main = do
  [iN, iM] <- getIL
  iSC      <- replicateM iM $ getIT2
  let sortedSC = sortBy (comparing fst) iSC

  let ret      = numbers sortedSC iN

  if ret == []
    then print (-1)
    else putStrLn $ foldl (\acc x -> acc ++ (show x)) [] ret

numbers :: [(Int, Int)] -> Int -> [Int]
numbers scs n = reverse $ reNumbers scs n 1 []

reNumbers :: [(Int, Int)] -> Int -> Int -> [Int] -> [Int]
reNumbers [] n cnt nums = if
  | cnt > n                        -> nums
  | cnt <= n && cnt == 1 && n == 1 -> reNumbers [] n (cnt + 1) (0 : nums)
  | cnt <= n && cnt == 1 && n /= 1 -> reNumbers [] n (cnt + 1) (1 : nums)
  | cnt <= n && cnt /= 1           -> reNumbers [] n (cnt + 1) (0 : nums)

reNumbers (sc : scs) n cnt nums = if
  | cnt > n && fst sc < cnt && head nums == snd sc -> reNumbers scs n cnt nums
  | cnt > n && fst sc < cnt && head nums /= snd sc -> []
  | cnt > n -> nums
  | fst sc == cnt && fst sc == 1 && snd sc == 0 && n /= 1 -> []
  | fst sc == cnt -> reNumbers scs n (cnt + 1) (snd sc : nums)
  | fst sc < cnt && head nums == snd sc -> reNumbers scs n cnt nums
  | fst sc < cnt && head nums /= snd sc -> []
  | cnt == 1 && n /= 1 -> reNumbers (sc : scs) n (cnt + 1) (1 : nums)
  | otherwise -> reNumbers (sc : scs) n (cnt + 1) (0 : nums)

{-
hMaxPoint :: Int
hMaxPoint = 101
hMaximum :: H.Heap Int -> Int
hMaximum h = hMaxPoint - (H.minimum h)
hInsertMax :: Int -> H.Heap Int -> H.Heap Int
hInsertMax x = H.insert (hMaxPoint - x)
hDeleteMax :: H.Heap Int -> H.Heap Int
hDeleteMax = H.deleteMin
-}

{-
aroundSquare y x h w =
  [ aYX
  | aYX@(aY, aX) <- [(y - 1, x), (y, x - 1), (y + 1, x), (y, x + 1), (y, x)]
  , aY >= 0
  , aY <= (h - 1)
  , aX >= 0
  , aX <= (w - 1)
  ]
-}

splitReadBSC8 :: BSC8.ByteString -> [Int]
splitReadBSC8 bs =
  let (bs1, newBs) = BSC8.splitAt 1 bs
  in  if newBs == BSC8.empty
        then [test $ BSC8.readInt bs1]
        else (test $ BSC8.readInt bs1) : (splitReadBSC8 newBs)
  where test (Just (i, _)) = i

read2DimChar :: VM.IOVector (VUM.IOVector Char) -> Int -> Int -> IO Char
read2DimChar vec y x = do
  vecY <- VM.read vec y
  VUM.read vecY x
write2DimChar :: VM.IOVector (VUM.IOVector Char) -> Int -> Int -> Char -> IO ()
write2DimChar vec y x t = do
  vecY <- VM.read vec y
  VUM.write vecY x t

readTwoDimVec :: VM.IOVector (VUM.IOVector Int) -> Int -> Int -> IO Int
readTwoDimVec vec y x = do
  vecY <- VM.read vec y
  VUM.read vecY x
writeTwoDimVec :: VM.IOVector (VUM.IOVector Int) -> Int -> Int -> Int -> IO ()
writeTwoDimVec vec y x t = do
  vecY <- VM.read vec y
  VUM.write vecY x t

getVUI :: IO (VU.Vector Int)
getVUI = VU.unfoldr (BSC8.readInt . BSC8.dropWhile isSpace) <$> BSC8.getLine

getRepVUI :: Int -> IO (VU.Vector Int)
getRepVUI n =
  VU.unfoldr (BSC8.readInt . BSC8.dropWhile isSpace)
    .   BSC8.unlines
    <$> replicateM n BSC8.getLine

getVS :: IO (V.Vector Char)
getVS = V.fromList <$> getLine

getVUC :: IO (VU.Vector Char)
getVUC = VU.fromList <$> getLine

readI :: BSC8.ByteString -> Int
readI = fst . fromJust . BSC8.readInt

readIL :: BSC8.ByteString -> [Int]
readIL = map readI . BSC8.words

readIT2 :: BSC8.ByteString -> (Int, Int)
readIT2 bs =
  let words = BSC8.words bs in (readI $ head words, readI $ last words)

getI :: IO Int
getI = readI <$> BSC8.getLine

getIL :: IO [Int]
getIL = readIL <$> BSC8.getLine

getIT2 :: IO (Int, Int)
getIT2 = readIT2 <$> BSC8.getLine

getIT2Order :: IO (Int, Int)
getIT2Order =
  getIL >>= \x@[xz, xo] -> if xz > xo then return (xo, xz) else return (xz, xo)

getNIS :: Int -> IO [(Int, BSC8.ByteString)]
getNIS 0 = return []
getNIS n = do
  [aFst, aSnd] <- BSC8.words <$> BSC8.getLine
  next         <- getNIS (n - 1)
  return ((readI aFst, aSnd) : next)

il2Words :: [Int] -> String
il2Words = unwords . map show

il2Lines :: [Int] -> String
il2Lines = unlines . map show

p :: Int -> Int
p x = x - 1

--modNum = 10 ^ 9 + 7
--modulus = 1000000007
modulus = 2019

newtype MInt = M Int

instance Num MInt where
  (M a) + (M b) = M $ mod (a + b) modulus

  (M a) - (M b) = M $ mod (a - b) modulus

  (M a) * (M b) = M $ mod (a * b) modulus

  fromInteger a = M $ mod (fromInteger a) modulus

  abs (M a) = M $ abs a

  signum (M a) = M $ signum a

instance Show MInt where
  show (M a) = show (mod a modulus)

int2MInt :: Int -> MInt
int2MInt a = M (mod a modulus)

mInt2Int :: MInt -> Int
mInt2Int (M a) = a

{-
factsMod :: V.Vector MInt
factsMod = V.scanl (\acc x -> M x * acc) 1 $ V.generate 2000000 (+ 1)

factMod :: Int -> MInt
factMod = (factsMod V.!)
-}

factMod :: Int -> MInt
factMod n | n == 0    = 1
          | n == 1    = 1
          | otherwise = M n * (factMod (n - 1))

factDivMod :: Int -> Int -> MInt
factDivMod n k | n == k    = M 1
               | otherwise = M n * (factDivMod (n - 1) k)

powMod :: MInt -> MInt -> MInt
powMod n mK@(M k) | k == 0       = M 1
                  | k == 1       = n
                  | mod k 2 == 0 = powMod (n ^ 2) (M (div k 2))
                  | otherwise    = n * powMod (n ^ 2) (M (div k 2))

invMod :: MInt -> MInt
invMod (M 0) = error "inverse of 0"
invMod x     = powMod x (M (modulus - 2))

{-
combiMod :: Int -> Int -> MInt
combiMod n k
  | n < 0 || k < 0 || k > n = 0
  | otherwise = factMod n * invMod (factMod (n - k)) * invMod (factMod k)
-}
combiMod :: Int -> Int -> MInt
combiMod n k | n < 0 || k < 0 || k > n = 0
             | k > n - k = factDivMod n k * invMod (factMod (n - k))
             | otherwise = factDivMod n (n - k) * invMod (factMod k)

combi :: Int -> Int -> Int
combi n k | n < 0 || k < 0 || k > n = 0
          | k == n                  = 1
          | otherwise               = div (div (fact n) (fact (n - k))) (fact k)

primes :: Integral a => [a]
primes = map fromIntegral $ [2, 3] ++ primes'
 where
  primes' = [5] ++ f 1 7 primes'
  f m s (p : ps) = [ n | n <- ns, gcd m n == 1 ] ++ f (m * p) (p * p) ps
    where ns = [ x + y | x <- [s, s + 6 .. p * p - 2], y <- [0, 4] ]

fact :: Int -> Int
fact 0         = 1
fact n | n > 0 = n * fact (n - 1)

knappsack :: Int -> [(Int, Int)] -> VU.Vector Int
knappsack maxW [] = VU.replicate (maxW + 1) 0
knappsack maxW ((w, v) : xs) =
  let tailDP = knappsack maxW xs
  in  VU.generate (maxW + 1) $ \i -> if w <= i
        then max (tailDP VU.! i) (tailDP VU.! (i - w) + v)
        else tailDP VU.! i

knappsackMaxVal
  :: AIO.IOArray (Int, Int) Int
  -> VU.Vector Int
  -> VU.Vector Int
  -> Int
  -> IO Int
knappsackMaxVal dp volumeVec valueVec dpSize = do
  mapM_
    (\i -> mapM_
      (\v -> do
        let volume = volumeVec VU.! i
            value  = valueVec VU.! i
        newdp <- if v < volume
          then AIO.readArray dp (i, v)
          else do
            dpIVm <- AIO.readArray dp (i, v - volume)
            dpIV  <- AIO.readArray dp (i, v)
            return (max (dpIVm + value) dpIV)
        AIO.writeArray dp (i + 1, v) newdp
      )
      [0 .. dpSize]
    )
    [0 .. VU.length volumeVec - 1]
  AIO.readArray dp (VU.length volumeVec, dpSize)

knappsackMinVol
  :: AIO.IOArray (Int, Int) Int
  -> VU.Vector Int
  -> VU.Vector Int
  -> Int
  -> Int
  -> IO Int
knappsackMinVol dp volumeVec valueVec dpSize maxVolume = do
  mapM_
    (\i -> mapM_
      (\v -> do
        let volume = volumeVec VU.! i
            value  = valueVec VU.! i
        newdp <- if (v + value) > dpSize
          then AIO.readArray dp (i, v)
          else do
            dpIVm <- AIO.readArray dp (i, v + value)
            dpIV  <- AIO.readArray dp (i, v)
            return (min (dpIVm - volume) dpIV)
        AIO.writeArray dp (i + 1, v) newdp
      )
      (reverse [0 .. dpSize])
    )
    [0 .. VU.length valueVec - 1]
  foldM
    (\acc x -> do
      num <- AIO.readArray dp (VU.length volumeVec, x)
      return $ if num <= maxVolume then x else acc
    )
    0
    [0 .. dpSize]

lcsTable :: BS.ByteString -> BS.ByteString -> V.Vector (VU.Vector Int)
lcsTable xs ys = V.scanl step (VU.replicate (m + 1) 0) xs'
 where
  n   = BS.length xs
  m   = BS.length ys
  xs' = V.generate n $ BSC8.index xs :: V.Vector Char
  ys' = VU.generate m $ BSC8.index ys :: VU.Vector Char
  step :: VU.Vector Int -> Char -> VU.Vector Int
  step v x = VU.scanl innerStep 0 (VU.zip3 v (VU.tail v) ys')
   where
    innerStep :: Int -> (Int, Int, Char) -> Int
    innerStep a (b, c, y) | x == y    = 1 + b
                          | otherwise = max a c
{-
main = do
xs <- BS.getLine
ys <- BS.getLine
print $ VU.last $ V.last $ lcsTable xs ys
-}

myFilter :: (a -> Bool) -> [a] -> ([a], [a])
myFilter _pred [] = ([], [])
myFilter pred (x : xs)
  | pred x    = let (l, r) = myFilter pred xs in (x : l, r)
  | otherwise = let (l, r) = myFilter pred xs in (l, x : r)

--UnionFindデータ構造
type UF_PARENT = VUM.IOVector Int
type UF_RANK = VUM.IOVector Int
type UF = (UF_PARENT, UF_RANK)
uf_makeSet :: Int -> IO UF
uf_makeSet n = do
  parent <- VU.thaw $ VU.generate n id
  rank   <- VUM.replicate n 0
  return (parent, rank)
uf_union :: UF -> Int -> Int -> IO ()
uf_union unionFind@(parents, ranks) x y = do
  xRoot     <- uf_find unionFind x
  yRoot     <- uf_find unionFind y
  xRootRank <- VUM.read ranks xRoot
  yRootRank <- VUM.read ranks yRoot
  if
    | xRootRank > yRootRank -> VUM.write parents yRoot xRoot
    | xRootRank < yRootRank -> VUM.write parents xRoot yRoot
    | xRoot /= yRoot -> do
      VUM.write parents yRoot xRoot
      VUM.write ranks xRoot (xRootRank + 1)
    | otherwise -> return ()

uf_find :: UF -> Int -> IO Int
uf_find unionFind@(parents, ranks) x = do
  xParent <- VUM.read parents x
  if xParent == x
    then return x
    else do
      xRootParent <- uf_find unionFind xParent
      VUM.write parents x xRootParent
      return xRootParent

--UnDirectedGraph(無向グラフ)データ構造
type UDG = M.Map Int [Int]
list2UDG :: [(Int, Int)] -> M.Map Int [Int]
list2UDG = foldl
  (\acc x ->
    let lr = M.insertWith (++) (fst x) [snd x] acc
    in  M.insertWith (++) (snd x) [fst x] lr
  )
  M.empty