import Data.List
main=do
 getLine
 a<-map(negate.read).words<$>getLine
 bc<-map((\[b,a]->(-a,b)).map read.words).lines<$>getContents
 let x=sort$bc++zip a(repeat 1)
 print$negate$f x(length a)
f((a,c):x)t
 |t>c=a*c+f x(t-c)
 |otherwise=a*t