Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = list(map(int, input().split()))

from collections import defaultdict

d = defaultdict(int)

for i in range(n):
    d[s[i]]+= 1

r = 0
key = sorted(list(d.keys()))
ng = len(key)
for i in range(ng)[::-1]:
    ki = key[i]
    for j in range(i+1)[::-1]:
        kj = key[j]
        if kj <= ki / 2:
            continue
        for k in range(j+1)[::-1]:
            ti = 0
            kk = key[k]
            if kj + kk <= ki:
                break
            if ki != kj and kj != kk and ki != kk:
                ti = d[ki] * d[kj] * d[kk]
            elif ki == kj and kj == kk:
                if d[ki] < 3:
                    continue
                else:
                    ti = d[ki] * (d[ki]-1) * (d[ki]-2) // 6
            elif ki == kj:
                if d[ki] < 2:
                    continue
                else:
                    ti = d[ki] * (d[ki]-1) * d[kk] // 2
            elif kj == kk:
                if d[kk] < 2:
                    continue
                else:
                    ti = d[j] * (d[kj]-1) * d[ki] // 2
            elif ki == kk:
                if d[ki] < 2:
                    continue
                else:
                    ti = d[ki] * (d[ki]-1) * d[kj] // 2
            r+=ti
print(r)




Fixed submission:
n = int(input())
s = list(map(int, input().split()))

import bisect

s = sorted(s)

res = 0
for i in range(n)[::-1]:
    for j in range(i)[::-1]:
        if s[j] <= s[i] / 2:
            break
        k = bisect.bisect_left(s, s[i]-s[j]+1)
        res += max(0, j-k)
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = list(map(int, input().split()))

from collections import defaultdict

d = defaultdict(int)

for i in range(n):
    d[s[i]]+= 1

r = 0
key = sorted(list(d.keys()))
ng = len(key)
for i in range(ng)[::-1]:
    for j in range(i+1):
        for k in range(j, i+1)[::-1]:
            ti = 0
            if key[i] == key[j] and key[j] == key[k]:
                if d[key[i]] < 3:
                    continue
                else:
                    ti = d[key[i]] * (d[key[i]]-1) * (d[key[i]]-2) // 6
            elif key[i] == key[j]:
                if d[key[i]] < 2:
                    continue
                else:
                    ti = d[key[i]] * (d[key[i]]-1) * d[key[k]] // 2
            elif key[j] == key[k]:
                if d[key[k]] < 2:
                    continue
                else:
                    ti = d[key[j]] * (d[key[j]]-1) * d[key[i]] // 2
            elif key[i] == key[k]:
                if d[key[i]] < 2:
                    continue
                else:
                    ti = d[key[i]] * (d[key[i]]-1) * d[key[j]] // 2
            else:
                ti = d[key[i]] * d[key[j]] * d[key[k]]
            if key[j] + key[k] <= key[i]:
                break
            else:
                r+=ti
print(r)


Fixed submission:
n = int(input())
s = list(map(int, input().split()))

import bisect

s = sorted(s)

res = 0
for i in range(n)[::-1]:
    for j in range(i)[::-1]:
        if s[j] <= s[i] / 2:
            break
        k = bisect.bisect_left(s, s[i]-s[j]+1)
        res += max(0, j-k)
print(res)