Problem:


Score : 500 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.

Then, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Constraints

1 \leq K \leq N \leq 2\times 10^5

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Sample Input 1

4 2
1 2 -3 -4

Sample Output 1

12

The possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12.

Sample Input 2

4 3
-1 -2 -3 -4

Sample Output 2

1000000001

The possible products of the three chosen elements are -24, -12, -8, and -6, so the maximum product is -6.

We print this value modulo (10^9+7), that is, 1000000001.

Sample Input 3

2 1
-1 1000000000

Sample Output 3

1000000000

The possible products of the one chosen element are -1 and 1000000000, so the maximum product is 1000000000.

Sample Input 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

Sample Output 4

999983200

Be sure to print the product modulo (10^9+7).



Sample input:
4 2
1 2 -3 -4


Sample output:
12

Submission status: Wrong Answer

Solution code:
from functools import reduce

n, k = map(int, input().split())
a = list(map(int, input().split()))

a.sort(key=lambda v: abs(v), reverse=True)
mod = 10 ** 9 + 7
if n == k:
    print(reduce(lambda ac, v: ac * v % mod, a, 1))
    exit()
elif len(list(filter(lambda v: v < 0, a))) == n and k % 2 == 1:
    print(reduce(lambda ac, v: ac * v % mod, a[n - k :], 1))
    exit()

ans = 1
mn = 0
for v in a[:k]:
    if v < 0:
        mn += 1
    ans *= abs(v)
    ans %= mod

if mn % 2 == 0:
    print(ans)
    exit()

mnp, mnn = 0, 0
for v in a[k - 1 :: -1]:
    if mnp != 0 and mnn != 0:
        break
    elif mnp == 0 and v > 0:
        mnp = v
    elif mnn == 0 and v < 0:
        mnn = v
mxp, mxn = 0, 0
for v in a[k:]:
    if mxp != 0 and mxn != 0:
        break
    elif mxp == 0 and v > 0:
        mxp = v
    elif mxn == 0 and v < 0:
        mxn = v
x, y = mxp, mnn
if mxn * mnn > mxp * mnp:
    x, y = mxn, mnp
ans *= pow(abs(y), mod - 2, mod)
ans %= mod
ans *= abs(x)
ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce

n, k = map(int, input().split())
a = list(map(int, input().split()))

a.sort(key=lambda v: abs(v), reverse=True)
mod = 10 ** 9 + 7
if len(list(filter(lambda v: v < 0, a))) == n and k % 2 == 1:
    print(reduce(lambda ac, v: ac * v % mod, a[n - k :], 1))
    exit()

ans = 1
mn = 0
for v in a[:k]:
    if v < 0:
        mn += 1
    ans *= abs(v)
    ans %= mod

if mn % 2 == 0:
    print(ans)
    exit()

mnp, mnn = 0, 0
for v in a[k - 1 :: -1]:
    if mnp != 0 and mnn != 0:
        break
    elif mnp == 0 and v > 0:
        mnp = v
    elif mnn == 0 and v < 0:
        mnn = v
mxp, mxn = 0, 0
for v in a[k:]:
    if mxp != 0 and mxn != 0:
        break
    elif mxp == 0 and v > 0:
        mxp = v
    elif mxn == 0 and v < 0:
        mxn = v
x, y = mxp, mnn
if mnp != 0 and mxn * mnn > mxp * mnp:
    x, y = mxn, mnp
ans *= pow(abs(y), mod - 2, mod)
ans %= mod
ans *= abs(x)
ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce

n, k = map(int, input().split())
a = list(map(int, input().split()))

a.sort(key=lambda v: abs(v), reverse=True)
mod = 10 ** 9 + 7
if len(list(filter(lambda v: v < 0, a))) == n:
    print(reduce(lambda ac, v: ac * v % mod, a[n - k :], 1))
    exit()

ans = 1
mn = 0
for v in a[:k]:
    if v < 0:
        mn += 1
    ans *= abs(v)
    ans %= mod

if mn % 2 == 0:
    print(ans)
    exit()

mnp, mnn = 0, 0
for v in a[k - 1 :: -1]:
    if mnp != 0 and mnn != 0:
        break
    elif mnp == 0 and v > 0:
        mnp = v
    elif mnn == 0 and v < 0:
        mnn = v
mxp, mxn = 0, 0
for v in a[k:]:
    if mxp != 0 and mxn != 0:
        break
    elif mxp == 0 and v > 0:
        mxp = v
    elif mxn == 0 and v < 0:
        mxn = v
x, y = mxp, mnn
if mnp != 0 and mxn * mnn > mxp * mnp:
    x, y = mxn, mnp
ans *= pow(abs(y), mod - 2, mod)
ans %= mod
ans *= abs(x)
ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce

n, k = map(int, input().split())
a = list(map(int, input().split()))

a.sort(key=lambda v: abs(v), reverse=True)
mod = 10 ** 9 + 7
if len(list(filter(lambda v: v < 0, a))) == n:
    print(reduce(lambda ac, v: ac * v % mod, a[k - 1 :: -1], 1))
    exit()

ans = 1
mn = 0
for v in a[:k]:
    if v < 0:
        mn += 1
    ans *= abs(v)
    ans %= mod

if mn % 2 == 0:
    print(ans)
    exit()

mnp, mnn = 0, 0
for v in a[k - 1 :: -1]:
    if mnp != 0 and mnn != 0:
        break
    elif mnp == 0 and v > 0:
        mnp = v
    elif mnn == 0 and v < 0:
        mnn = v
mxp, mxn = 0, 0
for v in a[k:]:
    if mxp != 0 and mxn != 0:
        break
    elif mxp == 0 and v > 0:
        mxp = v
    elif mxn == 0 and v < 0:
        mxn = v
x, y = mxp, mnn
if mnp != 0 and mxn * mnn > mxp * mnp:
    x, y = mxn, mnp
ans *= pow(abs(y), mod - 2, mod)
ans %= mod
ans *= abs(x)
ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce

n, k = map(int, input().split())
a = list(map(int, input().split()))

mod = 10 ** 9 + 7
if len(list(filter(lambda v: v < 0, a))) == n:
    a.sort(key=lambda v: (abs(v)))
    print(reduce(lambda ac, v: ((ac * v) + mod) % mod, a[:k], 1))
    print("hello")
    exit()

a.sort(key=lambda v: (abs(v)), reverse=True)
ans = 1
mn = 0
for v in a[:k]:
    if v < 0:
        mn += 1
    ans *= abs(v)
    ans %= mod

if mn % 2 == 0:
    print(ans)
    exit()

mnp, mnn = 0, 0
for v in a[k - 1 :: -1]:
    if mnp != 0 and mnn != 0:
        break
    elif mnp == 0 and v > 0:
        mnp = v
    elif mnn == 0 and v < 0:
        mnn = v
mxp, mxn = 0, 0
for v in a[k:]:
    if mxp != 0 and mxn != 0:
        break
    elif mxp == 0 and v > 0:
        mxp = v
    elif mxn == 0 and v < 0:
        mxn = v
x, y = mxp, mnn
if mnp != 0 and mxn * mnn > mxp * mnp:
    x, y = mxn, mnp
ans *= pow(abs(y), mod - 2, mod)
ans %= mod
ans *= abs(x)
ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce

n, k = map(int, input().split())
a = list(map(int, input().split()))

mod = 10 ** 9 + 7
if len(list(filter(lambda v: v < 0, a))) == n:
    a.sort(key=lambda v: (abs(v)))
    print(reduce(lambda ac, v: (ac * v) % mod, a[:k], 1))
    exit()

a.sort(key=lambda v: abs(v), reverse=True)
ans = 1
mn = 0
for v in a[:k]:
    if v < 0:
        mn += 1
    ans *= abs(v)
    ans %= mod

if mn % 2 == 0:
    print(ans)
    exit()

mnp, mnn = 0, 0
for v in a[k - 1 :: -1]:
    if mnp != 0 and mnn != 0:
        break
    elif mnp == 0 and v > 0:
        mnp = v
    elif mnn == 0 and v < 0:
        mnn = v
mxp, mxn = 0, 0
for v in a[k:]:
    if mxp != 0 and mxn != 0:
        break
    elif mxp == 0 and v > 0:
        mxp = v
    elif mxn == 0 and v < 0:
        mxn = v
x = ans * pow(abs(mnn), mod - 2, mod) % mod * abs(mxp) % mod
y = ans * pow(abs(mnp), mod - 2, mod) % mod * abs(mxn) % mod
print(y)

Submission status: Wrong Answer

Solution code:
from functools import reduce

n, k = map(int, input().split())
a = list(map(int, input().split()))

mod = 10 ** 9 + 7
if len(list(filter(lambda v: v < 0, a))) == n:
    a.sort(key=lambda v: (abs(v)))
    print(reduce(lambda ac, v: (ac * v) % mod, a[:k], 1))
    exit()

a.sort(key=lambda v: abs(v), reverse=True)
ans = 1
mn = 0
for v in a[:k]:
    if v < 0:
        mn += 1
    ans *= abs(v)
    ans %= mod

if mn % 2 == 0:
    print(ans)
    exit()

mnp, mnn = 0, 0
for v in a[k - 1 :: -1]:
    if mnp != 0 and mnn != 0:
        break
    elif mnp == 0 and v > 0:
        mnp = v
    elif mnn == 0 and v < 0:
        mnn = v
mxp, mxn = 0, 0
for v in a[k:]:
    if mxp != 0 and mxn != 0:
        break
    elif mxp == 0 and v > 0:
        mxp = v
    elif mxn == 0 and v < 0:
        mxn = v
x = ans * pow(abs(mnn), mod - 2, mod) % mod * abs(mxp) % mod
y = ans * pow(abs(mnp), mod - 2, mod) % mod * abs(mxn) % mod
print(x)

Submission status: Wrong Answer

Solution code:
from functools import reduce

n, k = map(int, input().split())
a = list(map(int, input().split()))

mod = 10 ** 9 + 7
if len(list(filter(lambda v: v < 0, a))) == n:
    a.sort(key=lambda v: (abs(v)))
    print(reduce(lambda ac, v: (ac * v) % mod, a[:k], 1))
    exit()

a.sort(key=lambda v: (abs(v)), reverse=True)
ans = 1
mn = 0
for v in a[:k]:
    if v < 0:
        mn += 1
    ans *= abs(v)
    ans %= mod

if mn % 2 == 0:
    print(ans)
    exit()

mnp, mnn = 0, 0
for v in a[k - 1 :: -1]:
    if mnp != 0 and mnn != 0:
        break
    elif mnp == 0 and v > 0:
        mnp = v
    elif mnn == 0 and v < 0:
        mnn = v
mxp, mxn = 0, 0
for v in a[k:]:
    if mxp != 0 and mxn != 0:
        break
    elif mxp == 0 and v > 0:
        mxp = v
    elif mxn == 0 and v < 0:
        mxn = v
x = ans * pow(abs(mnn), mod - 2, mod) % mod * abs(mxp) % mod
y = ans * pow(abs(mnp), mod - 2, mod) % mod * abs(mxn) % mod
print(max(x, y))

Submission status: Wrong Answer

Solution code:
from functools import reduce

n, k = map(int, input().split())
a = list(map(int, input().split()))

mod = 10 ** 9 + 7
if len(list(filter(lambda v: v < 0, a))) == n:
    a.sort(key=lambda v: (abs(v)))
    print(reduce(lambda ac, v: (ac * v) % mod, a[:k], 1))
    exit()

a.sort(key=lambda v: (abs(v)), reverse=True)
ans = 1
mn = 0
for v in a[:k]:
    if v < 0:
        mn += 1
    ans *= abs(v)
    ans %= mod

if mn % 2 == 0:
    print(ans)
    exit()

mnp, mnn = 0, 0
for v in a[k - 1 :: -1]:
    if mnp != 0 and mnn != 0:
        break
    elif mnp == 0 and v > 0:
        mnp = v
    elif mnn == 0 and v < 0:
        mnn = v
mxp, mxn = 0, 0
for v in a[k:]:
    if mxp != 0 and mxn != 0:
        break
    elif mxp == 0 and v > 0:
        mxp = v
    elif mxn == 0 and v < 0:
        mxn = v
x, y = mxp, mnn
if mnp != 0 and mxn * mnn > mxp * mnp:
    x, y = mxn, mnp
ans *= pow(abs(y), mod - 2, mod)
ans %= mod
ans *= abs(x)
ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce

n, k = map(int, input().split())
a = list(map(int, input().split()))

mod = 10 ** 9 + 7
if len(list(filter(lambda v: v < 0, a))) == n:
    a.sort(key=lambda v: (abs(v)))
    print(reduce(lambda ac, v: (ac * v) % mod, a[:k], 1))
    exit()

a.sort(key=lambda v: (abs(v)), reverse=True)
ans = 1
mn = 0
for v in a[:k]:
    if v < 0:
        mn += 1
    ans *= abs(v)
    ans %= mod

if mn % 2 == 0:
    print(ans)
    exit()

mnp, mnn = 0, 0
for v in a[k - 1 :: -1]:
    if mnp != 0 and mnn != 0:
        break
    elif mnp == 0 and v > 0:
        mnp = v
    elif mnn == 0 and v < 0:
        mnn = v
mxp, mxn = 0, 0
for v in a[k:]:
    if mxp != 0 and mxn != 0:
        break
    elif mxp == 0 and v > 0:
        mxp = v
    elif mxn == 0 and v < 0:
        mxn = v
x, y = mxp, mnn
if mxn * mnn > mxp * mnp:
    x, y = mxn, mnp
ans *= pow(abs(y), mod - 2, mod)
ans %= mod
ans *= abs(x)
ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce

n, k = map(int, input().split())
a = list(map(int, input().split()))

mod = 10 ** 9 + 7
if len(list(filter(lambda v: v < 0, a))) == n:
    a.sort(key=lambda v: (abs(v)))
    print(reduce(lambda ac, v: (ac * v) % mod, a[:k], 1))
    exit()

a.sort(key=lambda v: (abs(v)), reverse=True)
ans = 1
mn = 0
for v in a[:k]:
    if v < 0:
        mn += 1
    ans *= abs(v)
    ans %= mod

if mn % 2 == 0:
    print(ans)
    exit()

mnp, mnn = 0, 0
for v in a[:k:-1]:
    if mnp != 0 and mnn != 0:
        break
    elif mnp == 0 and v > 0:
        mnp = v
    elif mnn == 0 and v < 0:
        mnn = v
mxp, mxn = 0, 0
for v in a[k:]:
    if mxp != 0 and mxn != 0:
        break
    elif mxp == 0 and v > 0:
        mxp = v
    elif mxn == 0 and v < 0:
        mxn = v
x, y = mxp, mnn
ans *= pow(abs(y), mod - 2, mod)
ans %= mod
ans *= abs(x)
ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce

n, k = map(int, input().split())
a = list(map(int, input().split()))

mod = 10 ** 9 + 7
if len(list(filter(lambda v: v < 0, a))) == n:
    a.sort(key=lambda v: (abs(v)))
    print(reduce(lambda ac, v: (ac * v) % mod, a[:k], 1))
    exit()

a.sort(key=lambda v: (abs(v)), reverse=True)
ans = 1
mn = 0
for v in a[:k]:
    if v < 0:
        mn += 1
    ans *= abs(v)
    ans %= mod

if mn % 2 == 0:
    print(ans)
    exit()

mnp, mnn = 0, 0
for v in a[:k:-1]:
    if mnp != 0 and mnn != 0:
        break
    elif mnp == 0 and v > 0:
        mnp = v
    elif mnn == 0 and v < 0:
        mnn = v
mxp, mxn = 0, 0
for v in a[k:]:
    if mxp != 0 and mxn != 0:
        break
    elif mxp == 0 and v > 0:
        mxp = v
    elif mxn == 0 and v < 0:
        mxn = v
x, y = mxp, mnn
if mxn * mnn < mxp * mnp:
    x, y = mxn, mnp
ans *= pow(abs(y), mod - 2, mod)
ans %= mod
ans *= abs(x)
ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce

n, k = map(int, input().split())
a = list(map(int, input().split()))

mod = 10 ** 9 + 7
if len(list(filter(lambda v: v < 0, a))) == n:
    a.sort(key=lambda v: (abs(v)))
    print(reduce(lambda ac, v: (ac * v) % mod, a[:k], 1))
    exit()

a.sort(key=lambda v: (abs(v)), reverse=True)
ans = 1
mn = 0
for v in a[:k]:
    if v < 0:
        mn += 1
    ans *= abs(v)
    ans %= mod

if mn % 2 == 0:
    print(ans)
    exit()

mnp, mnn = 0, 0
for v in a[:k:-1]:
    if mnp != 0 and mnn != 0:
        break
    elif mnp == 0 and v > 0:
        mnp = v
    elif mnn == 0 and v < 0:
        mnn = v
mxp, mxn = 0, 0
for v in a[k:]:
    if mxp != 0 and mxn != 0:
        break
    elif mxp == 0 and v > 0:
        mxp = v
    elif mxn == 0 and v < 0:
        mxn = v
x, y = mxp, mnn
if mxn * mnn > mxp * mnp:
    x, y = mxn, mnp
ans *= pow(abs(y), mod - 2, mod)
ans %= mod
ans *= abs(x)
ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce

n, k = map(int, input().split())
a = list(map(int, input().split()))

mod = 10 ** 9 + 7
if len(list(filter(lambda v: v < 0, a))) == n:
    a.sort(key=lambda v: (abs(v)))
    print(reduce(lambda ac, v: (ac * v) % mod, a[:k], 1))
    exit()

a.sort(key=lambda v: (abs(v)), reverse=True)
ans = 1
mn = 0
for v in a[:k]:
    if v < 0:
        mn += 1
    ans *= abs(v)
    ans %= mod

if mn % 2 == 0:
    print(ans)
    exit()

mnp, mnn = 0, 0
for v in a[:k:-1]:
    if mnp != 0 and mnn != 0:
        break
    if v > 0:
        mnp = v
    elif v < 0:
        mnn = v
mxp, mxn = 0, 0
for v in a[k:]:
    if mxp != 0 and mxn != 0:
        break
    if v > 0:
        mxp = v
    elif v < 0:
        mxn = v
x, y = mxp, mnn
if mxn * mnn > mxp * mnp:
    x, y = mxn, mnp
ans *= pow(abs(y), mod - 2, mod)
ans %= mod
ans *= abs(x)
ans %= mod
print(ans)

Submission status: Wrong Answer

Solution code:
def solve():
    import sys
    input = sys.stdin.readline

    mod = 10 ** 9 + 7
    n, k = map(int, input().split())
    pos, neg = [], []
    for x in map(int, input().split()):
        if x < 0:
            neg.append(x)
        else:
            pos.append(x)

    ans = 1
    neg.sort()
    pos.sort(reverse=True)
    neg_r = neg[::-1]

    if not pos:
        for i in range(k):
            ans *= neg_r[i]
            ans %= mod
        print(ans)
        exit()

    x, y = 0, 0
    lp, ln = len(pos), len(neg)
    for i in range(k):
        if y >= ln:
            ans *= pos[x]
            ans %= mod
            x += 1
        elif x >= lp:
            ans *= neg[y]
            ans %= mod
            y += 1
        elif pos[x] >= -neg[y]:
            ans *= pos[x]
            ans %= mod
            x += 1
        else:
            ans *= neg[y]
            ans %= mod
            y += 1

    if n == k:
        print(ans)
        exit()

    if y & 1:
        ans_r = 1
        tmp1, tmp2 = -1, -1
        if y < ln and x != 0:
            tmp1 = neg[y] * neg[y - 1]
        if x < lp and y != 0:
            tmp2 = pos[x] * pos[x - 1]
        if tmp1 > tmp2:
            for i in range(x - 1):
                ans_r *= pos[i]
                ans_r %= mod
            for i in range(y + 1):
                ans_r *= neg[i]
                ans_r %= mod
        elif tmp1 <= tmp2:
            for i in range(x + 1):
                ans_r *= pos[i]
                ans_r %= mod
            for i in range(y - 1):
                ans_r *= neg[i]
                ans_r %= mod
        print(ans_r)
    else:
        print(ans % mod)


solve()

Submission status: Wrong Answer

Solution code:
def solve():
    import sys
    input = sys.stdin.readline

    mod = 10 ** 9 + 7
    n, k = map(int, input().split())
    pos, neg = [], []
    for x in map(int, input().split()):
        if x < 0:
            neg.append(x)
        else:
            pos.append(x)

    ans = 1
    neg.sort()
    pos.sort(reverse=True)
    neg_r = neg[::-1]

    if not pos:
        for i in range(k):
            ans *= neg_r[i]
            ans %= mod
        print(ans)
        exit()

    x, y = 0, 0
    lp, ln = len(pos), len(neg)
    for i in range(k):
        if y >= ln:
            ans *= pos[x]
            ans %= mod
            x += 1
        elif x >= lp:
            ans *= neg[y]
            ans %= mod
            y += 1
        elif pos[x] >= -neg[y]:
            ans *= pos[x]
            ans %= mod
            x += 1
        else:
            ans *= neg[y]
            ans %= mod
            y += 1

    if y & 1:
        ans_r = 1
        tmp1, tmp2 = 0, 0
        if y < ln and x != 0:
            tmp1 = neg[y] // pos[x - 1]
        if x < lp and y != 0:
            tmp2 = pos[x] // neg[y - 1]
        if tmp1 < tmp2:
            for i in range(x - 1):
                ans_r *= pos[i]
                ans_r %= mod
            for i in range(y + 1):
                ans_r *= neg[i]
                ans_r %= mod
        elif tmp1 > tmp2:
            for i in range(x + 1):
                ans_r *= pos[i]
                ans_r %= mod
            for i in range(y - 1):
                ans_r *= neg[i]
                ans_r %= mod
        else:
            ans_r = ans
        print(ans_r)
    else:
        print(ans % mod)


solve()

Submission status: Wrong Answer

Solution code:
def solve():
    mod = 10 ** 9 + 7
    n, k = map(int, input().split())
    pos, neg = [], []
    for x in map(int, input().split()):
        if x < 0:
            neg.append(x)
        else:
            pos.append(x)

    ans = 1
    neg.sort()
    pos.sort(reverse=True)

    if not pos:
        for i in range(k):
            ans *= neg[::-1][i]
            ans %= mod
        print(ans)
        exit()

    x, y = 0, 0
    lp, ln = len(pos), len(neg)
    for i in range(k):
        if y >= ln:
            ans *= pos[x]
            ans %= mod
            x += 1
        elif x >= lp:
            ans *= neg[y]
            ans %= mod
            y += 1
        elif pos[x] >= -neg[y]:
            ans *= pos[x]
            ans %= mod
            x += 1
        else:
            ans *= neg[y]
            ans %= mod
            y += 1

    if y & 1:
        ans_r = 1
        tmp1, tmp2 = 0, 0
        if y < ln and x != 0:
            tmp1 = neg[y] // pos[x - 1]
        if x < lp and y != 0:
            tmp2 = pos[x] // neg[y - 1]
        if tmp1 < tmp2:
            for i in range(x - 1):
                ans_r *= pos[i]
                ans_r %= mod
            for i in range(y + 1):
                ans_r *= neg[i]
                ans_r %= mod
        elif tmp1 > tmp2:
            for i in range(x + 1):
                ans_r *= pos[i]
                ans_r %= mod
            for i in range(y - 1):
                ans_r *= neg[i]
                ans_r %= mod
        print(ans_r)
    else:
        print(ans % mod)


solve()

Submission status: Wrong Answer

Solution code:
def solve():
    mod = 10 ** 9 + 7
    n, k = map(int, input().split())
    pos, neg = [], []
    for x in map(int, input().split()):
        if x < 0:
            neg.append(x)
        else:
            pos.append(x)

    ans = 1
    neg.sort()
    pos.sort(reverse=True)

    if not pos:
        for i in range(k):
            ans *= neg[::-1][i]
            ans %= mod
        print(ans)
        exit()

    x, y = 0, 0
    lp, ln = len(pos), len(neg)
    for i in range(k):
        if y >= ln:
            ans *= pos[x]
            x += 1
        elif x >= lp:
            ans *= neg[y]
            y += 1
        elif pos[x] >= -neg[y]:
            ans *= pos[x]
            x += 1
        else:
            ans *= neg[y]
            y += 1

    if ans < 0:
        ans1, ans2 = ans * 1, ans * 1
        if y < ln:
            ans1 = ans * neg[y] // pos[max(0, x - 1)]
        if x < lp:
            ans2 = ans * pos[x] // neg[max(0, y - 1)]
        print(max(ans1, ans2) % mod)
    else:
        print(ans % mod)

solve()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
As = list(mapint())
mod = 10**9+7

pos = []
neg = []
for a in As:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)
pos.sort(reverse=True)
neg.sort()
ans = 1
lenp, lenn = len(pos), len(neg)
if (lenp+lenn)==K:
    for a in As:
        ans *= a
        ans %= mod
    print(ans)
elif lenn==0:
    for i in range(K):
        ans *= pos[i]
        ans %= mod
    print(ans)
elif lenp==0:
    if K%2==0:
        for i in range(K):
            ans *= neg[i]
            ans %= mod
        print(ans)
    else:
        for i in range(K-1, -1, -1):
            ans *= neg[i]
            ans %= mod
        print(ans)        
else:
    p, n = 0, 0
    while p+n+1<K:
        if p+2<=lenp and n+2<=lenn:
            if pos[p]*pos[p+1]>=neg[n]*neg[n+1]:
                ans *= pos[p]
                p += 1
            else:
                ans *= neg[n]*neg[n+1]
                n += 2
        elif n+2<=lenn:
            ans *= neg[n]*neg[n+1]
            n += 2
        else:
            ans *= pos[p]
            p += 1
        ans %= mod
    if p+n<K:
        ans *= pos[p]
        ans %= mod
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
As = list(mapint())
mod = 10**9+7

pos = []
neg = []
for a in As:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)
pos.sort(reverse=True)
neg.sort()
ans = 1
lenp, lenn = len(pos), len(neg)
if (lenp+lenn)==K:
    for a in As:
        ans *= a
        ans %= mod
    print(ans)
elif lenn==0:
    for i in range(K):
        ans *= pos[i]
        ans %= mod
    print(ans)
elif lenp==0:
    if K%2==1:
        for i in range(K):
            ans *= neg[i]
            ans %= mod
        print(ans)
    else:
        for i in range(K-1, -1, -1):
            ans *= neg[i]
            ans %= mod
        print(ans)        
else:
    p, n = 0, 0
    while p+n+1<K:
        if p+2<=lenp and n+2<=lenn:
            if pos[p]*pos[p+1]>=neg[n]*neg[n+1]:
                ans *= pos[p]
                p += 1
            else:
                ans *= neg[n]*neg[n+1]
                n += 2
        elif n+2<=lenn:
            ans *= neg[n]*neg[n+1]
            n += 2
        else:
            ans *= pos[p]
            p += 1
        ans %= mod
    if p+n<K:
        ans *= pos[p]
        ans %= mod
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
As = list(mapint())
mod = 10**9+7

pos = []
neg = []
for a in As:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)
pos.sort(reverse=True)
neg.sort(reverse=True)
ans = 1
lenp, lenn = len(pos), len(neg)
if (lenp+lenn)==K:
    for a in As:
        ans *= a
        ans %= mod
    print(ans)
elif lenn==0:
    for i in range(K):
        ans *= pos[i]
        ans %= mod
    print(ans)
elif lenp==0:
    if K%2==0:
        for i in range(K):
            ans *= neg[i]
            ans %= mod
        print(ans)
    else:
        for i in range(K-1, -1, -1):
            ans *= neg[i]
            ans %= mod
        print(ans)        
else:
    cur = 0
    p, n = 0, 0
    while p+n+1<K:
        if p+2<=lenp and n+2<=lenn:
            if pos[p]*pos[p+1]>=neg[n]*neg[n+1]:
                ans *= pos[p]
                p += 1
            else:
                ans *= neg[n]*neg[n+1]
                n += 2
        elif n+2<=lenn:
            ans *= neg[n]*neg[n+1]
            n += 2
        else:
            ans *= pos[p]
            p += 1
        ans %= mod
    if p+n<K:
        ans *= pos[p]
        ans %= mod
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
As = list(mapint())
mod = 10**9+7

pos = []
neg = []
for a in As:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)
pos.sort(reverse=True)
neg.sort(reverse=True)
ans = 1
lenp, lenn = len(pos), len(neg)
if (lenp+lenn)==K:
    for a in As:
        ans *= a
        ans %= mod
    print(ans)
elif lenn==0:
    for i in range(K):
        ans *= pos[i]
        ans %= mod
    print(ans)
elif lenp==0:
    if K%2==0:
        for i in range(K):
            ans *= neg[i]
            ans %= mod
        print(ans)
    else:
        for i in range(K-1, -1, -1):
            ans *= neg[i]
            ans %= mod
        print(ans)        
else:
    cur = 0
    p, n = 0, 0
    while p+n<K:
        if p+2<=lenp and n+2<=lenn:
            if pos[p]*pos[p+1]>=neg[n]*neg[n+1]:
                ans *= pos[p]
                p += 1
            else:
                ans *= neg[n]*neg[n+1]
                n += 2
        elif n+2<=lenn:
            ans *= neg[n]*neg[n+1]
            n += 2
        else:
            ans *= pos[p]
            p += 1
        ans %= mod
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, K = mapint()
As = list(mapint())
mod = 10**9+7

pos = []
neg = []
for a in As:
    if a>=0:
        pos.append(a)
    else:
        neg.append(a)
pos.sort(reverse=True)
neg.sort(reverse=True)
ans = 1
lenp, lenn = len(pos), len(neg)
if (lenp+lenn)==K:
    for a in As:
        ans *= a
        ans %= mod
    print(ans)
elif lenn==0:
    for i in range(K):
        ans *= pos[i]
        ans %= mod
    print(ans)
elif lenp==0:
    if K%2==0:
        for i in range(K):
            ans *= neg[i]
            ans %= mod
        print(ans)
    else:
        for i in range(K-1, -1, -1):
            ans *= neg[i]
            ans %= mod
        print(ans)        
else:
    cur = 0
    p, n = 0, 0
    while p+n<K:
        if p+2<=lenp and n+2<=lenn:
            if pos[p]*pos[p+1]>=neg[n]*neg[n+1]:
                ans *= pos[p]
                p += 1
            else:
                ans *= neg[n]*neg[n+1]
                n += 2
        elif p<lenp:
            ans *= pos[p]
            p += 1
        else:
            ans *= neg[n]*neg[n+1]
            n += 2
        ans %= mod
    print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

import numpy

MAX = 10 ** 9 + 7

def prod_mod(la, mod):
  x = 1
  for a in la:
    x *= a
    x %= mod
  return x

def main():
  n, k = map(int, input().strip().split())
  la = numpy.array(list(map(int, input().strip().split())))
  la = la[numpy.argsort(numpy.abs(la))][::-1]
  if n == k:
    print(prod_mod(la, MAX))
  else:
    if k % 2 == 1 and (la >= 0).sum() == 0:
      print(prod_mod(la[-k:], MAX))
    else:
      lap = [a for a in la if a >= 0]
      lam = [a for a in la if a < 0]
      cp, cm = 0, 0
      ans = 1
      if k % 2 == 1:
        ans *= lap[cp]
        cp += 1
      while k - (cp + cm + 2) >= 0:
        if len(lap) <= cp+2 and numpy.prod(lap[cp:cp+2]) > numpy.prod(lam[cm:cm+2]):
          ans *= numpy.prod(lap[cp:cp+2])
          cp += 2
        else:
          ans *= numpy.prod(lam[cm:cm+2])
          cm += 2
        ans %= MAX
      print(ans % MAX)
          

if __name__=='__main__':
  main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

import numpy

MAX = 10 ** 9 + 7

def prod_mod(la, mod):
  x = 1
  for a in la:
    x *= a
    x %= mod
  return x

def main():
  n, k = map(int, input().strip().split())
  la = numpy.array(list(map(int, input().strip().split())))
  la = la[numpy.argsort(numpy.abs(la))][::-1]
  if n == k:
    print(prod_mod(la, MAX))
  else:
    if k % 2 == 1 and (la >= 0).sum() == 0:
      print(prod_mod(la[-k:], MAX))
    else:
      lap = [a for a in la if a >= 0] + [0, 0]
      lam = [a for a in la if a < 0] + [0, 0]
      cp, cm = 0, 0
      ans = 1
      if k % 2 == 1:
        ans *= lap[cp]
        cp += 1
      while k - (cp + cm + 2) >= 0:
        if len(lap) <= cp+2 and numpy.prod(lap[cp:cp+2]) > numpy.prod(lam[cm:cm+2]):
          ans *= numpy.prod(lap[cp:cp+2])
          cp += 2
        else:
          ans *= numpy.prod(lam[cm:cm+2])
          cm += 2
        ans %= MAX
      print(ans % MAX)
          

if __name__=='__main__':
  main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

import numpy

MAX = 10 ** 9 + 7

def prod_mod(la, mod):
  x = 1
  for a in la:
    x *= a
    x %= mod
  return x

def main():
  n, k = map(int, input().strip().split())
  la = numpy.array(list(map(int, input().strip().split())))
  la = la[numpy.argsort(numpy.abs(la))][::-1]
  if n == k:
    print(prod_mod(la, MAX))
  else:
    if k % 2 == 1 and (la >= 0).sum() == 0:
      print(prod_mod(la[-k:], MAX))
    else:
      lap = [a for a in la if a >= 0] 
      lam = [a for a in la if a < 0] 
      cp, cm = 0, 0
      ans = 1
      while k > (cp + cm):
        if numpy.prod(lap[cp:cp+2]) > numpy.prod(lam[cm:cm+2]) or k - 1 == (cp + cm):
          ans *= lap[cp]
          cp += 1
        else:
          ans *= numpy.prod(lam[cm:cm+2])
          cm += 2
        ans %= MAX
      print(ans % MAX)
          

if __name__=='__main__':
  main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

import numpy

MAX = 10 ** 9 + 7

def prod_mod(la, mod):
  x = 1
  for a in la:
    x *= a
    x %= mod
  return x

def main():
  n, k = map(int, input().strip().split())
  la = numpy.array(list(map(int, input().strip().split())))
  la = la[numpy.argsort(numpy.abs(la))][::-1]
  if n == k:
    print(prod_mod(la, MAX))
  else:
    if k % 2 == 1 and (la >= 0).sum() == 0:
      print(prod_mod(la[-k:], MAX))
    else:
      lap = [a for a in la if a >= 0] + [0, 0]
      lam = [a for a in la if a < 0] + [0, 0]
      cp, cm = 0, 0
      ans = 1
      while k > (cp + cm):
        if numpy.prod(lap[cp:cp+2]) > numpy.prod(lam[cm:cm+2]) or k - 1 == (cp + cm):
          ans *= lap[cp]
          cp += 1
        else:
          ans *= numpy.prod(lam[cm:cm+2])
          cm += 2
        ans %= MAX
      print(ans % MAX)
          

if __name__=='__main__':
  main()