Buggy python submission (verdict: Runtime Error):
import bisect
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if t>A[index] and max_index>index:
                A[index] = t
            else:
                return A
            index+=1
    return A
    
if __name__ == '__main__':
    A=main()
    print(sum(A))


Fixed submission:
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if max_index>index and  t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    return A
    
if __name__ == '__main__':
    A=main()
    print(sum(A))

Buggy python submission (verdict: Runtime Error):
import bisect
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if t>A[index] and max_index>index:
                A[index] = t
            else:
                return A
            index+=1
    
if __name__ == '__main__':
    A=main()
    print(sum(A))


Fixed submission:
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if max_index>index and  t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    return A
    
if __name__ == '__main__':
    A=main()
    print(sum(A))

Buggy python submission (verdict: Runtime Error):
import bisect
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if t>A[index] or max_index>index:
                A[index] = t
            else:
                return A
            index+=1
    
if __name__ == '__main__':
    A=main()
    print(sum(A))


Fixed submission:
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if max_index>index and  t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    return A
    
if __name__ == '__main__':
    A=main()
    print(sum(A))

Buggy python submission (verdict: Runtime Error):
import bisect
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    
if __name__ == '__main__':
    A=main()
    print(sum(A))


Fixed submission:
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if max_index>index and  t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    return A
    
if __name__ == '__main__':
    A=main()
    print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)

    for B,C in BC:
        index = bisect.bisect_right(A,C)
        if index==0:
            break
        if index > B:
            A = A[B:index] + [C] *B + A[index:]
        else:
            A = [C]*index + A[index:]

    print(sum(A))
    
if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if max_index>index and  t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    return A
    
if __name__ == '__main__':
    A=main()
    print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N,M = map(int,input().split())
A= list(map(int,input().split()))
A = sorted(A)

BC=[list(map(int,input().split())) for i in range(M)]
BC = sorted(BC, key=lambda x: x[1],reverse=True)

for B,C in BC:
    index = bisect.bisect_right(A,C)
    if index==0:
        break
    if index > B:
        A = A[B:index] + [C] *B + A[index:]
    else:
        A = [C]*index + A[index:]

print(sum(A))


Fixed submission:
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if max_index>index and  t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    return A
    
if __name__ == '__main__':
    A=main()
    print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

N,M = map(int,input().split())
A= list(map(int,input().split()))

BC=[list(map(int,input().split())) for i in range(M)]
[A.extend([C]*B) for B,C in BC]
print(sum(heapq.nlargest(N, A)))


Fixed submission:
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if max_index>index and  t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    return A
    
if __name__ == '__main__':
    A=main()
    print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import bisect
input = sys.stdin.readline

def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    for i in range(M):
        B,C = map(int,input().split())

        index = bisect.bisect_right(A,C)

        if index > B:
            A = A[B:index] + [C] *B + A[index:]
        else:
            A = [C]*index + A[index:]
    print(sum(A))
    
if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if max_index>index and  t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    return A
    
if __name__ == '__main__':
    A=main()
    print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline


N,M = map(int,input().split())
A= list(map(int,input().split()))


for i in range(M):
    B,C = map(int,input().split())
    
    A.extend([C]*B)
b=sorted(A)
print(sum(b[-N:]))

Fixed submission:
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if max_index>index and  t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    return A
    
if __name__ == '__main__':
    A=main()
    print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):

N,M = map(int,input().split())
A= list(map(int,input().split()))


for i in range(M):
    B,C = map(int,input().split())
    
    A.extend([C]*B)
b=sorted(A)
print(sum(b[-N:]))

Fixed submission:
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if max_index>index and  t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    return A
    
if __name__ == '__main__':
    A=main()
    print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N,M = map(int,input().split())
A= list(map(int,input().split()))

import bisect

A = sorted(A)

A

for i in range(M):
    B,C = map(int,input().split())
    
    index = bisect.bisect_right(A,C)
    
    if index > B:
        A = A[B:index] + [C] *B + A[index:]
    else:
        A = [C]*index + A[index:]

print(sum(A))

Fixed submission:
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if max_index>index and  t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    return A
    
if __name__ == '__main__':
    A=main()
    print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

N,M = map(int,input().split())
A= list(map(int,input().split()))

import bisect

A = sorted(A)

A

for i in range(M):
    B,C = map(int,input().split())
    
    index = bisect.bisect_right(A,C)
    
    if index > B:
        A = A[B:index] + [C] *B + A[index:]
    else:
        A = [C]*index + A[index:]

print(sum(A))

Fixed submission:
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if max_index>index and  t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    return A
    
if __name__ == '__main__':
    A=main()
    print(sum(A))

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

import bisect
# insert_index = bisect.bisect_left(a,x)

A = sorted(A)
for i in range(M):
    B,C = map(int,input().split())
    
    index = bisect.bisect_right(A,C)
    index = min(index, B)
    A = [C]*index + A[index:]
    A = sorted(A)


print(sum(A))

Fixed submission:
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if max_index>index and  t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    return A
    
if __name__ == '__main__':
    A=main()
    print(sum(A))

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

import bisect
# insert_index = bisect.bisect_left(a,x)

A = sorted(A)
for i in range(M):
    B,C = map(int,input().split())
    
    index = bisect.bisect_right(A,C)
    index = min(index, B)
    A = [C]*index + A[index:]
    A = sorted(A)
    print(A)

print(sum(A))

Fixed submission:
import sys
input = sys.stdin.readline
def main():
    N,M = map(int,input().split())
    A= list(map(int,input().split()))
    A = sorted(A)

    BC=[list(map(int,input().split())) for i in range(M)]
    BC = sorted(BC, key=lambda x: x[1],reverse=True)
    index=0
    max_index=len(A)
    
    for B,C in BC:
        temp = [C]*B
        for t in temp:
            if max_index>index and  t>A[index]:
                A[index] = t
            else:
                return A
            index+=1
    return A
    
if __name__ == '__main__':
    A=main()
    print(sum(A))