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:
import bisect
 
N = int(input())
Ls = list(map(int, input().split(" ")))
Ls.sort()
 
ans = 0
 
for i in range(N-1,1,-1):
  for j in range(i-1,0,-1):
    if 2*Ls[j] <= Ls[i]:
      break
    ans += (len(Ls[:j]) - bisect.bisect_right(Ls[:j],Ls[i]-Ls[j]))
       
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect
N = int(input())
L = list(map(int,input().split()))
L.sort()
cnt = 0

for i in range(N-1,1,-1):
    for j in range(i-1,0,-1):
        temp = L[i] - L[j]
        l = bisect(L,temp,lo=0,hi=j)
        cnt += j - l
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

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

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

print(sum)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

LI.sort(reverse=True)

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

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

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

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




print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

count = 0

for i in range(N):
    for j in range(i + 1, N - 1):
        target = int(D[N - i - 1]) - int(D[N - j - 1])
        l = D[:N - j - 1]
        count += len(l) - bisect_right(l, target)

print(count)

Submission status: Time Limit Exceeded

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

count = 0
for i in range(0, n-2):
  for j in range(i+1, n-1):
    t = L[i] + L[j]
    count += bisect.bisect_left(L[j+1:], t)

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(0, 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:
#-------------------------------------------------------------------
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):
	rMax = len(L)-1
	
	l=0
	r=rMax
	
	if L[r]<c: return r+1
	
#	iCount=0
#	while iCount<=rMax:
#		iCount+=1

	while True:
		k=(l+r)//2
		#p("c,iCount,l,r,k ",c,iCount,l,r,k)
		
		if k==0: return k
		#if L[r]<=c: return r+1
		#p(20)
		if l+1==r: return r
		#p(30)
		if L[k-1]<c<=L[k]: return k
		#p(40)
		if L[k]<=c:
			l=k
		#	p(50)
			continue
		if c<L[k]:
			r=k
		#	p(60)
			continue
		#p(70)

#L=[1,3,5,7]
#for i in range(6,10):
#	print(i, bsl(L,i) )



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:
N = int(input())
L = input().split()
LI = []
for l in L:
    LI.append(int(l))

LI.sort(reverse=True)

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

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

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




print(count)

Submission status: Time Limit Exceeded

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

v = L[:]
v.sort()

res = 0

import bisect

for mid in range(N-1,-1,-1):
    a = v[mid]
    for low in range(mid-1,-1,-1):
        b = v[low]
        sup = a + b
        inf = max(a-b,b-a)
        iterS = bisect.bisect_left(v,sup)
        iterI = bisect.bisect_left(v,inf+1)
        #print(a,b,sup,inf,iterS,iterI)
        res += max(low-iterI,0)

print(res)

Submission status: Time Limit Exceeded

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

import sys
from bisect import bisect_left

n = int(sys.stdin.readline().strip())
l = [ int(x) for x in sys.stdin.readline().strip().split() ]
l.sort()

c = 0
for i in range(1,n):
    for j in range(i+1,n):
        k = bisect_left(l,l[j]-l[i]+1,hi=i)
        if k==i: continue
        c += i-k
#
print(c)

Submission status: Time Limit Exceeded

Solution code:
from math import ceil

n = int(input())
l = list(map(int, input().split()))
l.sort()
# print(l)
ans = 0
for i in range(len(l)):
    for left in range(i+1, len(l)):
        right = left
        while right+1 < len(l) and l[right+1] < l[left] + l[i]:
            right += 1
        ans += right - left

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(n-2):
  for j in range(n-i-1):
    a = L[i]
    b = L[i+j+1]
    tmp = 0
    LL = L[i+j+2:]
    k = len(LL)//2
    while(len(LL)!=0):
      if LL[k] < (a+b):
        tmp += k+1
        LL = LL[k+1:]
        k = len(LL)//2
      else:
        LL = LL[:k]
        k = len(LL)//2
    ans += tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

ans = 0

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

Submission status: Time Limit Exceeded

Solution code:
import copy
import bisect

def main(N, L):
    L.sort(reverse=True)

    triangles = 0

    for i, P in enumerate(L):
        Q_list = copy.copy(L)
        del Q_list[:i + 1]
        for j, Q in enumerate(Q_list):
            if Q < P / 2:
                break
            minR = P - Q + 1
            R_list = copy.copy(Q_list)
            del R_list[:j + 1]
            R_list.reverse()
            triangles += len(R_list) - bisect.bisect_left(R_list, minR)

    print(triangles)

if __name__ == '__main__':
    N = int(input())
    L = list(map(int, input().split()))
    main(N, L)

Submission status: Time Limit Exceeded

Solution code:
import copy
import bisect

def main(N, L):
    L.sort(reverse=True)

    triangles = 0

    for i, P in enumerate(L):
        Q_list = copy.copy(L)
        del Q_list[:i + 1]
        for j, Q in enumerate(Q_list):
            if Q < P / 2:
                break
            minR = P - Q + 1
            R_list = copy.copy(Q_list)
            del R_list[:j + 1]
            R_list.reverse()
            # print(R_list, minR, bisect.bisect_left(R_list, minR))
            triangles += len(R_list) - bisect.bisect_left(R_list, minR)

            # for R in R_list:
            #     if R >= minR:
            #         triangles += 1
            #     else:
            #         break

    print(triangles)

if __name__ == '__main__':
    N = int(input())
    L = list(map(int, input().split()))
    main(N, L)

Submission status: Time Limit Exceeded

Solution code:
import bisect
 
N = int(input())
Ls = list(map(int, input().split(" ")))
Ls.sort()
 
ans = 0
 
for i in range(2,N):
  for j in range(1,i):
    if 2*Ls[j] <= Ls[i]:
      continue
    ans += (len(Ls[:j]) - bisect.bisect_right(Ls[:j],Ls[i]-Ls[j]))
       
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li = list(map(int,input().split()))
count = 0
li.sort()
for i in range(n - 2):
  for j in range(i + 1, n - 1):
    for k in range(j + 1, n):
      a = li[i]
      b = li[j]
      c = li[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 bisect

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

ans = 0

for i in range(2,N):
  for j in range(1,i):
    ans += (len(Ls[:j]) - bisect.bisect_right(Ls[:j],Ls[i]-Ls[j]))
       
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys
import random
import itertools
import math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt


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


def LF(): return list(map(float, input().split()))


def LI_(): return list(map(lambda x: int(x)-1, input().split()))


def II(): return int(input())


def IF(): return float(input())


def LS(): return list(map(list, input().split()))


def S(): return list(input().rstrip())


def IR(n): return [II() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def FR(n): return [IF() for _ in range(n)]


def LFR(n): return [LI() for _ in range(n)]


def LIR_(n): return [LI_() for _ in range(n)]


def SR(n): return [S() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]


def sosu(x):
    for i in range(2, int(sqrt(x)+1)):
        if x % i == 0:
            return False
    return True


mod = 1000000007
inf = float('INF')


def A():
    A, B = LI()
    ans = A-2*B
    if ans > 0:
        print(ans)
    else:
        print(0)
    return

# B


def B():
    N = II()
    d = LI()
    ans = 0
    for i in range(N):
        for x in range(i+1, N):
            ans += d[i]*d[x]
    print(ans)

    return

# C


def C():
    N = II()
    S = input()
    c = 0
    count = 0
    for i in S:
        if i != c:
            count += 1
        c = i
    print(count-1)

    return

# D


def D():
    N = II()
    L = LI()
    L.sort()

    L2 = deque(L)
    count = 0
    for i in range(N-1):
        L2.popleft()
        L3 = list(L2)
        L3 = deque(L3)

        for j in range(i+1, N):
            L3.popleft()
            if L3 == deque([]):
                break
            p = L[i]+L[j]

            if min(L3) >= p:
                continue
            #Lk = filter(lambda x: x % 2 == 1, L2)
            Lk = [x for x in L3 if p > x]

            for k in Lk:
                if k+L[i] > L[j] and k+L[j] > L[i]:
                    #print(k,L[i] , L[j])
                    count += 1
        if len(L2) == 2:
            break
    print(count)

    return

# E


def E():
    N, M = LI()
    bit_max = 1 << N
    dp = [[inf for i in range(bit_max)] for j in range(M+1)]
    dp[0][0] = 0

    for i in range(M):
        a, b = LI()

        c = LI()
        bit = 0

        for d in c:
            bit |= 1 << (d-1)

        for w in range(bit_max):

            t = w | bit
            cost = dp[i][w] + a

            dp[i+1][t] = min(dp[i+1][t], cost)

            dp[i+1][w] = min(dp[i][w], dp[i+1][w])

    print(-1) if dp[-1][-1] == inf else print(dp[-1][-1])

    return

# F


def F():
    N, M = LI()

    value = [0] * M
    power = [0] * M
    for i in range(M):
        a, b = LI()
        value[i] = a
        c = LI()
        L = ['0'] * N
        for k in c:
            L[k - 1] = '1'
        bint = ''.join(L)
        power[i] = int(bint, 2)
    bint = ''.join(list(['1'] * N))
    dp = [inf] * (int(bint, 2)+1)
    t = 0
    cost = 0

    dp[0] = 0
    for s in range(int(bint, 2) + 1):
        for i in range(M):
            t = s | power[i]
            cost = dp[s] + value[i]
            dp[t] = min(dp[t], cost)
    print(-1) if(dp[-1] == inf) else print(dp[-1])

    return

    # you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")


def solution(S):
    # write your code in Python 3.6
    import math

    vowels = "AIUEO"
    count = 0
    consonants = 0
    newC = []
    newV = []

    for i in S[:-1]:
        if i in vowels:
            count += 1
            newV.append(i)
        else:
            consonants += 1
            newC.append(i)

    if consonants < count or consonants - 2 > count:
        return 0

    a = math.factorial(len(set(newC))) * math.factorial(len(set(newV)))
    return a

    pass


# Solve
if __name__ == '__main__':
    D()

Submission status: Time Limit Exceeded

Solution code:
import copy

def main(N, L):
    L.sort(reverse=True)

    triangles = 0

    for i, P in enumerate(L):
        Q_list = copy.copy(L)
        del Q_list[:i + 1]
        for j, Q in enumerate(Q_list):
            if Q < P / 2:
                break
            minR = P - Q + 1
            R_list = copy.copy(Q_list)
            del R_list[:j + 1]
            for R in R_list:
                if R >= minR:
                    triangles += 1
                else:
                    break

    print(triangles)

if __name__ == '__main__':
    N = int(input())
    L = list(map(int, input().split()))
    main(N, L)

Submission status: Time Limit Exceeded

Solution code:
import bisect
def main():
 
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
 
    # print(L)
    ab =[L[0]+L[1]]
    ans = 0
    for i in range(2, N):
        c = L[i]
        j = bisect.bisect_right(ab, c)
        # print(j)
        # print(ab)
        ans += len(ab) - j
        for k in range(i):
            bisect.insort_left(ab, c+L[k])
        # print(ab)
    return ans
 
if __name__ == '__main__':
    print(main())

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
from bisect import bisect_right

N = int(input())
*L, = map(int, input().split())
L = sorted(L)
ans = 0
for i, j in combinations(reversed(range(N)), 2): # i > j
    cand = L[:j]
    if not cand: continue
    idx = bisect_right(cand, abs(L[i] - L[j]))
    ans += j - idx
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

N = int(input())
L = list(map(int, input().split()))
L.sort()
answer = 0
for i in range(N):
    for j in range(i+1, N):
        answer += bisect_left(L[j+1:], L[i]+L[j])
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import copy

def main(N, L):
    L.sort(reverse=True)

    triangles = 0

    for i, P in enumerate(L):
        Q_list = copy.copy(L)
        del Q_list[:i + 1]
        for j, Q in enumerate(Q_list):
            minR = P - Q + 1
            R_list = copy.copy(Q_list)
            del R_list[:j + 1]
            for R in R_list:
                if R >= minR:
                    triangles += 1
                else:
                    break

    print(triangles)

if __name__ == '__main__':
    N = int(input())
    L = list(map(int, input().split()))
    main(N, L)

Submission status: Time Limit Exceeded

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

L.sort(reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
ls=[int(s) for s in input().split()]
ls.sort()
ls.reverse()
ls2=[ls[0],ls[1]]
def plus(lst,num):
  val=0
  n=len(lst)
  for i in range(n):
    for k in range(1,n+1):
      if i+k>=n:
        val+=k-1
        break
      if lst[i]-lst[i+k]>=num:
        val+=k-1
        break
  return val
ans=0
for j in range(2,N):
  ans+=plus(ls2,ls[j])
  ls2.append(ls[j])
print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N):
    for j in range(i+1,N):
        B2  = L[j+1:][(L[j+1:]<L[i]+L[j]) & (L[j+1:]>abs(L[i]-L[j]))]
        counts +=len(B2)
print(counts)

Submission status: Time Limit Exceeded

Solution code:
import bisect

N = int(input())
L = list(map(int, input().split()))
L.sort()
answer = 0
for i in range(N):
    for j in range(i+1, N):
        answer += bisect.bisect_left(L[j+1:], L[i]+L[j])
print(answer)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
N = int(input())
L = [int(x) for x in input().split()]
L = sorted(L)
num_triangle = 0

for i in range(N-1, 1, -1):
    a = L[i]
    for j in range(i-1, 0, -1):
        b = L[j]
        c = L[j-1]
        if not a < b+c:
            break
#        print(a, b, L[:j], a-b, bisect.bisect_right(L[:j], a-b))
        num_triangle += len(L[:j]) - bisect.bisect_right(L[:j], a-b)

print(num_triangle)

Submission status: Time Limit Exceeded

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

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

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


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    L = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, L)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

L.sort()
#print(L)

from bisect import bisect_right,bisect_left

ans = 0
for i, a in enumerate(L[:-2]):
    for j, b in enumerate(L[i+1:-1]):
        j=i+1+j
        #print(a+b, abs(a-b), i, j)
        #for k, c in enumerate(L[j+1:]):
        #    if abs(a-b) < c and c < a+b:
        #        ans += 1
        #    if c >= a+b:
        #        break
        l = bisect_right(L[j+1:], abs(a-b))
        r = bisect_left(L[j+1:], a+b)
        #print(r-l)
        ans += r-l
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
nums = [int(i) for i in input().split()]
nums.sort()
ans = 0

for i in range(N-1,1,-1):
  point = 0
  for j in range(i-1,0,-1):
    for k in range(j-1,point-1,-1):
      if nums[k]+nums[j] > nums[i]:
        ans += 1
      else:
        point = k+1
        break

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()
#print(L)

from bisect import bisect_right,bisect_left

ans = 0
for i, a in enumerate(L):
    for j, b in enumerate(L[i+1:]):
        j=i+1+j
        #print(a+b, abs(a-b), i, j)
        #for k, c in enumerate(L[j+1:]):
        #    if abs(a-b) < c and c < a+b:
        #        ans += 1
        #    if c >= a+b:
        #        break
        l = bisect_right(L[j+1:], abs(a-b))
        r = bisect_left(L[j+1:], a+b)
        #print(r-l)
        ans += r-l
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.sort()
answer = 0
for i in range(N):
    for j in range(i+1, N):
        answer += sum(L[j+1:] < (L[i]+L[j]))
print(answer)

Submission status: Time Limit Exceeded

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

import sys

def search(a,x,offset=0,iend=None):
    n = len(a)
    if iend is None: iend = n
    assert 0<=offset<iend<=n

    n = iend - offset
    if a[iend-1]<x: return -1
    if n==1:
        assert a[offset]>=x
        return offset
    #
    mid = (n+1)//2 - 1 + offset
    mid1 = mid + 1
    
    if x<=a[mid]: return search(a,x,offset=offset,iend=mid1)
    else: return search(a,x,offset=mid1,iend=iend)
#
n = int(sys.stdin.readline().strip())
l = [ int(x) for x in sys.stdin.readline().strip().split() ]
l.sort()

c = 0
for i in range(1,n):
    for j in range(i+1,n):
        k = search(l, l[j]-l[i]+1, iend=i)
        if k<0: continue
        c += i-k
#
print(c)

Submission status: Time Limit Exceeded

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

def main():
    N = int(input())
    L = [int(x) for x in input().split()]
    ans = 0
    L_sorted = sorted(L)
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            a = L_sorted[i]
            b = L_sorted[j]
            l = bisect.bisect_left(L_sorted, max(a - b, b - a), lo=j + 1)
            r = bisect.bisect_left(L_sorted, a + b, lo=j + 1)
            ans += r - l

    print(ans)



    

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

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

result = 0

for i in range(len(comb)):
    a, b, c = comb[i]
    if a < b + c:
        if b < c + a:
            if c < a + b:
                result += 1

print(result)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

N = int(input())
L = list(map(int, input().split()))
L.sort()
answer = 0
for x, y, z in list(combinations(L, 3)):
    if x + y > z:
        answer += 1
print(answer)

Submission status: Time Limit Exceeded

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

def main():
    N = int(input())
    L = [int(x) for x in input().split()]
    ans = 0
    L_sorted = sorted(L)
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            a = L_sorted[i]
            b = L_sorted[j]
            l = bisect.bisect_left(L_sorted[j:], max(a - b, b - a))
            r = bisect.bisect_left(L_sorted[j:], a + b)
            ans += r - l - 1

    print(ans)



    

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# 入力が10**5とかになったときに100ms程度早い
import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N = read_a_int()
L = read_ints()
L.sort()
# import numpy as np
# L = np.array(L)
from bisect import bisect_left

cnt = 0
for i in range(N):
    a = L[i]
    for j in range(i+1, N):
        b = L[j]
        for k in range(j+1, N):
            c = L[k]
            if c >= a + b:
                break
            cnt += 1
        # cnt += bisect_left(L, a+b) - (j+1)

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys
import random
import itertools
import math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt


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


def LF(): return list(map(float, input().split()))


def LI_(): return list(map(lambda x: int(x)-1, input().split()))


def II(): return int(input())


def IF(): return float(input())


def LS(): return list(map(list, input().split()))


def S(): return list(input().rstrip())


def IR(n): return [II() for _ in range(n)]


def LIR(n): return [LI() for _ in range(n)]


def FR(n): return [IF() for _ in range(n)]


def LFR(n): return [LI() for _ in range(n)]


def LIR_(n): return [LI_() for _ in range(n)]


def SR(n): return [S() for _ in range(n)]


def LSR(n): return [LS() for _ in range(n)]


def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x % y)


def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]


def sosu(x):
    for i in range(2, int(sqrt(x)+1)):
        if x % i == 0:
            return False
    return True


mod = 1000000007
inf = float('INF')


def A():
    A, B = LI()
    ans = A-2*B
    if ans > 0:
        print(ans)
    else:
        print(0)
    return

# B


def B():
    N = II()
    d = LI()
    ans = 0
    for i in range(N):
        for x in range(i+1, N):
            ans += d[i]*d[x]
    print(ans)

    return

# C


def C():
    N = II()
    S = input()
    c = 0
    count = 0
    for i in S:
        if i != c:
            count += 1
        c = i
    print(count-1)

    return

# D


def D():
    N = II()
    L = LI()

    L2 = deque(L)
    count = 0
    for i in range(N-1):
        L2.popleft()
        L3 = list(L2)
        L3 = deque(L3)

        for j in range(i+1, N):
            L3.popleft()
            if L3 == deque([]):
                break
            p = L[i]+L[j]

            if min(L3) >= p:
                continue
            #Lk = filter(lambda x: x % 2 == 1, L2)
            Lk = [x for x in L3 if p > x]

            for k in Lk:
                if k+L[i] > L[j] and k+L[j] > L[i]:
                    #print(k,L[i] , L[j])
                    count += 1
        if len(L2) == 2:
            break
    print(count)

    return

# E


def E():
    N, M = LI()
    bit_max = 1 << N
    dp = [[inf for i in range(bit_max)] for j in range(M+1)]
    dp[0][0] = 0

    for i in range(M):
        a, b = LI()

        c = LI()
        bit = 0

        for d in c:
            bit |= 1 << (d-1)

        for w in range(bit_max):

            t = w | bit
            cost = dp[i][w] + a

            dp[i+1][t] = min(dp[i+1][t], cost)

            dp[i+1][w] = min(dp[i][w], dp[i+1][w])

    print(-1) if dp[-1][-1] == inf else print(dp[-1][-1])

    return

# F


def F():
    N, M = LI()

    value = [0] * M
    power = [0] * M
    for i in range(M):
        a, b = LI()
        value[i] = a
        c = LI()
        L = ['0'] * N
        for k in c:
            L[k - 1] = '1'
        bint = ''.join(L)
        power[i] = int(bint, 2)
    bint = ''.join(list(['1'] * N))
    dp = [inf] * (int(bint, 2)+1)
    t = 0
    cost = 0

    dp[0] = 0
    for s in range(int(bint, 2) + 1):
        for i in range(M):
            t = s | power[i]
            cost = dp[s] + value[i]
            dp[t] = min(dp[t], cost)
    print(-1) if(dp[-1] == inf) else print(dp[-1])

    return

    # you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")


def solution(S):
    # write your code in Python 3.6
    import math

    vowels = "AIUEO"
    count = 0
    consonants = 0
    newC = []
    newV = []

    for i in S[:-1]:
        if i in vowels:
            count += 1
            newV.append(i)
        else:
            consonants += 1
            newC.append(i)

    if consonants < count or consonants - 2 > count:
        return 0

    a = math.factorial(len(set(newC))) * math.factorial(len(set(newV)))
    return a

    pass


# Solve
if __name__ == '__main__':
    D()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
nums = [int(i) for i in input().split()]
nums.sort()
ans = 0

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

Submission status: Time Limit Exceeded

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

L.sort(reverse=True)

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