Buggy python submission (verdict: Runtime Error):
import bisect
from typing import List


def solve(ls: List[int]) -> int:
  n = len(ls)
  assert(3 <= n <= 2 * 10**3)

  def n_possible_cs(a: int, b: int, first_ic: int) -> int:
    # def bin_search_le_rightmost(s: int, e: int, target: int):
    #   if s == e:
    #     if ls[s] > target:
    #       return None
    #     else:
    #       return s
    #
    #   mid = s + (e - s) // 2
    #
    #   if ls[mid] <= target and (mid + 1 < n and ls[mid + 1] > target):
    #     return mid
    #   elif ls[mid] <= target:
    #     return bin_search_le_rightmost(mid + 1, e, target)
    #   else:
    #     return bin_search_le_rightmost(s, mid, target)

    idx = bisect.bisect_left(ls, a + b, first_ic)
    return 0 if idx == first_ic else idx - first_ic

  ret = 0
  for ia in range(0, n - 2):
    for ib in range(ia + 1, n - 1):
      a, b = (ls[ia], ls[ib])
      x = n_possible_cs(a, b, ib + 1)
      ret += x
  return ret


# n = int(input().strip())
# ls = [int(l) for l in input().split()]
#
# print(solve(sorted(ls)))

print(solve(sorted([218, 786, 704, 233, 645, 728, 389])))


Fixed submission:
import bisect


def solve(ls) -> int:
  n = len(ls)
  assert(3 <= n <= 2 * 10**3)

  def n_possible_cs(a: int, b: int, first_ic: int) -> int:
    # def bin_search_le_rightmost(s: int, e: int, target: int):
    #   if s == e:
    #     if ls[s] > target:
    #       return None
    #     else:
    #       return s
    #
    #   mid = s + (e - s) // 2
    #
    #   if ls[mid] <= target and (mid + 1 < n and ls[mid + 1] > target):
    #     return mid
    #   elif ls[mid] <= target:
    #     return bin_search_le_rightmost(mid + 1, e, target)
    #   else:
    #     return bin_search_le_rightmost(s, mid, target)

    idx = bisect.bisect_left(ls, a + b, first_ic)
    return 0 if idx == first_ic else idx - first_ic

  ret = 0
  for ia in range(0, n - 2):
    for ib in range(ia + 1, n - 1):
      a, b = (ls[ia], ls[ib])
      x = n_possible_cs(a, b, ib + 1)
      ret += x
  return ret


n = int(input().strip())
ls = [int(l) for l in input().split()]

print(solve(sorted(ls)))

Buggy python submission (verdict: Time Limit Exceeded):
def solve(ls) -> int:
  n = len(ls)
  assert(3 <= n <= 2 * 10**3)

  def n_possible_cs(a: int, b: int, first_ic: int) -> int:
    def bin_search_le_rightmost(s: int, e: int, target: int):
      if s == e:
        if ls[s] > target:
          return None
        else:
          return s

      mid = s + (e - s) // 2

      if ls[mid] <= target and (mid + 1 < n and ls[mid + 1] > target):
        return mid
      elif ls[mid] <= target:
        return bin_search_le_rightmost(mid + 1, e, target)
      else:
        return bin_search_le_rightmost(s, mid, target)

    idx = bin_search_le_rightmost(first_ic, n - 1, a + b - 1)
    return 0 if idx is None else idx - first_ic + 1

  ret = 0
  for ia in range(0, n - 2):
    for ib in range(ia + 1, n - 1):
      a, b = (ls[ia], ls[ib])
      x = n_possible_cs(a, b, ib + 1)
      ret += x
  return ret


n = int(input().strip())
ls = [int(l) for l in input().split()]

print(solve(sorted(ls)))


Fixed submission:
import bisect


def solve(ls) -> int:
  n = len(ls)
  assert(3 <= n <= 2 * 10**3)

  def n_possible_cs(a: int, b: int, first_ic: int) -> int:
    # def bin_search_le_rightmost(s: int, e: int, target: int):
    #   if s == e:
    #     if ls[s] > target:
    #       return None
    #     else:
    #       return s
    #
    #   mid = s + (e - s) // 2
    #
    #   if ls[mid] <= target and (mid + 1 < n and ls[mid + 1] > target):
    #     return mid
    #   elif ls[mid] <= target:
    #     return bin_search_le_rightmost(mid + 1, e, target)
    #   else:
    #     return bin_search_le_rightmost(s, mid, target)

    idx = bisect.bisect_left(ls, a + b, first_ic)
    return 0 if idx == first_ic else idx - first_ic

  ret = 0
  for ia in range(0, n - 2):
    for ib in range(ia + 1, n - 1):
      a, b = (ls[ia], ls[ib])
      x = n_possible_cs(a, b, ib + 1)
      ret += x
  return ret


n = int(input().strip())
ls = [int(l) for l in input().split()]

print(solve(sorted(ls)))

Buggy python submission (verdict: Time Limit Exceeded):
def solve(ls) -> int:
  n = len(ls)
  assert(3 <= n <= 2 * 10**3)

  ret = 0

  ia, ib, ic = (0, 1, 2)
  need_adjust = False
  while True:
    if need_adjust or ic == n:
      ib, ic = (ib + 1, ib + 2)
      if ic == n:
        ia, ib, ic = (ia + 1, ia + 2, ia + 3)
      # index check
      if ic == n:
        break
      need_adjust = False

    a, b, c = (ls[ia], ls[ib], ls[ic])
    if a < b + c and b < c + a and c < a + b:
      ret += 1
    elif c >= a + b:
      need_adjust = True

    ic = ic + 1

  return ret


n = int(input().strip())
ls = [int(l) for l in input().split()]

print(solve(sorted(ls)))


Fixed submission:
import bisect


def solve(ls) -> int:
  n = len(ls)
  assert(3 <= n <= 2 * 10**3)

  def n_possible_cs(a: int, b: int, first_ic: int) -> int:
    # def bin_search_le_rightmost(s: int, e: int, target: int):
    #   if s == e:
    #     if ls[s] > target:
    #       return None
    #     else:
    #       return s
    #
    #   mid = s + (e - s) // 2
    #
    #   if ls[mid] <= target and (mid + 1 < n and ls[mid + 1] > target):
    #     return mid
    #   elif ls[mid] <= target:
    #     return bin_search_le_rightmost(mid + 1, e, target)
    #   else:
    #     return bin_search_le_rightmost(s, mid, target)

    idx = bisect.bisect_left(ls, a + b, first_ic)
    return 0 if idx == first_ic else idx - first_ic

  ret = 0
  for ia in range(0, n - 2):
    for ib in range(ia + 1, n - 1):
      a, b = (ls[ia], ls[ib])
      x = n_possible_cs(a, b, ib + 1)
      ret += x
  return ret


n = int(input().strip())
ls = [int(l) for l in input().split()]

print(solve(sorted(ls)))

Buggy python submission (verdict: Time Limit Exceeded):
def solve(ls) -> int:
  n = len(ls)
  assert(3 <= n <= 2 * 10**3)

  ret = 0

  ia, ib, ic = (0, 1, 1)
  while True:
    # index adjustment
    ic = ic + 1
    if ic == n:
      ib, ic = (ib + 1, ib + 2)
      if ic == n:
        ia, ib, ic = (ia + 1, ia + 2, ia + 3)
    # index check
    if ic == n:
      break

    a, b, c = (ls[ia], ls[ib], ls[ic])
    if a < b + c and b < c + a and c < a + b:
      ret += 1

  return ret


n = int(input().strip())
ls = [int(l) for l in input().split()]

print(solve(ls))


Fixed submission:
import bisect


def solve(ls) -> int:
  n = len(ls)
  assert(3 <= n <= 2 * 10**3)

  def n_possible_cs(a: int, b: int, first_ic: int) -> int:
    # def bin_search_le_rightmost(s: int, e: int, target: int):
    #   if s == e:
    #     if ls[s] > target:
    #       return None
    #     else:
    #       return s
    #
    #   mid = s + (e - s) // 2
    #
    #   if ls[mid] <= target and (mid + 1 < n and ls[mid + 1] > target):
    #     return mid
    #   elif ls[mid] <= target:
    #     return bin_search_le_rightmost(mid + 1, e, target)
    #   else:
    #     return bin_search_le_rightmost(s, mid, target)

    idx = bisect.bisect_left(ls, a + b, first_ic)
    return 0 if idx == first_ic else idx - first_ic

  ret = 0
  for ia in range(0, n - 2):
    for ib in range(ia + 1, n - 1):
      a, b = (ls[ia], ls[ib])
      x = n_possible_cs(a, b, ib + 1)
      ret += x
  return ret


n = int(input().strip())
ls = [int(l) for l in input().split()]

print(solve(sorted(ls)))