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())
l = list(map(int,input().split()))

l.sort(reverse = 1)
ans = 0

for i in range(n-2):
    for j in range(i+1,n-1):
        indL = j
        indR = len(l)-1
        while indL <= indR:
            #print(i,indL,indR)
            if l[i] < l[indL] + l[indR]:
                ans += (indR - indL)
                break
            else:
                indR -= 1
            
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
t=tuple(map(int,input().split()))
T=0
for i in range(n-2):
  a=t[i]
  for j in range(n-i-1):
    b=t[i+j+1]
    for k in range(n-i-j-2):
      c=t[i+j+k+2]
      if a<b+c and b<c+a and c<a+b:
        T+=1
print(T)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=input().split()
for i in range(n):
	l[i]=int(l[i])
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] and l[j]<l[i]+l[k] and l[k]<l[i]+l[j]:
				ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

cnt = 0
for a, b, c in list(itertools.combinations(L, 3)):
    if a < b + c and b < c + a and c < a + b:
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def BinarySearch(lis, i, j):
    low, high = 0, len(lis)
    border = j-i
    while low < high:
        key = (low+high)//2
        value = lis[key]
        if value <= border:
            low = min([key, low]) + 1
        else:
            high = max([key, high]) - 1
    return low

n = int(input())
l = list(int(x) for x in input().split())
l.sort()
ans = 0
for i in range(n):
    for j in range(i+1, n):
        key = i-BinarySearch(l, l[i], l[j])
        ans += max([key, 0])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

n = int(input())
l = list(int(x) for x in input().split())
l.sort()
ans = 0
for i in range(n):
    for j in range(i+1, n):
        ans += i-bisect.bisect_left(l[:i], l[j]-l[i]+1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def BinarySearch(lis, i, j):
    low, high = 0, len(lis)
    border = j-i
    while low < high:
        key = (low+high)//2
        value = lis[key]
        if value <= border:
            low = min([key, low]) + 1
        else:
            high = max([key, high]) - 1
    return low

n = int(input())
l = list(int(x) for x in input().split())
l.sort()
ans = 0
for i in range(n):
    for j in range(i+1, n):
        key = i-BinarySearch(l[:i], l[i], l[j])
        ans += key
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#
import sys
sys.setrecursionlimit(12345678)
import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop


def main():
    n = int(input())
    ll = list(map(int, input().split()))
    ll.sort()

    ans = 0
    for i in range(n - 2):
        for j in range(i + 1, n - 1):
            rest = ll[j + 1:]
            a_plus_b = ll[i] + ll[j]
            pos_right = bisect.bisect_left(rest, a_plus_b)

            b_minus_a = ll[j] - ll[i]
            pos_left = bisect.bisect_right(rest, b_minus_a)
            count = pos_right - pos_left

            ans += count
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = input()
seq = [int(x) for x in input().split()]
total = []
x = list(itertools.combinations(seq,3))
 
for i in range(len(x)):
    if (x[i][0] < x[i][1] + x[i][2]):
        if x[i][1] < (x[i][2] + x[i][0]):
            if  x[i][2] < (x[i][1] + x[i][0]):
                total.append(x[i])
print(len(total))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
l = list(map(int,input().split()))
import bisect
l.sort()
count = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        count += bisect.bisect_left(l[j+1:],l[i]+l[j])
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int,input().split()))
import bisect
l.sort()
count = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        a = l[i]
        b = l[j]
        wa = a+b
        s = bisect.bisect_left(l[j+1:],wa)
        count += s
        # print(l,a,b,s)
print(count)

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-2):
    for j in range(i+1,n-1):
        a = l[i]
        b = l[j]
        wa = a+b
        l_part = [ i for i in l[j+1:] if i<wa ]
        count += len(l_part)
print(count)

Submission status: Time Limit Exceeded

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

l.sort()

# print(l)

count = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        a = l[i]
        b = l[j]
        wa = a+b
        l_part = [ i for i in l[j+1:] if i<wa ]
        # print(a,b,l_part)
        for c in l_part:
            if b < a+c and a < b+c :
                count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
b = [int(elem) for elem in input().split()]
b.sort(reverse=True)
counter = 0
for i in range(a-2):
	for j in range(i+1, a-1):
		for k in range(j+1, a):
			if b[i] < b[j] + b[k] and b[j] < b[i] + b[k] and b[k] < b[i] + b[j]:
				counter += 1
			else:
				break
print('{}'.format(counter))

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
b = [int(elem) for elem in input().split()]
counter = 0
for i in range(a-2):
	for j in range(i+1, a-1):
		for k in range(j+1, a):
			if b[i] < b[j] + b[k] and b[j] < b[i] + b[k] and b[k] < b[i] + b[j]:
				counter += 1
print('{}'.format(counter))

Submission status: Time Limit Exceeded

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

for i in range(1,N):
    for j in range(i):
      alis.append([i,(L[i]+L[j])])
    
s = 0
    
for i in range(len(alis)):
    for j in range(alis[i][0]+1, N):
        if L[j] < alis[i][1]:
            s += 1

print(s)

Submission status: Time Limit Exceeded

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

L.sort()
L.reverse()
#print(L)
count=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[i]-L[j]:
          count +=1
      else:
        break
            
print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
x = list(map(int,input().split()))

ct = 0
for i in list(itertools.combinations(x,3)):
    
    a = max(i[0]-i[1],i[1]-i[0])
    b = min(i[0]+i[1],i[1]+i[0])
    if(a<i[2] and i[2] < b):
        ct += 1
print(ct)

Submission status: Time Limit Exceeded

Solution code:
def find(target, lst):
    l = len(lst)
    if(l == 0):
        return 0
    if(l == 1):
        if(lst[0] < target):
            return 1
        else:
            return 0
    h = l//2
    if(lst[h-1] < target):
        return h + find(target, lst[h:])
    else:
        return find(target, lst[:h])

n = int(input())
l_list = list(map(int, input().split()))
l_list.sort()
count = 0
for i, li in enumerate(l_list):
    for j, lj in enumerate(l_list[i+1:], i+1):
        count += find(li + lj, l_list[j+1:])
print(count)

Submission status: Time Limit Exceeded

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

L.sort()
L.reverse()
#print(L)
count=0

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

Submission status: Time Limit Exceeded

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

#print(N,L)
count=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]:
        if L[j]<L[i]+L[k]:
          if 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()))

l.sort(reverse=True)
c = 0
for i in range(len(l) - 2):
    for j in range(i + 1, len(l) - 1):
        if l[i] >= l[j] + l[j+1]:
            break
        for k in range(j + 1, len(l)):
            if l[i] < l[j] + l[k]:
                c += 1
            else:
                break
print(c)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

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

count = 0
for a, b, c in combinations(L, 3):
    if a < b + c and b < c + a and c < a + b:
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

c = 0
for x in itertools.combinations(l_lst, 3):
    if (x[0] < x[1] + x[2] and
            x[1] < x[0] + x[2] and
            x[2] < x[0] + x[1]):
        c += 1

print(c)

Submission status: Time Limit Exceeded

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

ans = []
for i in range(N):
	a=L[i]
	for j in range(N):
		b=L[j]
		for k in range (N):
			c=L[k]
			if not i < j < k: #インデックスは降順で統一。その他は重複のため計上せず。
				continue
			elif a >= b + c or b >= c + a or c >= a + b: #三角形成立条件を満たさない組み合わせは計上せず。
				continue
			else:
				ans.append([a, b, c])
print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [int(i) for i in input().split()]
count = 0

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000000)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

import itertools as it

def main():
    N=ii()
    L=lmis()
    ans=0
    for a,b,c in it.combinations(L,3):
        m=max(a,b,c)
        if m<a+b+c-m:
            ans+=1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000000)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

import itertools as it

def main():
    N=ii()
    L=lmis()
    ans=0
    for a,b,c in it.combinations(L,3):
        if a+b>c and a+c>b and b+c>a:
            ans+=1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
Ln=list(map(int,input().split()))
Ln.sort(reverse=True)
Ans=0
 
for i in range(N-2):
    di=i+1
    for j in range(N-di-1):
        dj=di+j+1
        for k in range(N-dj):
            dk=dj+k+1
            a=Ln[di-1]
            b=Ln[dj-1]
            c=Ln[dk-1]
            if((a+b)>c)&((b+c)>a)&((c+a)>b) :
                Ans +=1
 
print(Ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

for i in range(n-2):
    shortest = l[i]
    for j in range(i+1, n-1):
        second = l[j]
        k = bisect_left(l[j+1:], shortest + second)
        ans += k

print(ans)

Submission status: Time Limit Exceeded

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

tmp=0
for i in range(n-2):
    for j in range(i+1, n-1):
        for k in range(j+1, n):
            a,b,c = l[i],l[j],l[k]
            if((a+b>c) & (b+c>a) & (c+a>b)):
                tmp+=1
print(tmp)

Submission status: Time Limit Exceeded

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

num_triangle = 0

L.sort() # L[i]を昇順にソート

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]:
                num_triangle += 1
            else:
                break

print(num_triangle)

Submission status: Time Limit Exceeded

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

num_triangle = 0

L.sort() # L[i]を昇順にソート

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]:
                num_triangle += 1

print(num_triangle)

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 - 2):
    for j in range(i + 1, n - 1):
        for k in range(j + 1, n):
            if l[k] < l[i] + l[j]:
                count += 1
            else:
                break
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 - 2):
    for j in range(i + 1, n - 1):
        for k in range(j + 1, n):
            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:
import itertools

n = int(input())
data = list(map(int, input().split()))
count = 0

def ifTriangle(a, b, c):
    if not a < b + c:
        return False
    elif not b < c + a:
        return False
    elif not c < a + b:
        return False
    return True

comb = list(itertools.combinations([i for i in range(n)],3))

for _ in comb:
    if ifTriangle(data[_[0]], data[_[1]], data[_[2]]):
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools

n = int(input())
data = list(map(int, input().split()))
count = 0

def ifTriangle(a, b, c):
    if not a < b + c:
        return False
    elif not b < c + a:
        return False
    elif not c < a + b:
        return False
    return True

comb = list(itertools.combinations(data,3))


for _ in comb:
    if ifTriangle(_[0], _[1], _[2]):
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
l = sorted(([int(x) for x in input().split()]))
count = 0
for i in range(n-1,-1,-1):
    for j in range(i-1,-1,-1):
        k = bisect.bisect_left(l[i+1::],l[i]+l[j])
        count += k
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
l = list(map(int, input().split()))
l_new = sorted(l)
count = 0
for i in range(n-1,-1,-1):
    for j in range(i-1,-1,-1):
        k = bisect.bisect_left(l_new[i+1::],l_new[i]+l_new[j])
        count += k
print(count)

Submission status: Time Limit Exceeded

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

sum = 0
count_b = 0
count_c = 0
for a in dlist:
    count_b = count_b + 1
    count_c = count_b
    for b in range(count_b, N):
        count_c = count_c + 1
        for c in range(count_c, N):
            if a < (dlist[b] + dlist[c]):
                if dlist[b] < (dlist[c] + a):
                    if dlist[c] < (a + dlist[b]):
                        sum  = sum + 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import bisect

n = int(input())
l = sorted([int(li) for li in input().split()])

sum = 0
for i in range(n)[::-1]:
    for j in range(i)[::-1]:
        k = bisect.bisect_right(l[:j],l[i]-l[j])
        sum += j-k

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import bisect

n = int(input())
l = sorted([int(li) for li in input().split()])

sum = 0
for i in range(n)[::-1]:
    for j in range(i)[::-1]:
        k = bisect.bisect_right(l,l[i]-l[j])
        sum += len(l[k:j])

print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = sorted(list(map(int, input().split())))
cnt = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        c_max = l[i] + l[j]
        for k in range(j + 1, n):
            if l[k] < c_max:
                cnt += 1
            else:
                break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def trianglable(a:int, b:int, c:int) -> bool:
    return (a < b + c) & (b < a + c) & (c < a + b)

n = int(input())
l = [int(li) for li in input().split()]

print(sum(trianglable(l[i],l[j],l[k]) 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:
n = int(input())
l = sorted(list(map(int, input().split())))
cnt = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        c_max = l[i] + l[j]
        for k in range(j + 1, n):
            if l[k] >= c_max:
                c_index = k
                break
        else:
            c_index = n
        cnt += c_index - (j + 1)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def comp(ext,list0,dev):
    c=int((ext[0]+ext[1])/2)
    if list0[int((ext[0]+ext[1])/2)]>dev:
        return ext[0],int((ext[0]+ext[1])/2)
    else:
        return int((ext[0]+ext[1])/2),ext[1]
def bis(a,b,list0,dev):
    ls=[a,b]
    while ls[0]+1<ls[1]:
        ls=comp(ls,list0,dev)
    if list0[ls[0]]==dev:
        return ls[0]-1
    elif list0[ls[1]]==dev:
        return ls[1]-1
    else:
        return ls[0]
n=int(input())
l=[int(x) for x in input().split()]
l.sort()
s=0
for a in range(0,n-2):
    for b in range(a+1,n-1):
        if l[a]+l[b]>l[n-1]:
            c=n-1
        elif l[a]+l[b]<l[b+1]:
            c=b
        else:
            c=bis(b+1,n-1,l,l[a]+l[b])
        s=s+c-b
print(s)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

    ans = 0
    for a, b, c in itertools.permutations(l, 3):
        if c >= a + b:
            continue
        if c <= abs(a - b):
            continue
        ans += 1
    ans = ans // 6
    print(ans)
    return()

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:


def comp(ext,list0,dev):
    c=int((ext[0]+ext[1])/2)
    if list0[int((ext[0]+ext[1])/2)]>dev:
        return ext[0],int((ext[0]+ext[1])/2)
    else:
        return int((ext[0]+ext[1])/2),ext[1]


def bis(a,b,list0,dev):
    ls=[a,b]
    while ls[0]+1<ls[1]:
        ls=comp(ls,list0,dev)
    if list0[ls[0]]==dev:
        return ls[0]-1
    elif list0[ls[1]]==dev:
        return ls[1]-1
    else:
        return ls[0]


n=int(input())
l=[int(x) for x in input().split()]
l.sort()
s=0
for a in range(0,n-2):
    for b in range(a+1,n-1):
        if l[a]+l[b]>l[n-1]:
            c=n-1
        elif l[a]+l[b]<l[b+1]:
            c=b
        else:
            c=bis(b,n-1,l,l[a]+l[b])
        s=s+c-b
print(s)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

    ans = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            # a = l[i], b = l[j], c = ?
            # c < a + b
            # max_c = a + b - 1
            # abs(a - b) < c
            # min_c = abs(a - b) + 1
            a = l[i]
            b = l[j]
            c = l[j+1:]
            max_c = bisect.bisect_right(c, a + b - 1)
            min_c = bisect.bisect_left(c, abs(a - b + 1))
            ans += max_c - min_c
    print(ans)
    return()

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
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] and l[j] < l[i] + l[k] and l[k] < l[i] + l[j]:
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, *l = map(int, open(0).read().split())

    ans = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            for k in range(j+1, n):
                _l = [l[i], l[j], l[k]]
                if _l[0] >= _l[1] + _l[2]:
                    continue
                if _l[1] >= _l[2] + _l[0]:
                    continue
                if _l[2] >= _l[0] + _l[1]:
                    continue
                ans += 1
    print(ans)
    return()

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
A=int(input())
L=input().split(' ')
L = [int(s) for s in L]
L.sort()
#print(L[3:])
flag = 0
for i in range(A):
    for j in range(i+1,A):
        low = [k for k, x in enumerate(L[j:]) if x < L[i]+L[j]]
        flag = flag + max(low)-j+j
print(flag)

Submission status: Time Limit Exceeded

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

def binary_search(list, item,J):
    low = J
    high = len(list) - 1
    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if (guess <= item and list[mid+1] >= item):
            return mid
        elif guess > item:
            high = mid -1
        else:
            low = mid + 1
    return None
    
for i in range(n-1):
    for j in range(i+1,n-1):
        if l[i] + l[j] > l[-1]:
            ans = ans + (n-j)*(n-j-1)//2
            #print(i,j)
            #print(ans)
            break
        else:
            k = binary_search(l,l[i]+l[j]-1,j)
            ans = ans +k-j
            #print(i,j,k)
            #print(ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
A=int(input())
L=input().split(' ')
L = [int(s) for s in L]

L.sort()
flag = 0
for i in range(A):
    for j in range(i+1,A):
        low = [k for k, x in enumerate(L) if x < L[i]+L[j]]
        flag = flag + max(low)-j
print(flag)

Submission status: Time Limit Exceeded

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

def chk(a,b,c):
     if a<b+c and b<c+a and c<a+b:
          return 1
     else:
          return 0
cn=0
for a in range(n-2):
     for b in range(a+1,n-1):
          for c in range(b+1,n):
               f=chk(l[a],l[b],l[c])
               if f==1:
                    cn+=1
print(cn)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())
ls = list(map(int, input().split()))
ls.sort()

ans = 0

ab = np.zeros(2001)

ab[(ls[0] + ls[1])] += 1

for i in range(2,n):
    l_temp = ls[i]
    ans += sum(ab[l_temp+1:])

    for j in range(i):
        ab[( l_temp + ls[j] )] += 1

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
arr=[int(_) for _ in input().split()]
count=0

for i in range(N-2):
  a=arr[i]
  for j in range(i+1,N-1):
    b=arr[j]
    s=a+b
    for k in range(j+1,N):
      c=arr[k]
      if c < s:
        t=b+c
        if a < t:
          u=c+a
          if b < u:
            count=count+1
print(count)

Submission status: Time Limit Exceeded

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

u.sort()
cnt = 0
for x in range(n-2):
    for y in range(x+1, n-1):
        for z in range(n-1, y, -1):
            if(u[z] < u[x]+u[y]):
                cnt += z - y
                break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
arr=[int(_) for _ in input().split()]
count=0

for i in range(N-2):
  a=arr[i]
  for j in range(i+1,N-1):
    b=arr[j]
    s=a+b
    for k in range(j+1,N):
      c=arr[k]
      t=b+c
      u=c+a
      if c < s:
        if a < t:
          if b < u:
            count=count+1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
arr=[int(_) for _ in input().split()]
count=0

for i in range(N-2):
  for j in range(i+1,N-1):
    for k in range(j+1,N):
      a=arr[i]
      b=arr[j]
      c=arr[k]
      if c < a+b:
        if a < b+c:
          if b < c+a:
            count=count+1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
arr=[int(_) for _ in input().split()]
count=0

for i in range(N-2):
  for j in range(i+1,N-1):
    for k in range(j+1,N):
      a=arr[i]
      b=arr[j]
      c=arr[k]
      if a < b+c:
        if b < c+a:
          if c < a+b:
            count=count+1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int,input().split()))
l.sort() # 昇順にソート

count = 0
for i in range(0,n-2):
    a = l[i]
    for j in range(i+1, n-1):
        ab = a+l[j]
        k=j+1
        while (k<n and l[k]< ab):
            k += 1
        count += k-(j+1)
print(count)

Submission status: Time Limit Exceeded

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

u.sort()
cnt = 0
for x in range(n-2):
    for y in range(x+1, n-1):
        for z in range(y+1, n):
            if(u[z] < u[x]+u[y]):
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

res = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        idx = bisect_left(L[j+1:], L[i]+L[j])
        res += idx

print(res)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
arr=[int(_) for _ in input().split()]
count=0

for i in range(N):
  for j in range(i+1,N):
    for k in range(j+1,N):
      a=arr[i]
      b=arr[j]
      c=arr[k]
      if a < b+c:
        if b < c+a:
          if c < a+b:
            count=count+1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
N = int(input())
L = list(map(int, input().split()))
L.sort()
res = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        idx = bisect_left(L[j+1:], L[i]+L[j])
        res += idx
print(res)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

arr=input().split(" ")
count=0

for i in range(N):
  for j in range(i+1,N):
    for k in range(j+1,N):
      a=int(arr[i])
      b=int(arr[j])
      c=int(arr[k])
      if a < b+c:
        if b < c+a:
          if c < a+b:
            count=count+1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int,input().split()))
l.sort() # 昇順にソート

count = 0
for i in range(0,n-2):
    a = l[i]
    for j in range(i+1, n-1):
        ab = a+l[j]
        # バイナリサーチでl[k] < a+b  となる最小のkを探す
        # j+1, ..., k で三角形ができる k-j 個
        left = j+1
        right = n
        while (left < right):
            middle = (left + right) // 2
            #print('left: {} middle:{} right: {}'.format(left, middle, right))
            c = l[middle]
            if (c < ab):
                left = middle+1 #  l[left-1]は三角形が作れる
            elif( ab <= c):
                right = middle  #  l[right] は三角形が作れない
        # left-1 が argmin_k(a+b>l[k])
        count += left-1 - j
print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools
from bisect import bisect_left

def comb_list(seq:list, n:int):
    return list(itertools.combinations(seq, n))

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

res = 0
for (i, j) in list(comb_list(list(range(N-1)), 2)):
    res += bisect_left(L[j+1:],L[i]+L[j])
print(res)

Submission status: Time Limit Exceeded

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

cnt = 0
for x in range(n-2):
    for y in range(x+1, n-1):
        for z in range(y + 1, n):
            if(u[x] < u[y]+u[z] and u[y] < u[x]+u[z] and u[z] < u[x]+u[y]):
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int,input().split()))
count = 0
for a in range(n-1):
  for b in range(n-(a+1)):
    for c in range(n-(a+b+2)):    
      if l[a]+l[a+b+1]+l[a+b+c+2] > max(map(int,[l[a],l[a+b+1],l[a+b+c+2]]))*2:
        count+=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

res = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        idx = bisect_left(L[j+1:], L[i]+L[j])
        res += idx

print(res)

Submission status: Time Limit Exceeded

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

cnt = 0
for x in range(n-2):
    for y in range(x+1, n-1):
        for z in range(y+1, n):
            if(l[x] < l[y]+l[z] and l[y] < l[x]+l[z] and l[z] < l[x]+l[y]):
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n=int(input())
l=np.array(list(map(int, input().split())))
l.sort()
counter=0
for i in range(2, n):
  for j in range(1, i):
    counter+=j-np.sum(l[:j]<=(l[i]-l[j]))
                      
print(counter)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [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 (l[i] < l[j] + l[k]) and \
               (l[j] < l[k] + l[i]) and \
               (l[k] < l[i] + l[j]):
               ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(n) for n in input().split()]
s = 0
for c in range(n):
    for cc in range(1, n):
        if cc <= c:
            continue
        else:
            for ccc in range(2, n):
                if ccc <= cc:
                    continue
                else:
                    ab = a[c]
                    bb = a[cc]
                    cb = a[ccc]
                    if ab < (bb + cb) and bb < (ab + cb) and cb < (ab + bb):
                        s += 1

print(s)

Submission status: Time Limit Exceeded

Solution code:
# coding: UTF-8
N = int(input())
L = list(map(int, input().split()))
L.sort()

cnt = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        sum = L[i] + L[j]

        for k in range(j+1, N):
            if sum > L[k]:
                cnt += 1
            else:
                break

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

L_MAX = 3000 + 7

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

cnt = np.zeros((L_MAX, ), dtype=np.int)
for i in range(N):
  cnt[L[i]] += 1

s = np.cumsum(cnt)

ans = 0
for _i in range(N*N):
  i = _i // N
  j = _i % N

  if i == j: continue

  a = L[i]
  b = L[j]

  c_min = abs(a-b)
  c_max = abs(a+b)

  add = s[c_max-1] - s[c_min]

  if c_min < a < c_max: add -= 1
  if c_min < b < c_max: add -= 1

  ans += add

print(ans // 6)