Problem p02616 (python)

Fixed submission:
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
import random
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


N, K = read_values()
def main():
    A = read_list()
    # A = [random.randint(-10 ** 9, 10 ** 9) for _ in range(N)]

    A.sort(reverse=True)
    if N == K or (A[0] < 0 and K % 2 == 1):
        res = 1
        for a in A[:K]:
            res *= a
            res %= mod
        print(res)
        return

    A.sort(key=lambda a: abs(a), reverse=True)

    P1 = list(filter(lambda a: a >= 0, A[:K]))
    P2 = list(filter(lambda a: a >= 0, A[K:]))
    N1 = list(filter(lambda a: a < 0, A[:K]))
    N2 = list(filter(lambda a: a < 0, A[K:]))

    if len(N1) % 2 == 1:
        # if N1[-1] * N2[0] : P1[-1] * P2[0]
        if len(P1) == 0 or len(N2) == 0:
            N1[-1] = P2[0]
        elif len(P2) == 0:
            P1[-1] = N2[0]
        else:
            if P1[-1] * P2[0] >= N1[-1] * N2[0]:
                N1[-1] = P2[0]
            else:
                P1[-1] = N2[0]
    
    res = 1
    for p in P1:
        res *= p
        res %= mod

    for n in N1:
        res *= n
        res %= mod
    
    print(res % mod)
        

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
import random
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


N, K = read_values()
def main():
    A = read_list()
    # A = [random.randint(-10 ** 9, 10 ** 9) for _ in range(N)]

    A.sort(reverse=True)
    if N == K or (A[0] < 0 and len(A) % 2 == 1):
        res = 1
        for a in A[:K]:
            res *= a
            res %= mod
        print(res)
        return

    A.sort(key=lambda a: abs(a), reverse=True)

    P1 = list(filter(lambda a: a >= 0, A[:K]))
    P2 = list(filter(lambda a: a >= 0, A[K:]))
    N1 = list(filter(lambda a: a < 0, A[:K]))
    N2 = list(filter(lambda a: a < 0, A[K:]))

    if len(N1) % 2 == 1:
        # if N1[-1] * N2[0] : P1[-1] * P2[0]
        if len(P1) == 0 or len(N2) == 0:
            N1[-1] = P2[0]
        elif len(P2) == 0:
            P1[-1] = N2[0]
        else:
            if P1[-1] * P2[0] >= N1[-1] * N2[0]:
                N1[-1] = P2[0]
            else:
                P1[-1] = N2[0]
    
    res = 1
    for p in P1:
        res *= p
        res %= mod

    for n in N1:
        res *= n
        res %= mod
    
    print(res % mod)
        

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N, K = read_values()
    A = read_list()
    A.sort(reverse=True)
    if A[0] < 0:
        res = 1
        for a in A[:K]:
            res *= a
            res %= mod
        print(res)
        return

    A.sort(key=lambda a: abs(a), reverse=True)

    P1 = list(filter(lambda a: a >= 0, A[:K]))
    P2 = list(filter(lambda a: a >= 0, A[K:]))
    N1 = list(filter(lambda a: a < 0, A[:K]))
    N2 = list(filter(lambda a: a < 0, A[K:]))

    if len(N1) % 2 == 1:
        # if N1[-1] * N2[0] : P1[-1] * P2[0]
        if len(P1) == 0 or len(N2) == 0:
            N1[-1] = P2[0]
        elif len(P2) == 0:
            P1[-1] = N2[0]
        else:
            if P1[-1] * P2[0] >= N1[-1] * N2[0]:
                N1[-1] = P2[0]
            else:
                P1[-1] = N2[0]
    
    res = 1
    for p in P1:
        res *= p
        res %= mod

    for n in N1:
        res *= n
        res %= mod
    
    print(res % mod)
        

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N, K = read_values()
    A = read_list()
    A.sort(reverse=True)
    if A[0] < 0:
        res = 1
        for a in A[:K]:
            res *= a
            res %= mod
        print(res)
        return

    A.sort(key=lambda a: abs(a), reverse=True)
    res = 1
    c = 0
    p = -1
    n = 1
    for a in A[:K]:
        if a < 0:
            c += 1
            n = a
        else:
            p = a
        res *= a
        res %= mod
    
    if c % 2 == 1:
        p1, n1 = None, None
        for a in A[K:]:
            if a >= 0 and p1 is None:
                p1 = a
            if a < 0 and p2 is None:
                n1 = a
        if p1 is None:
            res *= n1
            res *= pow(p, mod - 2, mod)
            res %= mod
        elif n1 is None or p < 0:
            res *= p1
            res *= pow(n, mod - 2, mod)
            res %= mod
        else:
            res *= pow(p, mod - 2, mod)
            res *= pow(n, mod - 2, mod)
            res %= mod
            t = min(p * n1, n * p1)
            res *= t
            res %= mod
    print(res)
            

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import bisect
import copy
import heapq
import sys
import itertools
import math
from functools import lru_cache
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7 

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N, K = read_values()
    A = read_list()
    A.sort()
    res = 1    
    if N == K:
        for a in A:
            res *= a
            res %= mod
    elif A[-1] < 0 or A[0] >= 0:
        for a in A[:-(K + 1):-1]:
            res *= a
            res %= mod
    else:
        k = bisect.bisect_left(A, 0)
        neg = A[:k]
        pos = A[k:][::-1]
        
        n = 0
        p = 0
        while n + p < K:
            if -neg[n] <= pos[p]:
                p += 1
            else:
                n += 1
        if n % 2 == 1:
            if p == 0 or n == len(neg):
                n -= 1
                p += 1
            elif p == len(pos):
                n += 1
                p -= 1
            elif pos[p] * pos[p - 1]  <= neg[n] * neg[n - 1]:
                n += 1
                p -= 1
            else:
                n -= 1
                p += 1

        for a in pos[:p]:
            res *= a
            res %= mod
        
        for a in neg[:n]:
            res *= a
            res %= mod

    print(res % mod)

    
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import bisect
import copy
import heapq
import sys
import itertools
import math
from functools import lru_cache
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7 

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N, K = read_values()
    A = read_list()
    A.sort()
    res = 1    
    if N == K:
        for a in A:
            res *= a
            res %= mod
    elif A[-1] < 0:
        for a in A[:-(K + 1):-1]:
            res *= a
            res %= mod
    else:
        k = bisect.bisect_left(A, 0)
        neg = A[:k]
        pos = A[k:][::-1]
        
        n = 0
        p = 0
        while n + p < K:
            if -neg[n] <= pos[p]:
                p += 1
            else:
                n += 1
        if n % 2 == 1:
            if p == 0 or n == len(neg):
                n -= 1
                p += 1
            elif p == len(pos):
                n += 1
                p -= 1
            elif pos[p] * pos[p - 1]  <= neg[n] * neg[n - 1]:
                n += 1
                p -= 1
            else:
                n -= 1
                p += 1

        for a in pos[:p]:
            res *= a
            res %= mod
        
        for a in neg[:n]:
            res *= a
            res %= mod

    print(res % mod)

    
if __name__ == "__main__":
    main()