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

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

l.sort(reverse=True)

ans = 0

for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            if is_triangle(l[i], l[j], l[k]):
                ans += 1
            else:
                break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())

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

# n = 100
# l = np.array([3 for i in range(n)])

l.sort()
# def num(first,second,third):
#     if l[first] < l[second] + l[third]:
#         dp[first] += 1
#         if third > 0:
#             num(first,second,third-1)
#         else:
#             # print(first,second,third)
#             return 
#     else:
#         # print(first,second,third)
#         return second, third





dp = np.array([0]*n)

for i in range(n-1,1,-1):
    
    for j in range(i-1,0,-1):
        if i // 2 > j:
            break 
        # print(i,j)
        # num(i,j,j-1)
        for k in range(j-1,-1,-1):
            
            if l[i] < l[j] + l[k]:
                dp[i] += 1
            else:
                second = j
                third = k
                break
        else:
            second = j
            third = k
        if second == third + 1:
            break
    # print(i)
print(np.sum(dp))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

n = int(input())

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

# n = 200
# l = np.array([3 for i in range(n)])

l.sort()
# def num(first,second,third):
#     if l[first] < l[second] + l[third]:
#         dp[first] += 1
#         if third > 0:
#             num(first,second,third-1)
#         else:
#             # print(first,second,third)
#             return 
#     else:
#         # print(first,second,third)
#         return second, third





dp = np.array([0]*n)

for i in range(n-1,1,-1):
    for j in range(i-1,0,-1):
        # print(i,j)
        # num(i,j,j-1)
        for k in range(j-1,-1,-1):
            
            if l[i] < l[j] + l[k]:
                dp[i] += 1
            else:
                second = j
                third = k
                break
        else:
            second = j
            third = k
        if second == third + 1:
            break
    # print(i)
print(np.sum(dp))

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):
  for k in range(i+1,n-1):
    a,b =L[i],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:
import numpy as np

# n = int(input())

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

n = 200
l = np.array([3 for i in range(n)])

l.sort()
# def num(first,second,third):
#     if l[first] < l[second] + l[third]:
#         dp[first] += 1
#         if third > 0:
#             num(first,second,third-1)
#         else:
#             # print(first,second,third)
#             return 
#     else:
#         # print(first,second,third)
#         return second, third





dp = np.array([0]*n)

for i in range(n-1,1,-1):
    for j in range(i-1,0,-1):
        # print(i,j)
        # num(i,j,j-1)
        for k in range(j-1,-1,-1):
            
            if l[i] < l[j] + l[k]:
                dp[i] += 1
            else:
                second = j
                third = k
                break
        else:
            second = j
            third = k
        if second == third + 1:
            break
    # print(i)
print(np.sum(dp))

Submission status: Time Limit Exceeded

Solution code:
import sys
N = int(input())
L = list(map(int, input().split()))
L.sort()
ret = 0
for i in range(0, N-2):
    for j in range(i+1, N-1):
        ret += len([k for k in L[j+1:] if L[i]+L[j]>k])
print(ret)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left as b_l
from bisect import bisect_right as b_r
N = int(input())
L = list(map(int, input().split()))
L.sort()
count = 0
for i in range(N-2):
  a = L[i]
  for j in range(i+1, N-1):
    b = L[j]
    c_1 = b_l(L, a+b)
    c_2 = b_l(L, abs(a-b))
    if c_2 <= j:
      c_2 = b_r(L, b)
    count += c_1 - c_2
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=raw_input()
LN=raw_input().split()

n = int(N)
ln = [int(i) for i in LN]

ln.sort()

def bin_search(s,e,t):
    # return minimum i where s <= i < e and ln[i] >= t
    m = (s + e)/2
    if m == s:
        return s

    if t < ln[m]:
        return bin_search(s,m,t)
    else:
        return bin_search(m,e,t)



r = 0
for i in range(n-1, 1, -1):
    a = ln[i]
    for j in range(i-1, 0, -1):
        b = ln[j]
        c = a - b
        if ln[j-1] > c:
            idx = bin_search(0,j,c+1)
            r += j - idx
        else:
            break

print r

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):
    left=j+1
    right=n-1
    while left<=right:
      center=(right+left)//2
      if center==n-1:
        if l[i]+l[j]>l[center]:
          count+=center-j
        break
      elif l[center+1]>l[i]+l[j]>l[center] :
        count+=center-j
        break
      elif l[i]+l[j]>l[center]:
        left=center+1
      else:
        right=center-1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=raw_input()
LN=raw_input().split()

n = int(N)
ln = [int(i) for i in LN]

ln.sort()

def bin_search(s,e,t):
    # return minimum i between s <= i < e where ln[i] >= t
    m = (s + e)/2
    if m == s:
        return s

    if t < ln[m]:
        return bin_search(s,m,t)
    else:
        return bin_search(m,e,t)



r = 0
for i in range(n-1, -1, -1):
    a = ln[i]
    for j in range(i-1, -1, -1):
        b = ln[j]
        c = a - b
        if j > 0 and ln[j-1] > c:
            idx = bin_search(0,j,c+1)
            r += j - idx

print r

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
count = 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:
        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(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:
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
N = int(input())
L = list(map(int,input().split()))
L.sort(reverse=True)
count=0;
#print(L)
for i in range(N-2):
    a = i
    for j in range(N - 1 - i-1):
        b = i + j + 1
        for k in range(N - b - 1):
            c = b + k + 1
            #s = '' + str(i) + ',' + str(b) + ',' + str(c)
            #print(s)
            if L[a] >= L[b] + L[c] :
                break
            count = count + 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
N=raw_input()
LN=raw_input().split()

n = int(N)
ln = [int(i) for i in LN]

ln.sort()

r = []
for i in range(n-1, -1, -1):
    a = ln[i]
    for j in range(i-1, -1, -1):
        b = ln[j]
        if a > 2*b:
            break
        for k in range(j-1, -1, -1):
            # always, a > b > c
            c = ln[k]
            if not a < b + c:
                break
            #if not b < c + a:
            #    break
            #if not c < a + b:
            #    continue
            r.append([a,b,c])

print len(r)
sys.stdout.flush()
sys.exit(0)

Submission status: Time Limit Exceeded

Solution code:
N=raw_input()
LN=raw_input().split()

n = int(N)
ln = [int(i) for i in LN]

ln.sort()

r = []
for i in range(n-1, -1, -1):
    a = ln[i]
    for j in range(i-1, -1, -1):
        b = ln[j]
        if a > 2*b:
            break
        for k in range(j-1, -1, -1):
            # always, a > b > c
            c = ln[k]
            if not a < b + c:
                break
            #if not b < c + a:
            #    break
            #if not c < a + b:
            #    continue
            r.append([a,b,c])

print len(r)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(map(int,input().split( )))
S = sorted(S)
tri_num=0
for i in range(N-2):
    k = i+2
    for j in range(i+1,N-1):
        while k < N:
            if S[i] + S[j] <= S[k]:
                tri_num = tri_num + k-j-1
                if k == i+2:
                    k+=1
                break
            elif k == N-1:
                tri_num = tri_num + k-j
            else:
                k+=1
print(tri_num)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
R=sorted(L)[::-1]

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input()) 
L=list(map(int, input().split())) 
L.sort(reverse = True)
L = np.array(L)
count = 0
            
for i in range(n-1):
  for k in range(i+1,n-1):
    a,b =L[i],L[k]
    L_temp = L[k+1:]
    count = count + np.count_nonzero(L_temp+b > a)
    
print(count)

Submission status: Time Limit Exceeded

Solution code:

n=int(input()) 
L=list(map(int, input().split())) 
L.sort(reverse = True)
 
count = 0
            
for i in range(n-1):
  for k in range(i+1,n-1):
    a,b =L[i],L[k]
    L_temp = L[k+1:]
    count = count + sum([1 for x in L_temp if b+x>a])
    
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int( input() )
t_list = list( map( int, input().split() ) )
counter = 0
a = 0
b = 1
for i in t_list:
  a += 1
  b = a
  for j in t_list[a:]:
    b += 1
    for k in t_list[b:]:
      if i < j + k:
        if j < i + k:
          if k < i + j:
            counter += 1
            
print(counter)

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
l=sorted(L,reverse=True)
ans=0
for i in range(N):
    c=l[i]
    for j in range(i+1,N):
        b=l[j]
        for k in range(j+1,N):
            a=l[k]
            if a+b>c:
                ans+=1
            else:
                break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
cnt = 0
for A in range(n):
  for B in range(n):
    for C in range(n):
      a = l[A]
      b = l[B]
      c = l[C]
      if (a<b+c and b<a+c and c<a+b and a!=b and b!=c and c!=a):
        cnt += 1
print(cnt//6)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
cnt = 0
for A in range(n):
  for B in range(n):
    for C in range(n):
      if not (A==B or A==C or B==C):
        a = l[A]
        b = l[B]
        c = l[C]
        if (a<b+c and b<a+c and c<a+b):
          cnt += 1
print(cnt//6)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(map(int,input().split( )))
tri=[]
for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            a = [S[i],S[j],S[k]]
            a = sorted(a)
            if a[0] + a[1] > a[2]:
                tri.append(a)

print(len(tri))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(map(int,input().split( )))
tri=[]
for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            a = [S[i],S[j],S[k]]
            a = sorted(a)
            if a[0] + a[1] > a[2]:
                tri.append(a)

for i in range(len(tri)):
    n=0
    for j in range(i+1,len(tri)):
        if tri[i] == tri[j-n]:
            tri.pop(j-n)
            n=n+1


print(len(tri))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(map(int,input().split( )))
tri=[]
for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            a = [S[i],S[j],S[k]]
            a = sorted(a)
            if a[0] + a[1] > a[2]:
                tri.append(a)

for i in range(len(tri)):
    n=0
    for j in range(i+1,len(tri)):
        if tri[i] == tri[j-n]:
            tri.pop(j)
            n=n+1


print(len(tri))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
L = np.array(list(map(int, input().split())))
L.sort()
c = 0
for n1 in range(0,N):
    for n2 in range(n1+1, N):
        L2 = np.sum(((L[n1] + L[n2]) > L[n2+1:]))
        c += L2
print(c)

Submission status: Time Limit Exceeded

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

L.sort()

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

print(s)

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:
    for x in range(i-1,-1,-1):
        if x==0:#0だとout of range
            if L_lst[x]+L_lst[i]>L_lst[j]:
                cnt+=1
        else:
            if L_lst[x]!=L_lst[x-1]:
                if L_lst[x]+L_lst[i]>L_lst[j]:
                    cnt+=1
                else:
                    break
    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:
n=int(input())
l=list(map(int,input().split()))
l.sort()
l.reverse()
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]:
        count+=1
      else:
        break
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
leng = [int(i) for i in input().split()]
leng.sort(reverse=True)
cnt = 0

for i, a in enumerate(leng[:-2]):
    for j, b in enumerate(leng[i+1:-1], i+1):
        left = j+1
        right = len(leng)-1
        while left <= right:
            mid = (left+right)//2
            if a < b + leng[mid] and b < a + leng[mid] and leng[mid] < a + b:
                left = mid+1
            else:
                right = mid-1
        cnt += len(leng[j+1:left])

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
L.sort()
c = 0
for n1 in range(0,N):
    for n2 in range(n1+1, N):
        for n3 in range(n2+1, N):
            if(L[n1] + L[n2] > L[n3]):
                c += 1
            else:
                break
print(c)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
L.sort()
c = 0
for n1 in range(0,N):
    for n2 in range(n1+1, N):
        for n3 in range(n2+1, N):
            if(L[n1] + L[n2] > L[n3]):
                c += 1
print(c)

Submission status: Time Limit Exceeded

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

L.sort()

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

print(s)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right
from itertools import combinations

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

for (i1, a), (i2, b) in combinations(enumerate(l[:-1]), 2):
    i3s = bisect_right(l, b - a, i2 + 1)
    i3e = bisect_left(l, a + b, i3s)
    ans += i3e - i3s

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right
from itertools import combinations

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

for l1, l2 in combinations(enumerate(l[:-1]), 2):
    (i1, a), (i2, b) = l1, l2
    i3s = bisect_right(l, b - a, i2 + 1)
    i3e = bisect_left(l, a + b, i3s)
    ans += i3e - i3s

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right

n = int(input())
l = sorted(map(int, input().split()))
ans = 0
for i1 in range(n - 2):
    for i2 in range(i1 + 1, n - 1):
        a = l[i1]
        b = l[i2]
        i3s = bisect_right(l, b - a, i2 + 1)
        i3e = bisect_left(l, a + b, i3s)
        ans += i3e - i3s
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = sorted([int(x) for x in input().split()])
a=0
for i in range(N-2):
    for j in range(i+1,N):
        b=S[i]+S[j]
        if (j+1!=N) and (b < S[j+1]) :
            continue
        elif j==N-1: break
        for k in range(j+1,N):
            if S[k] < b: a +=1
            else:break
print(a)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())
L = list(map(int,input().split()))
L.sort()
sum = 0
for i in range(0,N):
  L_1st= L[i]
  for j in range(i+1,N):
    L_2nd = L[j]
    c_min = L_2nd-L_1st+1
    c_max = L_2nd+L_1st-1
    l = bisect.bisect_left(L,c_min,lo = j+1)
    if l!= N:
      r = bisect.bisect_right(L,c_max,lo = j+1)
      if r:
        r = r-1
        if r>= l:
          sum += r-l+1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
leng = [int(i) for i in input().split()]
leng.sort(reverse=True)
cnt = 0

for i, a in enumerate(leng[:-2]):
    for j, b in enumerate(leng[i+1:-1], i+1):
        cnt += len([i for i in leng[j+1:] if a < b+i])

print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right

n = int(input())
l = sorted(map(int, input().split()))
ans = 0
for i1 in range(n - 2):
    for i2 in range(i1 + 1, n - 1):
        a = l[i1]
        b = l[i2]
        i3s = max(bisect_right(l, b - a), i2 + 1)
        i3e = bisect_left(l, a + b)
        ans += i3e - i3s
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = np.array(sorted([int(x) for x in input().split()]))
a=0
for i in range(N-2):
    for j in range(i+1,N):
        b=S[i]+S[j]
        if (j+1!=N) and (b < S[j+1]) :
            continue
        elif j==N-1: break
        l=S[np.where(S[j+1:] < b)]
        a += len(l)
print(a)

Submission status: Time Limit Exceeded

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

sma=L[-1]+L[-2]

a=0
b=1
c=2

ans=0

for i in range(n-2):
    for q in range(i+1,n-1):
        for j in range(q+1,n):
            if L[i]+L[q]>L[j]:
                ans+=1
                #print(i,q,j)
            else:
                break
    
print(ans)

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
print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = sorted([int(x) for x in input().split()])
a=0
for i in range(N-2):
    for j in range(i+1,N):
        b=S[i]+S[j]
        if (j+1!=N) and (b < S[j+1]) :
            continue
        elif j==N-1: break
        l = [k for k in S[j+1:] if k < b]
        a += len(l)
print(a)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = sorted([int(x) for x in input().split()])
a=0
for i in range(N):
    for j in range(i+1,N):
        b=S[i]+S[j]
        if (j+1!=N) and (b < S[j+1]) :
            continue
        elif j==N-1: break
        l = [k for k in S[j+1:] if k < b]
        a += len(l)
print(a)

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(i+1,N):
        min_index = j
        max_index = bisect_left(Lmat, Lmat[i]+Lmat[j])
        sum+= bisect_left(Lmat, Lmat[i]+Lmat[j]) - j - 1
print(sum)

Submission status: Time Limit Exceeded

Solution code:
import itertools
from bisect import bisect_left

N = int(input())
Lmat = [int(n) for n in input().split()]
Lmat.sort()

def iftriangle(first,second,third):
    if third < len(Lmat):
        if Lmat[first]>=Lmat[second]+Lmat[third]:
            return 0
        elif Lmat[second]>=Lmat[first]+Lmat[third]:
            return 0
        elif Lmat[third]>=Lmat[first]+Lmat[second]:
            return 0
        else:
            #print([first,second,third])
            return 1
    else:
        return 0

sum = 0
for i in range(N):
    for j in range(i+1,N):
        min_index = j
        max_index = bisect_left(Lmat, Lmat[i]+Lmat[j])
        sum+= bisect_left(Lmat, Lmat[i]+Lmat[j]) - j - 1
        """
        #print(binary_search_nearest(Lmat, Lmat[patern[0]]+Lmat[patern[1]]))#これだとjustの値しか取得できない=>bs自体を実装する必要がある)
        for Lk_index in range(min_index+1,max_index+1):
            sum += iftriangle(i,j,Lk_index)#ここは1に置き換え不可。計算が必要
        """

print(sum)

#elapsed_time = time.time() - start
#print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
Lmat = [int(n) for n in input().split()]
Lmat.sort()

import time
start = time.time()

def binary_search_nearest(list, item):#もっとも近い値のindexをbsで探して返す関数
    low = 0
    high = len(list) - 1
    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1
    return low-1

def iftriangle(first,second,third):
    if third < len(Lmat):
        if Lmat[first]>=Lmat[second]+Lmat[third]:
            return 0
        elif Lmat[second]>=Lmat[first]+Lmat[third]:
            return 0
        elif Lmat[third]>=Lmat[first]+Lmat[second]:
            return 0
        else:
            #print([first,second,third])
            return 1
    else:
        return 0

sum = 0
for i in range(N):
    for j in range(i+1,N):
        min_index = j
        max_index = binary_search_nearest(Lmat, Lmat[i]+Lmat[j])#これだとjustの値しか取得できない=>bs自体を実装する必要がある
        #print(binary_search_nearest(Lmat, Lmat[patern[0]]+Lmat[patern[1]]))#これだとjustの値しか取得できない=>bs自体を実装する必要がある)
        for Lk_index in range(min_index+1,max_index+1):
            sum += iftriangle(i,j,Lk_index)#ここは1に置き換え不可。計算が必要


print(sum)

elapsed_time = time.time() - start
#print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")

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):
        if l[i]/2 > l[j]:
            break
        for k in range(j+1,n):
            a,b,c = l[i],l[j],l[k]
            #print(i,j,k)
            if a >= b+c:
                break
            else:
                ans += 1
            
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = sorted([int(x) for x in input().split()])
a=0
for i in range(N):
    for j in range(i+1,N):
        b=S[i]+S[j]
        l = [k for k in S[j+1:] if k < b]
        a += len(l)
print(a)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

c = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    c += bisect_left(l[j+1:], l[i]+l[j])

print(c)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = sorted(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):
            a = l[i]
            b = l[j]
            c = l[k]
            if a-b<c and c<a+b:
                cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

print(a)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int, input().split()))
l = sorted(l)
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:
n = int(input())
l = list(map(int, input().split()))

c = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    c += sum(abs(l[i]-l[j]) < x < l[i]+l[j] for x in l[j+1:])

print(c)

Submission status: Time Limit Exceeded

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

c = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    c += sum(l[i]+l[j] > x for x in l[j+1:])

print(c)

Submission status: Time Limit Exceeded

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

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

c = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    for k in range(j+1, n):
      c += 1 if check(l[i], l[j], l[k]) else 0
      
print(c)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=sorted(list(map(int,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 s[i]+s[j]>s[k] :
        ans+=1
      
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
n=int(input())
L=list(map(int,input().split()))
L.sort()
ans=0
for b in range(1,n-1):
    for c in range(b+1,n):
        ans+=b-bisect.bisect_right(L[:b],L[c]-L[b])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
 
n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)
 
cnt = 0
for i in range(n):
  a = l[i]
  for j, b in enumerate(l[i+1:]):
    for c in l[i+j+2:]:
      if a < b+c:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort()
res = 0
for i in range(N-2):
    idx = (N-1) - i
    max_, second_max = L[idx], L[idx-1]
    for tmp_idx in range(1, idx):
        second_max = L[tmp_idx]
        tidx = bisect.bisect_right(L, max_ - second_max)
        if len(L[tidx:tmp_idx]) > 0:
            res += len(L[tidx:tmp_idx])
print(res)

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 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:
N = int(input())
L = list(map(int, input().split()))
 
import itertools
 
def check(c):
  if max(c) >= sum(c) -max(c) :return 0
  return 1
 
ans = sum([check(c) for c in list(itertools.combinations(L,3))])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
N = int(input())
L = [int(i) for i in input().split()]
L.sort()
ret = 0
for i in range(0, N-2):
    for j in range(i+1, N-1):
        ret += bisect.bisect_right(L[j+1:], L[i]+L[j]-1)
print(ret)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
d = sorted(list(map(int, input().split())))
res = 0
for i in range(0, N - 2):
    for j in range(i + 1, N - 1):
        tmp = d[i] + d[j]
        for k in range(j + 1, N):
            if tmp > d[k]:
                res = res + 1
            else:
                break
print(res)