Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3.4

# abc127_d

import collections
import itertools
import functools

def find_index(iterable, func, from_i):
    i = from_i
    l = len(iterable)
    while i<l and not func(iterable[i]):
        i += 1
    return i

def insert(deq, i, x):
    l = len(deq)
    if i < l/2:
        deq.rotate(-i)
        deq.appendleft(x)
        deq.rotate(i)
    else:
        k = l-i
        deq.rotate(k)
        deq.append(x)
        deq.rotate(-k)

def main():
    # Input
    _, m = [int(s) for s in input().split()]
    cards = [int(s) for s in input().split()]
    acts = [[int(s) for s in input().split()] for _ in range(m)]
    # Get ans
    cards = collections.deque(sorted(cards))
    acts.sort(key=lambda a: a[1])
    i = 0
    for b, c in acts:
        i = find_index(cards, lambda card: card>=c, i)
        #print(i, cards[:i], cards[i:])
        change_num = min(b, i)
        #cards = cards[:i][change_num:] + [c]*change_num + cards[i:]
        for _ in range(change_num):
            insert(cards, i, c)
            cards.popleft()
        #print(cards)
    ans = sum(cards)
    # Output
    print(ans)

if __name__ == '__main__':
    main()



Fixed submission:
#!/usr/bin/env python3.4

# abc127_d

import collections
import itertools
import functools

def main():
    # Input
    _, m = [int(s) for s in input().split()]
    cards = [int(s) for s in input().split()]
    acts = [[int(s) for s in input().split()] for _ in range(m)]
    # Get ans
    cards = collections.deque(sorted(cards))
    acts.sort(key=lambda a: a[1], reverse=True)
    for b, c in acts:
        for _ in range(b):
            if cards[0] >= c:
                break
            cards.popleft()
            cards.append(c)
    ans = sum(cards)
    # Output
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3.4

# abc127_d

import collections
import itertools
import functools

def find_index(iterable, func, from_i):
    i = from_i
    l = len(iterable)
    while i<l and not func(iterable[i]):
        i += 1
    return i

def insert(deq, i, x):
    deq.rotate(-i)
    deq.appendleft(x)
    deq.rotate(i)

def main():
    # Input
    _, m = [int(s) for s in input().split()]
    cards = [int(s) for s in input().split()]
    acts = [[int(s) for s in input().split()] for _ in range(m)]
    # Get ans
    cards = collections.deque(sorted(cards))
    acts.sort(key=lambda a: a[1])
    i = 0
    for b, c in acts:
        i = find_index(cards, lambda card: card>=c, i)
        #print(i, cards[:i], cards[i:])
        change_num = min(b, i)
        #cards = cards[:i][change_num:] + [c]*change_num + cards[i:]
        for _ in range(change_num):
            insert(cards, i, c)
            cards.popleft()
        #print(cards)
    ans = sum(cards)
    # Output
    print(ans)

if __name__ == '__main__':
    main()



Fixed submission:
#!/usr/bin/env python3.4

# abc127_d

import collections
import itertools
import functools

def main():
    # Input
    _, m = [int(s) for s in input().split()]
    cards = [int(s) for s in input().split()]
    acts = [[int(s) for s in input().split()] for _ in range(m)]
    # Get ans
    cards = collections.deque(sorted(cards))
    acts.sort(key=lambda a: a[1], reverse=True)
    for b, c in acts:
        for _ in range(b):
            if cards[0] >= c:
                break
            cards.popleft()
            cards.append(c)
    ans = sum(cards)
    # Output
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3.4

# abc127_d

import collections
import itertools
import functools

def find_index(iterable, func, from_i):
    i = from_i
    l = len(iterable)
    while i<l and not func(iterable[i]):
        i += 1
    return i

def main():
    # Input
    _, m = [int(s) for s in input().split()]
    cards = [int(s) for s in input().split()]
    acts = [[int(s) for s in input().split()] for _ in range(m)]
    # Get ans
    cards = collections.deque(sorted(cards))
    acts.sort(key=lambda a: a[1])
    i = 0
    for b, c in acts:
        i = find_index(cards, lambda card: card>=c, i)
        #print(i, cards[:i], cards[i:])
        change_num = min(b, i)
        #cards = cards[:i][change_num:] + [c]*change_num + cards[i:]
        for _ in range(change_num):
            cards.insert(i, c)
            cards.popleft()
        #print(cards)
    ans = sum(cards)
    # Output
    print(ans)

if __name__ == '__main__':
    main()



Fixed submission:
#!/usr/bin/env python3.4

# abc127_d

import collections
import itertools
import functools

def main():
    # Input
    _, m = [int(s) for s in input().split()]
    cards = [int(s) for s in input().split()]
    acts = [[int(s) for s in input().split()] for _ in range(m)]
    # Get ans
    cards = collections.deque(sorted(cards))
    acts.sort(key=lambda a: a[1], reverse=True)
    for b, c in acts:
        for _ in range(b):
            if cards[0] >= c:
                break
            cards.popleft()
            cards.append(c)
    ans = sum(cards)
    # Output
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3.4

# abc127_d

import itertools
import functools

def find_index(iterable, func, from_i):
    i = from_i
    l = len(iterable)
    while i<l and not func(iterable[i]):
        i += 1
    return i

def main():
    # Input
    n, m = [int(s) for s in input().split()]
    cards = [int(s) for s in input().split()]
    acts = [[int(s) for s in input().split()] for _ in range(m)]
    # Get ans
    cards.sort()
    acts.sort(key=lambda a: a[1])
    i = 0
    for b, c in acts:
        i = find_index(cards, lambda card: card>=c, i)
        #print(i, cards[:i], cards[i:])
        change_num = min(b, i)
        cards = cards[:i][change_num:] + [c]*change_num + cards[i:]
        #print(cards)
    ans = sum(cards)
    # Output
    print(ans)

if __name__ == '__main__':
    main()



Fixed submission:
#!/usr/bin/env python3.4

# abc127_d

import collections
import itertools
import functools

def main():
    # Input
    _, m = [int(s) for s in input().split()]
    cards = [int(s) for s in input().split()]
    acts = [[int(s) for s in input().split()] for _ in range(m)]
    # Get ans
    cards = collections.deque(sorted(cards))
    acts.sort(key=lambda a: a[1], reverse=True)
    for b, c in acts:
        for _ in range(b):
            if cards[0] >= c:
                break
            cards.popleft()
            cards.append(c)
    ans = sum(cards)
    # Output
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3.4

# abc127_d

import itertools
import functools

def find_index(iterable, func):
    for i, it in enumerate(iterable):
        if func(it):
            return i
    return i+1

def main():
    # Input
    n, m = [int(s) for s in input().split()]
    cards = [int(s) for s in input().split()]
    acts = [[int(s) for s in input().split()] for _ in range(m)]
    # Get ans
    cards.sort()
    acts.sort(key=lambda a: a[1])
    for b, c in acts:
        i = find_index(cards, lambda card: card>=c)
        #print(i, cards[:i], cards[i:])
        change_num = min(b, i)
        cards = cards[:i][change_num:] + [c]*change_num + cards[i:]
        #print(cards)
    ans = sum(cards)
    # Output
    print(ans)

if __name__ == '__main__':
    main()



Fixed submission:
#!/usr/bin/env python3.4

# abc127_d

import collections
import itertools
import functools

def main():
    # Input
    _, m = [int(s) for s in input().split()]
    cards = [int(s) for s in input().split()]
    acts = [[int(s) for s in input().split()] for _ in range(m)]
    # Get ans
    cards = collections.deque(sorted(cards))
    acts.sort(key=lambda a: a[1], reverse=True)
    for b, c in acts:
        for _ in range(b):
            if cards[0] >= c:
                break
            cards.popleft()
            cards.append(c)
    ans = sum(cards)
    # Output
    print(ans)

if __name__ == '__main__':
    main()