Problem p02616 (python)

Fixed submission:

def main():
  n, k = map(int, input().split())

  mod = 10 ** 9 + 7

  if n == k:
    ans = 1
    for num in input().split():
      ans = (ans * int(num)) % mod

    print(ans % mod)
    return

  plus = []
  minus = []
  for num in input().split():
    num = int(num)
    if num >= 0:
      plus.append(num)
    else:
      minus.append(num)

  pn = len(plus)
  mn = len(minus)

  if pn == 0 and k % 2 == 1:
    minus.sort(reverse = True)
    ans = 1
    for i in range(k):
      ans = (-ans * minus[i]) % mod

    print((-ans) % mod)
    return


  plus.sort(reverse = True)
  minus.sort()

  pi = 0
  mi = 0

  while k > 0:
    if pi + 1 == pn:
      if k % 2 == 0:
        mi += k
      else:
        pi += 1
        mi += k - 1

      break

    if pi == pn:
      mi += k
      break

    if mi + 1 >= mn:
      pi += k
      break

    if k == 1 or plus[pi] * plus[pi + 1] >= minus[mi] * minus[mi + 1]:
      pi += 1
      k -= 1
    else:
      mi += 2
      k -= 2

  ans = 1
  for i in range(pi):
    ans = (ans * plus[i]) % mod

  for i in range(mi):
    ans = (-ans * minus[i]) % mod

  print(ans % mod)


main()

Buggy submission (verdict: Runtime Error):

def main():
  n, k = map(int, input().split())

  plus = []
  minus = []

  for num in input().split():
    num = int(num)
    if num >= 0:
      plus.append(num)
    else:
      minus.append(num)

  pn = len(plus)
  mn = len(minus)

  mod = 10 ** 9 + 7

  if pn == 0 and k % 2 == 1:
    minus.sort(reverse = True)
    ans = 1
    for i in range(k):
      ans = (-ans * minus[i]) % mod

    print((-ans) % mod)
    return


  plus.sort(reverse = True)
  minus.sort()

  pi = 0
  mi = 0

  while k > 0:
    if pi + 1 == pn:
      if k % 2 == 0:
        mi += k
      else:
        pi += 1
        mi += k - 1

      break

    if pi == pn:
      mi += k
      break

    if mi + 1 >= mn:
      pi += k
      break

    if k == 1 or plus[pi] * plus[pi + 1] >= minus[mi] * minus[mi + 1]:
      pi += 1
      k -= 1
    else:
      mi += 2
      k -= 2

  ans = 1
  for i in range(pi):
    ans = (ans * plus[i]) % mod

  for i in range(mi):
    ans = (-ans * minus[i]) % mod

  print(ans % mod)


main()

Buggy submission (verdict: Runtime Error):

def main():
  n, k = map(int, input().split())

  plus = []
  minus = []

  for num in input().split():
    num = int(num)
    if num >= 0:
      plus.append(num)
    else:
      minus.append(num)

  pn = len(plus)
  mn = len(minus)

  mod = 10 ** 9 + 7

  if pn == 0 and k % 2 == 1:
    minus.sort(reverse = True)
    ans = 1
    for i in range(k):
      ans = (-ans * minus[i]) % mod

    print((-ans) % mod)
    return


  plus.sort(reverse = True)
  minus.sort()

  pi = 0
  mi = 0

  while k > 0:
    if pi + 1 == pn:
      if k % 2 == 0:
        mi += k
      else:
        pi += 1
        mi += k - 1

      break

    if pi == pn:
      mi += k
      break

    if mi + 1 >= mn:
      pi += k
      break

    if plus[pi] * plus[pi + 1] >= minus[mi] * minus[mi + 1]:
      pi += 1
      k -= 1
    else:
      mi += 2
      k -= 2

  ans = 1
  for i in range(pi):
    ans = (ans * plus[i]) % mod

  for i in range(mi):
    ans = (-ans * minus[i]) % mod

  print(ans % mod)


main()

Buggy submission (verdict: Runtime Error):

def main():
  n, k = map(int, input().split())

  plus = []
  minus = []

  for n in input().split():
    n = int(n)
    if n >= 0:
      plus.append(n)
    else:
      minus.append(n)

  mod = 10 ** 9 + 7

  if len(plus) == 0 and k % 2 == 1:
    minus.sort(reverse=True)
    ans = 1
    for i in range(k):
      ans *= minus[i]

    print(ans % mod)
    return


  plus.sort(reverse = True)
  minus.sort()

  pn = 0
  mn = 0

  while k > 0:
    if pn + 1 == len(plus):
      pn += 1
      mn += k - 1
      break

    if pn == len(plus):
      mn += k
      break

    if mn + 1 >= len(minus):
      pn += k
      break

    if plus[pn] * plus[pn + 1] >= minus[mn] * minus[mn + 1]:
      pn += 1
      k -= 1
    else:
      mn += 2
      k -= 2

  ans = 1
  for i in range(pn):
    ans = (ans * plus[i]) % mod

  for i in range(mn):
    ans = (ans * (-minus[i])) % mod

  print(ans % mod)


main()

Buggy submission (verdict: Runtime Error):

def main():
  n, k = map(int, input().split())

  plus = []
  minus = []

  for n in input().split():
    n = int(n)
    if n >= 0:
      plus.append(n)
    else:
      minus.append(n)

  mod = 10 ** 9 + 7

  if len(plus) == 0 and k % 2 == 1:
    minus.sort(reverse=True)
    ans = 1
    for i in range(k):
      ans *= minus[i]

    print(ans % mod)
    return


  plus.sort(reverse = True)
  minus.sort()

  pn = min(len(plus), k)
  mn = k - pn

  if len(plus) >= 2 and len(minus) >= 2:
    while pn >= 2:
      if plus[pn - 2] * plus[pn - 1] > minus[mn] * minus[mn + 1]:
        break

      pn -= 2
      mn += 2

  ans = 1
  for i in range(pn):
    ans = (ans * plus[i]) % mod

  for i in range(mn):
    ans = ans * minus[i]

  print(ans % mod)


main()

Buggy submission (verdict: Runtime Error):

def main():
  n, k = map(int, input().split())

  plus = []
  minus = []

  for n in input().split():
    n = int(n)
    if n >= 0:
      plus.append(n)
    else:
      minus.append(n)

  plus.sort(reverse = True)
  minus.sort()

  pn = min(len(plus), k)
  mn = k - pn

  if len(plus) >= 2 and len(minus) >= 2:
    while pn >= 2:
      if plus[pn - 2] * plus[pn - 1] > minus[mn] * minus[mn + 1]:
        break

      pn -= 2
      mn += 2

  mod = 10 ** 9 + 7
  ans = 1
  for i in range(pn):
    ans = (ans * plus[i]) % mod

  for i in range(mn):
    ans = ans * minus[i]

  if ans < 0:
    ans = 1
    for i in range(pn):
      ans = (ans * plus[-i - 1]) % mod

    for i in range(mn):
      ans = ans * minus[-i - 1]


  print(ans % mod)


main()