Problem p03038 (python)

Fixed submission:
import sys
# gcd
from fractions import gcd
# 切り上げ，切り捨て
# from math import ceil, floor
# リストの真のコピー（変更が伝播しない）
# from copy import deepcopy
# 累積和。list(accumulate(A))としてAの累積和
from itertools import accumulate
from bisect import bisect_right
# l = ['a', 'b', 'b', 'c', 'b', 'a', 'c', 'c', 'b', 'c', 'b', 'a']
# S = Counter(l)  # カウンタークラスが作られる。S=Counter({'b': 5, 'c': 4, 'a': 3})
# print(S.most_common(2))  # [('b', 5), ('c', 4)]
# print(S.keys())  # dict_keys(['a', 'b', 'c'])
# print(S.values())  # dict_values([3, 5, 4])
# print(S.items())  # dict_items([('a', 3), ('b', 5), ('c', 4)])
# from collections import Counter
# import math
# from functools import reduce
#
# input関係の定義
# fin = open('in_1.txt', 'r')
# sys.stdin = fin
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template


def iterate_tokens():
    for line in sys.stdin:
        for word in line.split():
            yield word


if __name__ == '__main__':

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    # B = [int()] * (M)  # type: "List[int]"
    # C = [int()] * (M)  # type: "List[int]"
    L = [[0, 0]for i in range(M)]
    for i in range(M):
        L[i][0] = int(next(tokens))
        L[i][1] = int(next(tokens))

    # write code
    A.sort()
    L.sort(key=lambda x: x[1], reverse=True)
    # print(L)
    index = [0]*M
    for i in range(M):
        index[i] = bisect_right(A, L[i][1] - 1)
    # print(index)
    changed = 0
    for i in range(M):
        if index[i] > changed:
            A[changed:min(index[i], changed + L[i][0])] = [L[i]
                                                           [1]]*(min(index[i], changed + L[i][0])-changed)
            changed = min(index[i], changed + L[i][0])
        else:
            continue
    # print(A)
    print(sum(A))

Buggy submission (verdict: Runtime Error):
import sys
# gcd
from fractions import gcd
# 切り上げ，切り捨て
# from math import ceil, floor
# リストの真のコピー（変更が伝播しない）
# from copy import deepcopy
# 累積和。list(accumulate(A))としてAの累積和
from itertools import accumulate
from bisect import bisect_left
# l = ['a', 'b', 'b', 'c', 'b', 'a', 'c', 'c', 'b', 'c', 'b', 'a']
# S = Counter(l)  # カウンタークラスが作られる。S=Counter({'b': 5, 'c': 4, 'a': 3})
# print(S.most_common(2))  # [('b', 5), ('c', 4)]
# print(S.keys())  # dict_keys(['a', 'b', 'c'])
# print(S.values())  # dict_values([3, 5, 4])
# print(S.items())  # dict_items([('a', 3), ('b', 5), ('c', 4)])
# from collections import Counter
# import math
# from functools import reduce
#
# input関係の定義
fin = open('in_4.txt', 'r')
sys.stdin = fin
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template


def iterate_tokens():
    for line in sys.stdin:
        for word in line.split():
            yield word


if __name__ == '__main__':

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    # B = [int()] * (M)  # type: "List[int]"
    # C = [int()] * (M)  # type: "List[int]"
    L = [[0, 0]for i in range(M)]
    for i in range(M):
        L[i][0] = int(next(tokens))
        L[i][1] = int(next(tokens))

    # write code
    A.sort()
    L.sort(key=lambda x: x[1], reverse=True)
    # print(L)
    index = [0]*M
    for i in range(M):
        index[i] = bisect_left(A, L[i][1] - 1)
    # print(index)
    changed = 0
    for i in range(M):
        if index[i] > changed:
            A[changed:min(index[i], changed + L[i][0])] = [L[i]
                                                           [1]]*(min(index[i], changed + L[i][0])-changed)
            changed = min(index[i], changed + L[i][0])
        else:
            continue
    # print(A)
    print(sum(A))