Problem p02948 (python)

Fixed submission:
#!/usr/bin/env python3
import sys
from collections import deque
import heapq

def solve(N: int, M: int, A: "List[int]"):
    a = sorted(A, reverse=False)
    a = deque(a)
    # ai(day) bi(reward)
    b_cands = []
    heapq.heapify(b_cands)

    ans = 0
    for M in range(M+1):
        while (len(a)!=0) and (a[0][0]==M):
            heapq.heappush(b_cands, -a.popleft()[1])
            # print(b_cands)
        if len(b_cands)>0:
            ans -= heapq.heappop(b_cands)

    print(ans)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = []

    for i in range(N):
        A.append((int(next(tokens)),int(next(tokens))))
    solve(N, M, A)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys


def solve(N: int, M: int, A: "List[int]"):
    a = sorted(A, reverse=True)
    bsum = 0
    p = []
    for day in range(M, -1, -1):
        rem = []
        for i, a_ in enumerate(a):
            if a_[0]==day:
                p.append(a_[1])
                rem.append(i)
            else:
                [a.pop(k) for k in rem]
                continue
        # print("day : {}, a:{}, p:{}".format(day, a, p))
        if len(p)>0:
            # p = sorted(p, key = lambda x : x[1], reverse=True)
            p_max = max(p)
            bsum += p_max
            p.remove(p_max)

    print(bsum)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = []

    for i in range(N):
        A.append((M-int(next(tokens)),int(next(tokens))))
    solve(N, M, A)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys


def solve(N: int, M: int, A: "List[int]"):
    a = sorted(A, reverse=True)
    bsum = 0
    p = []
    for day in range(M, -1, -1):

        rem = []

        for i, a_ in enumerate(a):
            if a_[0]==day:
                p.append(a_)
                rem.append(i)
            else:
                [a.pop(k) for k in rem] # 遅いかも
        
        # print("day : {}, a:{}, p:{}".format(day, a, p))
        if len(p)>0:
            # p = sorted(p, key = lambda x : x[1], reverse=True)
            p_max = max(p, key=lambda x: x[1])
            bsum += p_max[1]
            p.remove(p_max)
        

    print(bsum)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = []

    for i in range(N):
        A.append((M-int(next(tokens)),int(next(tokens))))
    solve(N, M, A)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys


def solve(N: int, M: int, A: "List[int]"):
    a = sorted(A, reverse=True)
    bsum = 0
    p = []
    for day in range(M, -1, -1):

        rem = []

        for i, a_ in enumerate(a):
            if a_[0]==day:
                p.append(a_)
                rem.append(i)
            else:
                [a.pop(k) for k in rem] # 遅いかも
        
        # print("day : {}, a:{}, p:{}".format(day, a, p))
        if len(p)>0:
            p = sorted(p, key = lambda x : x[1], reverse=True)
            bsum += p[0][1]
            p.pop(0)
        

    print(bsum)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = []

    for i in range(N):
        A.append((M-int(next(tokens)),int(next(tokens))))
    solve(N, M, A)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys


def solve(N: int, M: int, A: "List[int]"):
    a = sorted(A, reverse=True)
    bsum = 0
    p = []
    for day in range(M, -1, -1):

        rem = []

        for i, a_ in enumerate(a):
            if a_[0]==day:
                p.append(a_)
                rem.append(i)
            else:
                [a.pop(k) for k in rem] # 遅いかも
        
        # print("day : {}, a:{}, p:{}".format(day, a, p))
        if len(p)>0:
            p = sorted(p, key = lambda x : x[1], reverse=True)
            bsum += p[0][1]
            p.pop(0)
        

    print(bsum)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    A = []

    for i in range(N):
        A.append((M-int(next(tokens)),int(next(tokens))))
    solve(N, M, A)

if __name__ == '__main__':
    main()