Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ind =0
for b,c in l[::-1]:
    mx = min(N,ind+b)
    for i in range(ind,mx):
        if c>A[0]:
            A = A[1:]
            A.append(c)
    if mx==N:
        break
    ind += b
print(sum(A))

Fixed submission:
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ind =0
for b,c in l[::-1]:
    mx = min(N,ind+b)
    for i in range(ind,mx):
        if c>A[i]:
            A[i] = c
    if mx==N:
        break
    ind += b
print(sum(A))

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ind =0
for b,c in l[::-1]:
    mx = min(N,ind+b)
    for i in range(ind,mx):
        if c>A[i]:
            A = A[1:].append(c)
    if mx==N:
        break
    ind += b
print(sum(A))

Fixed submission:
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ind =0
for b,c in l[::-1]:
    mx = min(N,ind+b)
    for i in range(ind,mx):
        if c>A[i]:
            A[i] = c
    if mx==N:
        break
    ind += b
print(sum(A))

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
B =[]
C =[]

l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ttl = 0
m_score = 0
flag =False
for i in l[::-1]:
    p = i[0]
    k = i[1]
    ttl+=p
    t = ttl-1
    if t>=N:
        print("WA")
        for n,j in enumerate(i[t-p+1:]):
            if j<k:
                m_score+=p
                break
        m_score += sum(A[t-p+1+n:])
        flag = True
        break
    if A[t]<k:
        m_score += p*k
        continue
    else:
        for n,j in enumerate(A[t-p+1:t+1]):
            if j>k:
                flag = True
                break
        if flag:
            m_score += k*n+sum(A[t-p+1+n:])
            break
if not flag:
    if ttl<N:
        m_score += sum(A[ttl:])
print(m_score)

Fixed submission:
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ind =0
for b,c in l[::-1]:
    mx = min(N,ind+b)
    for i in range(ind,mx):
        if c>A[i]:
            A[i] = c
    if mx==N:
        break
    ind += b
print(sum(A))

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
B =[]
C =[]

l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ttl = 0
m_score = 0
flag =False
for i in l[::-1]:
    p = i[0]
    k = i[1]
    ttl+=p
    t = ttl-1
    if t>=N:
        for n,j in enumerate(i[t-p+1:]):
            if j<k:
                m_score+=p
                break
        m_score += sum(A[t-p+1+n:])
        flag = True
        break
    if A[t]<k:
        m_score += p*k
        continue
    else:
        for n,j in enumerate(A[t-p+1:t+1]):
            if j>k:
                flag = True
                break
        if flag:
            m_score += k*n+sum(A[t-p+1+n:])
            break
if not flag:
    if ttl<N:
        m_score += sum(A[ttl:])
print(m_score)


Fixed submission:
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ind =0
for b,c in l[::-1]:
    mx = min(N,ind+b)
    for i in range(ind,mx):
        if c>A[i]:
            A[i] = c
    if mx==N:
        break
    ind += b
print(sum(A))

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
B =[]
C =[]

l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ttl = 0
m_score = 0
flag =False
for i in l[::-1]:
    p = i[0]
    k = i[1]
    ttl+=p
    t = ttl-1
    if t>=N:
        for n,j in enumerate(i[t-p+1:]):
            if j<k:
                m_score+=p
                break
        m_score += sum(A[t-p+1+n:])
        break
    if A[t]<k:
        m_score += p*k
        continue
    else:
        for n,j in enumerate(A[t-p+1:t+1]):
            if j>k:
                flag = True
                break
        if flag:
            m_score += k*n+sum(A[t-p+1+n:])
            break
if not flag:
    if ttl<N:
        m_score += sum(A[ttl:])
print(m_score)

Fixed submission:
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ind =0
for b,c in l[::-1]:
    mx = min(N,ind+b)
    for i in range(ind,mx):
        if c>A[i]:
            A[i] = c
    if mx==N:
        break
    ind += b
print(sum(A))

Buggy python submission (verdict: Runtime Error):
 map(int,input().split(' '))
A = list(map(int,input().split(' ')))
B =[]
C =[]

l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ttl = 0
m_score = 0
flag =False
for i in l[::-1]:
    p = i[0]
    k = i[1]
    ttl+=p
    t = ttl-1
    if t>=N:
        t = N-1
    if A[t]<k:
        m_score += p*k
        continue
    else:
        for n,j in enumerate(A[t-p+1:t+1]):
            if j>k:
                flag = True
                break
        if flag:
            m_score += k*n+sum(A[t-p+1+n:])
            break
if not flag:
    if ttl<N:
        m_score += sum(A[ttl:])
print(m_scor

Fixed submission:
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ind =0
for b,c in l[::-1]:
    mx = min(N,ind+b)
    for i in range(ind,mx):
        if c>A[i]:
            A[i] = c
    if mx==N:
        break
    ind += b
print(sum(A))

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
B =[]
C =[]

l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ttl = 0
m_score = 0
flag =False
for i in l[::-1]:
    p = i[0]
    k = i[1]
    ttl+=p
    t = ttl-1
    if A[t]<k:
        m_score += p*k
        continue
    else:
        for n,j in enumerate(A[t-p+1:t+1]):
            if j>k:
                flag = True
                break
        if flag:
            m_score += k*n+sum(A[t-p+1+n:])
            break
if not flag:
    if ttl<N:
        m_score += sum(A[ttl:])
print(m_score)

Fixed submission:
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ind =0
for b,c in l[::-1]:
    mx = min(N,ind+b)
    for i in range(ind,mx):
        if c>A[i]:
            A[i] = c
    if mx==N:
        break
    ind += b
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
B =[]
C =[]

l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

flag =False
for i in l[::-1]:
    for j in A[:i[0]]:
        k = i[1]
        if j<k:
            if len(A)>1:
                A = A[1:]
                A.append(k)
            else:
                A[0] = k
        else:
            flag = True
            break
    if flag:
        break
print(sum(A))

Fixed submission:
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ind =0
for b,c in l[::-1]:
    mx = min(N,ind+b)
    for i in range(ind,mx):
        if c>A[i]:
            A[i] = c
    if mx==N:
        break
    ind += b
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
l = [list(map(int,input().split())) for i in range(M)]

def change(ll,b,c):
    ll.sort()
    if b>N:
        b=N
    for n,i in enumerate(ll[:b]):
        if c>i:
            ll[n] = c
        else:
            break
    return ll

for i in range(M):
    A = change(A,l[i][0],l[i][1])
print(sum(A))

Fixed submission:
N,M = map(int,input().split(' '))
A = list(map(int,input().split(' ')))
l = [list(map(int,input().split())) for i in range(M)]

A.sort()
l = sorted(l,key = lambda x:x[1])

ind =0
for b,c in l[::-1]:
    mx = min(N,ind+b)
    for i in range(ind,mx):
        if c>A[i]:
            A[i] = c
    if mx==N:
        break
    ind += b
print(sum(A))