import Control.Monad
import Data.List

main = do
  [n, m] <- map read . words <$> getLine
  cards <- map read . words <$> getLine
  reads <- replicateM m (map read . words <$> getLine)
  let xs = concat reads
  print $ func xs cards

func :: [Int] -> [Int] -> Int
func [] cards = sum cards
func (b:c:xs) cards = func xs (zipWith max (replicate b c ++ repeat 0) (sort cards))