Buggy python submission (verdict: Time Limit Exceeded):

def solve():
    N, M = [int(_) for _ in raw_input().split()]
    A = [int(_) for _ in raw_input().split()]
    A.sort()
    for i in range(M):
        Bi, Ci = [int(_) for _ in raw_input().split()]
        low_ind = 0
        up_ind = N - 1
        found = None
        if A[low_ind] > Ci:
            continue
        elif A[up_ind] < Ci:
            A = A[Bi:]
            A.extend([Ci] * Bi)
            continue
        while (up_ind - low_ind) > 1:
            mid = (up_ind + low_ind) / 2
            if A[mid] < Ci:
                low_ind = mid
            elif A[mid] > Ci:
                up_ind = mid
            elif A[mid] == Ci:
                found = mid
                break
        if found is not None:
            cnt = found
        else:
            cnt = up_ind
        repl = min(cnt,Bi)
        for i in range(repl):
            A.insert(i + cnt, Ci)
        A = A[repl:]
    print sum(A)

if __name__ == '__main__':
    solve()


Fixed submission:
def solve():
    N, M = [int(_) for _ in raw_input().split()]
    A = [int(_) for _ in raw_input().split()]
    A.sort(reverse = True)
    BC = []
    for i in range(M):
        Bi, Ci = [int(_) for _ in raw_input().split()]
        BC.append([Bi, Ci])
    BC.sort(key = lambda x: x[1], reverse = True)
    ind_BC = 0
    ind_A = 0
    merged = []
    while ind_A < N and ind_BC < M:
        Bi = BC[ind_BC][0]
        Ci = BC[ind_BC][1]
        if A[ind_A] > Ci:
            merged.append(A[ind_A])
            ind_A += 1
        else:
            merged.extend([Ci] * Bi)
            ind_BC += 1
        if len(merged) >= N:
            break
    if len(merged) < N:
        if ind_A < N:
            merged.extend(A[ind_A:])
        elif ind_BC < M:
            for i in range(ind_BC, M):
                Bi = BC[ind_BC][0]
                Ci = BC[ind_BC][1]
                merged.extend([Bi] * Ci)
                if len(merged) >= N:
                    break
    merged = merged[:N]
    print sum(merged)

if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    N, M = [int(_) for _ in raw_input().split()]
    A = [int(_) for _ in raw_input().split()]
    A.sort()
    BC = []
    for i in range(M):
        Bi, Ci = [int(_) for _ in raw_input().split()]
        BC.append([Bi, Ci])
    BC.sort(key = lambda x: x[1], reverse = True)
    for i in range(M):
        Bi = BC[i][0]
        Ci = BC[i][1]
        low_ind = 0
        up_ind = N - 1
        found = None
        if A[low_ind] > Ci:
            continue
        elif A[up_ind] < Ci:
            A = A[Bi:]
            A.extend([Ci] * Bi)
            continue
        while (up_ind - low_ind) > 1:
            mid = (up_ind + low_ind) / 2
            if A[mid] < Ci:
                low_ind = mid
            elif A[mid] > Ci:
                up_ind = mid
            elif A[mid] == Ci:
                found = mid
                break
        if found is not None:
            cnt = found
        else:
            cnt = up_ind
        repl = min(cnt,Bi)
        for i in range(repl):
            A.insert(i + cnt, Ci)
        A = A[repl:]
    print sum(A)

if __name__ == '__main__':
    solve()


Fixed submission:
def solve():
    N, M = [int(_) for _ in raw_input().split()]
    A = [int(_) for _ in raw_input().split()]
    A.sort(reverse = True)
    BC = []
    for i in range(M):
        Bi, Ci = [int(_) for _ in raw_input().split()]
        BC.append([Bi, Ci])
    BC.sort(key = lambda x: x[1], reverse = True)
    ind_BC = 0
    ind_A = 0
    merged = []
    while ind_A < N and ind_BC < M:
        Bi = BC[ind_BC][0]
        Ci = BC[ind_BC][1]
        if A[ind_A] > Ci:
            merged.append(A[ind_A])
            ind_A += 1
        else:
            merged.extend([Ci] * Bi)
            ind_BC += 1
        if len(merged) >= N:
            break
    if len(merged) < N:
        if ind_A < N:
            merged.extend(A[ind_A:])
        elif ind_BC < M:
            for i in range(ind_BC, M):
                Bi = BC[ind_BC][0]
                Ci = BC[ind_BC][1]
                merged.extend([Bi] * Ci)
                if len(merged) >= N:
                    break
    merged = merged[:N]
    print sum(merged)

if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    N, M = [int(_) for _ in raw_input().split()]
    A = [int(_) for _ in raw_input().split()]
    A.sort()
    BC = []
    for i in range(M):
        Bi, Ci = [int(_) for _ in raw_input().split()]
        BC.append([Bi, Ci])
    BC.sort(key = lambda x: x[1], reverse = True)
    for i in range(M):
        Bi = BC[i][0]
        Ci = BC[i][1]
        cnt = 0
        while cnt < len(A) and A[cnt] < Ci:
            cnt += 1
        repl = min(cnt,Bi)
        for i in range(repl):
            A.insert(i + cnt, Ci)
        A = A[repl:]
    print sum(A)

if __name__ == '__main__':
    solve()


Fixed submission:
def solve():
    N, M = [int(_) for _ in raw_input().split()]
    A = [int(_) for _ in raw_input().split()]
    A.sort(reverse = True)
    BC = []
    for i in range(M):
        Bi, Ci = [int(_) for _ in raw_input().split()]
        BC.append([Bi, Ci])
    BC.sort(key = lambda x: x[1], reverse = True)
    ind_BC = 0
    ind_A = 0
    merged = []
    while ind_A < N and ind_BC < M:
        Bi = BC[ind_BC][0]
        Ci = BC[ind_BC][1]
        if A[ind_A] > Ci:
            merged.append(A[ind_A])
            ind_A += 1
        else:
            merged.extend([Ci] * Bi)
            ind_BC += 1
        if len(merged) >= N:
            break
    if len(merged) < N:
        if ind_A < N:
            merged.extend(A[ind_A:])
        elif ind_BC < M:
            for i in range(ind_BC, M):
                Bi = BC[ind_BC][0]
                Ci = BC[ind_BC][1]
                merged.extend([Bi] * Ci)
                if len(merged) >= N:
                    break
    merged = merged[:N]
    print sum(merged)

if __name__ == '__main__':
    solve()