Buggy python submission (verdict: Time Limit Exceeded):
def sort_list():
    _ = input()
    K = [int(x) for x in input().split()]
    ans = 0
    n_lst = tuple(sorted(K, reverse=True))
    for idx in range(len(K)-2):
        for diff_idx in range(idx+1, len(K)-1):
            diff = n_lst[idx] - n_lst[diff_idx]
            for search in reversed(range(diff_idx+1, len(K))):
                if n_lst[search] > diff:
                    ans += search - diff_idx
                    break
    print(ans)


if __name__ == "__main__":
    sort_list()


Fixed submission:
import bisect


def sort_list():
    _ = input()
    K = [int(x) for x in input().split()]
    ans = 0

    len_k = len(K)
    n_lst = sorted(K)

    for idx in reversed(range(2, len_k)):
        for diff_idx in range(1, idx):
            diff = n_lst[idx] - n_lst[diff_idx]
            insert_index = bisect.bisect_right(n_lst, diff)
            if insert_index < diff_idx:
                # print(n_lst, diff, idx, diff_idx, insert_index)
                ans += diff_idx - insert_index
    print(ans)


if __name__ == "__main__":
    sort_list()

Buggy python submission (verdict: Time Limit Exceeded):
def sort_list():
    _ = input()
    K = [int(x) for x in input().split()]
    ans = 0
    n_lst = tuple(sorted(K, reverse=True))
    for idx in range(len(K)-2):
        for diff_idx in range(idx+1, len(K)-1):
            diff = n_lst[idx] - n_lst[diff_idx]
            for search in range(diff_idx+1, len(K)):
                if n_lst[search] <= diff:
                    search -= 1
                    break
            ans += search - diff_idx
    print(ans)


if __name__ == "__main__":
    sort_list()


Fixed submission:
import bisect


def sort_list():
    _ = input()
    K = [int(x) for x in input().split()]
    ans = 0

    len_k = len(K)
    n_lst = sorted(K)

    for idx in reversed(range(2, len_k)):
        for diff_idx in range(1, idx):
            diff = n_lst[idx] - n_lst[diff_idx]
            insert_index = bisect.bisect_right(n_lst, diff)
            if insert_index < diff_idx:
                # print(n_lst, diff, idx, diff_idx, insert_index)
                ans += diff_idx - insert_index
    print(ans)


if __name__ == "__main__":
    sort_list()

Buggy python submission (verdict: Time Limit Exceeded):
def sort_list():
    _ = input()
    K = [int(x) for x in input().split()]
    ans = 0
    n_lst = tuple(sorted(K, reverse=True))
    for idx in range(len(K)-2):
        for diff_idx in range(idx+1, len(K)-1):
            diff = n_lst[idx] - n_lst[diff_idx]
            for search in range(diff_idx+1, len(K)):
                if n_lst[search] <= diff:
                    search -= 1
                    break
            ans += search - diff_idx
    print(ans)


if __name__ == "__main__":
    sort_list()


Fixed submission:
import bisect


def sort_list():
    _ = input()
    K = [int(x) for x in input().split()]
    ans = 0

    len_k = len(K)
    n_lst = sorted(K)

    for idx in reversed(range(2, len_k)):
        for diff_idx in range(1, idx):
            diff = n_lst[idx] - n_lst[diff_idx]
            insert_index = bisect.bisect_right(n_lst, diff)
            if insert_index < diff_idx:
                # print(n_lst, diff, idx, diff_idx, insert_index)
                ans += diff_idx - insert_index
    print(ans)


if __name__ == "__main__":
    sort_list()

Buggy python submission (verdict: Time Limit Exceeded):
def sort_list(list_n):
    ans = 0
    n_lst = sorted(list_n, reverse=True)
    for idx in range(len(list_n)-2):
        for diff_idx in range(idx+1, len(list_n)-1):
            diff = n_lst[idx] - n_lst[diff_idx]
            for search in range(diff_idx+1, len(list_n)):
                if n_lst[search] <= diff:
                    search -= 1
                    break
            conbo = search - diff_idx
            ans += conbo
    return ans


def main():
    _ = input()
    K = [int(x) for x in input().split()]
    ans = sort_list(K)
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import bisect


def sort_list():
    _ = input()
    K = [int(x) for x in input().split()]
    ans = 0

    len_k = len(K)
    n_lst = sorted(K)

    for idx in reversed(range(2, len_k)):
        for diff_idx in range(1, idx):
            diff = n_lst[idx] - n_lst[diff_idx]
            insert_index = bisect.bisect_right(n_lst, diff)
            if insert_index < diff_idx:
                # print(n_lst, diff, idx, diff_idx, insert_index)
                ans += diff_idx - insert_index
    print(ans)


if __name__ == "__main__":
    sort_list()