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: Accepted

Solution code:
num = int(input())
pole_list = sorted([int(item) for item in input().split()])

from bisect import bisect_left as bil

cnt = 0

for i in range(num):
    for j in range(i + 1, num):
        cnt += bil(pole_list, pole_list[i] + pole_list[j]) - j - 1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect

N=int(raw_input())
LN=map(int, raw_input().split())

LN.sort()

r = 0
for i in xrange(N-1, 1, -1):
    a = LN[i]
    for j in xrange(i-1, 0, -1):
        b = LN[j]
        if a > 2*b:
            break
        idx = bisect.bisect_left(LN, a-b+1)
        if idx < j:
            r += j - idx

print r

Submission status: Accepted

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

l.sort(reverse=True)

ans = 0

#1番長い辺(a)は固定する
for i in range(n - 2):
    #2番目に長い辺(b)＝次に長い辺とする
    j = i + 1
    #3番目の長さの辺(c)＝リストの中で最短の辺からスタート
    k = n - 1

    while j < k:
        #b<a+c,c<a+b は自明。a<b+cを判定する。
        #満たす場合、このbにおいて、cまでの全ての要素は条件を満たす(cが小さいほうから攻めているから)
        if l[i] < l[j] + l[k]:
            ans += k - j
            j += 1
        #満たさない場合、cを次に長い辺として再度判定。
        else:
            k -= 1

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
 
N = int(input())
Lmat = [int(n) for n in input().split()]
Lmat.sort()
 
sum = 0

for i in range(N):
    for j in range(i + 1, N):
        sum += bisect_left(Lmat, Lmat[i]+Lmat[j]) - (j + 1)
        
print(sum)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())

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

l.sort()
l = l[::-1]
ans = 0
for i in range(n-2):
    j = i+1
    k = n-1
    while j < k:
        if l[i] < l[j] + l[k]:
            ans += k - j
            j += 1
        else:
            k -= 1
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right, bisect_left
n = int(input())
l = list(map(int, input().split()))
count = 0
l.sort()
for i in range(n-2):
    for j in range(i+1, n-1):
        count += bisect_left(l, l[i]+l[j]) -j -1
print(count)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right

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

L.sort()
ans = 0

for a in range(N):
    for b in range(a + 1, N):
        c = bisect_left(L, L[a] + L[b])
        ans += c - b - 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import bisect
N=int(input())
d=sorted(list(map(int,input().split())))
ans=0
for i in range(1,len(d)-1):
  for j in range(i+1,len(d)):
    v=(N-j)-(bisect.bisect_left(d,d[-i]-d[-j]+1))
    if v<=0:
      pass
    else:
      ans+=v
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import time
from bisect import bisect_left
n = int(input())
l = list(map(int, input().split()))
l = sorted(l)
#print(l)
#start = time.time()
res = 0
for i in range(n):
    for j in range(i+1,n):
        ab = l[i] + l[j]
        c_index = bisect_left(l, ab)
        #print(l[i], l[j],ab , c_index, j+1, res)
        res += c_index - j - 1
print(res)

Submission status: Accepted

Solution code:
import bisect
N=int(input())
L=list(map(int,input().split()))
l=sorted(L)
result=0
for i in range(N-2):
    for j in range(i+1,N-1):
        result+=bisect.bisect_left(l,l[i]+l[j])-j-1
print(result)

Submission status: Accepted

Solution code:
import bisect

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

L.sort()

buff = []
# ll = []


for i in range(N):
    l_b = []
    for j in range(i+1, N):
        l_b.append(L[i]+L[j])
        # ll.append(L[i]+L[j])
    if i != N-1:
        buff.append(l_b)
# buff.sort()
# print(L)
# print(buff)

ans = 0
_len = len(buff)

for i in range(_len):
    for j in range(len(buff[i])):
        index = bisect.bisect_left(L[i+j+1+1:], buff[i][j])
        # print(L[i+j+1+1:])
        # print(index, buff[i][j])
        ans += index
print(ans)


# for i in range(N):
#     index = bisect.bisect_left(buff, L[i])
#     print(index)
#     ans += index
# print(ans)

Submission status: Accepted

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

for i in range(n-2):
  for j in range(i+1, n-1):
    k = bisect.bisect_left(l, l[i]+l[j])
    count += k -j - 1
print(count)

Submission status: Accepted

Solution code:
import bisect
n=int(input())
l = list(map(int,input().split()))
l.sort()
うんち = 0
for i in range(1,n):
    for j in range(i):
        p = l[i] + l[j]
        q = bisect.bisect_left(l,p)
        うんち += q-i-1
print(うんち)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect

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

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

Submission status: Accepted

Solution code:
from bisect import bisect_left
from itertools import combinations

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

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

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

s=0
for i in range(ll):
    for j in range(i+1,ll):
        s+=bisect.bisect_right(L,L[i]+L[j]-1)-j-1

print(s)

Submission status: Accepted

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

Submission status: Accepted

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

#output
import heapq
import bisect

L = [-l for l in L]
heapq.heapify(L)

sum = 0
while len(L) > 2:
    largest = heapq.heappop(L)
    L.sort()
    for i in range(len(L)-1):
        p = bisect.bisect_left(L, largest-L[i])
        if p-(i+1) >0:
            sum += p-(i+1)
        
print(sum)

Submission status: Accepted

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

"""listl.sort(reverse=True)

#print(listl)
	ans=0
for i in range(n-2):
	for j in range(i+1,n-1):
		if listl[j]<=(listl[i]/2):
			break
		for k in range(j+1,n):

			if listl[i]<listl[j]+listl[k]:
				ans=ans+1
			else:
				break	

print(ans)"""
from bisect import bisect_right				 
listl.sort()

ans=0

for i in range(n-1,1,-1):
	a=listl[i]
	for j in range(i-1,0,-1):
		b=listl[j]
		c=bisect_right(listl,a-b)
		#print(c)
		num = j-c
		if num>0:
			ans+=num
print(ans)

Submission status: Accepted

Solution code:
#abc143d
import bisect

n=int(raw_input())
l=map(int,raw_input().split())
l.sort()
res=0
for i in range(n):
 for j in range(i+1,n):
  k=l[i]+l[j]
  res+=max(0,bisect.bisect_left(l,k)-j-1)
print res

Submission status: Accepted

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

arr.sort(reverse=True)
count = 0
for i in range(n-2):
    j = i+1
    k = n-1
    while j < k:
        if arr[i] < arr[j] + arr[k]:
            count += k-j
            j += 1
        else:
            k -= 1
print(count)

Submission status: Accepted

Solution code:
n = int(input())
l = [int(_) for _ in input().split()]
ans = 0
L = [0 for _ in range(1,2000+2)]
s = [0 for _ in range(0,2000+2)]
for k in range(0, n):
    L[l[k]-1] += 1
for x in range(0,2001):
    s[x+1] = s[x] + L[x]
 
for i in range(0, n-1):
    for j in range(i+1, n):
        exc = 0
        m = abs(l[i] - l[j])
        M = l[i] + l[j]
        if l[i] > m:
            exc += 1
        if l[j] > m:
            exc += 1
        ans += s[M] - s[m] - L[M-1] - exc
print(ans//3)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

sum = 0
for i in range(N):
    for j in range(i+1,N):
        sum+= bisect_left(Lmat, Lmat[i]+Lmat[j]) - j - 1
        x = 1 + 1
print(sum)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

sum = 0
for i in range(N):
    for j in range(i+1,N):
        sum+= bisect_left(Lmat, Lmat[i]+Lmat[j]) - j - 1
print(sum)

Submission status: Accepted

Solution code:
import time
from bisect import bisect_left
n = int(input())
l = list(map(int, input().split()))
l = sorted(l)

start = time.time()
res = 0
for i in range(n):
    for j in range(i+1,n):
        ab = l[i] + l[j]
        c_index = bisect_left(l, ab)
        res += c_index - j - 1
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
n = int(input())
l = list(map(int, input().split()))
l = sorted(l)
ans = 0
 
for i in range(n-2):
    for j in range(i+1, n-1):
        idx = bisect.bisect_right(l, l[i]+l[j]-1, j+1)
        ans+=idx-(j+1)
print(ans)

Submission status: Accepted

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

for i in range(n-2):
    for j in range(i+1, n-1):
        idx = bisect.bisect_right(l, l[i]+l[j]-1, j+1)
        ans+=idx-(j+1)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = [x for x in map(int, input().split())]
l = sorted(l)

cnt = 0
for i in range(n - 2):
    a = l[i]
    k = i + 2
    for j in range(i + 1, n - 1):
        b = l[j]
        while(l[k] < a + b):
            k += 1
            if k == n:
                break
            else:
                pass

        k -= 1
        cnt += k - j
print(cnt)

Submission status: Accepted

Solution code:
import bisect


def triangles():
    n = int(input())
    l = [int(v) for v in input().split()]
    l.sort()

    count = 0
    for i in range(n):
        for j in range(i + 1, n):
            c = l[i] + l[j]
            index = bisect.bisect_left(l, c)
            count += max(index - j - 1, 0)

    print(count)


if __name__ == '__main__':
    triangles()

Submission status: Accepted

Solution code:
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import bisect
import sys
input = sys.stdin.readline

setrecursionlimit(1000000)

dp = defaultdict(lambda x:-1)

def main():
	n = int(input())
	l = list(map(int,input().split()))
	l.sort()
	ans = 0
	for mx in reversed(range(1,n)):
		for sc in reversed(range(1,mx)):
			tr_mx = l[mx] - l[sc] + 1
			tr = bisect.bisect_left(l,tr_mx)
			if(sc - tr > 0):
				ans += sc-tr
	print(ans)

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(l):
	x = l.pop()
	y = l.pop()
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	l.append(min(x,y))
	return gcd(l) if len(l) > 1 else l[0]

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]
main()

Submission status: Accepted

Solution code:
import bisect

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

l.sort()
ans=0
for i in range(n-1,1,-1):
    for j in range(i-1,0,-1):
        c=bisect.bisect_right(l,l[i]-l[j])
        #print(l[i],l[j],i,j,c)
        if j-c > 0:
            ans+=(j-c)
            
print(ans)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
l = sorted(list(map(int, input().split())))
count = 0
for b in range(n-2):
  for c in range(b+1, n-1):
    count += max(0, bisect.bisect_left(l, l[b]+l[c])-c-1)

print(count)

Submission status: Accepted

Solution code:
import sys
from collections import Counter, deque, defaultdict
from math import factorial
import bisect
from heapq import heappop, heappush

import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

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

for i in range(n-2):
    a = l[i]
    for j in range(i+1, n-1):
        b = l[j]
        index = bisect.bisect_left(l,a+b)
        res += max(index-j-1,0)

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
L.sort()
cmax = L[-1] + L[-2]
csum = list(0 for _ in range(cmax+1))
for i in range(N): csum[L[i]] += 1
for i in range(1,cmax+1): csum[i] += csum[i-1]
ans = 0
for ib in range(N):
    b = L[ib]
    for ia in range(ib):
        a = L[ia]
        minL, maxL = abs(a-b), a+b
        ans += csum[maxL-1] - csum[minL]
        if minL < a < maxL: ans -= 1
        if minL < b < maxL: ans -= 1
print(ans//3)

Submission status: Accepted

Solution code:
from bisect import bisect_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):
            ans+=bisect_left(L,L[i]+L[j])-j-1
    print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
csum = list(0 for _ in range(2000+1))
L = []
cmax1, cmax2 = 0, 0
for l in map(int,input().split()):
    L.append(l)
    csum[l] += 1
    if cmax2 < l: cmax2 = l
    if cmax1 < l: cmax1, cmax2 = l, cmax1
for i in range(1,cmax1+cmax2+1): csum[i] += csum[i-1]
ans = 0
for ib in range(N):
    b = L[ib]
    for ia in range(ib):
        a = L[ia]
        minL, maxL = abs(a-b), a+b
        ans += csum[maxL-1] - csum[minL]
        if minL < a < maxL: ans -= 1
        if minL < b < maxL: ans -= 1
print(ans//3)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
L = sorted(list(map(int, input().split())))
res = 0
for i in range(N - 1, 1, - 1):
    j = i - 1
    k = 0
    while j > k:
        while j > k and L[i] >= L[j] + L[k]:
            k += 1
        res += j - k
        j -= 1
print(res)

Submission status: Accepted

Solution code:
import itertools
import heapq
from bisect import bisect_left


def heapsort(iterable):
    h = []
    for value in iterable:
        heapq.heappush(h, value)
    return [heapq.heappop(h) for i in range(len(h))]

N = int(input())
L = list(map(int, input().split()))
L.sort()
# L_sort = heapsort(L)
# print (L)
answer = 0
for i, a in enumerate(L[0:N-2]):
    for j, b in enumerate(L[i+1:N-1]):
        index = bisect_left(L, a+b)
        answer += max(index - (j+i+1) -1, 0)
        # print(L, a+b, index, j+i+1, answer)
print(answer)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
l = list(map(int,input().split()))
l.sort()
ans =0
for i in range(1,n):
    for j in range(i):
        p = l[i]+ l[j]
        q = bisect.bisect_left(l,p)
        ans += q-i-1

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, bisect
sys.setrecursionlimit(300000)


def solve(N: int, L: "List[int]"):
    ret = 0
    L.sort()
    for i in range(N):
        for j in range(i + 1, N):
            mx = L[i] + L[j] - 1
            r = bisect.bisect_right(L, mx)
            cnt = max(0, r - j - 1)
            ret += cnt
    print(ret)
    return

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# ABC143D - Triangles
def main():
    # fix the longest edge -> binary search
    N, *L = map(int, open(0).read().split())
    L.sort()
    ans = 0
    for i in range(N - 1, 1, -1):
        x, ng, ok = L[i], 0, i - 1
        while ng < ok:
            if L[ng] + L[ok] > x:
                ans += ok - ng
                ok -= 1
            else:
                ng += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect
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):
    k=bisect.bisect_left(L,L[i]+L[j],j+1)-1
    ans+=k-j
print(ans)

Submission status: Accepted

Solution code:
def find(edges):
    import bisect
    edges=sorted(edges)
    ans=0
    for i in range(len(edges)):
        for j in range(i+1,len(edges)):
            temp=edges[i]+edges[j]
            pos=bisect.bisect_left(edges,temp)
            ans+=max(0,pos-1-j)
            #print(edges[i],edges[j],max(0,pos-1-j))
    return ans

input()
print(find(list(map(int,input().strip().split(" ")))))

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

for i in range(n - 2):
    li = lll[i]
    for j in range(i + 1, n - 1):
        k = bisect_left(lll, li + lll[j], lo=j)
        ans += k - j - 1
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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


def get_bisect_left():
    cache = {}

    def _f(x):
        if x in cache:
            return cache[x]
        cache[x] = bisect_left(lll, x)
        return cache[x]

    return _f


bisect_left_cache = get_bisect_left()

for i in range(n - 2):
    li = lll[i]
    for j in range(i + 1, n - 1):
        k = bisect_left_cache(li + lll[j])
        ans += k - j - 1
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

#L[i]+L[j] > ?
ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        zz = L[i]+L[j]
        idx = bisect_left(L,zz)
        ans += idx-j-1
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

res = 0
for i in range(n):
	l1 = lengths[i]
	for j in range(i + 1, n - 1):
		l2 = lengths[j]
		res += bisect_left(lengths, l1 + l2, j + 1) - j - 1

print(res)

Submission status: Accepted

Solution code:
import bisect
 
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):
        k = bisect.bisect_left(L, L[i] + L[j], j + 1) - 1
        ans += k - j

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_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):
            ans += bisect_left(l,l[i]+l[j])-j-1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from bisect import bisect_left

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):
        ans += bisect_left(l,l[i]+l[j])-j-1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
cnt = 0 
import bisect
for i in range(0,n-2):
    a = l[i]
    for j in range(i+1,n-1):
        b = l[j]
        target = a + b 
  #      print("#",a,b)
        f1 = bisect.bisect_left(l,target,j+1,n)
        cnt+= f1 - 1- j
   #     print("cnt: ",cnt)
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split(" ")))
l.sort()
cnt = 0 
import bisect
for i in range(0,n-2):
    a = l[i]
    for j in range(i+1,n-1):
        b = l[j]
        target = a + b 
  #      print("#",a,b)
        f1 = bisect.bisect_left(l,target,j+1,n)
        cnt+= f1 - 1- j
   #     print("cnt: ",cnt)
print(cnt)

Submission status: Accepted

Solution code:
def slove():
    import sys
    import bisect
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    l = list(map(int, input().rstrip('\n').split()))
    l.sort()
    cnt = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            t = l[i] + l[j]
            p = bisect.bisect_left(l, t)
            cnt += (p - j - 1)
    print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
import bisect

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]
        c = bisect.bisect_left(l,a+b)
        ans += max(c-j-1,0)
print(ans)

Submission status: Accepted

Solution code:
import bisect

N = int(input())
L = [int(_) for _ in input().split()]
L.sort()

ans = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        r = L[i] + L[j]
        # 同じ長さでも違う棒はOK
        ri = bisect.bisect_left(L, r)
        ans += ri - j - 1

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

for i,a in enumerate(L[:N-2]):
    i += 1
    for j,b in enumerate(L[i:N-1]):
        j += i + 1
        n = bisect_left(L,a+b) - j
        result += n
print(result)

Submission status: Accepted

Solution code:
import bisect
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):
        ab=L[i]+L[j]
        r=bisect.bisect_left(L,ab)
        l=j+1
        ans+=r-l
print(ans)

Submission status: Accepted

Solution code:
import bisect

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

for i in range (0,N-2): #最小辺
  for j in range (i+1,N-1): #2番目の辺
    ij = L[i] + L[j]
    k = bisect.bisect_left(L,ij)
    ans = ans + k - j - 1
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

for aIndex,a in enumerate(L[:N-2]):
    for bIndex,b in enumerate(L[aIndex+1:N-1]):
        bIndex = aIndex + bIndex + 1
        ab = a+b
        n = bisect_left(L,ab) - bIndex - 1
        result += n
print(result)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from bisect import bisect_left, bisect_right

def solve(N: int, L: "List[int]"):
    L.sort()

    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            k = bisect_left(L, L[i]+L[j])
            ans += (k-j-1)

    print(ans)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    L = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, L)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
for i in range (N-1,1,-1): 
  s = 0 
  l = i - 1
  while l > s:
    if L[s] + L[l] > L[i]:
      ans = ans + (l - s)
      l = l - 1
    else:
      s = s + 1
print(ans)

Submission status: Accepted

Solution code:
import bisect

N = int(input())
L = list(map(int, input().split()))
L.sort() # index: 0,...,N-1

ans = 0
for i in range(N-2): # i: 0,...,N-3
    for j in range(i+1, N-1): # j: i+1,...,N-2
        # k の満たすべき条件:
        # 1. L[k] > L[j] - L[i]
        #     L[k] > L[j] なので自動的に満たす
        # 2. L[k] < L[i] + L[j]
        x = bisect.bisect_left(L, L[i]+L[j])
        # (<L[i]+L[j]), (<L[i]+L[j]), ..., (<L[i]+L[j]) | (L[i]+L[j]<=), (L[i]+L[j]<=), ...
        # | (x) より左側はOK
        # j+1 より右側はOK
        # L[j]<L[i]+L[j] と L[i]+L[j]<=L[x] より L[j]<L[x] つまり j<x
        ans += x - (j + 1)

print(ans)

Submission status: Accepted

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

import bisect
for i in range(N - 2):
    for  j in range(N - i - 2):
        s = C[i] + C[j + i + 1]
        t = bisect.bisect_left(C,s)
        u = t - (j + i + 1) - 1
        if u <= 0:
            pass
        else:
            count = count + u

print(count)

Submission status: Accepted

Solution code:
N=int(input())
L=list(map(int,input().split()))
L.sort()
ans = 0
i = 0
while i < N - 2:
    j = i + 1
    k = j + 1
    while j < N - 1:
        while k < N:
            if L[i] + L[j] <= L[k]:
                ans += k - j - 1
                break
            k += 1
        if k == N:
            ans += k - 1 - j
        j += 1
    i += 1
print(ans)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
Ls = list(map(int, input().split()))
Ls.sort()
#print(Ls)
r = 0
for i in range(N):
  for j in range(i+1, N):
    c = Ls[i]+Ls[j]
    k = bisect.bisect_left(Ls, c, j+1, N)
    #print(i, j, k)
    r += k-j-1
print(r)

Submission status: Accepted

Solution code:
from bisect import bisect_left

N = int(input())
L = list(map(int, input().split()))
L.sort()
# print (L)
count = 0
for a in range(N - 1):
    tmp = L[a]
    for b in range(a + 1, N):
        c = tmp + L[b]
        count += max(0, bisect_left(L, c) - b - 1)

print (count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
n=int(input())
l=[int(i) for i in input().split()]
l.sort()
ans=0
for i in range(n):
    for j in range(i+1,n):
        a=l[i]
        b=l[j]
        ans+=bisect.bisect_left(l,a+b,lo=j)-j-1
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right
def main():
	N = int(input())
	L = list(map(int, input().split(" ")))
	L.sort()
	k = 0
	for i in reversed(range(2, N)):
		a = L[i]
		for j in reversed(range(1, i)):
			b = L[j]
			index = bisect_right(L, a-b, hi=j)
			k += j - index
	print(k)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect
N = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
for i in range(len(L)-1):
    for j in range(i+1,len(L)-1):
        a = L[i]
        b = L[j]
        c = L[j+1]
        if c-a < b and b < c+a:
            ans += bisect.bisect_left(L,b+a)-j-1
print(ans)

Submission status: Accepted

Solution code:
import bisect


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

    ans = 0
    for i, a in enumerate(l_list):
        for j, b in enumerate(l_list[i+1:]):
            x = a + b
            temp = bisect.bisect_left(l_list, x)
            ans += temp - (i + 1 + j + 1)
    print(ans)


if __name__ == '__main__':
    main()