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:
N = int(input())
nums = [int(i) for i in input().split()]
ans = 0

for i in range(N-2):
  for j in range(i+1,N-1):
    for k in range(j+1,N):
      if nums[i]+nums[j]+nums[k] > 2*max(nums[i],nums[j],nums[k]):
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=sorted(list(map(int,input().split())))
count=0
d2breakFlag=False
for d1 in range(N-2):
    for d2 in range(d1+1,N-1):
        for d3 in range(d2+1,N):
            if L[d1]+L[d2]>L[d3]: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)
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[i] < l[j] + l[k]:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
import math

def main():
    n = int(input())
    l_s = list(sorted(map(int, input().split(' '))))

    ans = 0

    for c in range(2, n):
        for b in range(1, c):
            idx = bisect.bisect_right(l_s[:b], l_s[c] - l_s[b])
            ans += b - idx
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [int(_) for _ in input().split()]
ans = 0
L = [0 for _ in range(1,2000+2)]
s = [0 for _ in range(0,2000+2)]
for k in range(0, n):
    L[l[k]-1] += 1
for x in range(0,2001):
    s[x+1] = s[x] + L[x]

for i in range(0, n-1):
    for j in range(i+1, n):
        exc = 0
        m = abs(l[i] - l[j])
        M = l[i] + l[j]
        if l[i] > m:
            exc += 1
        if l[j] > m:
            exc += 1
        ans += s[M] - s[m] - L[M-1] - exc
print(ans//3)

Submission status: Time Limit Exceeded

Solution code:
import bisect

N = int(input())

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

d.sort()

ans = 0

for i in range(N-2):
    for j in range(i+1,N-1):
        S = d[i] + d[j]
        ans += bisect.bisect_left(d[j+1:],S)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
#input
n = int(input())
list1 = list(map(int,input().split()))
list1.sort()

cout = 0 #最終的な三角形の個数

for i in range(n-2): #リストから三角形が作れなくなるまでループ
	o = list1[i] #1辺を確定、そのままリストから消去
	for j in range(i+1,n-1): #残りの二辺のループ(iで使っている次の値から)
		p = list1[j] #２辺目を確定

		b = o+p -1 #昇順にソートしてあるので、c<a+b条件のみ計算

		d = bisect.bisect_right(list1[j+1:],b) #３辺目を確定
		
		cout += d

print(cout)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import numpy as np
import copy

def main():
    n = int(input())
    l = [int(t) for t in input().split()]

    # まずはLを昇順にソートする
    l.sort()
    
    count = 0
    # 1番短い棒(i)と2番目に短い棒(j)を決める
    for i in range(0, n):
        for j in range(i+1, n):
            lb, ub = j, n
            while ub - lb > 1:
                half = (lb + ub) // 2
                if l[half] < l[i] + l[j]:
                    lb = half
                else:
                    ub = half
            count += ub - (j + 1)

    print(count)

    return 0

if __name__ == '__main__':
    sys.exit(main())

Submission status: Time Limit Exceeded

Solution code:
# D - Triangles
N = int(input())
L = list(map(int, input().split()))

L.sort(reverse=True)
count = 0

for i in range(N):
    for j in range(i+1,N):
            for k in range(j+1 ,N):
                if L[j] < L[k]+L[i] and L[k] < L[j]+L[i] and L[i] < L[j]+L[k]:
                    count += 1
                else:
                    break
print(count)

Submission status: Time Limit Exceeded

Solution code:
#defnishitaradoudarou
def abc143c():
  n=int(input())
  l=list(map(int,input().split()))
  cnt=0
  for i in range(n):
    for j in range(i+1,n):
      for k in range(j+1,n):
        #print(i,j,k)
        a=l[i];b=l[j];c=l[k]
        if a<b+c and b<a+c and c<b+a:
          cnt+=1
  print(cnt)
abc143c()

Submission status: Time Limit Exceeded

Solution code:
import bisect

n = int(input())
l = sorted(map(int, input().split()))
ll = [l[:i] for i in range(n)]

ans = 0
for i in range(2,n)[::-1]:
    for j in range(1,i)[::-1]:
        a = bisect.bisect(ll[j], l[i] + l[j])
        b = bisect.bisect(ll[j], l[i] - l[j])
#         print(l[i] + l[j], l[i] , l[j], a, ll[j], l[i] - l[j], l[i] - l[j] < ll[i][a])
        if l[i] - l[j] < ll[i][a]:
            ans += a -b
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
N = int(input())
L = [i for i in map(int, input().split())]
ans = 0

L.sort()
#print(L)
if N == 3:
    if L[0] + L[1] > L[2]:
        print("1")
        sys.exit()

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]:
                ans += 1
                #print(L[i], L[j], L[k])
            else:
                #print("break")
                break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
from bisect import bisect_right


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

"""
短いほうから２本固定して条件に合うものをさがす（長いほうから２本固定でもOK）
最長と最短を固定するとややこしくなるから、こういう場合は最長/最短どちらかから固定するほうがいいか
"""


ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        # そもそもcがaやbより大きいので、下の解法のようにilを求める必要などなく、il=で考えれば良かったんや！
        k = bisect_left(L, L[i]+L[j], lo=j)
        ans += k - j - 1



"""
ans = 0
for i in range(N):
    for j in range(i+1, N):
        l = L[j] - L[i]
        r = L[j] + L[i]
        #  a < b < c（場合によっては等号を含む）で考えたときの、b - a < cを満たすもの 
        il = bisect_right(L, l, lo=j)
        #  a < b < c（場合によっては等号を含む）で考えたときの、c < a + bを満たすもの
        ir = bisect_left(L, r, lo=j)
        # b - a < cを満たすものがない場合、飛ばす
        if il == N-1:
            break
        
        ans += ir - il - 1
"""

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N-2):
	for j in range(i+1, N-1):
		M = L[j+1::]
		k = 0
		while k<len(M) and M[k]>(L[i]-L[j]):
			k += 1
			C += 1
				
print(C)

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))

        mid = int(len(LI) / 2)
        # print("\t\t"+str(mid))
        for k in range(j + 1, N):
            c = LI[k]
            if w < c and c <= b:
                count += 1
            else:
                break



print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
N = int(sys.stdin.readline())
L = list(map(int, input().split()))

def check_tri(lis):
    if lis[0] < lis[1] + lis[2] and lis[1] < lis[0] + lis[2] and lis[2] < lis[0] + lis[1] :
        return True
    else:
        return False

count = 0
x = itertools.combinations(L, 3)

for i in x:
    if check_tri(list(i)):
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
from bisect import bisect_right


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

"""
短いほうから２本固定して条件に合うものをさがす（長いほうから２本固定でもOK）
最長と最短を固定するとややこしくなるから、こういう場合は最長/最短どちらかから固定するほうがいいか
"""


ans = 0
for i in range(N):
    for j in range(i+1, N):
        l = L[j] - L[i]
        r = L[j] + L[i]

        #  a < b < c（場合によっては等号を含む）で考えたときの、b - a < cを満たすもの 
        il = bisect_right(L, l, lo=j)
        #  a < b < c（場合によっては等号を含む）で考えたときの、c < a + bを満たすもの
        ir = bisect_left(L, r, lo=j)
        # b - a < cを満たすものがない場合、飛ばす
        if il == N-1:
            break
        
        ans += ir - il - 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import itertools


def solve(N: int, L: "List[int]"):
    bar_3 = itertools.combinations(L, 3)

    result = 0
    for a, b, c in bar_3:
        if abs(b - c) < a < b + c:
            result += 1
        else:
            pass

    print(result)
    return


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:
import bisect
N=int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
for i in range(1,N-1):
  for j in range(i+1,N):
    a=l[i]
    b=l[j]
    ans+=i-bisect.bisect_right(l[:i],b-a)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
n=int(input())
lists=list(map(int,input().split()))
listss=sorted(lists)
count=0
for i in range(n):
    for j in range(n):
        if i!=j:
            index1=bisect.bisect_left(listss, abs(listss[j]-listss[i])+1)
            index2=bisect.bisect_right(listss,listss[i]+listss[j]-1)
            count+=(index2-index1)
            x=abs(listss[j]-listss[i])
            if  x+1<=listss[i]:
                count-=1
            if x+1<=listss[j]:
                count-=1
print(count//6)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
import bisect
import itertools


def solve(N: int, L: "List[int]"):
    L.sort()
    ans = 0
    ans = sum([bisect.bisect_left(L[j+1:], a+b)
               for i, a in enumerate(L[:-1], 1) for j, b in enumerate(L[i:], i)])
    print(ans)
    return


def main():
    def iterate_tokens():
        for sent in sys.stdin:
            for token in sent.split():
                yield token
    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())
listed = sorted(list(map(int,input().split())))
count = 0
for x in range(N-1,-1,-1):
    for y in range(x-1,-1,-1):
        for z in range(y-1,-1,-1):
            if listed[x] > listed[y] + listed[z]:
                break
            if listed[y] < listed[z] + listed[x] and listed[x] < listed[y] + listed[z] and listed[z] < listed[x] + listed[y]:
                count += 1
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import bisect

n = int(input())
l = np.array(list(map(int, input().split())))

l.sort()

ans = 0
for a in range(n - 2):
    for b in range(a + 1, n - 1):
        ans += bisect.bisect_left(l, l[a] + l[b]) - (b + 1)
print(ans)

Submission status: Time Limit Exceeded

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

L.sort()

cnt = 0
for a in range(n):
  for b in range(a+1,n):
    for c in range(b+1,n):
      if L[c] < L[a] + L[b]:
        cnt += 1
      else:
        break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_right#(sorted_list,x)で昇順ソート位置

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

l.sort()
ans = 0
for i in reversed(range(2,n)):
  for j in reversed(range(1,i)):
    s = l[i]-l[j]
    ans += len(l[:j])-bisect_right(l[:j],s)
print(ans)

Submission status: Time Limit Exceeded

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

import sys

def search(a,x):
    if a[-1]<=x: return -1
    if len(a)==1:
        if a[0]>x: return 0
        else: return -1
    #
    mid = (len(a)+1)//2 - 1
    if x<a[mid]: return search(a[:mid+1],x)
    else: return mid + 1 + search(a[mid+1:],x)
        
    
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(1,n):
    for j in range(i+1,n):
        k = search(l[:i], l[j]-l[i])
        if k<0: continue
        c += i-k
#
print(c)

Submission status: Time Limit Exceeded

Solution code:
def upperbound(arr, c):
    l, r = -1, len(arr)
    while r-l>1:
        mid = (l+r)//2
        if arr[mid]>c: r = mid
        else: l = mid
    return r

n, *l = map(int, open(0).read().split())
l.sort()

ans = 0
bd = [0]*(n+1)
for k in range(2,n):
    for j in range(1,k):
        ans += j - upperbound(l[:j], l[k]-l[j])
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N-2):
	for j in range(i+1, N-1):
		K = L[j+1::]
		K = list(filter(lambda x:x>(L[i]-L[j]), K))
		C += len(K)
				
print(C)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
import sys
readline = sys.stdin.readline
N = int(readline())
ls = list(map(int, readline().split()))
ls.sort()

ans = 0
for i in range(N-2):
  a = ls[i]
  for j in range(i+1, N-1):
    b = ls[j]
    pos = bisect.bisect_left(ls, a+b, lo=j)
    ans += pos - j - 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

#+++++

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


def main():
	n = int(input())
	ll = list(map(int, input().split()))
	n=len(ll)
	ll.sort(reverse = True)
	pa(ll)
	
	ret = 0
	for i,l1 in enumerate(ll[:-2]):
		for j, l2 in enumerate(ll[i+1:-1]):
			pa(ret)
			if not is_tr(l1, l2, ll[j+1]):
				break
			if is_tr(l1,l2,ll[-1]):
				pa('ggg')
				ret += n-(i+1+j+1)
				continue
			ok=i+j+1
			ng=n-1
			while ng-ok > 1:
				mid = (ok+ng)//2
				if is_tr(l1,l2,ll[mid]):
					ok =mid
				else:
					ng = mid
			pa((ok,j+i+1))
			ret += (ok - (i+j+1))
	
	print(ret)
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Time Limit Exceeded

Solution code:
def binarySearch(A, head, tail, key):
	left = head
	right = tail - 1 # 探索範囲:[left, right]
	i = 0
	while left <= right:
		mid = (left + right) // 2
		if key == A[mid]:
			return mid
		elif key < A[mid]:
			right = mid - 1
			i = mid
		else:
			left = mid + 1
			i = mid + 1
	return i

if __name__ == "__main__":
	N = int(input())
	L = list(map(int, input().split()))
	L.sort()
	# print(L)
	sum = 0
	for a in range(N - 2):
		for b in range(a + 1, N - 1):
			key = L[a] + L[b]
			c = binarySearch(L, b + 1, N, key)
			# print(a, b, c, c - b - 1)
			sum += c - b - 1
	print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [int(i) for i in input().split()]
l.sort()
ans = 0
for i in range(n):
    a = l[i]
    for j in range(i + 1, n):
        b = l[j]
        c = a+b
        for k in range(j + 1, n):
            if l[k] < c:
                ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
import sys
readline = sys.stdin.readline
N = int(readline())
ls = sorted(list(map(int, readline().split())))
#ls *= 100
#N *= 100
#print(N, ls)
#print(len(ls))

ans = 0
for i in range(N-2):
  a = ls[i]
  for j in range(i+1, N-1):
    b = ls[j]
    #pos = bisect.bisect_left(ls, a+b)
    #ans += pos - j - 1
    pos = bisect.bisect_left(ls, a+b, lo=j)
    ans += pos - j - 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
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)

Submission status: Time Limit Exceeded

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

def bisec(i,k): #ギリギリokなj<=j_を探す
    ci = i
    ck = k
    i+=1
    k-=1
    j = (i+k)//2
    while k-i>1:
        if l[ci]+l[j]>l[ck]:
            k = j
            j = (i+k)//2
        else:
            i = j+1
            j = (i+k)//2
    if l[ci]+l[i]>l[ck]:
        return i
    elif l[ci]+l[i+1]>l[ck]:
        return i+1
    else:
        return -1

for i in range(n):
#     for j in range(i+1,n):  i<j<kでl[i]+l[j]>l[k]を満たすものを二分探索
    for k in range(i+2,n):
        j = bisec(i,k)
        if j>=0:
            ans += k-j
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
ans = 0
for i in range(n):
    for j in range(i + 1, n):
        for k in range(j + 1, n):
            a, b, c = l[i], l[j], l[k]
            if a >= b + c:
                continue
            if b >= c + a:
                continue
            if c >= a + b:
                continue
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int,input().split()))
L.sort()
cnt = 0
for i in range(N-1,1,-1):
    for j in range(i-1,0,-1):
        temp = L[i] - L[j]
        k = j-1
        while L[k] > temp and k >= 0:
            cnt += 1
            k -= 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import itertools

def main():
    N = int(input())
    L = [int(x) for x in input().split()]
    ans = 0
    for a, b, c in itertools.combinations(L, 3):
        if a < b + c and b < a + c and c < a + b:
            ans += 1

    print(ans)



    

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def Triangles(N, L):
    asc = sorted(L)
    desc = sorted(L, reverse=True)
    cnt = 0
    for i in range(N-2):
        if desc[i] >= desc[i+1]*2: break
        for j in range(i+1, N-1):
            nokori = desc[i] - desc[j]
            tmp = [asc[i] for i in range(N-j-1) if asc[i] > nokori]
            cnt += len(tmp)
    return cnt

N = int(input())
L = [int(x) for x in input().split()]
print(Triangles(N, L))

Submission status: Time Limit Exceeded

Solution code:
import bisect

N = int(input())

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

e = sorted(d)

ans = 0

for i in range(N-2):
    for j in range(i+1,N-1):
        S = e[i] + e[j]
        ans += bisect.bisect_left(e[j+1:],S)
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
L = np.array([int(l) for l in input().split(' ')])

counts =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]) & (abs(L[i]-L[j])<L[k]):
                counts+=1
        
        
                                                      
print(counts)

Submission status: Time Limit Exceeded

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

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):
  for b in range(a, 1001):
    for c in range(b, 1001):
      if a < b + c and b < c + a and c < a + b:
        if a == b == c:
          ans += x[a] * (x[b]-1) * (x[c]-2) // 6
        elif a == b:
          ans += x[a] * (x[b]-1) // 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 bisect
import copy
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

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




N=one_int()
L=many_int()

L=sorted(L)

sankaku_dict={}
count=0
for i in range(len(L)-1):
    low=L[i]
    Lcopy = copy.deepcopy(L)
    Lcopy.remove(low)
    
    for j in range(i+1, len(L)):
        #すでに探索済みか判定
        high = L[j]
        Lcopy2 = Lcopy[j:]
        
        #下限を取得    
#         under_index = bisect.bisect_right(Lcopy2,high-low)
        #上限を取得
        high_index = bisect.bisect_left(Lcopy2,high+low)
        
#         for va in Lcopy2[under_index:high_index]:
#             temp = sorted([high,low, va])
#             temp = (temp[0]*10**8) + (temp[1]*10**4) + temp[2]
#             sankaku_dict[temp]=1
        count+=len(Lcopy2[:high_index])

# len(sankaku_dict)
print(count)
# print(len(sankaku_dict.keys()))

Submission status: Time Limit Exceeded

Solution code:
input1 = int(input())
input_list = list(map(int, input().split()))
 
cnt = 0
for i in range(input1):
    for j in range(i+1, input1):
        for k in range(j+1, input1):
            if (input_list[i]  < input_list[j] + input_list[k]
                and input_list[j] < input_list[k] + input_list[i]
                and input_list[k] < input_list[i] + input_list[j]):
#                print("  ", input_list[i], input_list[j], input_list[k])
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left#(sorted_list,x)で昇順ソート位置

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):
    s = l[i]+l[j]
    ans += bisect_left(l[j+1:],s)
print(ans)

Submission status: Time Limit Exceeded

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


def solve(N: int, L: "List[int]"):
    L.append(2001)
    L.sort()
    sum=0
    if len(L)<3:
        print(sum)
        return

    for i in range(N-2):
        for j in range(i+1,N-1):
            tansum=L[i]+L[j]
            if tansum<=L[j+1]:
                continue
            for k in range(j+2,N+1):
                if (tansum<=L[k]):
                    sum=sum+k-j-1
                    break
    print(sum)
    return


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())
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))
        for k in range(j + 1, N):
            c = LI[k]
            if w < c and c < e:
                count += 1
            else:
                break



print(count)

Submission status: Time Limit Exceeded

Solution code:
##################################################################
def main():
    Input = [input() for i in range(2)]
    N = int(Input[0])
    L = list(map(int,Input[1].split()))
    L.sort()
    ans = 0
    for i1 in range(0,N-2):
        a = L[i1]
        for i2 in range(i1+1,N-1):
            b = L[i2]
            i3 = i2+1
            while (i3<N and a+b>L[i3]):
                i3 += 1
            ans += i3 - i2 -1
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

# comb = list(combinations(l, 3))

ans = 0
l.sort(reverse=True)
# for i in comb:
#     a = i[0]
#     b = i[1]
#     c = i[2]
#     if (a < b + c) and (b < c + a) and (c < a + b):
#         ans += 1

for i in range(n):
    for j in range(i + 1, n):
        a = l[i]
        b = l[j]
        for k in range(j + 1, n):
            c = l[k]
            # print(a, b, c)
            if (a < b + c) and (b < c + a) and (c < a + b):
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

L.sort(reverse=True)

cnt = 0
for a in range(n):
  for b in range(a+1,n):
    for c in range(b+1,n):
      if L[a] >= L[b] + L[c]:
        break
      elif L[b] >= L[a]+L[c]:
        break
      elif L[c] >= L[a] + L[b]:
        break
      else:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
    number_of_bars = int(input())
    bar_list = list(map(int,input().split()))
    print(len(traiangle_maker(bar_list))//3)
    # print(traiangle_maker(bar_list))


def traiangle_maker(bar_list):
    bar_list.sort(reverse=True)
    result_list = []
    for i in range(len(bar_list)):
        for j in range(len(bar_list)):
            if i == j or bar_list[i] < bar_list[j]:
                continue
            minimum_length = bar_list[i] - bar_list[j]
            max_length = bar_list[i] + bar_list[j]
            for k in bar_list:
                if k < minimum_length:
                    break
                if k > minimum_length and k < max_length and k != bar_list[i] and k != bar_list[j]:
                    traiangle = [bar_list[i],bar_list[j],k]
                    result_list.append(traiangle)

    return result_list


main()

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations 

n = int(input())
arr = list(map(int, input().split()))

def is_triangle(a, b, c):
	return (a+b > c and a+c > b and b+c > a)

count = 0

for a, b, c in combinations(arr, 3):
	if is_triangle(a, b, c):
		count += 1
print(count)

Submission status: Time Limit Exceeded

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

L = sorted(L)
ans = 0
for i in range(N - 2):
    for j in range(i + 2, N):
        k = j - 1
        while L[k] > L[j] - L[i] and k > i:
            k -= 1
        ans += j - k - 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ln = sorted([int(i) for i in input().split()])
ans = 0
for j in range(n - 2):
    for k in range(j + 1, n - 1):
        if j >= k:
            continue
        else:
            if ln[j] + ln[k] > ln[k + 1]:
                aa = ln[k + 1::]
                aa.append(ln[j] + ln[k])
                aa.sort()
                aaa = aa.index(ln[j] + ln[k])
                ans += aaa
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect

n = int(input())
*l, = map(int, input().split())
l = sorted(l)
count = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        c = bisect.bisect_left(l[j+1:], l[i]+l[j])
        count += c
print(count)

Submission status: Time Limit Exceeded

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

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
		
		for k in range(j + 1, m):
			c = ls[k]
			ts.add((a, b, c))
		
print(len(ts))

Submission status: Time Limit Exceeded

Solution code:
import bisect

N=int(input())
D=[int(x) for x in input().split()]

D.sort()
total=0

for i in range(N-2):
    for j in range(i+1,N-1):
        total+= bisect.bisect_left(D[j+1:],D[i]+D[j])

print(total)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))


n = inp()
a = inpl()
ans = 0
a.sort()
for i in range(n-2):
    for j in range(i+1,n-1):
        for k in range(j+1,n):
            if a[k] < a[j] + a[i]:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

L.sort(reverse=True)

cnt = 0
for a in range(n):
  for b in range(a+1,n):
    for c in range(b+1,n):
      if L[a] >= L[b] + L[c]:
        continue
      elif L[b] >= L[a]+L[c]:
        continue
      elif L[c] >= L[a] + L[b]:
        break
      else:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import bisect
n = int(input())
l = np.array([int(i) for i in input().split()])
l = np.sort(l)

ans = 0
for i in range(1, n - 1):
    m = l[:i]
    for j in range(i + 1, n):
        tmp = l[j] - l[i]
        cnt = bisect.bisect_right(m, tmp)
        ans += i-cnt
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    l = list(map(int, input().rstrip('\n').split()))
    l.sort()
    cnt = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            for k in range(j+1, n):
                if l[i] + l[j] > l[k]:
                        cnt += 1
                else:
                    break
    print(cnt)


if __name__ == '__main__':
    slove()