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()
count = 0
min = 0
max = 0
hen2 = 0

for i in range(0, N-2):
	for j in range(i+1, N-1):
		min = j+1
		max = N-1
		hen2 = L[i] + L [j]
		
		while min <= max:
			mid = (min+max) // 2
			if mid < N-1:
				if ( L[mid] < hen2 ) and ( L[mid+1] >= hen2 ):
					count += (mid - min +1)
					break
				elif ( L[mid+1] < hen2 ) :
					if mid +1 == max:
						count += (max - j)
						break
					else:
						min = mid
				elif ( hen2 <= L[mid] ):
					if min +1 == max:
						break
					else:
						max = mid
			else:
				if L[mid] < hen2 :
					count += (mid - min +1)
					break
				elif hen2 <= L[mid] :
					break

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
ary = sorted(map(int,input().split()))
dp = [[-1]*N for _ in range(N)]
c = 0
for i in range(N):
    for j in range(i,N):
        if i == j:continue
        for k in range(j,N):
            if j == k:continue
            if ary[i] > abs(ary[j] - ary[k]):
                c += 1
print(c)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int,input().split()))
a = 0
l.sort()
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]:
                a += 1
print(a)

Submission status: Time Limit Exceeded

Solution code:
input()
data=[int(i) for i in input().split()]
data.sort()
tri=0
#print(data)
for aid in range(len(data)-2):
  for bid in range(aid+1, len(data)-1):
    for cid in range(bid+1, len(data)):
      if data[aid]+data[bid]>data[cid]:
        #print(data[aid],data[bid],data[cid])
        tri=tri+1
      else:
        break
print(tri)

Submission status: Time Limit Exceeded

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

ls = sorted(l)

count = 0
for i in range(n):
    for j in range(i+1, n):
        x, y = ls[i], ls[j]
        index = bisect.bisect_left(ls[j+1:], x + y)
        count += index
print(count)

Submission status: Time Limit Exceeded

Solution code:
def check(data):
  data=[int(i) for i in data]
  if data[0]>=data[1]+data[2]:
    return False
  elif data[1]>=data[0]+data[2]:
    return False
  elif data[2]>=data[0]+data[1]:
  	return False
  else:
    return True
input()
data=[i for i in input().split()]
triangles=set()
for id1,i in enumerate(data):
  for id2,j in enumerate(data):
    if id1==id2:
      continue
    for id3,k in enumerate(data):
      if id1==id3 or id2==id3:
        continue
      tri=[i,j,k]
      tri.sort()
      if "-".join(tri) not in triangles:
        if check(tri):
          triangles.add("-".join(tri))
print(len(triangles))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = [int(x) for x in input().split(" ")]
l.sort(reverse=True)
num =0

for i in range(N-2):
  for j in range(i+1,N-1):
    if l[i] >= 2*l[j]:
      break
    else:
      for k in range(j+1,N):
        if l[i]<l[j]+l[k]:
          # print(l[i],l[j],l[k])
          num+=1
        else:
          break
print(num)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l_list = list(map(int,input().split()))
l_list.sort()
 
count = 0
 
# prevに前の位置を保存
for i,l1 in enumerate(l_list):
    if i <= n-3:
        for j,l2 in enumerate(l_list[i+1:]):
            num = l1 + l2
            for k,l3 in enumerate(l_list[i+j+2:]):
                if l3 < num:
                    count += 1
                else:
                    break
 
 
print(count)

Submission status: Time Limit Exceeded

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

count = 0

# prevに前の位置を保存
for i,l1 in enumerate(l_list):
    if i <= n-3:
        for j,l2 in enumerate(l_list[i+1:]):
            num = l1 + l2
            for k,l3 in enumerate(l_list[i+j+2:]):
                if l3 < num:
                    count += 1


print(count)

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(N-2):
    for j in range(N-i-1):
        a = L[i]
        b = L[i+j+1]
        c_left = bisect.bisect_right(L[i+j+2:], b - a)
        c_right = bisect.bisect_left(L[i+j+2:], b + a)
        ans += c_right - c_left
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = [int(x) for x in input().split(" ")]
l.sort(reverse=True)
num =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]:
          num+=1
      else:
        break

print(num)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = input().split(' ')
l = [int(i) for i in l]
l.sort(reverse=True)

addList = []
for i in range(n):
    for j in range(i+1, n):
        addList.append({
            "num": l[i] + l[j],
            "i": i,
            "j": j
        })

# print(l)
cnt = 0
st = 0
for a in addList:
    for i in range(st, n):
        if a["i"] <= i or a["j"] <= i:
            # print(i, a["i"], a["j"])
            continue
        if a["num"] > l[i]:
            cnt += 1
            # print(i, a["i"], a["j"])
        else:
            st = i

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = [int(x) for x in input().split(" ")]
l.sort()
cnt =0
num =0

for i in range(N):
  for j in range(i+1,N):
    for k in range(j+1,N):
      if l[k]<l[j]+l[i]:
          num+=1

print(num)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = [int(x) for x in input().split(" ")]
l.sort()
cnt =0
num =0

for i in range(N):
  for j in range(i+1,N):
    for k in range(j+1,N):
      if l[k]<l[j]+l[i]:
          num+=1
      else:
        break

print(num)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = [int(x) for x in input().split(" ")]
l.sort()
num =0

for i in range(N):
  for j in range(i+1,N):
    for k in range(j+1,N):
      if l[k]<l[j]+l[i]:
          num+=1

print(num)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = [int(x) for x in input().split(" ")]
l.sort()
cnt =0
num =0
#print(l)

for i in range(N):
  for j in range(i+1,N):
    for k in range(j+1,N):
      if l[j]<l[i]+l[k]:
        if l[k]<l[j]+l[i]:
          num+=1

print(num)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = [int(x) for x in input().split(" ")]
cnt =0
num =0

for i in range(N):
  for j in range(i+1,N):
    for k in range(j+1,N):
      #print(i,j,k)
      cnt+=1
      if l[i]<l[j]+l[k] and l[j]<l[i]+l[k] and l[k]<l[j]+l[i]:
        num+=1

print(num)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = input().split(' ')
l = [int(i) for i in l]
l.sort(reverse=True)

cnt = 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]):
                # print(i, j, k)
                cnt += 1

print(cnt)
# print(l)

Submission status: Time Limit Exceeded

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

from itertools import combinations

comb = combinations(L, 3)

ans = 0
for c in comb:
    if 2*max(c)<sum(c):
        ans+=1
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
number = int(input())
length_list = list(map(int,input().split()))

possible_method_number=0

def check_triangle(a,b,c):
	if a+b>c and b+c>a and c+a>b:
		return 1
	else:
		return 0

for index_min in range(number):
	for index_mid in range(number):
		if index_min>=index_mid:
			continue
		for index_max in range(number):
			if index_mid>=index_max:
				continue
			possible_method_number=possible_method_number+check_triangle(
									length_list[index_min],length_list[index_mid],length_list[index_max])

print(possible_method_number)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
lst = sorted(list(map(int, input().split())))
ret = 0
for idx_a, a in enumerate(lst[:-2]):
    for idx_b, b in enumerate(lst[idx_a + 1:-1]):
        for idx_c, c in enumerate(lst[idx_a + idx_b + 2:]):
            if a < b + c and b < c + a and c < a + b:
                ret += 1
            else:
                break
print(ret)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int,input().split(" ")))
l.sort()
cnt = 0 
def search(arr,target,start,last):
    while(last > start):
        mid = (last + start)//2
        #print("start:",start, "last:" ,last, "mid:",mid)
        if arr[mid] < target:
            start = mid + 1 
        else:
            last = mid 
    return start

for i in range(0,n-2):
    a = l[i]
    for j in range(i+1,n-1):
        b = l[j]
        target = a + b 
        f1 = search(l,target,j+1,n)
        #print(f1)
        cnt+= f1 - j -1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
lis=list(map(int, input().split()))
lis.sort()
lis.reverse()
ans=0
for i in range(a):
  for j in range(i+1,a):
    for k in range(j+1,a):
      if lis[i]>=lis[j]+lis[k]:
        break
      ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
lis=list(map(int, input().split()))
lis.sort()
lis.reverse()
ans=0
for i in range(a):
  for j in range(i+1,a):
    for k in range(j+1,a):
      if lis[i]<lis[j]+lis[k]:
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(n):
    min = 1001
    min_j = -1
    for j in range(i, n):
        if l[j] < min:
            min = l[j]
            min_j = j
    l[i], l[min_j] = l[min_j], l[i]
    
ans = 0
for i in range(n):
    for j in range(i+1, n):
        big = l[i]+l[j]
        
        if l[i] > l[j]:
            small = l[i] - l[j]
        else:
            small = l[j] - l[i]
        
        ch_max = n
        ch_min = j+1
        
        while ch_min < ch_max:
            ch_i = (ch_min + ch_max) // 2
            if l[ch_i] < big:
                ch_min = ch_i + 1
            else:
                ch_max = ch_i
                
        big_i = ch_max
        
        ch_max = n
        ch_min = j+1
        
        while ch_min < ch_max:
            ch_i = (ch_min + ch_max) // 2
            if l[ch_i] < small:
                ch_min = ch_i + 1
            else:
                ch_max = ch_i
                
        small_i = ch_min
        
        ans += big_i - small_i

        if i < big_i and i >= small_i:
            ans -= 1
        if j < big_i and j >= small_i:
            ans -= 1
                
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
import sys
import bisect
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]

    N = II()
    L = LMIIS()
    L.sort()
    count = 0
    for i in range(N):
        a = L[i]
        for j in range(i+1,N):
            b = L[j]
            k = j+1
            while k < N and a+b > L[k]:
                k += 1
                count += 1
    print(count)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
# 入力
N = int(input())
L = list(map(int, input().split()))

L.sort(reverse=True)
Lmax = L[0]
id_L = np.zeros(Lmax+1, dtype=np.uint16)

val = Lmax
for n in range(1, N):
  if(L[n] != val):
    id_L[L[n]:val] = n-1
    val = L[n]
id_L[0:val] = N-1

sum = 0
for i in range(N-2):
  idx2 = id_L[int(L[i]/2.0)]
  for j in range(i+1, idx2+1):
    val3 = L[i] - L[j]
    idx3 = id_L[val3]
    if(idx3 > j):
      sum +=idx3 - j

print("{}".format(sum))

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

ans = 0
dif = 0
count = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        dif = L[i] - L[j] + 1
        L_descend = L[j+1:]
        L_descend = L_descend[::-1]
        # print(L_descend, bisect.bisect_left(L_descend, dif))
        index = bisect.bisect_left(L_descend, dif)
        count = len(L_descend) - index
        ans += count
        # print("ans = ", ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
L.sort()
count = 0
min = 0
max = 0

for i in range(0, N-2):
	for j in range(i+1, N-1):
		min = j+1
		max = N-1
		
		while min <= max:
			mid = (min+max) // 2
			if mid < N-1:
				if ( L[mid] < (L[i]+L[j]) ) and ( L[mid+1] >= (L[i]+L[j]) ):
					count += (mid - min +1)
					break
				elif ( L[mid+1] < (L[i]+L[j]) ) :
					if mid +1 == max:
						count += (max - min +1)
						break
					else:
						min = mid
				elif ( (L[i]+L[j]) <= L[mid] ):
					if min +1 == max:
						break
					else:
						max = mid
			else:
				if L[mid] < (L[i]+L[j]) :
					count += (mid - min +1)
					break
				elif (L[i]+L[j]) <= L[mid] :
					break

print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

ans = 0
dif = 0
count = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        dif = L[i] - L[j] + 1
        L_descend = sorted(L[j+1:])
        # print(L_descend, bisect.bisect_left(L_descend, dif))
        index = bisect.bisect_left(L_descend, dif)
        count = len(L_descend) - index
        ans += count
        # print("ans = ", ans)
print(ans)

Submission status: Time Limit Exceeded

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

import random
import time
from bisect import bisect_left

L_len = len(L)
L.sort()

count = 0
for i in range(0, L_len - 2):
    for j in range(i + 1, L_len - 1):
        length = L[i] + L[j]
        idx = bisect_left(L, length, lo = j + 1, hi = L_len)
        count += idx - j - 1

print(count)

Submission status: Time Limit Exceeded

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

import random
from bisect import bisect_left

L_len = len(L)
L.sort()

count = 0
for i in range(0, L_len - 2):
    for j in range(i + 1, L_len - 1):
        length = L[i] + L[j]
        idx = bisect_left(L, length, lo = j + 1, hi = L_len)
        count += idx - j - 1

print(count)

Submission status: Time Limit Exceeded

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

#m = bisect.bisect_left(l, a + b)
#count += (m - j - 1)
    
count = 0        
for i in range(n-1):
  a = L[i]
  for k in range(i+1,n-1):
    b = L[k]
    L_temp = L[k+1:]
    count += (bisect.bisect_left(L_temp, a + b)) 
print(count)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

#m.sort(reverse=True)

m2 = list(itertools.combinations(m,3))
m2.sort()
s=0
for i in range(len(m2)):
    if sum(m2[i]) - max(m2[i])*2>0:
        s+=1
print(s)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
l2=sorted(l)
box=[0] #各長さの棒が何本あるか　最初の０は数合わせ
ans=0

num=1
while num<l2[0]: #最小の棒までは０をappend
    box.append(0)
    num+=1
    
now=l2[0]
bounum=1
for i in range(1,n):
    if l2[i]!=now:
        box.append(bounum)
        while now+1<l2[i]:
            box.append(0)
        now=l2[i]
        bounum+=1
    else:
        bounum+=1
if len(box)!=l2[-1]+1:
    box.append(bounum) #boxの完成

for i in range(n-2):
    for j in range(i+1,n-1):
        c=l2[i]+l2[j]-1
        if c>l2[-1]:
            c=l2[-1]
        add=box[c]-(j+1)
        if add<0:
            add=0
        ans+=add

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def BinaryIndexSearch_floor(p, SL):
	#SL = sorted(L)
	l = 0
	r = len(SL) - 1

	while True:
		if l == r:
			if SL[l] > p:
				return l
			else:
				return -1
		elif r-l == 1:
			if SL[l] > p:
				return l
			elif SL[r] > p:
				return r
			else:
				return -1

		center = l + ((r-l) // 2)

		if SL[center] <= p:
			l = center + 1
			continue

		else:
			r = center
			continue

def BinaryIndexSearch_ceil(p, SL):
	#SL = sorted(L)
	l = 0
	r = len(SL) - 1

	while True:
		if l == r:
			if SL[l] < p:
				return l
			else:
				return -1
		elif r-l == 1:
			if SL[r] < p:
				return r
			elif SL[l] < p:
				return l
			else:
				return -1
		center = l + ((r-l) // 2)

		if SL[center] < p:
			l = center 
			continue

		else:
			r = center - 1
			continue

N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for a in range(N-2):
	for b in range(a+1, N-1):
		A = L[a]
		B = L[b]
		l = abs(A-B)
		r = A+B

		l_ind = BinaryIndexSearch_floor(l, L[b+1:])
		r_ind = BinaryIndexSearch_ceil(r, L[b+1:])

		if l_ind == -1 or r_ind == -1:
			continue
		else:
			ans += r_ind - l_ind + 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#import pysnooper
#import numpy
#import os,re,sys,operator
#from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement,permutations
from sys import stdin,setrecursionlimit
#from bisect import bisect_left
#from copy import deepcopy
#import heapq
#import math
#import string

setrecursionlimit(10**6)
input=stdin.readline

def nibu(A,lo,hi,a,b):
    #print(lo,hi)
    while lo<hi:
        m=(lo+hi)//2
        if abs(A[a]-A[b])<A[m]<A[a]+A[b]: lo=m+1
        else: hi=m
        #print(lo,hi)
    return m

n=int(input().rstrip())
l=sorted([int(i) for i in input().split()],reverse=True)
#print(*l)
ans=0
for i in range(n-2):
    for j in range(i+1,n-1):
        for c in range(j+1,n):
            if abs(l[i]-l[j])<l[c]<l[i]+l[j]:
                t=nibu(l,c,n,i,j)
                #print(t,j)
                ans+=t-j
                break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from bisect import bisect_left,bisect_right


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

ret = 0
for a in range(N-1,1,-1):
  for b in range(a-1,0,-1):
    if Ln[a] >= Ln[b] + Ln[b-1]:
      continue
    else:
      c = b-1
      while Ln[a]<Ln[b]+Ln[c] and c>=0:
        ret += 1
        c -= 1
print(ret)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
b=[]
L=[]
b=input().split()
M=[]
for i in range(N):
    L.append(int(b[i]))
L.sort()
L.reverse()
#print(L)
x=0
y=0
index=0
for i in range(N-2):
    x=L.pop(0)
    for j in range(N-2-i):
        y=L.pop(0)
        for k in range(N-2-i-j):
#            print('i,j,k :'+str(i)+str(j)+str(k)+':'+str(x)+str(y)+str(L[k]))
            if x<y+L[k]:
                index+=1
            else:
                break
        M.append(y)
    L=M+L
    M.clear()
print(index)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
b=[]
L=[]
b=input().split()
M=[]
for i in range(N):
    L.append(int(b[i]))
L.sort()
L.reverse()
#print(L)
x=0
y=0
index=0
for i in range(N-2):
    x=L.pop(0)
    for j in range(N-2-i):
        y=L.pop(0)
        for k in range(N-2-i-j):
#            print('i,j,k :'+str(i)+str(j)+str(k)+':'+str(x)+str(y)+str(L[k]))
            if x<y+L[k]:
                index+=1
        M.append(y)
    L=M+L
    M.clear()
print(index)

Submission status: Time Limit Exceeded

Solution code:
import bisect
import copy

ans = 0
n = int(input())
l = [int(x) for x in input().split()]
l.sort()
for i in range(n-2):
  ln = copy.copy(l)
  del ln[:i+1]
  for j in range(i+1, n-1):
    a = l[i]
    b = l[j]
    del ln[0]
    k = bisect.bisect_right(ln, b - a)
    m = bisect.bisect_left(ln, a + b)
    ans += (m - k)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
import copy

ans = 0
n = int(input())
l = list(map(int, input().split()))
l.sort()
for i in range(n-2):
  for j in range(i+1, n-1):
    a = l[i]
    b = l[j]
    ln = copy.copy(l)
    del ln[:j+1]
    k = bisect.bisect_right(ln, b - a)
    m = bisect.bisect_left(ln, a + b)
    ans += (m - k)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
l = np.array(list(map(int, input().split())))
l = np.sort(l)
ans = 0
for i in range(n):
    for j in range(i + 1, n):
        ans += l[j+1:][l[j+1:] < l[i] + l[j]].size
print(ans)

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(N):
    for j in range(N):
        if i == j:
            continue
        for k in range(N):
            if i == k or j == k:
                continue
            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//6)

Submission status: Time Limit Exceeded

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

L.sort()

s = 0

ma = L[N-1]
for j in range(N-2):
    ja = L[j]
    for k in range(j+1,N-1):
        ka = L[k]
        # L[i] < L[j]+L[k] となる L[i]の数を求める
        key = ja + ka
        l = k + 1
        r = N - 1
        while r - l > 1:
            m = l + (r - l) // 2
            if L[m] < key:
                l = m
            else:
                r = m
        kka = L[k+1]
        if kka < key <= ma:
            s += l-k
        elif key <= kka:
            s += 0
        else:
            s += N-k-1

print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from copy import copy

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

for _ in range(len(l)-2):
    first = l.pop()
    m = copy(l)
    for _ in range(len(m)-1):
        second = m.pop()
        n = copy(m)
        for _ in range(len(n)):
            third = n.pop()
            if (first < second + third) & (second < first + third) & (third < first + second):
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def compose():
    N = int( input() )
    t_list = list( map( int, input().split() ) )
    t_list.sort()
    t_list.reverse()
    counter = 0
    a = 0
    for i in t_list:
        a += 1
        b = a
        for j in t_list[a:]:
            b += 1
            new_list = [ k for k in t_list[b:] if k > i - j ]
            counter += len(new_list)
    return counter

def main():
    print(compose())
    
main()

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
l = list(map(int, input().split()))
memo = [[0] for i in range(n)]
for j in range(n):
    c = Counter(l[j:])
    tmp = 0
    for i in range(1000):
        if c[i+1]:
            tmp  += c[i+1]
        memo[j].append(tmp)

ans = 0
for i in range(n):
    for j in range(i+1, n - 1):
        b = l[i]
        c = l[j]
        lo = abs(b - c)
        up = min(b + c, 1001)
        ans += memo[j+1][up-1] - memo[j+1][lo]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

n = int(input())
s = list(int(i) for i in input().split())
l = list(itertools.combinations(s, 3))
count = 0
for inner in l:
    if (inner[0] < inner[1] + inner[2]) and (inner[1] < inner[0] + inner[2]) and(inner[2] < inner[1] + inner[0]):
        count = 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,reverse=True)
a = 0

for x in range(0,N-2):
  for y in range(x+1,N-1):
	   for i in range(y+1,N):
	       if L[i] > L[x] - L[y]:
	           a += 1
print(a)

Submission status: Time Limit Exceeded

Solution code:

import bisect

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



cnt = 0
for i in range(0, len(L)-2):
    l1 = L[i]
    rest = L[i+1:]
    for i2 in range(0, len(rest)-1):
        l2 = rest[i2]
        if l2 < l1 / 2:
            break
        if l2 < l1 / 2:
            break
        rest2 = rest[i2+1:]
        cnt += bisect.bisect_left(rest2, l2+l1) - bisect.bisect_left(rest2, l2-l1)

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
L_lst=sorted(list(map(int,input().split())))
cnt=0
for j in range(n-1,1,-1):
    i=j-1
    x=0
    while x<i:
        if L_lst[i]+L_lst[x]>L_lst[j]:
            cnt+=i-x
            i-=1
            x=0
        else:
            x+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
L_lst=sorted(list(map(int,input().split())))
i=n-2
j=n-1
cnt=0
x=0
while j>1:
    while x<i:
        if L_lst[i]+L_lst[x]>L_lst[j]:
            cnt+=i-x
            i-=1
            x=0
        else:
            x+=1
    x=0
    j-=1
    i=j-1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int, input().split()))
ans=0
for i in range(0,n-2,1):
  for j in range(i+1,n-1,1):
    for k in range(j+1,n,1):
      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())
L_lst=sorted(list(map(int,input().split())))
i=n-2
j=n-1
cnt=0
while j>1:
    x=0
    while x<i:
        if L_lst[i]+L_lst[x]>L_lst[j]:
            cnt+=i-x
            break
        else:
            x+=1
    i-=1
    if i==0 or (L_lst[j])//2 >= L_lst[i]:
        j-=1
        i=j-1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#-------------------------------------------------------------------
import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
#-------------------------------------------------------------------
from bisect import bisect_left  as bl
from bisect import bisect_right as br
from bisect import insort_left  as il    # insort_leftはO(N)
from bisect import insort_right as ir
#-------------------------------------------------------------------
n = int(input())
L = list( map(int, input().split()) )	# 1 2 3 4 5 ...

L.sort()

ans=0


def bsl(L,c):
	if len(L)==0: return 0
	
	rMax = len(L)-1

	if L[rMax]<c: return rMax+1
	if c<=L[0]: return 0
	
	l=0
	r=rMax
	
	while True:
		k=(l+r)//2
		
		if c<=L[k]: r=k
		if L[k]<c:  l=k+1
		
		if r<=l: return r
		




for i in range(n-2):
	a=L[i]
	
	for j in range(i+1, n-1):
		b=L[j]
		
		#k = bl(L,a+b)
		k = bsl(L,a+b)
		
		ans+=k-j-1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import bisect

N = int(input())
L = list(map(int,stdin.readline().strip().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[i]+L[j]):
                ans+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
L_lst=sorted(list(map(int,input().split())))
i=n-2
j=n-1
cnt=0
while j>1:
    x_min=0
    x_max=i
    while x_max>x_min+1:
        x_mid=(x_max+x_min)//2
        if L_lst[x_mid]+L_lst[i]>L_lst[j]:
            x_max=x_mid
        else:
            x_min=x_mid
    if L_lst[x_min]+L_lst[i]>L_lst[j]:
        cnt+=i-x_min
    else:
        cnt+=max(i-x_min-1,0)
    i-=1
    if i==0 or (L_lst[j])//2 >= L_lst[i]:
        j-=1
        i=j-1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
pole_list = sorted([int(item) for item in input().split()])

import numpy as np
pole_list = np.array(pole_list)

cnt = 0

for i in range(num):
    for j in range(i + 1, num):
        cnt += len(np.where(pole_list < pole_list[i] + pole_list[j])[0]) - j - 1
print(cnt)

Submission status: Time Limit Exceeded

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

L_len = len(L)

L.sort(reverse=True)

count = 0
for i in range(0, L_len - 2):
    for j in range(i + 1, L_len - 1):
        left = j + 1
        right = L_len - 1
        length = L[i] - L[j]
        index = -1
        while right >= left  :
            center = int((left + right + 1)/2)
            if L[center] > length:
                index = 0
                left = center
                if left == right:
                    break

            else:
                right = center - 1
            

        if index != -1:
            count += right - j

print(count)

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
pole_list = sorted([int(item) for item in input().split()])

import numpy as np
pole_list = np.array(pole_list)

cnt = 0

for i in range(num):
    for j in range(i + 1, num):
        cnt += len(pole_list[pole_list < pole_list[i] + pole_list[j]]) - (j + 1)

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
 
N = int(input())
Lmat = [int(n) for n in input().split()]
Lmat.sort()
 
sum = 0

for i in range(N):
    for j in range(N):
        if i < j:
            less_than_c = bisect_left(Lmat, Lmat[i]+Lmat[j])
            more_than_b = bisect_left(Lmat, Lmat[j]+1)

            sum += less_than_c - more_than_b 

print(sum)

Submission status: Time Limit Exceeded

Solution code:
n=int(input()) 
L=list(map(int, input().split())) 
L.sort()
 
def binary_search(list, item):
    low = 0
    high = len(list) - 1
    
    while low <= high:
        if item > list[high]:
          return high+1
          break
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
          return mid
        elif guess > item:
          high = mid -1
          if list[mid-1] < item:
            return mid-1
            break
        elif guess < item:
          low = mid + 1
          if list[mid+1] > item:
            return mid+1
            break
        else:
          print('error')
 
    return 0
 
 
count = 0        
for i in range(n-1):
  a = L[i]
  for k in range(i+1,n-1):
    b = L[k]
    L_temp = L[k+1:]
    binary_search(L_temp, a+b)
    count = count + binary_search(L_temp, a+b)
    
print(count)

Submission status: Time Limit Exceeded

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

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