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: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
L=[int(x) for x in input().split()]
L.sort()
print(L)
count=0

for i,a in enumerate(L):
    for j,b in enumerate(L[i+1:]):
        for k,c in enumerate(L[i+j+2:]):
            if c>a+b:
                break
            count+=1
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = sorted([int(a) for a in input().split()])
X = [0] * 1001
for a in A:
    X[a] += 1
for i in range(1, 1001):
    X[i] += X[i-1]

ans = 0
for i in range(N):
    for j in range(i+1, N):
        ans += X[min(A[i]+A[j]-1, 1000)] - X[A[j]]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
def tri(a, b, c):
  if a < b + c and b < a + c and c < a + b:
    return True
  else:
    return False
ans = 0
for a in range(0,N-2):
  for b in range(a+1,N-1):
    for c in range(b+1,N):
      if tri(L[a],L[b],L[c]):
        ans += 1
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

def main():
    N = int(input())
    L = list(map(int, input().split()))
    ans = 0
    for a, b, c in itertools.combinations(L, 3):
        if c < a + b and a < b + c and b < c + a:
            ans += 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import collections
from os import remove
n = int(input())  # nは入力回数
num_list = list(map(int, input().split()))
sort_list = list(sorted(num_list))
ans = 0

for i in range(len(sort_list)-2):
    for j in range(i+1,len(sort_list)-1):
        for k in range(j+1,len(sort_list)):
            if sort_list[i] + sort_list[j] <= sort_list[k]:
                break
            else:
                ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n = int(input())
l = list(map(int,input().split()))
comb = combinations(l,3)
cnt = 0
for a,b,c in comb:
    if a<b+c and b<c+a and c<a+b:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import itertools

N = int(input())
L_list = list(map(int, input().split()))
ans = 0
for (i, j, k) in itertools.combinations(range(N), 3):
    if (
        L_list[i] < L_list[j] + L_list[k]
        and L_list[j] < L_list[k] + L_list[i]
        and L_list[k] < L_list[i] + L_list[j]
    ):
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

#import
import math
#import numpy as np
N = int(input())
L = list(map(int, input().split()))

L.sort()

def is_ok(arg, cmin, cmax):
    return L[arg] > cmin and L[arg] < cmax

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

ans = 0

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        a = L[i]
        b = L[j]
        cmin = b - a
        cmax = a + b
        t = meguru_bisect(j, N, cmin, cmax)
        ans += t - j

print(ans)

Submission status: Time Limit Exceeded

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

count=0

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
import math
import itertools
import copy
import bisect

N = int(input())
L = [int(x) for x in input().split()]
Ls = sorted(L) # Lを降順にソート
#print(Ls)
cnt = 0

for i in range(N) :
    a = Ls[i]
    for j in range(i+1,N) :
        #print(" ")
        b = Ls[j]
        L_roop = copy.deepcopy(Ls[j+1:])
        #print(L_roop)

        # 2分探索 x2 indexを求める
        ba = bisect.bisect(L_roop,abs(a-b)+0.1)
        #print(abs(a-b)+0.1,ba)
        L_roop.insert(ba,abs(a-b)+0.1)
        #print("a",L_roop)

        bb = bisect.bisect(L_roop,abs(a+b)-0.1)
        #print(abs(a+b)-0.1,bb)
        L_roop.insert(bb,abs(a+b)-0.1)
        #print("b",L_roop)

        #print("ba,bb",ba,bb)
        if bb - ba > 1 :
            #print(Ls[i],Ls[j],L_roop[ba+1])
            cnt += bb - ba - 1
    
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = [int(x) for x in input().split()]
cnt = 0
for i in range(N) :
    for j in range(i+1,N) :
        for k in range(j+1,N) :
            if abs(L[i]-L[j])/L[k] < 1 and abs(L[i]+L[j])/L[k] > 1 :
                #print(i,j,k)
                cnt += 1
print(cnt)

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):
        k = j+1
        while k < n and l[k] > max(l[i]-l[j], l[j]-l[i]):
            k += 1
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.read
#readlines = sys.stdin.readlines
#from bisect import bisect_left
from itertools import combinations
def main():
    n, *l = map(int, read().split())
    com = tuple(combinations(l, 3))
    r = 0
    for c in com:
        r += sum(c) > max(c) * 2
    print(r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def bin_search(arr,ls,c):
    l=ls-1
    r = len(arr)
    while (r-l) > 1:
        m = l + (r-l)//2
        if arr[m] >= c:
            r = m
        else:
            l = m
    return l-ls+1

def resolve():
    readline=sys.stdin.readline

    n=int(readline())
    arr=list(map(int, readline().rstrip().split()))
    arr.sort()
    ans=0
    for i in range(n-2):
        for j in range(i+1,n-1):
            c = arr[i] + arr[j]
            ans += bin_search(arr,j+1,c)
    print(ans)
    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Time Limit Exceeded

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def bin_search(arr,c):
    l = -1
    r = len(arr)
    while (r-l) > 1:
        m = l + (r-l)//2
        if arr[m] >= c:
            r = m
        else:
            l = m
    return l+1

def resolve():
    readline=sys.stdin.readline

    n=int(readline())
    arr=list(map(int, readline().rstrip().split()))
    arr.sort()
    ans=0
    for i in range(n-2):
        for j in range(i+1,n-1):
            c = arr[i] + arr[j]
            l = arr[j+1:]
            ans += bin_search(l,c)
    print(ans)
    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Time Limit Exceeded

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def bin_search(arr,c):
    l = -1
    r = len(arr)
    while (r-l) > 1:
        m = l + (r-l)//2
        if arr[m] >= c:
            r = m
        else:
            l = m
    return l+1

def resolve():
    readline=sys.stdin.readline

    n=int(readline())
    arr=list(map(int, readline().rstrip().split()))
    arr.sort()
    ans=0
    for i in range(n):
        for j in range(i+1,n):
            c = arr[i] + arr[j]
            l = arr[j+1:]
            ans += bin_search(l,c)
    print(ans)
    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

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

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
def resolve():
    n = int(input())
    l = tuple(map(int,input().split()))
    cnt = 0
    for i in itertools.combinations(l,3):
        a,b,c = i
        if a<b+c and b<c+a and c<a+b:
            cnt += 1
    print(cnt)
resolve()

Submission status: Time Limit Exceeded

Solution code:
import bisect

N = int(input())
L = sorted([int(n) for n in input().split()])
#print(L)

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

print(count)

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 + 1, N):
        l = L[j] - L[i]
        a = 0
        b = i - 1
        if L[a] > l:
            ans += i
        elif L[b] > l:
            while b - a > 1:
                c = (a + b) // 2
                if L[c] > l:
                    b = c
                else:
                    a = c
            ans += i - b
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 + 1, N):
        l = L[j] - L[i]
        a = 0
        b = i - 1
        while b - a > 1:
            if L[(a + b) // 2] > l:
                b = (a + b) // 2
            else:
                a = (a + b) // 2
        if L[a] > l:
            ans += i
        elif L[b] > l:
            ans += i - b
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = 0

for i in range(n):
  for j in range(n):
    for k in range(n):
      if i!=j and i!=k and j!=k:
        if a[i]+a[j]>a[k] and a[i]+a[k]>a[j] and a[j]+a[k]>a[i]:
          cnt += 1
print(cnt//6)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

ans = 0
for i in range(n):
    for j in range(i+1,n):
        k = bisect_left(l[j:], l[i] + l[j]) + j
        ans += k - j - 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

ans = 0
for i in range(len(l)):
    for j in range(len(l))[i + 1:]:
        k = bisect_left(l[j:], l[i] + l[j]) + j
        ans += k - j - 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

ans = 0
for i in range(len(l)):
    for j in range(len(l))[i + 1:]:
        k = bisect_left(l[j:], l[i] + l[j]) + j
        ans += len(l[j:k]) - 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect


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

    count = 0
    for i in range(n - 1):
        for j in range(i + 1, n):

            lst = l_lst[j + 1:]
            a = l_lst[i]
            b = l_lst[j]
            count += bisect.bisect_left(lst, a + b)

    print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect


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

    count = 0
    for i in range(n - 1):
        for j in range(i + 1, n):

            lst = l_lst[j + 1:]
            a = l_lst[i]
            b = l_lst[j]
            count += bisect.bisect(lst, a + b - 1)

    print(count)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def binery_search(left,right,L,a,b):
    while True:
            #print(left,right,mid,cnt)
            mid = int( (left+right)/2 )
            if a+b <= L[mid]:
                right = mid
            elif a+b > L[mid]:
                left = mid + 1

            if left == right:
                return left

def main():
    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,1):
            a = L[i]
            b = L[j]
            num = binery_search(j+1,N,L,a,b)
            #print(L,a,b,num)
            ans += num-(j+1)

    return ans

print(main())

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations

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

ans = 0
for a, b, c in combinations(arr, 3):
    if a < b + c and b < c + a and c < a + b:
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

for v in itertools.combinations(L, 3):
    M.append(list(v))

cnt = 0

for v in M:
    if v[0] < v[1]+v[2] and v[1] < v[0]+v[2] and v[2] < v[0]+v[1]:
        cnt += 1
        
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations as comb

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

count = 0

for x, y, z in comb(range(1, N+1), 3):
  a, b, c = L[x-1], L[y-1], L[z-1]
  if a < b + c: count += 1

print(count)

Submission status: Time Limit Exceeded

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

for x, y, z in comb(range(1, N+1), 3):
  a, b, c = L[x-1], L[y-1], L[z-1]
  if a < b + c and b < c + a and c < a + b:
    count += 1

print(count)

Submission status: Time Limit Exceeded

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = N * (N - 1) * (N - 2) // 6
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        for k in range(j + 1, N):
            if L[k] >= L[i] + L[j]:
                ans -= N - k
                break
print(ans)

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

Submission status: Time Limit Exceeded

Solution code:
def bisect_left(a, x, lo=0, hi=None):
    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
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):
        tar=l[j]+l[i]
        temp=bisect_left(l,tar)
        ans+=temp-j-1
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(N):
        if a >= b:
            continue
        for c in range(N):
            if b >= c or a >=c:
                continue
            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:
N = int(input())
L = list(map(int, input().split()))

ANS = 0
for a in range(N):
    for b in range(N):
        for c in range(N):
            if a >= b or b >= c or a >=c:
                continue
            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:
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[k]+L[i] and L[k] < L[i]+L[j]:
                count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
v = [int(x) for x in input().split()]

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

res = 0
for i in range(n):
  for j in range(i+1,n):
    for k in range(j+1,n):
      if check(v[i],v[j],v[k]):
        res += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
n=int(input())
l=list(map(int,input().split()))
ans=0
l.sort()
for i in range(n-2):
    for j in range(i+1,n-1):
        for k in range(j+1,n):
            if l[i]+l[j]>l[k]:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
l.sort()
x=[0]*(10**3+1)
y=[]
for i in range(n):
    x[l[i]]+=1
for i in range(10**3+1):
    if i==0:
        y.append(0)
    else:
        y.append(y[i-1]+x[i])
y.append(y[len(y)-1])
ans=0
for i in range(n):
    for j in range(n):
        if i!=j:
            a=min(l[i],l[j])
            b=max(l[i],l[j])
            m=b-a
            M=min(a+b,10**3+1)
            g=y[M-1]-y[m]
            ans+=g
            if m<a<M:
                ans-=1
            if m<b<M:
                ans-=1
print(ans//6)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
import bisect
N = int(input())
L = list(map(int, input().split()))
ans = 0
LS = sorted(L)
l = len(LS)
for i in range(l):
  for j in range(l):
    if i < j:
      for k in range(j+1,l):
        if LS[i]+LS[j] > LS[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()))
ans = 0
for x in itertools.combinations(L, 3):
  if x[0] < x[1] + x[2] and x[1] < x[0] + x[2] and x[2] < x[0] + x[1]:
    ans += 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = sorted(list(map(int, input().split())))
ans = 0
for a in range(n-2):
    for b in range(a+1, n-1):
        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:
n = int(input())
l = list(map(int, input().split()))
ans = 0
for a in range(n-2):
    for b in range(a+1, n-1):
        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:
n=int(input())
ans=0
l=sorted(list(map(int,input().split())))
for i in range(n-2):
    for j in range(i+1,n-1):
        for k in range(j+1,n):
            if l[i]+l[j]>l[k]:
                ans+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int,input().split()))
L.sort()
cnt = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        for h in range(j+1,N):
            if L[i] + L[j] > L[h]:
                cnt += 1
            else:
                break
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 h in range(j+1,N):
            if (L[i] + L[j] + L[h] - max(L[i],L[j],L[h])) > max(L[i],L[j],L[h]):
                cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

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

arr.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 arr[i]+arr[j]>arr[k]:
                ans+=1
            else:
                break
    
                
        
print(ans)

Submission status: Time Limit Exceeded

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

ans=0
for i in combinations(arr,3):
    a,b,c=i[0],i[1],i[2]
    if a<b+c and c<b+a and b<a+c:
        ans+=1
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
max = l[N-1]
tc = N
st = [j+1 for j in range(N-1)]
for i in range(N-2):
    if l[i]+l[i+1] > max:
        k = N-i-1
        ans += ((k-1)*k*(k+1))//6
        break
    for j in range(i+1,N-1):
        if j == tc:
            ans += tp
            break
        s = l[i]+l[j]
        if s <= l[j+1]:
            continue
        if s > max:
            tc = j 
            tp = (N-j-1) * (N-j) //2 
            ans += tp
            break
        start = st[j]
        end = N
        while start != end:
            center = (start + end)//2
            if s <= l[center]:
                end = center
            else:
                start = center + 1
        st[j] = end
        ans += end-j-1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
max = l[N-1]
tc = N
for i in range(N-2):
    if l[i]+l[i+1] > max:
        k = N-i-1
        ans += ((k-1)*k*(k+1))//6
        break
    for j in range(i+1,N-1):
        if j == tc:
            ans += tp
            break
        s = l[i]+l[j]
        if s <= l[j+1]:
            continue
        if s > max:
            tc = j 
            tp = (N-j-1) * (N-j) //2
            ans += tp
            break
        start = j+1
        end = N
        while start != end:
            center = (start + end)//2
            if s <= l[center]:
                end = center
            else:
                start = center + 1
        ans += end-j-1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
ans=0
c=[0]*1001
for i in L:c[i]+=1
for i in range(1000):c[i+1]=c[i]+c[i+1]
for i in range(N-1):
    for j in range(i+1,N):
        l,r=abs(L[i]-L[j]),min(L[i]+L[j]-1,1000)
        s=c[r]-c[l]
        if l<L[i] and L[i]<=r:s-=1
        if l<L[j] and L[j]<=r:s-=1
        ans+=max(s,0)
print(ans//3)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
ans=0
c=[0]*1001
for i in L:c[i]+=1
for i in range(1000):c[i+1]=c[i]+c[i+1]
for i in range(N):
    for j in range(i+1,N):
        l,r=abs(L[i]-L[j]),min(L[i]+L[j]-1,1000)
        s=c[r]-c[l]
        if l<L[i] and L[i]<=r:s-=1
        if l<L[j] and L[j]<=r:s-=1
        ans+=max(s,0)
print(ans//3)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
ans=0
c=[0]*1001
for i in L:c[i]+=1
for i in range(1000):c[i+1]=c[i]+c[i+1]
for i in range(N):
    for j in range(N):
        if i!=j:
            l,r=abs(L[i]-L[j]),min(L[i]+L[j]-1,1000)
            s=c[r]-c[l]
            if l<L[i] and L[i]<=r:s-=1
            if l<L[j] and L[j]<=r:s-=1
            ans+=max(s,0)
print(ans//6)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
max = l[N-1]
for i in range(N-2):
    if l[i]+l[i+1] > max:
        k = N-i-1
        ans += ((k-1)*k*(k+1))//6
        break
    for j in range(i+1,N-1):
        s = l[i]+l[j]
        if s <= l[j+1]:
            continue
        if s > max:
            ans += (N-j-1) * (N-j) //2
            break
        start = j+1
        end = N
        while start != end:
            center = (start + end)//2
            if s <= l[center]:
                end = center
            else:
                start = center + 1
        ans += end-j-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):
        s = l[i]+l[j]
        if s <= l[j+1]:
            continue
        if s > l[N-1]:
            ans += (N-j-1) * (N-j) //2
            break
        start = j+1
        end = N
        while start != end:
            center = (start + end)//2
            if s <= l[center]:
                end = center
            else:
                start = center + 1
        ans += end-j-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):
        s = l[i]+l[j]
        if s <= l[j+1]:
            continue
        start = j+1
        end = N
        while start != end:
            center = (start + end)//2
            if s <= l[center]:
                end = center
            else:
                start = center + 1
        ans += end-j-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):
        s = l[i]+l[j]
        start = j+1
        end = N
        while start != end:
            center = (start + end)//2
            if s <= l[center]:
                end = center
            else:
                start = center + 1
        ans += end-j-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):
        s = l[i]+l[j]
        for k in range(j+1,N):
            if l[k] >= s:
                break
            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):
        for k in range(j+1,N):
            if l[k] >= l[i] + l[j]:
                break
            ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

from bisect import insort, bisect_left
bit = [L[0]+L[1]]
ans = 0
for k in range(2, N):
  ans += len(bit) - bisect_left(bit, L[k]+1)
  for j in range(k):
    insort(bit, L[j]+L[k])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int, input().split()))
L.sort()
def binary_search(list, item):
    low = 0
    high = len(list) - 1
    mid = (low + high) //2
    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            #return mid
            break
        if guess > item:
            high = mid -1
        else:
            low = mid + 1

    if mid < len(list):
      while item >= list[mid]:
        mid += 1
        if mid == len(list):
          break
    #return None
    return len(list) - mid

from bisect import insort
bit = [L[0]+L[1]]
ans = 0
for k in range(2, N):
  ans += binary_search(bit, L[k])
  for j in range(k):
    insort(bit, L[j]+L[k])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import bisect
n=int(input())
count = 0
a=sorted(list(map(int, input().split(" "))))
for i in range(2,n):
  for j, k in itertools.combinations(a[:i], 2):
    if j + k > a[i]:
      count += 1
print(count)

Submission status: Time Limit Exceeded

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

ans = 0
ans2 = 0
for k in range(N-2):
  for j in range(k+1, N-1):
    cnt = 0
    for l in range(N-1, j, -1):
      if L[k] + L[j] <= L[l]:
        cnt += 1
        continue
      else:
        ans += N-1-j-cnt
        break
print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import bisect
n=int(input())
count = 0
a=sorted(list(map(int, input().split(" "))))
for i in range(1,n-1):
  b = a[:i]
  c = a[i+1:]
  #print(a[i], b, c)
  for j, k in itertools.product(b,c):
    if a[i] + j > k:
      count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
# Binary Indexed Tree (Fenwick Tree)
class BIT:
    def __init__(self, n):
        self.n = n
        self.data = [0]*(n+1)
        self.el = [0]*(n+1)
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.data[i]
            i -= i & -i
        return s
    def add(self, i, x):
        # assert i > 0
        self.el[i] += x
        while i <= self.n:
            self.data[i] += x
            i += i & -i
    def get(self, i, j=None):
        if j is None:
            return self.el[i]
        return self.sum(j) - self.sum(i)


N = int(input())
L = list(map(int, input().split()))
L.sort()
bit = BIT(L[-1]*2+1)
bit.add(L[0]+L[1], 1)
total = 1
ans = 0
for k in range(2, N):
  ans += total - bit.sum(L[k])
  for j in range(k):
    bit.add(L[j]+L[k], 1)
    total += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n=int(input())
count = 0
a=list(map(int, input().split(" ")))
for ia, ib, ic in itertools.combinations(a, 3):
  if max(ia, ib, ic) < (ia + ib + ic) - max(ia, ib, ic):
    count += 1
print(count)