Problem p02616 (python)

Fixed submission:
def main():
    n,k = map(int,input().split())
    A = list(map(int,input().split()))
    MOD = 10**9+7


    B = []
    flg_m = 0
    flg_p = 0

    for a in A:
        if a < 0:
            B.append([abs(a),-1])
            flg_m = 1
        else:
            B.append([abs(a),1])
            flg_p = 1

        b = 0
    ans = -10**9
    if k == 1:
        for a in A:
            ans = max(ans,a)
        print(ans)
        exit()

    B.sort(reverse=True)
    # print(B)
    if flg_p == 0 and k%2 == 1:
        B.sort()
        L = B[:k]
        ans = cal_ans(L)
        ans *=-1
        print(ans%MOD)
        exit()
    elif flg_p * flg_m == 0:
        L = B[:k]
        ans = cal_ans(L)
        print(ans)
        exit()

    flg_p = 0
    flg_m = 0
    flg_lm = 0
    flg_lp = 0
    # print(B)
    for i,(n,sign) in enumerate(B):
        if i < k:
            if sign == -1:
                flg_lp = 1
                last_m = n
            else:
                flg_lm = 1
                last_p = n
        else:
            if sign == -1:
                if flg_m == 0:
                    first_m = n
                    flg_m = 1
            else:
                if flg_p == 0:
                    first_p = n
                    flg_p = 1

    L = B[:k]
    ans = cal_ans(L)
    c = 0
    for n,sign in L:
        if sign == -1:
            c += 1
    if c % 2 == 0:
        print(ans)
        exit()
    if flg_p == 1 and flg_m == 1 and flg_lp == 1 and flg_lm == 1:
        if first_p * last_p >= first_m*last_m:
            a = first_p
            b = last_m
        else:
            a = first_m
            b = last_p
    elif flg_p == 1:
            a = first_p
            b = last_m
    elif flg_m == 1:
            a = first_m
            b = last_p
    else:
        ans = 1
        for a in A:
            ans *= a
            ans %= MOD
        print(ans)
        exit()


    ans *= a
    ans %= MOD
    ans *= pow(b,MOD-2,MOD)
    ans %= MOD
    print(ans)

def cal_ans(L):
    MOD = 10**9+7
    ans = 1
    for i,j in L:
        ans *= i
        ans %= MOD
    return ans



main()

Buggy submission (verdict: Runtime Error):
def main():
    n,k = map(int,input().split())
    A = list(map(int,input().split()))
    MOD = 10**9+7


    B = []
    flg_m = 0
    flg_p = 0

    for a in A:
        if a < 0:
            B.append([abs(a),-1])
            flg_m = 1
        else:
            B.append([abs(a),1])
            flg_p = 1

        b = 0
    ans = -10**9
    if k == 1:
        for a in A:
            ans = max(ans,a)
        print(ans)
        exit()

    B.sort(reverse=True)
    if flg_p == 0 and k%2 == 1:
        B.sort()
        L = B[:k]
        ans = cal_ans(L)
        ans *=-1
        print(ans%MOD)
        exit()
    elif flg_p * flg_m == 0:
        L = B[:k]
        ans = cal_ans(L)
        print(ans)
        exit()

    flg_p = 0
    flg_m = 0
    # print(B)
    for i,(n,sign) in enumerate(B):
        if i < k:
            if sign == -1:
                last_m = n
            else:
                last_p = n
        else:
            if sign == -1:
                if flg_m == 0:
                    first_m = n
                    flg_m = 1
            else:
                if flg_p == 0:
                    first_p = n
                    flg_p = 1

    L = B[:k]
    ans = cal_ans(L)
    c = 0
    for n,sign in L:
        if sign == -1:
            c += 1
    if c % 2 == 0:
        print(ans)
        exit()
    if flg_p == 1 and flg_m == 1:
        if first_p * last_p >= first_m*last_m:
            a = first_p
            b = last_m
        else:
            a = first_m
            b = last_p
    elif flg_p == 1:
            a = first_p
            b = last_m
    elif flg_m == 1:
            a = first_m
            b = last_p
    else:
        ans = 1
        for a in A:
            ans *= a
            ans %= MOD
        print(ans)
        exit()


    ans *= a
    ans %= MOD
    ans *= pow(b,MOD-2,MOD)
    ans %= MOD
    print(ans)

def cal_ans(L):
    MOD = 10**9+7
    ans = 1
    for i,j in L:
        ans *= i
        ans %= MOD
    return ans



main()

Buggy submission (verdict: Runtime Error):
def main():
    n,k = map(int,input().split())
    A = list(map(int,input().split()))
    MOD = 10**9+7

    B = []
    flg_m = 0
    flg_p = 0
    for a in A:
        if a < 0:
            B.append([abs(a),-1])
            flg_m = 1
        else:
            B.append([abs(a),1])
            flg_p = 1

    B.sort(reverse=True)
    if flg_p == 0 and k%2 == 1:
        B.sort()
        L = B[:k]
        ans = cal_ans(L)
        ans *=-1
        print(ans%MOD)
        exit()
    elif flg_p * flg_m == 0:
        L = B[:k]
        ans = cal_ans(L)
        print(ans)
        exit()

    flg_p = 0
    flg_m = 0
    # print(B)
    for i,(n,sign) in enumerate(B):
        if i < k:
            if sign == -1:
                last_m = n
            else:
                last_p = n
        else:
            if sign == -1:
                if flg_m == 0:
                    first_m = n
                    flg_m = 1
            else:
                if flg_p == 0:
                    first_p = n
                    flg_p = 1

    L = B[:k]
    ans = cal_ans(L)
    c = 0
    for n,sign in L:
        if sign == -1:
            c += 1
    if c % 2 == 0:
        print(ans)
        exit()
    if flg_p == 1 and flg_m == 1:
        if first_p * last_p >= first_m*last_m:
            a = first_p
            b = last_m
        else:
            a = first_m
            b = last_p
    elif flg_p == 1:
            a = first_p
            b = last_m
    elif flg_m == 1:
            a = first_m
            b = last_p
    else:
        ans = 1
        for a in A:
            ans *= a
            ans %= MOD
        print(ans)
        exit()


    ans *= a
    ans %= MOD
    ans *= pow(b,MOD-2,MOD)
    ans %= MOD
    print(ans)

def cal_ans(L):
    MOD = 10**9+7
    ans = 1
    for i,j in L:
        ans *= i
        ans %= MOD
    return ans



main()

Buggy submission (verdict: Runtime Error):
def main():
    n,k = map(int,input().split())
    A = list(map(int,input().split()))
    MOD = 10**9+7

    B = []
    flg_m = 0
    flg_p = 0
    for a in A:
        if a < 0:
            B.append([abs(a),-1])
            flg_m = 1
        else:
            B.append([abs(a),1])
            flg_p = 1

    B.sort(reverse=True)
    if flg_p == 0 and k%2 == 1:
        B.sort()
        L = B[:k]
        ans = cal_ans(L)
        ans *=-1
        print(ans%MOD)
        exit()
    elif flg_p * flg_m == 0:
        L = B[:k]
        ans = cal_ans(L)
        print(ans)
        exit()

    flg_p = 0
    flg_m = 0
    # print(B)
    for i,(n,sign) in enumerate(B):
        if i < k:
            if sign == -1:
                last_m = n
            else:
                last_p = n
        else:
            if sign == -1:
                if flg_m == 0:
                    first_m = n
                    flg_m = 1
            else:
                if flg_p == 0:
                    first_p = n
                    flg_p = 1

    L = B[:k]
    ans = cal_ans(L)
    c = 0
    for n,sign in L:
        if sign == -1:
            c += 1
    if c % 2 == 0:
        print(ans)
        exit()
    if flg_p == 1 and flg_m == 1:
        if first_p * last_p >= first_m*last_m:
            a = first_p
            b = last_m
        else:
            a = first_m
            b = last_p
    elif flg_p == 1:
            a = first_p
            b = last_m
    elif flg_m == 1:
            a = first_m
            b = last_p

    ans *= a
    ans %= MOD
    ans *= pow(b,MOD-2,MOD)
    ans %= MOD
    print(ans)

def cal_ans(L):
    MOD = 10**9+7
    ans = 1
    for i,j in L:
        ans *= i
        ans %= MOD
    return ans



main()

Buggy submission (verdict: Runtime Error):
def main():
    n,k = map(int,input().split())
    A = list(map(int,input().split()))
    MOD = 10**9+7

    B = []
    flg_m = 0
    flg_p = 0
    for a in A:
        if a < 0:
            B.append([abs(a),-1])
            flg_m = 1
        else:
            B.append([abs(a),1])
            flg_p = 1

    B.sort(reverse=True)
    if flg_p == 0 and k%2 == 1:
        B.sort()
        L = B[:k]
        ans = cal_ans(L)
        ans *=-1
        print(ans%MOD)
        exit()
    elif flg_p * flg_m == 0:
        L = B[:k]
        ans = cal_ans(L)
        print(ans)
        exit()

    flg_p = 0
    flg_m = 0
    # print(B)
    for i,(n,sign) in enumerate(B):
        if i < k:
            if sign == -1:
                last_m = n
            else:
                last_p = n

        else:
            if sign == -1:
                if flg_m == 0:
                    first_m = n
                    flg_m = 1
            else:
                if flg_p == 0:
                    first_p = n
                    flg_p = 1

    L = B[:k]
    ans = cal_ans(L)
    c = 0
    for n,sign in L:
        if sign == -1:
            c += 1
    if c % 2 == 0:
        print(ans)
        exit()

    if first_p * last_p >= first_m*last_m:
        a = first_p
        b = last_m
    else:
        a = first_m
        b = last_p

    ans *= a
    ans %= MOD
    ans *= pow(b,MOD-2,MOD)
    ans %= MOD
    print(ans)

                


def cal_ans(L):
    MOD = 10**9+7
    ans = 1
    for i,j in L:
        ans *= i
        ans %= MOD
    return ans



main()

Buggy submission (verdict: Runtime Error):
def main():
    n,k = map(int,input().split())
    A = list(map(int,input().split()))
    MOD = 10**9+7

    B = []
    flg_m = 0
    flg_p = 0
    for a in A:
        if a < 0:
            B.append([abs(a),-1])
            flg_m = 1
        else:
            B.append([abs(a),1])
            flg_p = 1

    B.sort(reverse=True)
    if flg_p == 0 and k%2 == 1:
        B.sort()
        L = B[:k]
        ans = cal_ans(L)
        ans *=-1
        print(ans%MOD)
        exit()
    elif flg_p * flg_m == 0:
        L = B[:k]
        ans = cal_ans(L)
        print(ans)
        exit()

    flg_p = 0
    flg_m = 0
    for i,(n,sign) in enumerate(B):
        if i < k:
            if sign == -1:
                last_m = n
            else:
                last_p = n

        else:
            if sign == -1:
                if flg_m == 0:
                    first_m = n
                    flg_p = 1
            else:
                if flg_p == 0:
                    first_p = n
                    flg_p = 1

    L = B[:k]
    ans = cal_ans(L)
    c = 0
    for n,sign in L:
        if sign == -1:
            c += 1
    if c % 2 == 0:
        print(ans)
        exit()

    if first_p * last_p >= first_m*last_m:
        a = first_p
        b = last_m
    else:
        a = first_m
        b = last_p

    ans *= a
    ans %= MOD
    ans *= pow(b,MOD-2,MOD)
    ans %= MOD
    print(ans)

                


def cal_ans(L):
    MOD = 10**9+7
    ans = 1
    for i,j in L:
        ans *= i
        ans %= MOD
    return ans



main()

Buggy submission (verdict: Runtime Error):
def main():
    n,k = map(int,input().split())
    A = list(map(int,input().split()))

    MOD = 10**9 + 7

    absA = [abs(a) for a in A]
    flg = 0
    m = []
    p = []
    dict = []
    T = []
    for i in A:
        if i < 0:
            m.append(i)
            T.append(tuple([abs(i),-1]))
        elif i > 0:
            p.append(i)
            T.append(tuple([abs(i),1]))
        else:
            flg = 1

    T.sort(reverse=True)


    ans = T[:k]
    c = 0
    for i in ans:
        if i[1] == -1:
            c += 1

    if c % 2 == 0:
        l = 1
        for i in ans:
            l *= i[0]
            l %= MOD
        print(l)

    else:
        flg_1,flg_2 = 0,0
        for j in range(k-1,-1,-1):
            if T[j][1] == -1:
                flg_1 = 1
                min_m = T[j][0]
                break

        for j in range(k,len(T)):
            if T[j][1] == 1:
                flg_2 ^= 1
                # print('AaA')
                max_p = T[j][0]
                break

        # print(min_m,max_p)

        if flg_1 and flg_2:
            flg = 1
        else:
            flg = 0

        flg_1,flg_2 = 0,0

        for j in range(k-1,-1,-1):
            if T[j][1] == 1:
                flg_1 = 1
                min_p = T[j][0]
                break
        
        for j in range(k,len(T)):
            if T[j][1] == -1:
                flg_2 = 1
                max_m = T[j][0]
                break

        if flg_1 and flg_2:
            flg2 = 1
        else:
            flg2 = 0


        l = 1
        for i in ans:
            l *= abs(i[0])
            l %= MOD 

        # print(l)

        if flg == 1 and flg2 == 1:
            if min_p * max_p >= min_m * max_m:
                l *= max_p
                l *= pow(min_m,MOD-2,MOD)
            else:
                l *= max_m
                l *= pow(min_p,MOD-2,MOD)
        elif flg == 1:
                l *= max_p
                l *= pow(min_m,MOD-2,MOD)
        elif flg2 == 1:
                l *= max_m
                l *= pow(min_p,MOD-2,MOD)
        else:
            if 0 in A:
                l = 0
            else:
                A.sort(reverse=True)
                l = 1
                for i in range(k):
                    l *= A[i]
                    l %= MOD
                print(l)
                exit()

            
        print(l%MOD)




    

            

main()

Buggy submission (verdict: Runtime Error):
def main():
    n,k = map(int,input().split())
    A = list(map(int,input().split()))

    MOD = 10**9 + 7

    absA = [abs(a) for a in A]
    flg = 0
    m = []
    p = []
    dict = []
    T = []
    for i in A:
        if i < 0:
            m.append(i)
            T.append(tuple([abs(i),-1]))
        elif i > 0:
            p.append(i)
            T.append(tuple([abs(i),1]))
        else:
            flg = 1

    T.sort(reverse=True)
    # print(T)


    ans = T[:k]
    # print(ans)
    c = 0
    for i in ans:
        if i[1] == -1:
            c += 1

    if c % 2 == 0:
        l = 1
        for i in ans:
            l *= i[0]
            l %= MOD
        print(l)

    else:
        flg_1,flg_2 = 0,0
        # print(T)
        for j in range(k-1,-1,-1):
            if T[j][1] == -1:
                flg_1 = 1
                min_m = T[j][0]
                break

        for j in range(k,len(T)):
            if T[j][1] == 1:
                flg_2 ^= 1
                # print('AaA')
                max_p = T[j][0]
                break

        # print(min_m,max_p)

        if flg_1 and flg_2:
            flg = 1
        else:
            flg = 0

        flg_1,flg_2 = 0,0

        for j in range(k-1,-1,-1):
            if T[j][1] == 1:
                flg_1 = 1
                min_p = T[j][0]
                break
        
        for j in range(k,len(T)):
            if T[j][1] == -1:
                flg_2 = 1
                max_m = T[j][0]
                break

        if flg_1 and flg_2:
            flg2 = 1
        else:
            flg2 = 0


        l = 1
        for i in ans:
            l *= abs(i[0])
            l %= MOD 

        # print(l)

        if flg == 1 and flg2 == 1:
            if min_p * max_p >= min_m * max_p:
                l *= max_p
                l *= pow(min_m,MOD-2,MOD)
            else:
                l *= max_m
                l *= pow(min_p,MOD-2,MOD)
        elif flg == 1:
                l *= max_p
                t = pow(min_m,MOD-2,MOD)
                l *= pow(min_m,MOD-2,MOD)
        elif flg2 == 1:
                l *= max_m
                l *= pow(min_p,MOD-2,MOD)
        else:
            if 0 in A:
                l = 0
            else:
                A.sort(reverse=True)
                l = 1
                for i in range(k):
                    l *= A[i]
                    l %= MOD
                print(l)
                exit()
                # print(A)


            
        print(l%MOD)




    

            

main()

Buggy submission (verdict: Runtime Error):
def main():
    n,k = map(int,input().split())
    A = list(map(int,input().split()))

    MOD = 10**9 + 7

    absA = [abs(a) for a in A]
    flg = 0
    m = []
    p = []
    dict = []
    T = []
    for i in A:
        if i < 0:
            m.append(i)
            T.append(tuple([abs(i),-1]))
        elif i > 0:
            p.append(i)
            T.append(tuple([abs(i),1]))
        else:
            flg = 1

    T.sort(reverse=True)
    # print(T)


    ans = T[:k]
    # print(ans)
    c = 0
    for i in ans:
        if i[1] == -1:
            c += 1

    if c % 2 == 0:
        l = 1
        for i in ans:
            l *= i[0]
        print(l)

    else:
        flg_1,flg_2 = 0,0
        # print(T)
        for j in range(k-1,-1,-1):
            if T[j][1] == -1:
                flg_1 = 1
                min_m = T[j][0]
                break

        for j in range(k,len(T)):
            if T[j][1] == 1:
                flg_2 ^= 1
                # print('AaA')
                max_p = T[j][0]
                break

        # print(min_m,max_p)

        if flg_1 and flg_2:
            flg = 1
        else:
            flg = 0

        flg_1,flg_2 = 0,0

        for j in range(k-1,-1,-1):
            if T[j][1] == 1:
                flg_1 = 1
                min_p = T[j][0]
                break
        
        for j in range(k,len(T)):
            if T[j][1] == -1:
                flg_2 = 1
                max_m = T[j][0]
                break

        if flg_1 and flg_2:
            flg2 = 1
        else:
            flg2 = 0


        l = 1
        for i in ans:
            l *= abs(i[0])
            l %= MOD 

        # print(l)

        if flg == 1 and flg2 == 1:
            if min_p * max_p >= min_m * max_p:
                l *= max_p
                l *= pow(min_m,MOD-2,MOD)
            else:
                l *= max_m
                l *= pow(min_p,MOD-2,MOD)
        elif flg == 1:
                l *= max_p
                t = pow(min_m,MOD-2,MOD)
                l *= pow(min_m,MOD-2,MOD)
        elif flg2 == 1:
                l *= max_m
                l *= pow(min_p,MOD-2,MOD)
        else:
            if 0 in A:
                l = 0
            else:
                A.sort(reverse=True)
                l = 1
                for i in range(k):
                    l *= A[i]
                    l %= MOD
                print(l)
                exit()
                # print(A)


            
        print(l%MOD)




    

            

main()