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:
import time
t0 = time.time()
def wait(s):
    c = 0
    while time.time() - t0 < s:
        c += 1

N, K = map(int, input().split())
A = [(int(x), abs(int(x))) for x in input().split()]

MOD = 10 ** 9 + 7

if K == 1:
    A.sort(key=lambda x: -x[0])
    ans = A[0][0] % MOD
    wait(0.9)
else:
    A.sort(key=lambda x: -x[1])

    # A

    ans = 1
    pos = 1
    for i in range(K-2):
#         print(ans)
        ans = (ans * A[i][1]) % MOD
        if A[i][0] > 0:
            pass
        elif A[i][0] < 0:
            pos *= -1
        else:
            pos = 0

    # ans, pos

    if pos == 0:
        ans = 0
        wait(1)
    elif pos > 0:
        pp_c = 0
        pp = 1
        mm_c = 0
        mm = 1
        for i in range(K-2, N):
            if pp_c < 2 and A[i][0] > 0:
                pp *= A[i][0]
                pp_c += 1
            if mm_c < 2 and A[i][0] < 0:
                mm *= A[i][0]
                mm_c += 1
            if pp_c >= 2 and mm_c >= 2:
                break
        if pp_c == mm_c == 2:
            ans = (ans * max(pp, mm)) % MOD
            wait(1.1)
        elif pp_c == 2:
            ans = (ans * pp) % MOD
            wait(1.2)
        elif mm_c == 2:
            ans = (ans * mm) % MOD
            wait(1.3)
        else:
            if A[-1][0] == 0:
                ans = 0
                wait(1.4)
            else:
                ans = (ans * pp * mm) % MOD
                wait(1.5)
    elif pos < 0:
        pp_c = 0
        pp = 1
        mm_c = 0
        mm = 1
        for i in range(K-2, N):
            if pp_c == 0 and A[i][0] > 0:
                pp *= A[i][1]
                pp_c += 1
            if mm_c == 0 and A[i][0] < 0:
                mm *= A[i][1]
                mm_c += 1
            if pp_c == 1 and mm_c == 1:
                break
        if pp_c == mm_c == 1:
            ans = (ans * pp % MOD) * mm % MOD
            wait(1.6)
        else:
            ans = 1
            count = 0
            for i in range(N-1, -1, -1):
                if A[i][0] == 0: continue
                ans = (ans * A[i][0]) % MOD
                count += 1
                if count == K:
                    break
            if count < K:
                ans = 0
                wait(1.7)
            else:
                wait(1.8)
print(ans)

Submission status: Wrong Answer

Solution code:
import time
t0 = time.time()
def wait(s):
    c = 0
    while time.time() - t0 < s:
        c += 1

N, K = map(int, input().split())
A = [(int(x), abs(int(x))) for x in input().split()]

MOD = 10 ** 9 + 7

if K == 1:
    A.sort(key=lambda x: -x[0])
    ans = A[0][0] % MOD
    wait(0.9)
else:
    A.sort(key=lambda x: -x[1])

    # A

    ans = 1
    pos = 1
    for i in range(K-2):
#         print(ans)
        ans = (ans * A[i][1]) % MOD
        if A[i][0] > 0:
            pass
        elif A[i][0] < 0:
            pos *= -1
        else:
            pos = 0

    # ans, pos

    if pos == 0:
        ans = 0
        wait(1)
    elif pos > 0:
        pp_c = 0
        pp = 1
        mm_c = 0
        mm = 1
        for i in range(K-2, N):
            if pp_c < 2 and A[i][0] > 0:
                pp *= A[i][0]
                pp_c += 1
            if mm_c < 2 and A[i][0] < 0:
                mm *= A[i][0]
                mm_c += 1
            if pp_c >= 2 and mm_c >= 2:
                break
        if pp_c == mm_c == 2:
            ans = (ans * max(pp, mm)) % MOD
            wait(1.1)
        elif pp_c == 2:
            ans = (ans * pp) % MOD
            wait(1.2)
        elif mm_c == 2:
            ans = (ans * mm) % MOD
            wait(1.3)
        else:
            if A[-1][0] == 0:
                ans = 0
                wait(1.4)
            else:
                ans = (ans * pp * mm) % MOD
                wait(1.5)
    elif pos < 0:
        pp_c = 0
        pp = 1
        mm_c = 0
        mm = 1
        for i in range(K-2, N):
            if pp_c == 0 and A[i][0] > 0:
                pp *= A[i][1]
                pp_c += 1
            if mm_c == 0 and A[i][0] < 0:
                mm *= A[i][1]
                mm_c += 1
            if pp_c == 1 and mm_c == 1:
                break
        if pp_c == mm_c == 1:
            ans = (ans * pp * mm) % MOD
            wait(1.6)
        else:
            ans = 1
            count = 0
            for i in range(N-1, -1, -1):
                if A[i][0] == 0: continue
                ans = (ans * A[i][0]) % MOD
                count += 1
                if count == K:
                    break
            if count < K:
                ans = 0
                wait(1.7)
print(ans)

Submission status: Wrong Answer

Solution code:
import time
t0 = time.time()
def wait(s):
    c = 0
    while time.time() - t0 < s:
        c += 1

N, K = map(int, input().split())
A = [(int(x), abs(int(x))) for x in input().split()]

MOD = 10 ** 9 + 7

if K == 1:
    A.sort(key=lambda x: -x[0])
    ans = A[0][0] % MOD
    wait(0.9)
else:
    A.sort(key=lambda x: -x[1])

    # A

    ans = 1
    pos = 1
    for i in range(K-2):
#         print(ans)
        ans = (ans * A[i][1]) % MOD
        if A[i][0] > 0:
            pass
        elif A[i][0] < 0:
            pos *= -1
        else:
            pos = 0

    # ans, pos

    if pos == 0:
        ans = 0
        wait(1)
    elif pos > 0:
        pp_c = 0
        pp = 1
        mm_c = 0
        mm = 1
        for i in range(K-2, N):
            if pp_c < 2 and A[i][0] > 0:
                pp *= A[i][0]
                pp_c += 1
            if mm_c < 2 and A[i][0] < 0:
                mm *= A[i][0]
                mm_c += 1
            if pp_c >= 2 and mm_c >= 2:
                break
        if pp_c == mm_c == 2:
            ans = (ans * max(pp, mm)) % MOD
            wait(1.1)
        elif pp_c == 2:
            ans = (ans * pp) % MOD
            wait(1.2)
        elif mm_c == 2:
            ans = (ans * mm) % MOD
            wait(1.3)
        else:
            if A[-1][0] == 0:
                ans = 0
                wait(1.4)
            else:
                ans = (ans * pp * mm) % MOD
                wait(1.5)
    elif pos < 0:
        pp_c = 0
        pp = 1
        mm_c = 0
        mm = 1
        for i in range(K-2, N):
            if pp_c == 0 and A[i][0] > 0:
                pp *= A[i][0]
                pp_c += 1
            if mm_c == 0 and A[i][0] < 0:
                mm *= A[i][0]
                mm_c += 1
            if pp_c == 1 and mm_c == 1:
                break
        if pp_c == mm_c == 1:
            ans = (ans * pp * mm) % MOD
            wait(1.6)
        else:
            ans = 1
            count = 0
            for i in range(N-1, -1, -1):
                if A[i][0] == 0: continue
                ans = (ans * A[i][0]) % MOD
                count += 1
                if count == K:
                    break
            if count < K:
                ans = 0
                wait(1.7)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [(int(x), abs(int(x))) for x in input().split()]

MOD = 10 ** 9 + 7

if K == 1:
    A.sort(key=lambda x: -x[0])
    ans = A[0][0] % MOD
else:
    A.sort(key=lambda x: -x[1])

    # A

    ans = 1
    pos = 1
    for i in range(K-2):
#         print(ans)
        ans = (ans * A[i][1]) % MOD
        if A[i][0] > 0:
            pass
        elif A[i][0] < 0:
            pos *= -1
        else:
            pos = 0

    # ans, pos

    if pos == 0:
        ans = 0
    elif pos > 0:
        pp_c = 0
        pp = 1
        mm_c = 0
        mm = 1
        for i in range(K-2, N):
            if pp_c < 2 and A[i][0] > 0:
                pp *= A[i][0]
                pp_c += 1
            if mm_c < 2 and A[i][0] < 0:
                mm *= A[i][0]
                mm_c += 1
            if pp_c >= 2 and mm_c >= 2:
                break
        if pp_c == mm_c == 2:
            ans = (ans * max(pp, mm)) % MOD
        elif pp_c == 2:
            ans = (ans * pp) % MOD
        elif mm_c == 2:
            ans = (ans * mm) % MOD
        else:
            if A[-1][0] == 0:
                ans = 0
            else:
                ans = (ans * pp * mm) % MOD
    elif pos < 0:
        pp_c = 0
        pp = 1
        mm_c = 0
        mm = 1
        for i in range(K-2, N):
            if pp_c == 0 and A[i][0] > 0:
                pp *= A[i][0]
                pp_c += 1
            if mm_c == 0 and A[i][0] < 0:
                mm *= A[i][0]
                mm_c += 1
            if pp_c == 1 and mm_c == 1:
                break
        if pp_c == mm_c == 1:
            ans = (ans * pp * mm) % MOD
        else:
            ans = 1
            count = 0
            for i in range(N-1, -1, -1):
                if A[i][0] == 0: continue
                ans = (ans * A[i][0]) % MOD
                count += 1
                if count == K:
                    break
            if count < K:
                ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [(int(x), abs(int(x))) for x in input().split()]

MOD = 10 ** 9 + 7


if K == 1:
    A.sort(key=lambda x: -x[0])
    ans = A[0][0] % MOD
else:
    A.sort(key=lambda x: -x[1])

    # A

    ans = 1
    pos = 1
    for i in range(K-2):
        print(ans)
        ans = (ans * A[i][1]) % MOD
        if A[i][0] > 0:
            pass
        elif A[i][0] < 0:
            pos *= -1
        else:
            pos = 0

    # ans, pos

    if pos == 0:
        ans = 0
    elif pos > 0:
        pp_c = 0
        pp = 1
        mm_c = 0
        mm = 1
        for i in range(K-2, N):
            if pp_c < 2 and A[i][0] > 0:
                pp *= A[i][0]
                pp_c += 1
            if mm_c < 2 and A[i][0] < 0:
                mm *= A[i][0]
                mm_c += 1
            if pp_c >= 2 and mm_c >= 2:
                break
        if pp_c == mm_c == 2:
            ans = (ans * max(pp, mm)) % MOD
        elif pp_c == 2:
            ans = (ans * pp) % MOD
        elif mm_c == 2:
            ans = (ans * mm) % MOD
        else:
            if A[-1][0] == 0:
                ans = 0
            else:
                ans = (ans * pp * mm) % MOD
    elif pos < 0:
        pp_c = 0
        pp = 1
        mm_c = 0
        mm = 1
        for i in range(K-2, N):
            if pp_c == 0 and A[i][0] > 0:
                pp *= A[i][0]
                pp_c += 1
            if mm_c == 0 and A[i][0] < 0:
                mm *= A[i][0]
                mm_c += 1
            if pp_c == 1 and mm_c == 1:
                break
        if pp_c == mm_c == 1:
            ans = (ans * pp * mm) % MOD
        else:
            ans = 1
            count = 0
            for i in range(N-1, -1, -1):
                if A[i][0] == 0: continue
                ans = (ans * A[i][0]) % MOD
                count += 1
                if count == K:
                    break
            if count < K:
                ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7

N, K = map(int, input().split())
A = list(map(int, input().split()))

neg = []
zero = 0
pos = []
for a in A:
    if a < 0:
        neg.append(a)
    else:
        pos.append(a)

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

if len(pos) == 0:
    res = 1
    if K % 2 == 0:
        neg.reverse()
    for a in neg[:K]:
        res *= a
        res %= mod
else:
    res = 1
    if K % 2 == 1:
        res = pos.pop()
        K -= 1
    while K > 1 and len(pos) > 1 and len(neg) > 1:
        if pos[-1] * pos[-2] > neg[-1] * neg[-2]:
            res *= pos.pop()
            res %= mod
            res *= pos.pop()
            res %= mod
        else:
            res *= neg.pop()
            res %= mod
            res *= neg.pop()
            res %= mod
        K -= 2
    while K > 1 and len(pos) > 1:
        res *= pos.pop()
        res %= mod
        res *= pos.pop()
        res %= mod
        K -= 2
    while K > 1 and len(neg) > 1:
        res *= neg.pop()
        res %= mod
        res *= neg.pop()
        res %= mod
        K -= 2

print(res)

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7

N, K = map(int, input().split())
A = list(map(int, input().split()))

neg = []
zero = 0
pos = []
for a in A:
    if a < 0:
        neg.append(a)
    else:
        pos.append(a)

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

if len(pos) == 0:
    res = 1
    if K % 2 == 0:
        neg.reverse()
    for a in neg[:K]:
        res *= a
        res %= mod
else:
    res = 1
    while K > 1 and len(pos) > 1 and len(neg) > 1:
        if pos[-1] * pos[-2] > neg[-1] * neg[-2]:
            res *= pos.pop()
            res %= mod
            res *= pos.pop()
            res %= mod
        else:
            res *= neg.pop()
            res %= mod
            res *= neg.pop()
            res %= mod
        K -= 2
    while K > 1 and len(pos) > 1:
        res *= pos.pop()
        res %= mod
        res *= pos.pop()
        res %= mod
        K -= 2
    while K > 1 and len(neg) > 1:
        res *= neg.pop()
        res %= mod
        res *= neg.pop()
        res %= mod
        K -= 2
    while K and pos:
        res *= pos.pop()
        res %= mod
        K -= 1
    while K and neg:
        res *= neg.pop()
        res %= mod
        K -= 1


print(res)

Submission status: Wrong Answer

Solution code:
mod = 10 ** 9 + 7

N, K = map(int, input().split())
A = list(map(int, input().split()))

neg = []
zero = 0
pos = []
for a in A:
    if a < 0:
        neg.append(a)
    else:
        pos.append(a)

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

if len(pos) == 0:
    res = 1
    A = A[:K] if K % 2 == 1 else A[-K:]
    for a in A:
        res *= a
        res %= mod
else:
    res = 1
    while K > 1 and len(pos) > 1 and len(neg) > 1:
        if pos[-1] * pos[-2] > neg[-1] * neg[-2]:
            res *= pos.pop()
            res %= mod
            res *= pos.pop()
            res %= mod
        else:
            res *= neg.pop()
            res %= mod
            res *= neg.pop()
            res %= mod
        K -= 2
    while K > 1 and len(pos) > 1:
        res *= pos.pop()
        res %= mod
        res *= pos.pop()
        res %= mod
        K -= 2
    while K > 1 and len(neg) > 1:
        res *= neg.pop()
        res %= mod
        res *= neg.pop()
        res %= mod
        K -= 2
    while K and pos:
        res *= pos.pop()
        res %= mod
        K -= 1
    while K and neg:
        res *= neg.pop()
        res %= mod
        K -= 1


print(res)

Submission status: Wrong Answer

Solution code:
#coding:utf-8

N,K = map(int,input().split())

A = [int(y) for y in input().split()]

A.sort(reverse = True)

count = 0
ans = 1
flag = True

if K%2 == 1 and A[0] <= 0:
    for i in range(K):
        ans = ans * A[i]
        ans = ans % (10**9 + 7)
    flag = False

while count < K and flag:
    if count == (K - 1):
        ans = ans * A[0]
        del A[0]
        count += 1
    else:
        right = A[0] * A[1]
        left = A[-1] * A[-2]
        if right >= left:
            ans = ans * A[0]
            del A[0]
            count += 1
        else:
            ans = ans * left
            del A[-1]
            del A[-2]
            count += 2

    ans = ans % (10**9 + 7)

print("{}".format(ans))

Submission status: Wrong Answer

Solution code:
suus,cleck = map(int,input().split()); cuc = 1
fef = sorted(map(int,input().split()), reverse = True)
for joj in range(cleck):
  cuc = (cuc * fef[joj])%1000000007
print(cuc)

Submission status: Wrong Answer

Solution code:
a, b = map(int,input().split())
li = list(map(int,input().split()))

xx = 10**9+7
li = sorted(li,key = abs)

def mut(lis):
    res = 1
    for i in lis:
        res*=i
    return res

if all([i<0 for i in li]):
    res = 1
    for i in li[:b]:
        res *= i
    print (int(res%xx))
else:
    while mut(li[-b:])<0:
        for i in li[-b:]:
            if i < 0:
                li.pop(li.index(i))
                break
    print(int(mut(li[-b:])%xx))

Submission status: Wrong Answer

Solution code:
a, b = map(int,input().split())
li = list(map(int,input().split()))

li = sorted(li,key = abs)

def mut(lis):
    res = 1
    for i in lis:
        res*=i
    return res

if all([i<0 for i in li]):
    res = 1
    for i in li[-b:]:
        res *= i
    print(res)
else:
    while mut(li[-b:])<0:
        for i in li[-b:]:
            if i < 0:
                li.pop(li.index(i))
                break
    print(mut(li[-b:]))

Submission status: Wrong Answer

Solution code:
INF = 10**9 + 7
N, K = map(int, input().split())
A = list(map(int, input().split()))
plus_lst, minus_lst = [], []
plus, minus = 0, 0
ans = 1
if K == N:
  for i in A:
    ans = (ans * i) % INF
  print(ans)
else:
  for _ in range(N):
    a = A.pop()
    if a >= 0:
      plus_lst += [a]
      # plus += 1
    else:
      minus_lst += [-a]
      # minus += 1
  if K % 2 and not plus_lst:
    minus_lst = sorted(minus_lst)
    for i in range(K):
      ans = (ans * minus_lst[i]) % INF
    ans *= -1
    ans %= INF
    print(ans)
  else:
    plus_lst = sorted(plus_lst, reverse=True)
    minus_lst = sorted(minus_lst, reverse=True)
    plus_cnt = len(plus_lst)
    minus_cnt = len(minus_lst)
    x, y = 0, 0
    a = plus_lst[x] if x < plus_cnt else 0
    b = plus_lst[x+1] if x < plus_cnt-1 else 0
    c = minus_lst[y] if y < minus_cnt else 0
    d = minus_lst[y+1] if y < minus_cnt-1 else 0
    for i in range(K//2):
      plus_num = a * b
      minus_num = c * d
      if plus_num > minus_num:
        ans = (ans * plus_num) % INF
        x += 2
        a = plus_lst[x] if x < plus_cnt else 0
        b = plus_lst[x+1] if x < plus_cnt-1 else 0
      else:
        ans = (ans * minus_num) % INF
        y += 2
        c = minus_lst[y] if y < minus_cnt else 0
        d = minus_lst[y+1] if y < minus_cnt-1 else 0
    if K % 2 == 1:
      ans = (ans * a) % INF
    print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int, input().split())
A=list(map(int, input().split()))
D,E=[],[]
zcnt,scnt,fcnt=0,0,0
for i in A:
  if i==0:
    zcnt+=1
    D.append(0)
  elif i>0:
    D.append(i)
    scnt+=1
  else:
    E.append(i)
    fcnt+=1
mod=10**9+7
ans=1
#全て使う場合は問答無用でかける
if K==N:
  for i in A:
    ans*=i
    ans%=mod
  print(ans)
  exit()
#マイナスを奇数個絶対かける→確実に答えはマイナス
if K%2==1 and max(A)<0:
  A=sorted(A)[::-1]
  for i in range(K):
    ans*=A[i]
    ans%=mod
  print(ans)
  exit()
#絶対0をかけなきゃいけない
if K>scnt+fcnt:
  print(0)
  exit()
D,E=sorted(D)[::-1],sorted(E)
#print(D,E)
ans=1
cnt=0
a,b=0,0
while K-cnt>1:
  if a+1<=len(D)-1 and b+1<=len(E)-1:
    d,e=D[a]*D[a+1],E[b]*E[b+1]
    if d>e:
      ans*=D[a]
      a+=1
      cnt+=1
      ans%=mod
    else:
      ans*=e
      b+=2    
      ans%=mod
      cnt+=2
  elif a+1<=len(D)-1:
    d=D[a]*D[a+1]
    ans*=D[a]
    a+=1
    cnt+=1
    ans%=mod
  elif b+1<=len(E)-1:
    e=E[b]*E[b+1]
    ans*=e
    b+=2
    cnt+=2
    ans%=mod

if K%2==1:
  Z=[]
  if a!=scnt:
    Z.append(D[a])
  if b!=fcnt:
    Z.append(E[-1])
  if 0 in A:
    Z.append(0)
  ans*=max(Z)
  ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int, input().split())
A=list(map(int, input().split()))
D,E=[],[]
zcnt,scnt,fcnt=0,0,0
for i in A:
  if i==0:
    zcnt+=1
    D.append(0)
  elif i>0:
    D.append(i)
    scnt+=1
  else:
    E.append(i)
    fcnt+=1
mod=10**9+7
ans=1
#全て使う場合は問答無用でかける
if K==N:
  for i in A:
    ans*=i
    ans%=mod
  print(ans)
  exit()
#マイナスを奇数個絶対かける→確実に答えはマイナス
if K%2==1 and max(A)<0:
  A=sorted(A)[::-1]
  for i in range(K):
    ans*=A[i]
    ans%=mod
  print(ans)
  exit()
#絶対0をかけなきゃいけない
if K>scnt+fcnt:
  print(0)
  exit()
D,E=sorted(D)[::-1],sorted(E)
#print(D,E)
ans=1
a,b=0,0
for i in range(K//2):
  if a+1<=len(D)-1 and b+1<=len(E)-1:
    d,e=D[a]*D[a+1],E[b]*E[b+1]
    if d>e:
      ans*=d
      a+=2
      ans%=mod
    else:
      ans*=e
      b+=2    
      ans%=mod
  elif a+1<=len(D)-1:
    d=D[a]*D[a+1]
    ans*=d
    a+=2
    ans%=mod
  elif b+1<=len(E)-1:
    e=E[b]*E[b+1]
    ans*=e
    b+=2
    ans%=mod

if K%2==1:
  Z=[]
  if a!=scnt:
    Z.append(D[a])
  if b!=fcnt:
    Z.append(E[-1])
  if 0 in A:
    Z.append(0)
  ans*=max(Z)
  ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
import datetime
import string
import re
import math

def isort(list, _beg, _end):
    if _beg < _end:
        beg = _beg
        end = _end
        pivot = abs(list[int((beg + end)/2)])
        while(True):
            while abs(list[beg]) < pivot: beg += 1
            while abs(list[end]) > pivot: end -= 1
            if beg <= end:
                tmp = list[beg]
                list[beg] = list[end]
                list[end] = tmp
                beg += 1
                end -= 1
            else:
                break
        isort(list, _beg, end)
        isort(list, beg, _end)

def searchByBool(list):
    for i in range(len(list)):
        if check(list[i]): return i
    return -1

def check(num):
    if num >= 0: return True
    else: return False

sp = list(map(int, input().split()))
N = sp[0]
K = sp[1]
alist = list(map(int, input().split()))
klist = []
isK = True

isort(alist, 0, len(alist) - 1)
alist.reverse()

numMinus = 0
for i in range(K):
    if check(alist[i]) == False: numMinus += 1

# select biggest K numbers in terms of absolute value

# for all chosen numbers
# if the number of negative number is even
if numMinus % 2 == 0:
    isK = False

# if odd
# -> choose the smallest number, and search opposite number
if isK and K < N:
    _right = -1
    _left = -1
    for right in range(K,N):
        if check(alist[K-1]) != check(alist[right]):
            _right = right
            break
    for left in range(K):
        if check(alist[K-1-left]) != check(alist[K]):
            _left = left
            break
    isK = False
    if _right != -1 and _left != -1:
        if abs(alist[K-1-left]*alist[_right]) < abs(alist[K-1]*alist[K]):
            alist[K-1-_left] = alist[K]
        else:
            alist[K-1] = alist[_right]
    elif _right == 0 and _left != 0:
        alist[K-1-_left] = alist[K]
    elif _right != 0 and _left == 0:
        alist[K-1] = alist[_right]
    else:
        isK = True

if isK:
    alist.reverse()
klist = alist[0:K]

multi = klist[0]
for i in range(1, K):
    multi *= klist[i]
    multi = multi % 1000000007

print(multi)

Submission status: Wrong Answer

Solution code:
import datetime
import string
import re
import math

def isort(list, _beg, _end):
    if _beg < _end:
        beg = _beg
        end = _end
        pivot = abs(list[int((beg + end)/2)])
        while(True):
            while abs(list[beg]) < pivot: beg += 1
            while abs(list[end]) > pivot: end -= 1
            if beg <= end:
                tmp = list[beg]
                list[beg] = list[end]
                list[end] = tmp
                beg += 1
                end -= 1
            else:
                break
        isort(list, _beg, end)
        isort(list, beg, _end)

def searchByBool(list):
    for i in range(len(list)):
        if check(list[i]): return i
    return -1

def check(num):
    if num >= 0: return True
    else: return False

sp = list(map(int, input().split()))
N = sp[0]
K = sp[1]
alist = list(map(int, input().split()))
klist = []
isK = True

isort(alist, 0, len(alist) - 1)
alist.reverse()

numMinus = 0
for i in range(K):
    if check(alist[i]) == False: numMinus += 1

# select biggest K numbers in terms of absolute value

# for all chosen numbers
# if the number of negative number is even
if numMinus % 2 == 0:
    isK = False

# if odd
# -> choose the smallest number, and search opposite number
if isK and K < N:
    _right = -1
    _left = -1
    for right in range(K,N):
        if check(alist[K-1]) != check(alist[right]):
            _right = right
            break
    for left in range(K):
        if check(alist[K-1-left]) != check(alist[K]):
            _left = left
            break
    isK = False
    if _right != -1 and _left != -1:
        if abs(alist[K-1-left]*alist[_right]) < abs(alist[K-1]*alist[K]):
            alist[K-1-_left] = alist[K]
        else:
            alist[K-1] = alist[_right]
    elif _right == 0 and _left != 0:
        alist[K-1-_left] = alist[K]
    elif _right != 0 and _left == 0:
        alist[K-1] = alist[_right]
    else:
        isK = False

if isK:
    alist.reverse()
klist = alist[0:K]

multi = klist[0]
for i in range(1, K):
    multi *= klist[i]
    multi = multi % 1000000007

print(multi)

Submission status: Wrong Answer

Solution code:
import datetime
import string
import re
import math

def isort(list, _beg, _end):
    if _beg < _end:
        beg = _beg
        end = _end
        pivot = abs(list[int((beg + end)/2)])
        while(True):
            while abs(list[beg]) < pivot: beg += 1
            while abs(list[end]) > pivot: end -= 1
            if beg <= end:
                tmp = list[beg]
                list[beg] = list[end]
                list[end] = tmp
                beg += 1
                end -= 1
            else:
                break
        isort(list, _beg, end)
        isort(list, beg, _end)

def searchByBool(list):
    for i in range(len(list)):
        if check(list[i]): return i
    return -1

def check(num):
    if num >= 0: return True
    else: return False

sp = list(map(int, input().split()))
N = sp[0]
K = sp[1]
alist = list(map(int, input().split()))
klist = []
isK = True

isort(alist, 0, len(alist) - 1)
alist.reverse()

numMinus = 0
for i in range(K):
    if check(alist[i]) == False: numMinus += 1

# select biggest K numbers in terms of absolute value

# for all chosen numbers
# if the number of negative number is even
if numMinus % 2 == 0:
    klist = alist[0:K]
    isK = False

# if odd
# -> choose the smallest number, and search opposite number
if isK and K < N:
    _right = -1
    for right in range(K,N):
        if check(alist[K-1]) != check(alist[right]):
            isK = False
            _right = right
            klist = alist[0:K]
            klist[K-1]=alist[_right]
            break
    for left in range(K):
        if check(alist[K-1-left]) != check(alist[K]):
            if _right == -1:
                alist[K-1-left] = alist[K]
            elif abs(alist[K-1-left]*alist[_right]) < abs(alist[K-1]*alist[K]):
                alist[K-1-left] = alist[K]
            klist = alist[0:K]
            isK = False
            break
if isK:
    alist.reverse()
    klist = alist[0:K]

multi = klist[0]
for i in range(1, K):
    multi *= klist[i]
    multi = multi % 1000000007

print(multi)

Submission status: Wrong Answer

Solution code:
import datetime
import string
import re
import math

def isort(list, _beg, _end):
    if _beg < _end:
        beg = _beg
        end = _end
        pivot = abs(list[int((beg + end)/2)])
        while(True):
            while abs(list[beg]) < pivot: beg += 1
            while abs(list[end]) > pivot: end -= 1
            if beg <= end:
                tmp = list[beg]
                list[beg] = list[end]
                list[end] = tmp
                beg += 1
                end -= 1
            else:
                break
        isort(list, _beg, end)
        isort(list, beg, _end)

def searchByBool(list):
    for i in range(len(list)):
        if check(list[i]): return i
    return -1

def check(num):
    if num >= 0: return True
    else: return False

sp = list(map(int, input().split()))
N = sp[0]
K = sp[1]
alist = list(map(int, input().split()))
klist = []
isK = True

isort(alist, 0, len(alist) - 1)
alist.reverse()

numMinus = 0
for i in range(K):
    if check(alist[i]) == False: numMinus += 1

# select biggest K numbers in terms of absolute value

# for all chosen numbers
# if the number of negative number is even
if numMinus % 2 == 0:
    klist = alist[0:K]
    isK = False

# if odd
# -> choose the smallest number, and search opposite number
if isK and K < N:
    _right = -1
    for right in range(K,N):
        if check(alist[K-1]) != check(alist[right]):
            isK = False
            _right = right
            klist = alist[0:K]
            klist[K-1]=alist[_right]
            break
    for left in range(K):
        if check(alist[K-1-left]) != check(alist[K]):
            if _right == -1:
                alist[K-1-left] = alist[K]
            # elif abs(alist[K-1-left])/abs(alist[K]) < abs(alist[K-1])/abs(alist[_right]):
            #     alist[K-1-left] = alist[K]
            klist = alist[0:K]
            isK = False
            break
if isK:
    alist.reverse()
    klist = alist[0:K]

multi = klist[0]
for i in range(1, K):
    multi *= klist[i]
    multi = multi % 1000000007

print(multi)

Submission status: Wrong Answer

Solution code:
import datetime
import string
import re
import math

def isort(list, _beg, _end):
    if _beg < _end:
        beg = _beg
        end = _end
        pivot = abs(list[int((beg + end)/2)])
        while(True):
            while abs(list[beg]) < pivot: beg += 1
            while abs(list[end]) > pivot: end -= 1
            if beg <= end:
                tmp = list[beg]
                list[beg] = list[end]
                list[end] = tmp
                beg += 1
                end -= 1
            else:
                break
        isort(list, _beg, end)
        isort(list, beg, _end)

def searchByBool(list):
    for i in range(len(list)):
        if check(list[i]): return i
    return -1

def check(num):
    if num >= 0: return True
    else: return False

sp = list(map(int, input().split()))
N = sp[0]
K = sp[1]
alist = list(map(int, input().split()))
klist = []
isK = True

isort(alist, 0, len(alist) - 1)
alist.reverse()

numMinus = 0
for i in range(K):
    if check(alist[i]) == False: numMinus += 1

# select biggest K numbers in terms of absolute value

# for all chosen numbers
# if the number of negative number is even
if numMinus % 2 == 0:
    klist = alist[0:K]
    isK = False

# if odd
# -> choose the smallest number, and search opposite number
if isK:
    for i in range(K,N):
        if check(alist[K-1]) != check(alist[i]):
            alist[K-1]=alist[i]
            klist = alist[0:K]
            isK = False
            break

if isK and K < N:
    for i in range(K-1):
        if check(alist[K-1-i]) != check(alist[K-1]):
            alist[K-1-i] = alist[K]
            klist = alist[0:K]
            isK = False
            break

if isK:
    alist.reverse()
    klist = alist[0:K]

multi = klist[0]
for i in range(1, K):
    multi *= klist[i]
    multi = multi % 1000000007

print(multi)