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 sys
input = sys.stdin.readline
import numpy as np

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):
            num=np.searchsorted(l[j+1:],l[i]+l[j]-0.5,side='right')
            ans+=num
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

#print(l)
ans = 0
for i in range(n-2):
    for j in range(i + 1, n-1):
        mi = l[i] - l[j]
        ma = l[i] + l[j]
        ans +=sum([mi < k < ma for k in l[j + 1 :]])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = sorted(map(int,input().split()),reverse=True)
num = 0

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

Submission status: Time Limit Exceeded

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

num = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            array = [L[i],L[j],L[k]]
            array.sort(reverse=True)
            if(array[0]>=array[1]+array[2]):
                continue
            else:
                num += 1
print(num)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = sorted(list(map(int,input().split())),reverse = False)
cnt = 0
for a in range(0,N-2):
    A = L[a]
    for b in range(a+1,N-1):
        B = L[b]
        low = b+1
        high = N-1
        while(low+1 != high and low != high):
            mid = (low+high)//2
            if(A+B > L[mid]):
                low = mid
            else:
                high = mid
        if(A+B > L[high]):
            cnt += high-b
        elif(A+B > L[low]):
            cnt += low-b
print(cnt)

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):
  for j in range(i + 1, N):
    for k in range(N - 1, j, -1):
      if L[k] < L[i] + L[j]:
        count += k - j 
        break
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):
  for j in range(i + 1, N):
    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())
L = list(map(int,input().split()))
L.sort()
count = 0

for i in range(N):
  for j in range(i + 1, N):
    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())
li=list(map(int,input().split()))
li.sort()
result=[]
count=0
def bis(li,target):
    result=None
    left=0
    right=len(li)-1
    while left<=right:
        mid=(left+right)//2
        if li[mid]==target:
            result=mid
            break
        elif li[mid]>target:
            right=mid-1
        else:
            left=mid+1
    if result==None:
        return left-1#left番目まで数えればよい
    else:
        getter=result#例えば、[7,7,7]のとき真ん中の7のインデックスがgetterに格納されることになる。
        for i in range(0,getter):
            if li[getter-i]<li[getter]:
                return getter-i#例の時、[6,7...]なら6のインデックスを取得

for i in range(0,N-1):
    for j in range(i+1,N):#li[i]+li[j]の値以上の値を発見し、その値より一以上小さい値のインデックスを取得。次にその値とj+1の間の要素数をカウント
        target=li[i]+li[j]
        searchindex=bis(li,target)
        count+=searchindex-j
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
li=list(map(int,input().split()))
result=[]
count=0
for i in range(0,N):
    for j in range(i+1,N):
        for k in range(j+1,N):
            if li[i]<li[j]+li[k] and li[j]<li[i]+li[k] and li[k]<li[i]+li[j]:
                newli=[li[i],li[j],li[k]]
                newli.sort()
                result.append(newli)
                count+=1
for i in range(len(result)):
    for j in range(i+1,len(result)):
        if result[i]==result[j]:
            count-=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
l.sort()
ans=0
from bisect import bisect_left as bl,bisect_right as br
for i in range(n):
    for j in range(i+1,n):
        a,s=l[i],l[j]
        ans+=-1-int(s<2*a)+bl(l,a+s)-br(l,s-a)
print(ans//3)

Submission status: Time Limit Exceeded

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

L.sort()
cnt=0
for i in range(n-2):
    for j in range(i+1,n-1):
        if L[i]+L[j]<=L[j+1]:
            continue
        if L[i]+L[j]>L[n-1]:
            cnt+=n-j-1
            continue
        left=j+1
        right=n-1
        while right-left>1:
            mid=left+(right-left)//2
            if L[mid]<L[i]+L[j]:
                left=mid
            else:
                right=mid
        cnt+=left-j
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import itertools 
 
n=int(input())
 
L=list(map(int,input().split()))
cnt=0
for v in itertools.combinations(L,3):
    v=list(v)
    v.sort()
    if v[0]+v[1]>v[2]:
        cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(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[j]+l[i] :
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

ls.sort()

dis = 0
for i in range(0, n-2):
    for j in range(i+1, n-1):
        for k in range(j+1, n):
            if ls[i] + ls[j] <= ls[k]: 
                dis += 1

ans = n*(n-1)*(n-2)//6-dis
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
L.sort()
count = 0
for a in range(N):
    for b in range(a + 1, N):
        for c in range(b + 1, N):
            if L[c] >= L[a] + L[b]:
                break
            elif L[a] >= L[b] + L[c] or L[b] >= L[c] + L[a]:
                continue
            else:
                count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int,input().split()))
from itertools import combinations
import numpy as np 
com = list(combinations(l,3))
count = 0
for key in com:
    ma = max(key)
    mi = min(key)
    mid = np.sum(key) - ma - mi
    if min(key)+ mid > ma:
        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):
  for j in range(i+1,n-1):
    x=j+1
    y=n-1
    while y!=x:
      if L[i]+L[j]>L[(x+y)//2]:
        x=(x+y)//2
      else:
        y=(x+y)//2
    p=y if L[i]+L[j]>L[y] else x
    if L[i]+L[j]<=L[p]:
      count+=0
    else:
      count+=p-j
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
L=list(map(int,input().split()))
L.sort()
count=0
for i in range(n-2):
  for j in range(i+1,n-1):
    x=j+1
    y=n-1
    while y!=x+1 and y!=x:
      if L[i]+L[j]>L[(x+y)//2]:
        x=(x+y)//2
      else:
        y=(x+y)//2
    p=y if L[i]+L[j]>L[y] else x
    if L[i]+L[j]<=L[p]:
      count+=0
    else:
      count+=p-j
print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
L=list(map(int,input().split()))
L.sort()
count=0
for i in range(n-2):
  for j in range(i+1,n-1):
    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:
from sys import stdin
from itertools import takewhile
        
N = int(stdin.readline().rstrip())
L = [int(x) for x in stdin.readline().rstrip().split()]
L.sort()

ans = 0
for a in range(len(L)-2):
    for b in range(a + 1, len(L)-1):
        ab = L[a] + L[b]
        c = list(takewhile(lambda x: x < ab, L[b+1:]))
        ans += len(c)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect

N = int(input())
L = [int(x) for x in input().split()]
L.sort()
ret = 0
# print(L)
for i in range(N-2):
    for j in range(i+1, N-1):
        add = bisect.bisect_left(L,L[i]+L[j]-0.01) - (j+1)
        # print(f"{L[i]}, {L[j]} , add:{add}")
        ret += add

print(ret)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import itertools

n = int(stdin.readline().rstrip())
l = [int(x) for x in stdin.readline().rstrip().split()]
c = itertools.combinations(l, 3)

cnt = 0
for i in c:
    a, b, c = i[0], i[1], i[2]
    if (a < b + c) and (b < c + a) and (c < a + b):
        cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n=int(input())
L=map(int,input().split())
ans=0
for a, b, c in combinations(L,3):
    if abs(b-c) < a < b+c:
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
from itertools import combinations
input = lambda: sys.stdin.readline().rstrip()


def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return


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


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

    c = list(combinations(Ls, 3))

    cnt = 0
    for i in range(len(c)):
        cnt += check(*c[i])
    print(cnt)
    return

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

l.sort()

import bisect
ans = 0
for i in range(n):
    for j in range(i+1, n):
        ans += bisect.bisect_left(l[j+1:], l[i] + l[j])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools 

n=int(input())

L=list(map(int,input().split()))
cnt=0
for v in itertools.combinations(L,3):
    v=list(v)
    v.sort()
    if v[0]+v[1]>v[2]:
        cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())

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

L.sort()
cnt=0
for i in range(n-2):
    for j in range(i+1,n-1):
        for k in range(j+1,n):
            if L[i]+L[j]>L[k]:
                cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
import numpy as np

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

hoge = np.sort(l)
ans = 0

for v in combinations(hoge, 2):
    s = sum(v)
    ans += len(hoge[(v[1] < hoge) & (hoge < s)])

print(ans)

"""
これだとTLEが出る
combinationの全パターンを数え上げると遅すぎる
ソート済み配列 × なにかで組み合わせを数え上げる必要

from itertools import combinations

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

ans = 0

for v in combinations(l, 3):
    if v[0] < v[1] + v[2] and v[1] < v[0] + v[2] and v[2] < v[0] + v[1]:
        ans += 1

print(ans)
"""

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
import numpy as np

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

hoge = np.sort(l)
ans = 0

for v in combinations(hoge, 3):
    if v[2] < v[0] + v[1]:
        ans += 1

print(ans)

"""
これだとTLEが出る
combinationの全パターンを数え上げると遅すぎる
ソート済み配列 × なにかで組み合わせを数え上げる必要

from itertools import combinations

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

ans = 0

for v in combinations(l, 3):
    if v[0] < v[1] + v[2] and v[1] < v[0] + v[2] and v[2] < v[0] + v[1]:
        ans += 1

print(ans)
"""

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

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

ans = 0

for v in combinations(l, 3):
    if v[0] < v[1] + v[2] and v[1] < v[0] + v[2] and v[2] < v[0] + v[1]:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

ans = 0

L = np.sort(L)

for a in range(N-2):
    for b in range(a+1, N-1):
        ans += sum((L < (L[a]+L[b])) * (L > L[b]))

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[c] + L[a] and L[c] < L[a] + L[b]:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

n = int(input())
l_ls = sorted(map(int, input().split()))

ans = 0
for i, a in enumerate(l_ls):
    for j, b in enumerate(l_ls):
        if i >= j:
            continue

        ls = l_ls[:i] + l_ls[i+1:j] + l_ls[j+1:]

        left = bisect_left(ls, max(a-b, b-a)+1)
        right = bisect_left(ls, a+b)
        ans += right-left


print(ans//3)

Submission status: Time Limit Exceeded

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

ans = 0
memo = []
for i, a in enumerate(l_ls):
    for j, b in enumerate(l_ls):
        if i == j:
            continue

        for k, c in enumerate(l_ls):
            if i == k or j == k:
                continue

            if sorted([a, b, c]) in memo:
                continue

            if a < b+c and b < c+a and c < a+b:
                ans += 1
                memo.append(sorted([a, b, c]))

print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
for i, a in enumerate(l_ls):
    for j, b in enumerate(l_ls):
        if i == j:
            continue
        for k, c in enumerate(l_ls):
            if i == k or j == k:
                continue

            if a < b+c and b < c+a and c < a+b:
                ans += 1
print(ans//6)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
# s=input()
# a,b=map(int,input().split())
# h=list(map(int,input().split()))
l=sorted(list(map(int,input().split())))

# mod=10**9+7
# n=20
# fact=[1]
# for i in range(n):
#     fact+=fact[-1]*(i+1)%mod,
# print(fact)
# print(l)
# ほんと？
from bisect import bisect_left,bisect_right
ans=0
for i in range(n):
    for j in range(i+1,n):
        a=l[i]
        b=l[j]
        ll=l[j+1:]
        # print(ll)
        # abs(a-b)<c<a+bをみたすcは何個？
        ans+=bisect_left(ll,a+b) # -bisect_right(ll,b-a)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
# s=input()
# a,b=map(int,input().split())
# h=list(map(int,input().split()))
l=sorted(list(map(int,input().split())))

# mod=10**9+7
# n=20
# fact=[1]
# for i in range(n):
#     fact+=fact[-1]*(i+1)%mod,
# print(fact)
# print(l)
# ほんと？
from bisect import bisect_left,bisect_right
ans=0
for i in range(n):
    for j in range(i+1,n):
        a=l[i]
        b=l[j]
        ll=l[j+1:]
        # print(ll)
        # abs(a-b)<c<a+bをみたすcは何個？
        ans+=bisect_left(ll,a+b)-bisect_right(ll,b-a)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

n = int(input().rstrip())
bo = [j * -1 for j in sorted([int(i) * -1 for i in input().rstrip().split(" ")])]

def get_count(start:int, num:int):
    if bo[len(bo)-1] > num:
        return len(bo)-1
    if bo[start] <= num:
        return -1
    return get_count_sub(start, len(bo)-1, num)

def get_count_sub(min:int, max:int, target:int):
    if max - min <= 1:
        if bo[max] > target:
            return max
        else:
            return min

    mid = int((max+min)/2)
    if bo[mid] > target:
        return get_count_sub(mid, max, target)
    else:
        return get_count_sub(min, mid, target)

ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        if j < int(i/2):
            break
        idx = get_count(j + 1, bo[i] - bo[j])
        if idx < 0:
            break
        ans += idx - j

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int,input().split()))
output = 0
from itertools import combinations

M = list(combinations(L,3))
NM = len(M)
for i in range(NM):
    T = M[i]
    if abs(T[0]-T[1])<T[2]:
        if T[2]<(T[0]+T[1]):
            output+=1

print(output)

Submission status: Time Limit Exceeded

Solution code:
import bisect

n = int(input())
L = sorted([int(x) for x in input().split()])
ans = 0
for ai in range(n - 1):
    for bi in range(ai + 1, n):
        a = L[ai]
        b = L[bi]
        l, r = abs(a - b) + 1, a + b - 1
        li = bisect.bisect_left(L, l)
        ri = bisect.bisect_right(L, r)
        ans += ri - li
        if li <= ai <= ri:
            ans -= 1
        if li <= bi <= ri:
            ans -= 1
print(int(ans / 3))

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):
    a = L[i]
    for j in range(i + 1, N - 1):
        b = L[j]
        left = j
        right = N
        if L[left + 1] >= a + b:
            continue
        while right - left > 1:
            mid = (left + right) // 2
            c = L[mid]
            if c < a + b:
                left = mid
            else:
                right = mid
        ans += left - j

print(ans)

Submission status: Time Limit Exceeded

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

s = set()

for i, x in list(enumerate(v))[:-2]:
    for j, y in list(enumerate(v))[i+1:-1]:
        for k, z in list(enumerate(v))[j+1:]:
            if z < x+y and (x, y, z) not in s:
                s.add((x, y, z))

print(len(s))

Submission status: Time Limit Exceeded

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

from bisect import bisect_left

cnt = 0
for i in range(0, N - 2):
    for j in range(i + 1, N - 1):
        t = L[i] + L[j]
        idx = bisect_left(L, t, lo=j + 1, hi=len(L))
        cnt += max(0, idx - j - 1)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from _bisect import bisect_left

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

cnt = 0
for i in range(0, N - 2):
    for j in range(i + 1, N - 1):
        t = L[i] + L[j]
        idx = bisect_left(L, t, lo=j + 1)
        cnt += max(0, idx - j - 1)
print(cnt)

Submission status: Time Limit Exceeded

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


def bisect_left(a, x, lo=0, hi=None):
    """Return the index where to insert item x in list a, assuming a is sorted.

    The return value i is such that all e in a[:i] have e < x, and all e in
    a[i:] have e >= x.  So if x already appears in the list, a.insert(x) will
    insert just before the leftmost x already there.

    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """

    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


cnt = 0
for i in range(0, N - 2):
    for j in range(i + 1, N - 1):
        t = L[i] + L[j]
        idx = bisect_left(L, t, lo=j + 1)
        cnt += max(0, idx - j - 1)
print(cnt)

Submission status: Time Limit Exceeded

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


def bisect_left(a, x, lo=0, hi=None):
    """Return the index where to insert item x in list a, assuming a is sorted.

    The return value i is such that all e in a[:i] have e < x, and all e in
    a[i:] have e >= x.  So if x already appears in the list, a.insert(x) will
    insert just before the leftmost x already there.

    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """

    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


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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def bisecsearch(l,t,lo,hi):
  while lo < hi:
    mid = (lo+hi)//2
    if l[mid] < t:
      lo = mid + 1
    else:
      hi = mid
  return lo

cnt = 0
for i in range(N-2):
  for j in range(i+1,N-1):
    t = L[i]+L[j]
    k = bisecsearch(L,t,j+1,N)
    cnt += max(0,k-j-1)
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

cnt = 0
for i in range(0,N-2):
  for j in range(i+1,N-1):
    t = L[i]+L[j]
    idx = bisect_left(L,t,lo=j+1)
    cnt += max(0,idx-j-1)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
 
N = int(input())
L = [int(i) for i in input().split()]
L.sort()
 
cnt = 0
for i in range(0,N):
  for j in range(i+1,N):
    t = L[i]+L[j]
    idx = bisect_left(L,t,lo=j+1)
    cnt += max(0,idx-j-1)
print(cnt)

Submission status: Time Limit Exceeded

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

cnt = 0
for i in range(N-2):
  for j in range(i+1,N-1):
    lo = j+1
    hi = N
    while lo < hi:
      k = (lo+hi)//2
      if L[k] < L[i] + L[j]:
        lo = k + 1
      else:
        hi = k
    cnt += max(0,lo-j-1)
print(cnt)

Submission status: Time Limit Exceeded

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

def CheckTriangle(e):
  a,b,c=e
  return(a<b+c)*(b<c+a)*(c<a+b)

d = dict()
for i in range(0,N-2):
  for j in range(i+1,N-1):
    for k in range(j+1,N):
      e = tuple(sorted((L[i],L[j],L[k])))
      if CheckTriangle(e):
        d[e] = 1

print(len(d))

Submission status: Time Limit Exceeded

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

total = 0
for i, x in enumerate(v[:n-2]):
    for j, y in enumerate(v[i+1:n-1]):
        for k, z in enumerate(v[i+j+2:]):
            if z >= x + y:
                break
            total += 1



print(total)

Submission status: Time Limit Exceeded

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

num = 0
for na in range(N-2):
    for nb in range(na+1, N - 1):
        for nc in range(nb+1, N):
            if L[nc] < L[na] + L[nb]:
                num += 1

print(num)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N = inputInt()
	L = inputList()

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


	print(ans)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# special thanks :
#  https://nagiss.hateblo.jp/entry/2019/07/01/185421
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

for i in range(0, n):
    for j in range(i+1, n):
        for k in range(i+1, j):
            if list_score[k] + list_score[i] > list_score[j]:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

n = int(input())
list_score = list(map(int, input().split()))
list_score.sort()
p_list = list(itertools.combinations(list_score, 3))
a = len(p_list)
ans = 0

for i in range(a):
    if p_list[i][0] + p_list[i][1] > p_list[i][2]:
       ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

p_list = list(itertools.combinations(list_score, 3))
a = len(p_list)
ans = 0

for i in range(a):
    if p_list[i][0] + p_list[i][1] > p_list[i][2] and p_list[i][1] + p_list[i][2] > p_list[i][0] and p_list[i][2] + p_list[i][0] > p_list[i][1]:
       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(1,n-1):
  for j in range(i):
    if L[n-1] - L[i]-L[j] < 0:
      ans += (n-1) - (i+1) + 1
      continue
    elif L[i+1] - L[i]-L[j] >= 0:
      continue
    l = i+1
    r = n-1
    while abs(r-l) > 1:
      mid = (l+r)//2
      if L[i]+L[j] - L[mid]> 0:
        l = mid
      else:
        r= mid
    ans += l-(i+1)+ 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect as bi
N=int(input())
L=list(map(int,input().split()))

count=0
l=sorted(L)
#print(l)
for i in range(N-2):
    for j in range(i+1,N-1):
        count+=bi.bisect_right(l[j+1:],l[i]+l[j]-1)
         
print(count)

Submission status: Time Limit Exceeded

Solution code:
import bisect as bi
N=int(input())
L=list(map(int,input().split()))

count=0
l=sorted(L)
#print(l)
for i in range(N-2):
    for j in range(i+1,N-1):
        index=bi.bisect_right(l[j+1:],l[i]+l[j]-1)
        count+=index
        
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

count=0
l=sorted(L)
#print(l)
for i in range(N-2):
    for j in range(i+1,N-1):
        a=j
        b=N
        while a+1!=b and a!=b:
            if l[(a+b)//2]<l[i]+l[j]:
                a=(a+b)//2
            else:
                b=(a+b)//2
        #print(a,b)
        if a!=b:
            count+=a-j
        else:
            if a==b:
                if l[a]<l[i]+l[j]:
                    count+=b-j
                elif l[a]>l[i]+l[j]:
                    pass
            else:
                if l[a]<l[i]+l[j]:
                    count+=a-j



print(count)