Problem p02616 (python)

Fixed submission:
import functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1

def multiply(x,y):
    ans = x*y
    if ans>=10**9+7 or ans<0:
        ans %= (10**9+7)
    return ans

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

if (A[-1]<0 and K%2==1):
    combi = A[-K:]
    if K >1:
        answer =  functools.reduce(multiply,combi)

elif K == N:
    answer = functools.reduce(multiply,A)

else:
    signs = 1
    i,j = 0,0
    for _ in range(K):
        if abs(A[i]) > abs(A[N-j-1]):
            signs *= sign(A[i])
            i += 1

        else:
            signs *= sign(A[N-j-1])
            j += 1
    combi = A[N-j:] + A[:i]
    del A[N-j:]
    del A[:i]
    combi = combi[::-1]

    if signs == -1:
        if A:
            if combi[-1]<0:
                del combi[0]
                combi.append(A[-1])
            elif A[0]>=0:
                del combi[0]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[0]*A[0] > combi[-1]*A[-1]:
                    del combi[-1]
                    combi.insert(0,A[0])
                else:
                    del combi[0]
                    combi.append(A[-1])
            elif A[-1] <= 0:
                del combi[-1]
                combi.insert(0,A[0])

    answer =  functools.reduce(multiply,combi)
print(answer)

Buggy submission (verdict: Runtime Error):
import functools

def multiply(x, y):
  mod = pow(10,9) + 7
  ans = x * y
  if ans >= mod or ans < 0:
    ans %= mod
  return ans

n, k = map(int, input().split())
num = list(map(int, input().split()))
sort_num = sorted(num, reverse=True)
abssort_num = sorted(num, key = abs, reverse=True)
minusNum = 0
cnt = 0
answer = 1
mod = pow(10,9) + 7
flg = -1
minusCnt = 0
delFlg = 0
if (sort_num[0] < 0) and (k % 2 == 1):
  combi = abssort_num[-k:]
  answer = functools.reduce(multiply, combi)
elif k == n:
  answer = functools.reduce(multiply, num)
else:
  for i in range(n):
    if abssort_num[i] < 0:
      minusCnt += 1
      flg = i
  if minusCnt != 0:
    if (minusCnt % 2) == 1:
      del abssort_num[flg]
      n -= 1
  for i in range(n):
    if cnt == k:
      break
    if cnt == k - 1:
      if minusNum % 2 == 0:
        if abssort_num[i] > 0:
          answer *= abssort_num[i]
          answer %= mod
          break
        else:
          continue
      else:
        if abssort_num[i] < 0:
          answer *= abssort_num[i]
          answer %= mod
          break
        else:
          continue
    else:
      answer *= abssort_num[i]
      answer %= mod
      cnt += 1
      if abssort_num[i] < 0:
        minusNum += 1
  delNum = []
  for i in range(n):
      if abssort_num[i] < 0:
          delNum.append(i)
          n -= 1
  for i in delNum:
      del abssort_num[i]
  if n >= k:
      combi = abssort_num[:k]
      if functools.reduce(multiply, combi) > answer:
          answer = functools.reduce(multiply, combi)
print(answer)

Buggy submission (verdict: Runtime Error):
import functools

def multiply(x, y):
  mod = pow(10,9) + 7
  ans = x * y
  if ans >= mod or ans < 0:
    ans %= mod
  return ans

n, k = map(int, input().split())
num = list(map(int, input().split()))
sort_num = sorted(num, reverse=True)
abssort_num = sorted(num, key = abs, reverse=True)
minusNum = 0
cnt = 0
answer = 1
mod = pow(10,9) + 7
flg = -1
minusCnt = 0
if (sort_num[0] < 0) and (k % 2 == 1):
  combi = abssort_num[-k:]
  answer = functools.reduce(multiply, combi)
elif k == n:
  answer = functools.reduce(multiply, num)
else:
  for i in range(n):
    if abssort_num[i] < 0:
      minusCnt += 1
      flg = i
  if minusCnt != 0:
    if (minusCnt % 2) == 1:
      del abssort_num[flg]
  for i in range(n):
    if cnt == k:
      break
    if cnt == k - 1:
      if minusNum % 2 == 0:
        if abssort_num[i] > 0:
          answer *= abssort_num[i]
          answer %= mod
          cnt += 1
        else:
          continue
      else:
        if abssort_num[i] < 0:
          answer *= abssort_num[i]
          answer %= mod
          cnt += 1
        else:
          continue
    else:
      answer *= abssort_num[i]
      answer %= mod
      cnt += 1
      if abssort_num[i] < 0:
        minusNum += 1
print(answer)

Buggy submission (verdict: Runtime Error):
import functools

def multiply(x, y):
  mod = pow(10,9) + 7
  ans = x * y
  if ans >= mod or ans < 0:
    ans %= mod
  return ans

n, k = map(int, input().split())
num = list(map(int, input().split()))
sort_num = sorted(num, reverse=True)
abssort_num = sorted(num, key = abs, reverse=True)
minusNum = 0
cnt = 0
answer = 1
mod = pow(10,9) + 7
flg = -1
minusCnt = 0
if (sort_num[0] < 0) and (k % 2 == 1):
  combi = abssort_num[-k:]
  answer = functools.reduce(multiply, combi)
elif k == n:
  answer = functools.reduce(multiply, num)
else:
  for i in range(n):
    if abssort_num[i] < 0:
      minusCnt += 1
      flg = i
  if (minusCnt % 2) == 1:
    del abssort_num[flg]
  for i in range(n):
    if cnt == k:
      break
    if cnt == k - 1:
      if minusNum % 2 == 0:
        if abssort_num[i] > 0:
          answer *= abssort_num[i]
          answer %= mod
          cnt += 1
        else:
          continue
      else:
        if abssort_num[i] < 0:
          answer *= abssort_num[i]
          answer %= mod
          cnt += 1
        else:
          continue
    else:
      answer *= abssort_num[i]
      answer %= mod
      cnt += 1
      if abssort_num[i] < 0:
        minusNum += 1
print(answer)