program integer_cards
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int64):: n,m,i,sumi,ans
    integer(int64), allocatable:: a(:),b(:),c(:)
    integer(int64), allocatable:: change(:)

    read*, n,m
    allocate(a(n),change(n))
    allocate(b(m),c(m))
    read*, a(:)
    do i=1,m
        read*, b(i), c(i)
    end do
    call merge_sort(a,1_8,n)
    call double_sort(c,b,1_8,m)
    sumi=1
    change(:) = 0
    do i=1,m
        change(sumi:min(sumi+b(i)-1,n)) = c(i)
        sumi=sumi+b(i)
        if (sumi > n) exit
    end do
    ans=0
    do i=1,n
        ans=ans+max(a(i),change(i))
    end do
    print'(i0)', ans
    

contains
    recursive subroutine merge_sort(ar, fst, lst)
        integer(8),intent(inout):: ar(:)
        integer(8),intent(in):: fst,lst
        integer(8):: mdl

        if (lst-fst < 2) then
            if (ar(fst) > ar(lst)) call swap(ar(fst),ar(lst))
            return
        end if

        mdl = (fst+lst)/2
        call merge_sort(ar, fst, mdl)
        call merge_sort(ar, mdl+1, lst)
        call merge_(ar, fst, mdl, lst)
    end subroutine

    subroutine merge_(ar, fst, mdl, lst)
        integer(8),intent(inout):: ar(:)
        integer(8),intent(in):: fst, mdl, lst
        integer(8),allocatable:: tmp(:)
        integer(8):: li, ri, ti

        allocate(tmp(lst-fst+1))

        li=fst
        ri=mdl+1   
        ti=1

        do while (li <= mdl .and. ri <= lst)
            if (ar(li) <= ar(ri)) then
                tmp(ti) = ar(li)
                li=li+1
            else
                tmp(ti) = ar(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            tmp(ti:) = ar(li:mdl)
        else
            tmp(ti:) = ar(ri:lst)
        end if

        ar(fst:lst) = tmp(:)
        deallocate(tmp)
    end subroutine

    subroutine swap(x,y)
        integer(8),intent(inout):: x,y
        integer(8):: tmp
        tmp = x
        x = y
        y = tmp
    end subroutine

    recursive subroutine double_sort(ar1, ar2, fst, lst)
        integer(8),intent(inout):: ar1(:),ar2(:)
        integer(8),intent(in):: fst,lst
        integer(8):: mdl

        if (lst - fst < 2) then
            if (ar1(fst) < ar1(lst)) then
                call swap(ar1(fst), ar1(lst))
                call swap(ar2(fst), ar2(lst))
            end if
            return
        end if

        mdl = (fst+lst)/2

        call double_sort(ar1,ar2,fst,mdl)
        call double_sort(ar1,ar2,mdl+1,lst)
        call double_merge_(ar1,ar2,fst,mdl,lst)
    end subroutine


    subroutine double_merge_(ar1,ar2,fst,mdl,lst)
        integer(8),intent(inout):: ar1(:),ar2(:)
        integer(8),intent(in):: fst,mdl,lst
        integer(8),allocatable:: t1(:),t2(:)
        integer(8):: li,ri,ti

        allocate(t1(lst-fst+1), t2(lst-fst+1))

        li=fst
        ri=mdl+1
        ti=1

        do while(li <= mdl .and. ri <= lst)
            if (ar1(li) >= ar1(ri)) then
                t1(ti) = ar1(li) 
                t2(ti) = ar2(li)
                li=li+1
            else
                t1(ti) = ar1(ri)
                t2(ti) = ar2(ri)
                ri=ri+1
            end if
            ti=ti+1
        end do

        if (li <= mdl) then
            t1(ti:) = ar1(li:mdl)
            t2(ti:) = ar2(li:mdl)
        else
            t1(ti:) = ar1(ri:lst)
            t2(ti:) = ar2(ri:lst)
        end if

        ar1(fst:lst) = t1(:)
        ar2(fst:lst) = t2(:)

        deallocate(t1,t2)
    end subroutine
end program integer_cards

Measured memory_bytes: 4644