Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)

dp = [[-1 for i in range(1001)] for j in range(n)]

cnt = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    tgt = l[i] - l[j]
    if dp[j+1][tgt] != -1:
      cnt += dp[j+1][tgt]
    else:
      cptr = ((n-1) + (j+1)) // 2
      fixed = [-1] * n
      while fixed[cptr] == -1:
        if l[cptr] > tgt:
          fixed[cptr] = 0
          cptr = ((n-1) + (cptr+1)) // 2
        else:
          fixed[cptr] = 1
          cptr = ((cptr) + (j+1)) // 2
      inc = cptr - j - fixed[cptr]
      cnt += inc
      dp[j+1][tgt] = inc
#      print('J='+str(j)+', CPTR='+str(cptr)+', CNT='+str(cnt))

print(cnt)

Fixed submission:
n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)

lptr = [-1] * 1001
lptr[0] = n - 1
for i in range(1, 1001):
  maxptr = n -1
  minptr = 0
  fixed = [-1] * n
  cptr = (maxptr + minptr + 1) // 2

  while fixed[cptr] == -1:

#    print(str(cptr)+','+str(l[cptr]))
    if l[cptr] > i:
      fixed[cptr] = 0
      minptr = cptr
    else:
      fixed[cptr] = 1
      maxptr = cptr
    cptr = (maxptr + minptr + 1) // 2
      
  lptr[i] = cptr - fixed[cptr]

#print(lptr)

    
cnt = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    tgt = l[i] - l[j]
    if lptr[tgt] > j:
      cnt += lptr[tgt] - j

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)

dp = [[-1 for i in range(1001)] for j in range(n)]

cnt = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    tgt = l[i] - l[j]
    if dp[j+1][tgt] != -1:
      cnt += dp[j+1][tgt]
    else:
      lcnt = 0
      for k in range(j+1, n):    
        if l[k] > tgt:
          cnt += 1
          lcnt += 1
        else:
          break
      dp[j+1][tgt] = lcnt

print(cnt)

Fixed submission:
n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)

lptr = [-1] * 1001
lptr[0] = n - 1
for i in range(1, 1001):
  maxptr = n -1
  minptr = 0
  fixed = [-1] * n
  cptr = (maxptr + minptr + 1) // 2

  while fixed[cptr] == -1:

#    print(str(cptr)+','+str(l[cptr]))
    if l[cptr] > i:
      fixed[cptr] = 0
      minptr = cptr
    else:
      fixed[cptr] = 1
      maxptr = cptr
    cptr = (maxptr + minptr + 1) // 2
      
  lptr[i] = cptr - fixed[cptr]

#print(lptr)

    
cnt = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    tgt = l[i] - l[j]
    if lptr[tgt] > j:
      cnt += lptr[tgt] - j

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)

cnt = 0
for i in range(n):
  for j in range(i+1,n):
    tgt = l[i] - l[j]
    for k in range(j+1, n):
      if l[k] > tgt:
        cnt += 1
      else:
        break

print(cnt)


Fixed submission:
n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)

lptr = [-1] * 1001
lptr[0] = n - 1
for i in range(1, 1001):
  maxptr = n -1
  minptr = 0
  fixed = [-1] * n
  cptr = (maxptr + minptr + 1) // 2

  while fixed[cptr] == -1:

#    print(str(cptr)+','+str(l[cptr]))
    if l[cptr] > i:
      fixed[cptr] = 0
      minptr = cptr
    else:
      fixed[cptr] = 1
      maxptr = cptr
    cptr = (maxptr + minptr + 1) // 2
      
  lptr[i] = cptr - fixed[cptr]

#print(lptr)

    
cnt = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    tgt = l[i] - l[j]
    if lptr[tgt] > j:
      cnt += lptr[tgt] - j

print(cnt)