Problem:


Score : 400 points

Problem Statement

Takahashi has N sticks that are distinguishable from each other. The length of the i-th stick is L_i.

He is going to form a triangle using three of these sticks. Let a, b, and c be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:

a < b + c

b < c + a

c < a + b

How many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.

Constraints

All values in input are integers.

3 \leq N \leq 2 \times 10^3

1 \leq L_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Constraints

Print the number of different triangles that can be formed.

Sample Input 1

4
3 4 2 1

Sample Output 1

1

Only one triangle can be formed: the triangle formed by the first, second, and third sticks.

Sample Input 2

3
1 1000 1

Sample Output 2

0

No triangles can be formed.

Sample Input 3

7
218 786 704 233 645 728 389

Sample Output 3

23



Sample input:
4
3 4 2 1


Sample output:
1

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
# D - Triangles
N = int(input())
S = list(map(int, input().split()))
S = sorted(S, reverse=True)

cnt = 0
for i in range(N-2):
	A = S[i]
	for j in range(i+1,N-1):
		B = S[j]
		for k in range(j+1,N):
			C = S[k]
			if C > A - B:
				cnt += 1
			else:
				break

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

from bisect import bisect_left, bisect_right

def main():
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
    ret = 0
    for a in range(N-2):
        for b in range(a+1,N-1):
#            a<b+c => a-b<c
#            b<c+a => b-a<c
#            c<a+b
# b-a<c<a+b
            rr = bisect_right(L[b+1:], L[a]+L[b]-1)
            ll = 0
            ret += rr - ll

    print(ret)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

from bisect import bisect_left, bisect_right

def main():
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
    ret = 0
    for a in range(N-2):
        for b in range(a+1,N-1):
#            a<b+c => a-b<c
#            b<c+a => b-a<c
#            c<a+b
# b-a<c<a+b
            rr = bisect_right(L[b+1:], L[a]+L[b]-1)
            ll = bisect_left(L[b+1:], L[b]-L[a]+1)
            ret += rr - ll

    print(ret)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
L = list(map(int, input().split()))
A = list(itertools.combinations(L, 3))
ans = 0
for i in A:
    if i[0] >= i[1]+i[2]:
        continue
    if i[1] >= i[0]+i[2]:
        continue
    if i[2] >= i[1]+i[0]:
        continue
    ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N=int(input().rstrip('\n'))
ls = [int(i) for i in input().rstrip('\n').split()]
ls=sorted(ls,reverse = True)
res = 0
for x in range(N-2):
  a = ls[x]
  for y in range(x+1,N-1):
    b = ls[y]
    if a-b>ls[y+1]:
      break
    else:
      for z in range(y+1,N):
        if a-b<ls[z]:
          res += 1
        else:
          break
print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N=int(input().rstrip('\n'))
ls = [int(i) for i in input().rstrip('\n').split()]
ls=sorted(ls,reverse = True)
res = 0
for x in range(N-2):
  a = ls[x]
  for y in range(x+1,N-1):
    b = ls[y]
    if a>(b+ls[y+1]):
      break
    else:
      for z in ls[y+1:]:
        if a<(b+z):
          res += 1
        else:
          break
print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N=int(input().rstrip('\n'))
ls = [int(i) for i in input().rstrip('\n').split()]
ls=sorted(ls,reverse = True)
res = 0
for x in range(N-2):
  a = ls[x]
  for y in range(x+1,N-1):
    b = ls[y]
    if a>(b+ls[y+1]):
      break
    else:
      for z in range(y+1,N):
        if a<(b+ls[z]):
          res += 1
        else:
          break
print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N=int(input().rstrip('\n'))
ls = [int(i) for i in input().rstrip('\n').split()]
ls=sorted(ls,reverse = True)
res = 0
for x in range(N-2):
  a = ls[x]
  for y in range(x+1,N-1):
    b = ls[y]
    if a>(b+ls[y+1]):
      break
    else:
      for z in range(y+1,N):
        if a<(b+ls[z]):
          res += 1
        else:
          break
print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
from itertools import combinations
N=int(input().rstrip('\n'))
ls = [int(i) for i in input().rstrip('\n').split()]
ls = sorted(ls)
ok = [x for x in combinations(ls,3) if x[0]+x[1]>x[2]]
print(len(ok))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
from itertools import combinations
N=int(input().rstrip('\n'))
ls = [int(i) for i in input().rstrip('\n').split()]
ls = sorted(ls)
res = 0
for x in range(N):
  a = ls[x]
  for y in range(x+1,N):
    b = ls[y]
    for z in range(y+1,N):
      c = ls[z]
      if c<a+b:
        res += 1
      else:
        break
print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict
import bisect
from sys import stdin
input = stdin.readline


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

  # dic = defaultdict(int)
  # for l in L:
  #   dic[l] += 1
  # uniq = sorted(dic.keys())
  # sum_ = [0]*(uniq[-1]+1)
  # for i in range(1, uniq[-1]+1):
  #   sum_[i] = sum_[i-1] + dic[i]
  L = sorted(L)

  ans = 0
  for i in range(N):
    for j in range(i+1, N):
      if i == j:
        continue
      a = L[i]
      b = L[j]
      # lower = bisect.bisect_right(L[:i]+L[i+1:j]+L[j+1:], abs(a-b)) - 1
      # upper = bisect.bisect_right(L[:i]+L[i+1:j]+L[j+1:], a+b) - 1
      lower = bisect.bisect_right(L[j+1:], abs(a-b)) - 1
      upper = bisect.bisect_left(L[j+1:], a+b) - 1
      # print(a, b, lower, upper)
      # print(f'--> {L[j+1:]}')

      # if lower < 0 or upper < 0: continue
      if upper < 0: continue
      ans += (upper - max(lower, 0) + 1)

  # print(dic)
  # print(uniq)
  # print(sum_)
  print(ans)


if(__name__ == '__main__'):
  main()

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

ans = 0
for i in range(len(L)):
    for j in range(i + 1, len(L)):
        a = L[i]
        b = L[j]
        ans += bisect.bisect_right(L[j + 1:], a + b - 1)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

ans = set()
for i in range(len(L)):
    for j in range(i + 1, len(L)):
        a = L[i]
        b = L[j]
        left = b - a
        right = a + b
        for k in range(bisect.bisect_right(L, left), bisect.bisect_left(L, right)):
            if k != i and k != j:
                ans.add(tuple(sorted([a, b, L[k]])))

print(len(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys

ri = lambda: int(sys.stdin.readline())
rl = lambda: list(map(int, sys.stdin.readline().split()))

a = ri()
li = sorted(rl(), reverse=True)
cnt = 0
for i in range(a-2):
    for j in range(i+1, a-1):
        left = j+1
        right = a
        lii = li[i]
        lij = li[j]
        if lii >= lij + li[j+1]:
            continue
        while right-left > 1:
            t = (left + right)//2
            lit = li[t]
            if lii < lij + lit:
                left = t
            else:
                right = t
        cnt += left-j
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

a = ri()
li = sorted(rl(), reverse=True)
cnt = 0
for i in range(a-2):
    for j in range(i+1, a-1):
        left = j+1
        right = a
        lii = li[i]
        lij = li[j]
        while right-left > 1:
            lit = li[(left + right)//2]
            if lii < lij + lit:
                left = (left + right)//2
            else:
                right = (left + right)//2
        if lii < lij + li[(left + right)//2]:
            cnt += left-j
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools

ri = lambda: int(sys.stdin.readline())
rl = lambda: list(map(int, sys.stdin.readline().split()))

a = ri()
li = rl()
cnt = 0
for lis in list(itertools.combinations(li, 3)):
    max_ = max(lis)
    sum_ = sum(lis)
    if max_ < sum_-max_:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)

n=int(input())
a1=list(map(int,input().split()))
a1.sort()
r=0
def func(ans,ll,rr):
  #print(ans,ll,rr)
  if ans<=a1[ll]:
    return 0
  elif ans>a1[rr-1]:
    return rr-ll
  else:
    mm=ll+(rr-ll)//2
    #print("mm",mm)
    if ans<=a1[mm]:
      return func(ans,ll,mm)
    else:
      return func(ans,mm,rr)
  
  
  return rr
import numpy as np
for ii in range(len(a1)-2):
  for jj in np.arange(ii+1,len(a1)-1):
    ans=a1[ii]+a1[jj]
    ans
    r+=func(ans,jj+1,len(a1))
    """
    for kk in np.arange(jj+1,len(a1)):
      if a1[ii]+a1[jj]<=a1[kk]:
        break
      r+=1
    """
print(r)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a1=list(map(int,input().split()))
a1.sort()
r=0
import numpy as np
for ii in range(len(a1)-2):
  for jj in np.arange(ii+1,len(a1)-1):
    for kk in np.arange(jj+1,len(a1)):
      if a1[ii]+a1[jj]<=a1[kk]:
        break
      r+=1
print(r)

Submission status: Time Limit Exceeded

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

L.sort()
from bisect import*
i=0
ans=0
for i in range(N-1):
    for j in range(i+1,N):
        ans+=bisect_left(L[j:],L[i]+L[j])-1

print(ans)

Submission status: Time Limit Exceeded

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

L.sort()
from bisect import*
i=0
ans=0
while i<N:
    j=i+1
    while j<N:
        ans+=bisect_left(L[j:],L[i]+L[j])-1
        j+=1
    i+=1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect as bs
N=int(input())
y=list(map(int, input().split()))
y.sort()
ans=0
for i in range(N-2):
  for j in range(i+1, N-1):
    tmp=y[i]+y[j]
    ans+=bs.bisect_left(y[(j+1):],tmp)
    
print(ans)

Submission status: Time Limit Exceeded

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

from bisect import bisect_left as bi

for j in range(N):
    for k in range(j + 1,N - 1):
        right = L[j] + L[k]
        L_slice = L[k:N]
        ite_right = bi(L_slice,right)
        count = count + (ite_right - 1)

print(count)

Submission status: Time Limit Exceeded

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

from bisect import bisect_left as bi

for j in range(N):
    for k in range(j + 1,N - 1):
        right = L[j] + L[k]
        left = abs(L[j] - L[k])
        L_slice = L[k:N]
        ite_right = bi(L_slice,right)
        count = count + (ite_right - 1)

print(count)

Submission status: Time Limit Exceeded

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

import bisect as bi

for j in range(N):
    for k in range(j + 1,N - 1):
        right = L[j] + L[k]
        left = abs(L[j] - L[k])
        L_slice = L[k:N]
        ite_right = bi.bisect_left(L_slice,right)
        ite_left = bi.bisect_left(L_slice,left)
        count = count + (ite_right - ite_left - 1)

print(count)

Submission status: Time Limit Exceeded

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

for j in range(N):
    for k in range(j + 1,N):
        right = L[j] + L[k]
        left = abs(L[j] - L[k])
        for l in range(k + 1,N):
            if(L[l] < right and L[l] > left):
                count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
L.sort()
from bisect import bisect_left
ans = 0
for i1 in range(N-2):
  for i2 in range(i1+1,N-1):
    l1,l2 = L[i1],L[i2]
    th = bisect_left(L[i2+1:],l1+l2)
    #print(l1,l2,L[i2+1:],(l2-l1+1)*(-1),th)
    ans += th
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(lambda x: int(x)*(-1),input().split()))
L.sort()
from bisect import bisect
ans = 0
for i1 in range(N-2):
  for i2 in range(i1+1,N-1):
    l1,l2 = L[i1],L[i2]
    th = bisect(L[i2+1:],(l2-l1+1)*(-1))
    #print(l1,l2,L[i2+1:],(l2-l1+1)*(-1),th)
    ans += th
print(ans)

Submission status: Time Limit Exceeded

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

L.sort()

cnt=0
for i, a in enumerate(L):
  if i==len(L)-2:break
  for j, b in enumerate(L[i+1:]):
    j = i+j+1
    if j==len(L)-1:break
    cnt+=len([c for c in L[j+1:]if c < a + b])
print(cnt)

Submission status: Time Limit Exceeded

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

L.sort()

cnt=0
for i, a in enumerate(L):
  if i==len(L)-2:break
  for j, b in enumerate(L[i+1:]):
    j = i+j+1
    if j==len(L)-1:break
    for c in L[j+1:]:
      #ソートされているため，a+b<=cなら，それ以降
      if not c < a + b:
        #print("c >= a + b")
        break
      
      if b < c + a and a < b + c :
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
y=list(map(int, input().split()))
from itertools import combinations
C=list(combinations(range(N), 3))

def cond(a, b, c):
  c1=a+b>c
  c2=b+c>a
  c3=c+a>b
  if c1 and c2 and c3:
    return True
  else:
    return False

ans=0
for c in C:
  if cond(y[c[0]], y[c[1]], y[c[2]]):
    ans+=1

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

cnt = 0
start_j = 1
start_k = 2
for i in range(N):
    for j in range(i+1, N):
        for k in range(j+1, N):
            if l_list[k] < l_list[i] + l_list[j]:
                cnt += 1
            else:
                break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:

def binary_search(border, b):
    ok = b
    ng = n
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if L[mid] < border:
            ok = mid
        else:
            ng = mid

    return ok


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

for a in range(0,n-1):
    for b in range(a+1, n):
        a_b = L[a] + L[b]
        ans += binary_search(a_b, b) - b

print(ans)

Submission status: Time Limit Exceeded

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

s = []
for i in range(n - 2):
    a = L[i]
    for j in range(i + 1, n - 1):
        b = L[j]
        c_min = b - a
        c_max = a + b
        if c_max > c_min + 1:
            s.append((c_min, c_max, j + 1))

ans = 0
for x, y, j in s:
    i_min = bisect.bisect_right(L[j:], x)
    i_max = bisect.bisect_left(L[j:], y)
    ans += i_max - i_min
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import bisect

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

ans = 0

for a,b in itertools.combinations(LS,2):
  a,b = min(a,b),max(a,b)
  lo = b - a
  up = a + b
  
  cnt = bisect.bisect_left(LS,up) - bisect.bisect_right(LS,lo) - 1
  if lo < a:
    cnt -= 1
    
  if cnt <= 0:
    continue
    
  ans += cnt
  
print(ans // 3)

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):
  for j in range(i+1,n):
    a=l[i]
    b=l[j]
    le=bisect.bisect_left(l,max(a-b,b-a)+1)
    ri=bisect.bisect_right(l,a+b-1)
    #print(le,ri)
    f=0
    if le<=i<=ri:
      f-=1
    if le<=j<=ri:
      f-=1
    if ri-le+f>0:
      ans+=ri-le+f
    #print(a,b,ans)

print(ans//3)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
L=list(map(int,input().split()))
L.sort(reverse = True)
ans = 0
for i in range(n):
    for j in range(i+1,n):
        cnt = 0
        start=j+1
        goal=n
        while goal!=start:
            half = (start + goal)//2
            if L[i] < L[j] + L[half]:
                start = half + 1
                cnt = max(half-j,cnt)
            else:
                goal = half
        ans += cnt
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from math import ceil,floor,comb,factorial,gcd,pow,sqrt,log2,cos,sin,tan,pi,inf
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heappop,heappush
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

def is_ok(arg):
    return l[arg] < l[i] + l[j]

def meguru_bisect(ng, ok):
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid
    return ok

n = INT()
l = sorted(LIST())

ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        ans += meguru_bisect(n, j) - j
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def bisect_left_reverse(a, x):
    '''
    reverseにソートされたlist aに対してxを挿入できるidxを返す。
    xが存在する場合には一番左側のidxとなる。
    '''
    if a[0] <= x:
        return 0
    if x < a[-1]:
        return len(a)
    # 二分探索
    ok = len(a) - 1
    ng = 0
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if a[mid] <= x:
            ok = mid
        else:
            ng = mid
    return ok
def main2():
    n = int(input())
    l=[int(i) for i in input().split()]
    l.sort(reverse = True)
    res = 0
    for i in range(n-1):
        for j in range(i+1, n-1):
            #index がj以降で　k < c を満たすcで一番小さいc'を探す
            #index(c')-j - 1が三角形の条件を満たす
            nibu = l[j+1:]
            k = max(l[i]-l[j],l[j]-l[i])
            left=bisect_left_reverse(nibu,k)
            res += left
    print(res)

if __name__ == '__main__':
    main2()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline


def bisect_left(a, x, lo=0, hi=None):
    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if a[mid] < x: lo = mid+1
        else: hi = mid
    return lo


def solve(N, L):
    L.sort()
    ans = 0
    for a in range(N - 2):
        for b in range(a + 1, N - 1):
            c = bisect_left(L, L[a] + L[b]) - 1
            ans += c - b
    return ans


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

    ans = solve(N, L)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline


def binary_search(a, key):
    """Meguru type binary search"""
    ng = -1
    ok = len(a)

    while (ok - ng > 1):
        mid = (ok + ng) // 2
        if a[mid] >= key:
            ok = mid
        else:
            ng = mid

    return ok


def solve(N, L):
    L.sort()
    ans = 0
    for a in range(N - 2):
        for b in range(a + 1, N - 1):
            c = binary_search(L, L[a] + L[b]) - 1
            ans += c - b
    return ans


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

    ans = solve(N, L)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline


def binary_search(a, key):
    """Meguru type binary search"""
    ng = -1
    ok = len(a)

    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if a[mid] >= key:
            ok = mid
        else:
            ng = mid

    return ok


def solve(N, L):
    L.sort()
    ans = 0
    for a in range(N - 2):
        for b in range(a + 1, N - 1):
            c = binary_search(L, L[a] + L[b]) - 1
            ans += c - b
    return ans


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

    ans = solve(N, L)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline


def is_ok(a, key, idx):
    if a[idx] >= key:
        return True
    else:
        return False


def binary_search(a, key):
    """Meguru type binary search"""
    ng = -1
    ok = len(a)

    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(a, key, mid):
            ok = mid
        else:
            ng = mid

    return ok


def solve(N, L):
    L.sort()
    ans = 0
    for a in range(N - 2):
        for b in range(a + 1, N - 1):
            c = binary_search(L, L[a] + L[b]) - 1
            ans += c - b
    return ans


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

    ans = solve(N, L)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
import numpy as np
L = np.array(input().split(),np.int16)
L.sort()
L = L[::-1]
#L.sort(reverse=True)
counter = 0

for index1,c in enumerate(L):
    index2 = index1+1
    if index2>N-1:
      break
    b = L[index2]
    while b>=c/2:
        
        index3 = index2+1
        if index3>N-1:
            break
        a = L[index3]
        
        while a+b>c:
            counter += 1
            index3+=1
            if index3>N-1:
                break
            a = L[index3]

        index2+=1
        if index2>N-1:
            break
        b = L[index2]


print(counter)

Submission status: Time Limit Exceeded

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

L.sort(reverse=True)
counter = 0

for index1,c in enumerate(L):
    index2 = index1+1
    if index2>N-1:
      break
    b = L[index2]
    while b>=c/2:
        
        index3 = index2+1
        if index3>N-1:
            break
        a = L[index3]
        
        while a+b>c:
            counter += 1
            index3+=1
            if index3>N-1:
                break
            a = L[index3]

        index2+=1
        if index2>N-1:
            break
        b = L[index2]


print(counter)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
L = list(map(int, input().split()))
comb = itertools.combinations
lists = list(comb(L, 3))

cnt = 0

for lis in lists:
  if not lis[0] < lis[1] + lis[2]:
    continue
  if not lis[1] < lis[0] + lis[2]:
    continue
  if not lis[2] < lis[0] + lis[1]:
    continue
  cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

L.sort(reverse=True)

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):
            if a - b < L[k]:
                total += 1

print(total)

Submission status: Time Limit Exceeded

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

for i in range(N-2):
    for j in range(i+1, N-1):
        a = max(L[i], L[j])
        b = min(L[i], L[j])
        for k in range(j+1, N):
            if a - b < L[k] < a + b:
                total += 1

print(total)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
total = 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 b + c > a and a + b > c and a + c > b:
                total += 1

print(total)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math


# input = sys.stdin.readline

def binary_search(arr, key,j):
    ok = j
    ng = len(arr)

    while abs(ok - ng) > 1:
        mid = int((ok + ng) / 2)

        if arr[mid] < key:
            ok = mid
        else:
            ng = mid

    return ok


def main():
    N = int(input())
    L = sorted(list(map(int, input().split())))
    ans =0
    for i in range(N-2):
        for j in range(i+1,N-1):
            ok = binary_search(L,L[i]+L[j],j)
            ans += ok-j
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

# a, bをfixしてcを探索
# n*n*logn
# b-a < c_i < a+b をにぶたん？
def binary_search(list, item):
    # listからitem以下の最大のindexを取得
    low = 0
    high = len(list) - 1
    if list[high] < item:
        return high+1
    if list[low] >= item:
        return low
    while True:
        mid = (low + high) //2
        guess = list[mid]
        if high - low <= 1:
            return high
        if guess > item:
            high = mid
        else:
            low = mid

ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        a = l[i]
        b = l[j]
        c_low = binary_search(l[j+1:], b-a)
        c_high = binary_search(l[j+1:], b+a)
        ans += c_high - c_low
        # print(a, b, c_low, c_high)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ll = [int(w) for w in input().split()]
ll.sort(reverse=True)

ans = 0
for ei, i in enumerate(ll):
    for ej, j in enumerate(ll[ei + 1:]):
        ans += len([w for w in ll[ei + ej + 2:] if w > i - j])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ll = [int(w) for w in input().split()]
ll.sort(reverse=True)

ans = 0
for ei, i in enumerate(ll):
    for ej, j in enumerate(ll[ei + 1:]):
        for k in ll[ei + ej + 2:]:
            if i < (j + k):
                ans += 1
            else:
                break


print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
N = int(input())
l = list(map(int, input().split()))
l.sort()
count = 0
now = l[:2]
for i in range(2, N):
    for j in combinations(now, 2):
        if j[0]+j[1] > l[i]:
            count += 1
    now.append(l[i])
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
ll = [int(w) for w in input().split()]
ll.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 ll[i] < (ll[j] + ll[k]):
                ans += 1
            else:
                break


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N = int(input())
L = sorted(list(map(int,input().split())),reverse = True)

ans = 0

for i in range(N-2):
  c = itertools.combinations(L[i+1:],2)
  for j in c:
    if sum(j) > L[i]:
      ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

from itertools import combinations
LS = list(combinations(A,3))
cnt = 0
for L in LS:
    if abs(L[0]-L[1]) < L[2] < L[0]+L[1]:
        cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

cnt = 0

for i in range(n-2):
    for j in range(i+1,n-1):
        for k in range(j+1, n):
            a = l[i]
            b = l[j]
            c = l[k]
            if a < b+c and b < c+a and c < a+b:
                cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import bisect


N = int(input())
str_l = input()
num = 0
L = []
for v in str_l.split(' '):
    L.append(int(v))

L.sort()
for i in range(N - 1):
    a = L[i]
    for j in range(i, N - 2):
        b = L[j + 1]
        index = bisect.bisect_left(L[j + 2:], a + b)
        for k in range(0, index):
            
            c = (L[j + 2:])[k]
            if b + c > a and a + c > b:
                num += 1

print(num)

Submission status: Time Limit Exceeded

Solution code:
def binary_search(list, item):
    low = 0
    lengyh = len(list)
    if lengyh != 0 and list[lengyh - 1] < item:
        return lengyh - 1
    high = lengyh - 1

    while low <= high:
        mid = (low + high) // 2
        guess = list[mid]

        if guess == item - 1 or (mid < lengyh - 1 and (guess < item and list[mid + 1] > item)):
            return mid
        if guess > item:
            high = mid - 1
        else:
            low = mid + 1

    return None


N = int(input())
str_l = input()
num = 0
L = []
for v in str_l.split(' '):
    L.append(int(v))

L.sort()
for i in range(N):
    a = L[i]
    for j in range(i, N - 1):
        b = L[j + 1]
        index = binary_search(L[j + 2:], a + b)
        if index is None:
            continue
        else:
            for k in range(0, index + 1):
                c = (L[j + 2:])[k]
                if b + c > a and a + c > b:
                    num += 1

print(num)