import Data.List f::[(Int,Int)]->[(Int,Int)]->Int->Int->Int f [] _ _ ans=ans f _ [] _ ans=ans f xs2@((x,c1):xs) ys2@((y,c2):ys) n ans |x+yn=f xs2 ys n ans |otherwise=f xs ys n (ans+c1*c2) main = do e<-getLine e2<-getLine let ts=map read (words e2)::[Int] xs=group $ sort $ ts ys=reverse xs (n:x:[])=map read (words e)::[Int] zs1=zip (map head xs) (map length xs) zs2=zip (map head ys) (map length ys) print $ f zs1 zs2 x 0