Problem p02616 (python)

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

A_neg = []
A_pos = []

for i in range(N):
  if A[i] <= 0:
    A_neg.append(-A[i])
  else:
    A_pos.append(A[i])

A_neg.sort(reverse=True)
A_pos.sort(reverse=True)

ans = 1

if K == N:
  for i in range(N):
    ans *= (A[i]%1000000007)
    ans %= 1000000007
  print(ans)

elif K%2==1 and len(A_pos)==0:
  for i in range(1, K+1):
    ans *= ((-A_neg[-i])%1000000007)
    ans %= 1000000007
  print(ans)

else:
  if K%2==1:
    ans *= A_pos.pop(0)
    K -= 1
  buf = []
  for i in range(len(A_pos)//2):
    buf.append(A_pos[2*i]*A_pos[2*i+1])
  for i in range(len(A_neg)//2):
    buf.append(A_neg[2*i]*A_neg[2*i+1])
  buf.sort(reverse=True)
  for i in range(K//2):
    ans *= (buf[i]%1000000007)
    ans %= 1000000007
  print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))

A_neg = []
A_pos = []

for i in range(N):
  if A[i] <= 0:
    A_neg.append(-A[i])
  else:
    A_pos.append(A[i])

A_neg.sort(reverse=True)
A_pos.sort(reverse=True)

ans = 1

if K == N:
  for i in range(N):
    ans *= (A[i]%1000000007)
    ans %= 1000000007
  print(ans)

elif K%2==1 and len(A_pos)==0:
  for i in range(1, K+1):
    ans *= ((-A_neg[-i])%1000000007)
    ans %= 1000000007
  print(ans)

else:
  if K%2==1:
    ans *= A_pos.pop(0)
    K -= 1
  buf = []
  if len(len(A_pos)>=2):
    for i in range(len(A_pos)//2):
      buf.append(A_pos[2*i]*A_pos[2*i+1])
  if len(len(A_pos)>=2):
    for i in range(len(A_neg)//2):
      buf.append(A_neg[2*i]*A_neg[2*i+1])
  buf.sort(reverse=True)
  for i in range(K//2):
    ans *= (buf[i]%1000000007)
    ans %= 1000000007
  print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))

A_neg = []
A_pos = []

for i in range(N):
  if A[i] <= 0:
    A_neg.append(-A[i])
  else:
    A_pos.append(A[i])

A_neg.sort(reverse=True)
A_pos.sort(reverse=True)

ans = 1

if K%2==1 and len(A_pos)==0:
  for i in range(1, K+1):
    ans *= ((-A_neg[-i])%1000000007)
    ans %= 1000000007
  print(ans)

else:
  if K%2==1:
    ans *= A_pos.pop(0)
    K -= 1
  buf = []
  for i in range(len(A_pos)//2):
    buf.append(A_pos[2*i]*A_pos[2*i+1])
  for i in range(len(A_neg)//2):
    buf.append(A_neg[2*i]*A_neg[2*i+1])
  buf.sort(reverse=True)
  for i in range(K//2):
    ans *= (buf[i]%1000000007)
    ans %= 1000000007
  print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))

A_neg = []
A_pos = []

for i in range(N):
  if A[i] <= 0:
    A_neg.append(-A[i])
  else:
    A_pos.append(A[i])

A_neg.sort(reverse=True)
A_pos.sort(reverse=True)

ans = 1

if K%2==1 and len(A_pos)==0:
  for i in range(1, K+1):
    ans *= (A_neg[-i]%1000000007)
    ans %= 1000000007
  print(-ans)

else:
  if K%2==1:
    ans *= A_pos.pop(0)
    K -= 1
  buf = []
  for i in range(len(A_pos)//2):
    buf.append(A_pos[2*i]*A_pos[2*i+1])
  for i in range(len(A_neg)//2):
    buf.append(A_neg[2*i]*A_neg[2*i+1])
  buf.sort(reverse=True)
  for i in range(K//2):
    ans *= (buf[i]%1000000007)
    ans %= 1000000007
  print(ans)

Buggy submission (verdict: Runtime Error):
N, K = int(input())
A = list(map(int, input().split()))

A_neg = []
A_pos = []

for i in range(N):
  if A[i] <= 0:
    A_neg.append(-A[i])
  else:
    A_pos.append(A[i])

A_neg.sort(reverse=True)
A_pos.sort(reverse=True)

ans = 1

if K%2==1 and len(A_pos)==0:
  for i in range(1, K+1):
    ans *= (A_neg[-i]%1000000007)
    ans %= 1000000007
  print(-ans)

else:
  if K%2==1:
    ans *= A_pos.pop(0)
    K -= 1
  buf = []
  for i in range(len(A_pos)//2):
    buf.append(A_pos[2*i]*A_pos[2*i+1])
  for i in range(len(A_neg)//2):
    buf.append(A_neg[2*i]*A_neg[2*i+1])
  buf.sort(reverse=True)
  for i in range(K//2):
    ans *= (buf[i]%1000000007)
    ans %= 1000000007
  print(ans)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
A = sorted(A, key = lambda x:abs(x), reverse=True)
#print(A)
 
ans = []
negidx = 0
posidx = 0
neg = False
for i in range(K):
  a = A[i]
  ans.append(a)
  if a<0:
    negidx = i
    neg = not neg
  else:
    posidx = i
#print(ans, neg, negidx)
answer = 1
if neg:
  poppos = list(ans)
  pg = poppos.pop(posidx)
  popneg = list(ans)
  ng = popneg.pop(negidx)
  pgratio = None
  ngratio = None
  found = 0
  for i in range(K+1, N):
    if A[i]>=0 and ngratio is None:
      found = 1
      popneg.append(A[i])
      ngratio = -A[i]/ng
    if A[i]<0 and pgratio is None:
      found = 2
      poppos.append(A[i])
      pgratio = -A[i]/pg
    if pgratio is not None and npratio is not None:
      found = 3
      break
  if found == 1:
    for a in popnep:
      answer *= abs(a)%1000000007
    print(answer%1000000007)
  elif found == 2:
    for a in poppos:
      answer *= abs(a)%1000000007
    print(answer%1000000007)
  elif found == 3:
    if pgratio > ngratio:
      for a in poppos:
        answer *= abs(a)%1000000007
      print(answer%1000000007)
    else:
      for a in popneg:
        answer *= abs(a)%1000000007
      print(answer%1000000007)
  else:
    for i in range(K):
      answer *= A[-(i+1)]%1000000007
    print(answer%1000000007)
else:
  for a in ans:
    answer *= abs(a)%1000000007
  print(answer%1000000007)

Buggy submission (verdict: Runtime Error):

Copy
Copy
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
A = sorted(A, key = lambda x:abs(x), reverse=True)
#print(A)
 
ans = []
negidx = 0
posidx = 0
neg = False
for i in range(K):
  a = A[i]
  ans.append(a)
  if a<0:
    negidx = i
    neg = not neg
  else:
    posidx = i
#print(ans, neg, negidx)
answer = 1
if neg:
  poppos = list(ans)
  pg = poppos.pop(posidx)
  popneg = list(ans)
  ng = popneg.pop(negidx)
  pgratio = None
  ngratio = None
  found = 0
  for i in range(K+1, N):
    if A[i]>=0 and ngratio is None:
      found = 1
      popneg.append(A[i])
      ngratio = -A[i]/ng
    if A[i]<0 and pgratio is None:
      found = 2
      poppos.append(A[i])
      pgratio = -A[i]/pg
    if pgratio is not None and npratio is not None:
      found = 3
      break
  if found == 1:
    for a in popnep:
      answer *= abs(a)%1000000007
    print(answer%1000000007)
  elif found == 2:
    for a in poppos:
      answer *= abs(a)%1000000007
    print(answer%1000000007)
  elif found == 3:
    if pgratio > ngratio:
      for a in poppos:
        answer *= abs(a)%1000000007
      print(answer%1000000007)
    else:
      for a in popneg:
        answer *= abs(a)%1000000007
      print(answer%1000000007)
  else:
    for i in range(K):
      answer *= A[-(i+1)]%1000000007
    print(answer%1000000007)
else:
  for a in ans:
    answer *= abs(a)%1000000007
  print(answer%1000000007)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))

A = sorted(A, key = lambda x:abs(x), reverse=True)
#print(A)

ans = []
negidx = 0
posidx = 0
neg = False
for i in range(K):
  a = A[i]
  ans.append(a)
  if a<0:
    negidx = i
    neg = not neg
  else:
    posidx = i
#print(ans, neg, negidx)
answer = 1
if neg:
  poppos = list(ans)
  pg = poppos.pop(posidx)
  popneg = list(ans)
  ng = popneg.pop(negidx)
  pgratio = None
  ngratio = None
  found = 0
  for i in range(K+1, N):
    if A[i]>=0 and ngratio is None:
      found = 1
      popneg.append(A[i])
      ngratio = -A[i]/ng
    if A[i]<0 and pgratio is None:
      found = 2
      poppos.append(A[i])
      pgratio = -A[i]/pg
    if pgratio is not None and npratio is not None:
      found = 3
      break
  if found == 1:
    for a in popnep:
      answer *= abs(a)%1000000007
    print(answer%1000000007)
  elif found == 2:
    for a in poppos:
      answer *= abs(a)%1000000007
    print(answer%1000000007)
  elif found == 3:
    if pgratio > ngratio:
      for a in poppos:
        answer *= abs(a)%1000000007
      print(answer%1000000007)
    else:
      for a in popneg:
        answer *= abs(a)%1000000007
      print(answer%1000000007)
  else:
    for i in range(K):
      answer *= A[-(i+1)]%1000000007
    print(answer%1000000007)
else:
  for a in ans:
    answer *= abs(a)%1000000007
  print(answer%1000000007)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))

A = sorted(A, key = lambda x:abs(x), reverse=True)
#print(A)

ans = []
negidx = 0
posidx = 0
neg = False
for i in range(K):
  a = A[i]
  ans.append(a)
  if a<0:
    negidx = i
    neg = not neg
  else:
    posidx = i
#print(ans, neg, negidx)
answer = 1
if neg:
  poppos = list(ans)
  pg = poppos.pop(posidx)
  popneg = list(ans)
  ng = popneg.pop(negidx)
  pgratio = None
  ngratio = None
  found = 0
  for i in range(K+1, N):
    if A[i]>=0 and ngratio is None:
      found = 1
      popneg.append(A[i])
      ngratio = -A[i]/ng
    if A[i]<0 and pgratio is None:
      found = 2
      poppos.append(A[i])
      pgratio = -A[i]/pg
    if pgratio is not None and npratio is not None:
      found = 3
      break
  if found == 1:
    for a in popnep:
      answer *= a
    print(answer%1000000007)
  elif found == 2:
    for a in poppos:
      answer *= a
    print(answer%1000000007)
  elif found == 3:
    if pgratio > ngratio:
      for a in poppos:
        answer *= a
      print(answer%1000000007)
    else:
      for a in popneg:
        answer *= a
      print(answer%1000000007)
  else:
    for i in range(K):
      answer *= A[-(i+1)]
    print(answer%1000000007)
else:
  for a in ans:
    answer *= a
  print(answer%1000000007)