Problem:


Score : 400 points

Problem Statement

Takahashi has N sticks that are distinguishable from each other. The length of the i-th stick is L_i.

He is going to form a triangle using three of these sticks. Let a, b, and c be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:

a < b + c

b < c + a

c < a + b

How many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.

Constraints

All values in input are integers.

3 \leq N \leq 2 \times 10^3

1 \leq L_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Constraints

Print the number of different triangles that can be formed.

Sample Input 1

4
3 4 2 1

Sample Output 1

1

Only one triangle can be formed: the triangle formed by the first, second, and third sticks.

Sample Input 2

3
1 1000 1

Sample Output 2

0

No triangles can be formed.

Sample Input 3

7
218 786 704 233 645 728 389

Sample Output 3

23



Sample input:
4
3 4 2 1


Sample output:
1

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

N = int(input())
L = list(map(int, input().split()))

L = sorted(L)
# print(L)

ans = 0


def rec_bisearch(ls, start, end, threshould):
    n = len(ls[start:end])
    if n == 1:
        if threshould > ls[start]:
            return len(ls)
        else:
            return start

    p = n // 2 - 1
    if ls[start+p] < threshould:
        return rec_bisearch(ls, start+p+1, end, threshould)
    else:
        return rec_bisearch(ls, start, start+p+1, threshould)


def bisearch(ls, start, end, threshould):
    boundary = rec_bisearch(ls, start, end, threshould)
    return boundary


for a in range(N-2):
    for b in range(a+1, N-1):
        boundary = bisearch(L, b+1, N, L[a]+L[b])
        ans += boundary - 1 - b

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
L = sorted(list(map(int, input().split())))

ans = 0
for x in itertools.combinations(L,3):
  if x[0] + x[1] >  x[2]:
    ans += 1
print (ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
L = np.array(input().split(),dtype=int)
total = 0

L_sort = np.sort(L)


for i in range(N):
	for j in range(i+1,N - 1):
						temp_L = L_sort[j + 1:]
						
						total += len(temp_L[temp_L < L_sort[i]+ L_sort[j]])
						
						if len(temp_L) == 1:
							break
				  	
print(total)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
M = max(L)

cntL = [0] * (M + 1)
for l in L:
    cntL[l] += 1

accCntL = [0] * (M + 2)
for i in range(1, M + 2):
    accCntL[i] = accCntL[i - 1] + cntL[i - 1]

ans = 0
for i, A in enumerate(L):
    for j, B in enumerate(L):
        if i == j:
            continue

        right = min(A + B, M + 1)
        left = abs(A - B) + 1
        ans += accCntL[right] - accCntL[left]

        if left <= A < right:
            ans -= 1
        if left <= B < right:
            ans -= 1

print(ans // 6)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
M = max(L)

cntL = [0] * (M + 1)
for l in L:
    cntL[l] += 1

accCntL = [0] * (M + 2)
for i in range(1, M + 2):
    accCntL[i] = accCntL[i - 1] + cntL[i - 1]

ans = 0
for i, A in enumerate(L):
    for j, B in enumerate(L):
        if i == j:
            continue
        ans += accCntL[min(A + B, M + 1)] - accCntL[abs(A - B) + 1]

        if abs(A - B) < A < min(A + B, M + 1):
            ans -= 1
        if abs(A - B) < B < min(A + B, M + 1):
            ans -= 1

print(ans // 6)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int,input().split()))

l = sorted(l)
count = 0
for i in range(len(l)-2):
    for j in range(i+1, len(l)-1):
        for k in range(j+1, len(l)):
            if (l[i] + l[j]) > l[k]:
                count += 1
            else:
                break
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)

def f(j, max1, max2):
    L_temp = L[j+1:]
    # print("max1 = ", max1, "max2 = ", max2, L_temp)
    dif = max1 - max2
    left = -1
    right = len(L_temp)
    while right - left != 1:
        temp = (left+right)//2
        if dif < L_temp[temp]:
            left = temp
        else:
            right = temp
    if left != N:
        return left+1
    else:
        return left


ans = 0
max1 = 0
max2 = 0
for i in range(N):
    max1 = L[i]
    for j in range(i+1, N-1):
        max2 = L[j]
        ans += f(j, max1, max2)
        # print("ans = ", ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int,input().split()))

l = sorted(l)
count = 0
for i in range(len(l)-2):
    for j in range(i+1, len(l)-1):
        for k in range(j+1, len(l)):
            if (l[i] + l[j]) > l[k]:
                count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    L = list(map(int, input().split()))

    L.sort()
    ans = 0
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            n = bisect_left(L[j+1:], L[i] + L[j])
            ans += n
    print(ans)


main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))

count = 0
for i in range(n):
    for j in range(i):
        for k in range(j):
            if l[i] < l[j] + l[k] and l[j] < l[i] + l[k] and l[k] < l[i] + l[j]:
                count += 1   
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))

count = 0
for i in range(n):
    for j in range(n):
        for k in range(n):
            if i > j > k:
                if l[i] < l[j] + l[k] and l[j] < l[i] + l[k] and l[k] < l[i] + l[j]:
                    count += 1
                    
print(count)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left,bisect_right
import sys
def input(): return sys.stdin.readline().strip()
n=int(input())
ll=sorted(list(map(int,input().split())))
c=0
lll=len(ll)
for i in range(lll-2):
    for j in range(i+1, lll-1):
        uk = bisect_left(ll, ll[i] + ll[j], j+1)
        lk = bisect_right(ll, abs(ll[j] - ll[i]), j+1, uk)
        c+=uk-lk
print(c)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = input().split()
Li = []
for i in L:
  Li.append(int(i))
Li.sort()
ret = 0
for i in range(2, N):
  for j in range(1,i):
    for k in range(0,j):
      if Li[j]+Li[k] > Li[i]:
        ret += j-k
#        print(i)
#        print(j)
#        print(k)
        break
      else:
        pass
print(ret)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n = int(input())
l=list(map(int, input().split()))
res = [ c for c in itertools.combinations(l,3) if c[0] < c[1] + c[2] and c[1] < c[0] + c[2] and c[2] < c[0] + c[1] ]
print(len(res))

Submission status: Time Limit Exceeded

Solution code:
import bisect
import sys

input = sys.stdin.readline


def read_i():
    return list(map(int, input().split()))


n = int(input())
ls = sorted(read_i())
ls2 = ls[::-1]
res = 0
for i, x in enumerate(ls2):
    for j, y in enumerate(ls2[i + 1:], i + 1):
        res += n - j - 1 - bisect.bisect(ls[:-(j + 1)], x - y)
print(res)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int,input().split()))
L = sorted(L, reverse =True)
counter = 0
for i in range(N):
  for j in range(N-1-i):
    for k in range(N -2 -i -j):
      a = L[i]
      b = L[i+j+1]
      c = L[i+j+k+2]
      if c <= a-b:
        break
      elif b < a + c:
        counter += 1
    
print(counter)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
l.append(99999999)
c = 0
for i in range(n - 2):
    t = i + 2
    for j in range(i + 1, n - 1):
        for k in range(t, n + 1):
            if l[i] + l[j] <= l[k]:
                t = k
                break
        if t > j: c += t - j - 1
        if j + 1 == t: t += 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
import os, sys, atexit
from io import BytesIO, StringIO
 
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
_OUTPUT_BUFFER = StringIO()
sys.stdout = _OUTPUT_BUFFER
 
@atexit.register
def write():
    sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
n = int(input())
l = list(map(int, input().split()))
l.sort()
l.append(9999999999999)
c = 0
for i in range(n - 2):
    t = i + 2
    for j in range(i + 1, n - 1):
        for k in range(t, n + 1):
            if l[i] + l[j] <= l[k]:
                t = k
                break
        if t > j: c += t - j - 1
        if j + 1 == t: t += 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
l.append(9999999999999)
c = 0
for i in range(n - 2):
    t = i + 2
    for j in range(i + 1, n - 1):
        for k in range(t, n + 1):
            if l[i] + l[j] <= l[k]:
                t = k
                break
        if t > j: c += t - j - 1
        if j + 1 == t: t += 1
print(c)                











# t = 2
# c = 0
# for i in range(n - 2):
#     k = 2
#     for j in range(i + 1, n - 1):
#         for k in range(t, n):
#             print(i, j, k, t)
#             if j == k:
#                 t = k + 1
#                 continue
#             if l[i] + l[j] > l[k]: continue
#             else:
#                 t = k
#                 break
#         print("t, j = ", t, j)
#         c += (t - j - 1)
# print(c)
#

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
l.append(9999999999999)
c = 0
for i in range(n - 2):
    t = i + 2
    for j in range(i + 1, n - 1):
        for k in range(t, n + 1):
            if l[i] + l[j] <= l[k]:
                t = k
                break
        # print(t, j, c)
        c += t - j - 1
        if j + 1 == t: t += 1
print(c)                











# t = 2
# c = 0
# for i in range(n - 2):
#     k = 2
#     for j in range(i + 1, n - 1):
#         for k in range(t, n):
#             print(i, j, k, t)
#             if j == k:
#                 t = k + 1
#                 continue
#             if l[i] + l[j] > l[k]: continue
#             else:
#                 t = k
#                 break
#         print("t, j = ", t, j)
#         c += (t - j - 1)
# print(c)
#

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int, input().split()))
L.sort()
print(sum(L[k]<L[i]+L[j] for i in range(N) for j in range(i+1,N) for k in range(j+1,N)))

Submission status: Time Limit Exceeded

Solution code:
import sys


def main():
    n = int(sys.stdin.readline().strip())
    l = [int(s) for s in sys.stdin.readline().strip().split()]
    # n = 4
    # l = [1, 2, 3, 4]

    l.sort()

    # [lo, hi)
    # Returns -1 if no valid item
    def b_search(lo, hi, limit):
        if lo >= hi:
            return -1
        if lo + 1 == hi:
            return lo
        assert lo + 1 < hi
        mid = (lo + hi) // 2
        if l[mid] < limit:
            return b_search(mid, hi, limit)
        else:
            return b_search(lo, mid, limit)

    ans = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            a_plus_b = l[i] + l[j]
            valid_longest_idx = b_search(j, n, a_plus_b)
            ans += valid_longest_idx - j

    print(ans)


main()

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
l_li = [int(x) for x in input().split()]
l_li.sort()

count = 0
  
for i in range(n):
  for j in range(i+1,n):
    wa = l_li[i] + l_li[j]
    target = l_li[j+1:]
    count += bisect.bisect_left(target, wa)
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l_li = [int(x) for x in input().split()]
l_li.sort()

count = 0

for i in range(n):
  for j in range(i+1,n):
    wa = l_li[i] + l_li[j]
    for k in range(j+1,n):
      if l_li[k] >= wa:
        break
      count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l_li = [int(x) for x in input().split()]

count = 0

for i in range(n):
  for j in range(i+1,n):
    a = l_li[i]
    b = l_li[j]
    wa = a + b
    sa = a - b
    if sa < 0:
      sa = -1 * sa
    target = l_li[j+1:]
    count += sum([sa<x<wa for x in target])
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
l.sort(reverse=True)
ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        k=(n+j)//2
        r = n-k
        t=l[i]-l[j]
        lk = l[k]
        try:
            lk1 = l[k + 1]
        except:
            lk1 = 0
        while not(lk>t and lk1<=t) and k>j:
            r = r//2 if r//2>0 else 1
            if lk1>t:
                k += r
            else: k-=r
            lk=l[k]
            try:
                lk1=l[k+1]
            except:
                lk1=0
        if k>j:
            ans += k-j
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l_li = [int(x) for x in input().split()]

count = 0

for i in range(n):
  for j in range(i+1,n):
    a = l_li[i]
    b = l_li[j]
    wa = a + b
    sa = a - b
    if sa < 0:
      sa = -1 * sa
    for k in range(j+1,n):
      c = l_li[k]
      if c <= sa or c >= wa:
        continue
      count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left,bisect_right
from copy import deepcopy
n = int(input())
L = list(map(int,input().split()))
L.sort()
LL = deepcopy(L)
LL.sort(reverse=1)
# print(L,LL)
ans = 0
for i_max,abc_max in enumerate(LL[:-2]):
	ans_bef = ans
	# print("i ",i_max,abc_max)
	for j_mid,abc_mid in enumerate(LL[i_max+1:],i_max+1):
		# print("j ",j_mid,abc_mid)
		# for k_min,abc_min in enumerate(L[j_mid+1:]):
		right = bisect_left(L,abc_max+1-abc_mid)
		# print(right,abc_max+1-abc_mid)
		cur = len(LL[j_mid:-(right+1)])
		if not cur:
			break
		ans += cur
		# print("ans->",ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
#ABC143D
import sys
import math
import collections
#sys.setrecursionlimit(100000)
 
 
n=int(input())
 
tmp = input().split()
l = list(map(lambda a: int(a), tmp))
 
sum=0
 
for i in range(n):
	for j in range(i+1,n):
		tmp1 = l[i]+l[j]
		tmp2 = abs(l[i]-l[j])
		for k in range(j+1,n):
			if((tmp1>l[k])and(tmp2<l[k])):
				sum+=1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=sorted(list(map(int,input().split())))
count=0
def nibutan(s,g,num):
  low=s
  high=g-1
  while low<=high:
    mid=(low+high)//2
    num1=l[mid]
    if num>l[-1]:
      return n-1
    elif num<=l[mid] and l[mid-1]<num:
      return mid-1
    elif l[mid-1]>num:
      high=mid-1
    elif l[mid]<num:
      low=mid+1
    else:
      return mid-2
for i in range(n-2):
  for j in range(i+1,n-1):
    if i==n-3 and j==n-2:
      if l[i]+l[j]>l[-1]:
        count+=1
    else:
      num=nibutan(j+1,n,l[i]+l[j])
      count+=num-j
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = input().split()
LI = []
for l in L:
    LI.append(int(l))

LI.sort(reverse=True)

# print(LI)
count = 0
for i in range(N-2):
    a = LI[i]
    # print(a)
    for j in range(i + 1, N - 1):
        b = LI[j]
        # print(b)

        q = a - b
        w = b - a
        w = max([q, w])
        e = a + b
        # print("\t"+str(w))

        low = j + 1
        high = len(LI) - 1
        if b < w:
            continue
        if LI[int((low + high) / 2)] <= w:
            continue
        if LI[-1] > w:
            count += len(LI) - j - 1
            continue
        while True:
            mid = int((low + high) / 2)
            tmp = LI[mid]
            # print("\t\t" + str(mid))
            # print("\t\t" + str(tmp))
 

            if tmp > w:
                low = mid + 1
            else:
                # print("\t\t\t" + str(LI.index(tmp)-j-1))
                count += LI.index(tmp)-j-1
                break
            # else:
            if low > high:
                # print("\t\t\t" + str(len(LI)-j-1))
                count += len(LI)-j-1
                break


print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
h = list(map(int, input().split()))
h.sort()

def triangle(n,h):
    x = 0
    for i in range(0,n-2):
        for j in range(i+1,n-1):
            for k in range(j+1, n):
                if h[k] < h[j] + h[i] :
                    x = x + 1
    return x
print(triangle(n,h))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = input().split()
LI = []
for l in L:
    LI.append(int(l))
 
LI.sort(reverse=True)
 
# print(LI)
count = 0
for i in range(N-2):
    a = LI[i]
    # print(a)
    for j in range(i + 1, N - 1):
        b = LI[j]
        # print(b)
 
        q = a - b
        w = b - a
        w = max([q,w])
        e = a + b
        # print("\t"+str(w))
        # print("\t"+str(e))
 
        low = j + 1
        high = len(LI) - 1
        if LI[int((low + high) / 2)] <= w:
            continue
        while True:
            mid = int((low + high) / 2)
            tmp = LI[mid]
            # print("\t\t" + str(mid))
            # print("\t\t" + str(tmp))
            if LI[-1] > w:
                count += len(LI) - j - 1
                break
 
            if tmp > w:
                low = mid + 1
            else:
                # print("\t\t\t" + str(LI.index(tmp)-j-1))
                count += LI.index(tmp)-j-1
                break
            # else:
            if low > high:
                # print("\t\t\t" + str(len(LI)-j-1))
                count += len(LI)-j-1
                break
 
 
 
 
print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect
import random

def test(L):
    lst = []
    N = len(L)
    for a in range(N):
        for b in range(a):
            for c in range(b):
                if L[a] < L[b] + L[c]:
                    lst.append((c, b, a))
    return lst

def test_len(L):
    return len(test(L))

def test2(L):
    return len(set((l[0], l[2]) for l in test(L)))

N = int(input())
L = sorted(map(int, input().split()))
# L = [1, 1, 1000]
# L = [218, 233, 389, 645, 704, 728, 786]
# N = 10
# L = sorted([random.randint(0, 1000) for _ in range(N)])
# L = [150, 190, 192, 281, 294, 374, 501, 686, 769, 954]
# print(L)
# N = len(L)
S = 0
for a in range(2, N):
    for b in range(1, a):
        c = bisect.bisect_left(L, L[a] - L[b])
        c = min(b - 1, c)
        # assert L[a] < L[b] + L[c] or c == 0, (c, b, a)
        while not(L[a] < L[b] + L[c]):
            c += 1
        assert L[a] < L[b] + L[c]
        if b <= c:
            continue
        else:
            S += b - c
# print(test_len(L))
print(S)

Submission status: Time Limit Exceeded

Solution code:
from scipy.misc import comb
n = int(input())
h = list(map(int, input().split()))
def triangle(n,h):
    x = comb(n, 3, exact = True)
    for i in range(0,n-2):
        for j in range(i+1,n-1):
            for k in range(j+1, n):
                if h[i] >= h[j] + h[k] or h[j] >= h[i] + h[k] or h[k] >= h[j] + h[i]:
                    x = x - 1
    return x
print(triangle(n,h))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import itertools
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right
from collections import Counter


def solve(N: int, L: "List[int]"):
    ans = 0
    L.sort()

    for x in range(N):
        for y in range(x+1,N):
            # if x==y : continue
            zs = bisect_right(L, max(L[x],L[y]))
            ze = bisect_left(L, L[x]+L[y])            
            cnt = 0
            if(zs <= x <= ze):
                cnt += 1
            if(zs <= y <= ze):
                cnt += 1
            ans += ((ze-zs)-cnt)

    print(ans)

    return
 


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    L = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, L)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
da=list(map(int,input().split()))
ans=0
da=sorted(da)
for i in range(1,n-1):
    for j in range(i):
        ok,ng=0,0
        ok+=i
        ng+=n
        while ng-ok!=1:
            mi=(ok+ng)//2
            if da[mi]<da[i]+da[j]:
                ok=0
                ok+=mi
            else:
                ng=0
                ng+=mi
        ans+=ok-i
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations as comb
n = int(input())
l = list(map(int,input().split()))
l.sort()
tri = set()
for a,b,c in comb(l,3):
  if a<b+c and b<c+a and c<a+b:
    tri.add((a,b,c))
print(len(tri))

Submission status: Time Limit Exceeded

Solution code:
import bisect
N=int(input())
ls=[int(s) for s in input().split()]
ls.sort()
ans=0
for i in range(1,N-1):
  for j in range(i+1,N):
    k = bisect.bisect_right(ls[:i],ls[j]-ls[i])
    ans+=i-k
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, M, = map(int, input().split())
N = int(input())
L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

L.sort()
ans = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        t = L[i] + L[j]
        a = bisect.bisect_left(L[j+1:], t)
        c = j + 1 + a
        if c == N:
            ans += N - j - 1
        else:
            if c - j != 1:
                ans += c - j - 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
da=list(map(int,input().split()))

def bina(x,ok,ng):
    global da
    while ng-ok!=1:
        mi=(ok+ng)//2
        if da[mi]<x:
            ok=0
            ok+=mi
        else:
            ng=0
            ng+=mi
    return ok


ans=0
da=sorted(da)
for i in range(1,n-1):
    for j in range(i):
        ans+=bina(da[i]+da[j],i,n)-i
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

import sys
from bisect import bisect_left

n = int(sys.stdin.readline().strip())
l = [ int(x) for x in sys.stdin.readline().strip().split() ]
l.sort()
c = 0
n1 = n - 1
for i in range(n-2):
    li = l[i]
    for j in range(i+1,n1):
        k = bisect_left(l,l[j]+li,lo=j)
        c += k-j-1
#
print(c)

Submission status: Time Limit Exceeded

Solution code:
import bisect
a = int(input())
l = list(map(int,input().split()))
l.sort()
s = 0
for n,x in enumerate(l):
    for m,y in enumerate(l[n+1:]):
        # print(n+m+2)
        t = x + y
        s += bisect.bisect_left(l[n+m+2:],t)


print(s)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3

import sys
from bisect import bisect_left

n = int(sys.stdin.readline().strip())
l = [ int(x) for x in sys.stdin.readline().strip().split() ]
l.sort()
c = 0
for i in range(n-1):
    for j in range(i+1,n-1):
        k = bisect_left(l,l[j]+l[i],lo=j)
        c += k-j-1
#
print(c)

Submission status: Time Limit Exceeded

Solution code:
n  = int(input())
ls = [int(x) for x in input().split()]
t  = 0
 
ls.sort()
 
for i in range(0, n -2):
	a = ls[i]
	for j in range(i + 1, n - 1):
		b = ls[j]
		
		l = j + 1
		r = n - 1
		
		while l <= r:
			m = (l + r) // 2
			c = ls[m]
		
			if c < a + b:
				l = m + 1
			else:
				r = m - 1
		
		if m < n:
			c = ls[m]
			
			if c < a + b:
				m += 1
		
		t += m - j - 1
		
print(t)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = [int(i) for i in input().split()]
 
result = 0
 
for aIndex,a in enumerate(L):
    for bIndex,b in enumerate(L[aIndex+1:]):
        for c in L[aIndex+bIndex+2:]:
            if a<b+c and b<a+c and c<a+b:
                result+=1
 
print(result)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

L = list(map(int, input().split()))

def check_triangle(a, b, c):
    if a < b + c and b < c + a and c < a + b:
        return True
    return False

sum = 0 
L.sort()
for i, l_1 in enumerate(L[:-2]):
    for j, l_2 in enumerate(L[i+1:-1]):
        for l_3 in L[i + j + 2:]:
            if check_triangle(l_1, l_2, l_3):
                sum += 1
            else:
                break

print(sum)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    L = list(map(int, input().split()))
    L.sort()
    counter = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            result = 0
            left = j+1
            right = n-1
            while L[i]+L[j] > L[left]:
                if right - left <= 1:
                    if L[i]+L[j] > L[right]:
                        result = right - j
                    else:
                        result = left - j
                    break
                mid = (left+right)//2
                if L[i]+L[j] > L[mid]:
                    left = mid
                else:
                    right = mid
            counter += result
    print(counter)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
da=list(map(int,input().split()))

def bina(x,ok,ng):
    global da
    if ng-ok==1:
        return ok
    mi=(ng+ok)//2
    if da[mi]<x:
        ok=mi
    else:
        ng=mi
    return bina(x,ok,ng)

ans=0
da=sorted(da)
for i in range(1,n-1):
    for j in range(i):
        ans+=bina(da[i]+da[j],i,n)-i
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right
N = int(input())
D = list(map(int, input().split()))
D.sort(reverse=False)

count = 0

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        l = D[:N - j - 1]
        ind = bisect_right(D, D[i] + D[j] - 1)
        count += max(ind - j - 1, 0)

print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))

import bisect
L.sort()
ans=0
i=0
while i<N-2:
    j=i+1
    while j<N-1:
        x=bisect.bisect_left(L[j+1:],L[i]+L[j])
        ans+=x
        j+=1
    i+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = list(map(int, input().split()))
L.sort()

def binary_s(left, right, a, b):
    result = 0
    while L[a]+L[b] > L[left]:
        if right - left <= 1:
            if L[a]+L[b] > L[right]:
                result = right - b
            else:
                result = left - b
            break
        mid = (left+right)//2
        if L[a]+L[b] > L[mid]:
            left = mid
        else:
            right = mid
    return result
counter = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        counter += binary_s(j+1, n-1, i, j)
print(counter)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = list(map(int, input().split()))
x = [0] * 1010
for l in L:
  x[l] += 1

ans = 0;
for a in range(1, 1001):
  if x[a] == 0:
    continue
  for b in range(a, 1001):
    if x[b] == 0:
      continue
    for c in range(b, 1001):
      if x[c] == 0:
        continue
      if c >= a + b:
        break
      if a == b == c:
        ans += x[a] * (x[b]-1) * (x[c]-2) // 6
      elif a == b:
        ans += x[a] * (x[b]-1) * x[c] // 2
      elif b == c:
        ans += x[a] * x[b] * (x[c]-1) // 2
      else:
        ans += x[a] * x[b] * x[c]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import defaultdict, deque
from heapq import *
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, M, = map(int, input().split())
N = int(input())
L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

L.sort(reverse=True)
ans = 0
for i in range(N):
    for j in range(i + 1, N):
        for k in range(j + 1, N):
            if L[i] < L[j] + L[k]:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n  = int(input())
ls = [int(x) for x in input().split()]
t  = 0
 
ls.sort()
ls.reverse()
 
for i in range(0, n -2):
	a = ls[i]
	for j in range(i + 1, n - 1):
		b = ls[j]
		
		l = j + 1
		r = n - 1
		
		while l <= r:
			m = (l + r) // 2
			c = ls[m]
		
			if a + b <= c or a + c <= b or b + c <= a:
				r = m - 1
			else:
				l = m + 1
		
		if m < n:
			c = ls[m]
			
			if a + b <= c or a + c <= b or b + c <= a:
				pass
			else:
				m += 1
		
		t += m - j - 1
		
print(t)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        for k in range(j+1,n):
            if l[k]<l[j]+l[i]:
                ans += 1

            
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

N = int(input())
L = sorted(list(map(int, input().split())))

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        ans += bisect.bisect_left(L[j + 1:], L[i] + L[j])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
L = list(map(int, input().split()))
L.sort()

def binary_s(left, right, a, b):
    result = 0
    while L[a]+L[b] > L[left]:
        if right - left <= 1:
            if L[a]+L[b] > L[right]:
                result = right - max(a, b)
            else:
                result = left - max(a, b)
            break
        mid = (left+right)//2
        if L[a]+L[b] > L[mid]:
            left = mid
        else:
            right = mid
    return result
counter = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        counter += binary_s(j+1, n-1, i, j)
print(counter)

Submission status: Time Limit Exceeded

Solution code:
def judge(a, b, c):
    if(a < b + c):
        if(b < c + a):
            if(c < a + b):
                return 1
    return 0

n = int(input())
l = [int(e) for e in input().split(" ")]
i = 0; j = 1; k = 0; ans = 0;
count = 0
while(i < n):
    j = i + 1
    k = j + 1
    while(j < n):
        while(k < n):
            ans += judge(l[i], l[j], l[k])
            #print(f"{count:03}:ans={ans}, l[{i}]={l[i]}, l[{j}]={l[j]}, l[{k}]={l[k]}");count += 1;
            k += 1
        j += 1
        k = j + 1
    i += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def judge(a, b, c):
    if(a < b + c):
        if(b < c + a):
            if(c < a + b):
                return 1
    return 0

n = int(input())
l = [int(e) for e in input().split(" ")]
i = 0; j = 1; k = 0; ans = 0;
count = 0
while(i < n):
    j = i + 1
    k = j + 1
    while(j < n):
        while(k < n):
            ans += judge(l[i], l[j], l[k])
            #print(f"{count:03}:ans={ans}, l[{i}]={l[i]}, l[{j}]={l[j]}, l[{k}]={l[k]}")
            count += 1
            k += 1
        j += 1
        k = j + 1
    i += 1
print(ans)