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:
import bisect
n=int(input())
l=list(map(int, input().split()))

i=1
j=0
k=0
ta=0
rb=0
tc=0
cnt=0
m=0
ls=sorted(l)



for i in range(0,len(ls)-2):

    for j in range(i+1,len(ls)-1):
        l1 = ls[i]
        l2 = ls[j]
        ll= l1+l2
        # mid = 0
        k = bisect.bisect_left(ls,ll) - 1

        cnt+=k-j
print(cnt)

Submission status: Accepted

Solution code:
#coding:utf-8
import sys
import bisect
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]


    n = II()
    l = LMIIS()
    a = [l[i] + l[j] for i in range(n) for j in range(i+1, n)]

    a.sort()
    ans = 0
    for ll in l:
        x =  bisect.bisect(a, ll)
        ans += x
    print(n*(n-1)*(n-2)//6 - ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect

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

for i in range(n - 1):
    for j in range(i + 1, n):
        a = l[i]
        b = l[j]
        a_b = a + b
        insert_index = bisect.bisect_left(l, a_b)
        cnt += insert_index - (j + 1)

print(cnt)

Submission status: Accepted

Solution code:

n = int(input())
A= [int(c) for c in input().strip().split(' ')]
# s = input().strip()
A = sorted(A)
cnt =0
for i in range(n-1,1,-1):
    l,r=0,i-1

    while(l<r):
        # print(l,r,i)
        if(A[l]+A[r]>A[i]):
            cnt+=r-l
            r-=1
        else:
            l+=1
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int, input().split()))
L.sort()
count = 0
for i in range(N):
    a = L[i]
    k = i+2
    for j in range(i+1,N):
        b = L[j]
        while k < N and a+b > L[k]:
            k += 1
        count += k - 1 - j
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

a = [l[i] + l[j] for i in range(n) for j in range(i+1, n)]

len_a = n*(n-1)//2

a.sort()

ans = 0
for ll in l:
    x =  bisect.bisect(a, ll)
    ans += x

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

Submission status: Accepted

Solution code:
import sys
import bisect
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
MOD = 1000000007
def POW(x, y): return pow(x, y, MOD)
def INV(x, m=MOD): return pow(x, m - 2, m)
def DIV(x, y, m=MOD): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())

N=II()
LS = LI()
LS.sort()

res = 0
for i in range(N):
    for j in range(i+1,N):
        upper = LS[i]+LS[j]
        lower = abs(LS[i]-LS[j])

        ui = bisect.bisect_left(LS, upper, j+1)
        li = bisect.bisect_left(LS, lower+1, j+1)
        num = ui-li
        res += num
print(res)

Submission status: Accepted

Solution code:
import bisect
n=int(input())
l=sorted(map(int,input().split()))
print(sum(bisect.bisect_left(l,l[i]+l[j])-j-1for i in range(n)for j in range(i+1,n)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
L = list(map(int, input().split()))

def main():
    tri = 0
    L.sort()
    for i in range(N-1,-1,-1): #range(初期値,終了条件,変化量)
        mn = 0
        sl = i-1
        while(mn<sl):
            if (L[mn] + L[sl]) > L[i]:
                tri += sl - mn
                sl -= 1
            else:
                mn += 1
    print(tri)
    
main()

Submission status: Accepted

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

import bisect

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

answer = 0
for b in range(1, len(L)):
    for a in range(b):
        # c は b より大きい index であり、かつ `L[a] + L[b] < L[c]` である
        # c を二分探索で探す
        r = bisect.bisect_left(L, L[a] + L[b], 0, N)
        l = b + 1

        answer += max(0, r - l)

print(answer)

Submission status: Accepted

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

'''

1 2 3 4

1 < 2+3
2 < 1+3
3 < 1+2

6 - 1 = 5
6 - 2 = 4
6 - 3 = 3

'''

Submission status: Accepted

Solution code:
import sys

def bi_search(array, target):
    l, r = -1, len(array)-1
    while r-l > 1:
        mid = int((r+l)/2)
        if array[mid] > target:
            r = mid
        else:
            l = mid
    return r

N = int(sys.stdin.readline().strip())
l_list = list(sorted(list(map(int, sys.stdin.readline().strip().split(" ")))))
count = 0
#print(l_list)

for i in reversed(range(N)):
    a = l_list[i]
    for j in reversed(range(i)):
        b = l_list[j]
        index = bi_search(l_list[:j], a-b)
        """
        print("====")
        print(b-a)
        print(i, j, index)
        """
        if index > -1 and index < j and l_list[index] + b > a:
            count += (j-index)

print(count)

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-1):
    for j in range(i+1, n):
        ans += bisect_left(L, L[i] + L[j]) - j - 1

print(ans)

Submission status: Accepted

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

import bisect

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

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

Submission status: Accepted

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

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

print(result)

Submission status: Accepted

Solution code:
import bisect

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

ans = 0
for i in range(N):
  for j in range(i+1, N):
    num = L[i] + L[j]
    tmp = bisect.bisect_left(L, num) - 1
    #print(i, j, num)
    #print(tmp)
    ans += max(tmp - j, 0)
    #print(ans)
    #print("---")
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left
from itertools import combinations 

input = sys.stdin.readline
 
N = int(input())
l= list(map(int, input().split()))
 
l.sort()
cnt = 0
 
for n,i in enumerate(l[:N-2]):
    for m,j in enumerate(l[n+1:N-1],n+1):
        cnt += max(0,bisect_left(l,i+j)-(m+1)) 
        
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

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):
    for j in range(i+1,n-1):
        ind = bisect.bisect_left(l,l[i]+l[j])
        ans += ind-j-1

print(ans)

Submission status: Accepted

Solution code:
import bisect

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):
        idx = bisect.bisect_left(L, L[i] + L[j])
        ans += max(0, idx - j - 1)
print(ans)

Submission status: Accepted

Solution code:
import bisect

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


for i in range(n):
    for j in range(i+1,n):
        k=l[i]+l[j]-1
        index = bisect.bisect_right(l,k,lo=j+1)
        count+=index-j-1
        

print(count)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
 
def main():
    N = int(input())
    L = [int(c) for c in input().split()]
    L = sorted(L)
    num = 0
    for ai in range(N):
        for bi in range(ai+1, N):
            if bi - ai == 1:
                continue
            a = L[ai]
            b = L[bi]
            ci_left = bisect_right(L, abs(L[ai] - L[bi]))
            ci_right = bisect_left(L, L[ai] + L[bi])
            if ci_left == ci_right:
                continue
            else:
                ci_left = max(ci_left, ai + 1)
                ci_right = min(ci_right, bi)
            # print(a, b, ci_left, min(ci_right, bi) - max(ci_left, ai) - 1)
            # num += min(ci_right, bi) - max(ci_left, ai) - 1
                num += ci_right - ci_left
            # print(a, b, ci_right, ci_left, num)
 
    print(num)
 
 
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()
count = 0

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

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import bisect


def slove():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    a.sort()
    cnt = 0
    for i in range(n-2):
        for j in range(i + 1, n-1):
            p = bisect.bisect_left(a, a[i] + a[j])
            cnt += (p - j - 1)
    print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
import bisect

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

m = 0

for a in range(n-2):
	for b in range(a+1,n-1):
		m+=bisect.bisect_left(d,d[a]+d[b])-1-b

print(m)

Submission status: Accepted

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

L.sort()

count = 0
for a in range(0, len(L)-2):
    for b in range(a+1, len(L)-1):
        count += binary_search(L[b+1:], L[a]+L[b])
print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
n = int(input())
l = list(map(int, input().split()))
l.sort()
result = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        tmp = l[i] + l[j]
        ind = bisect.bisect_left(l, tmp)
        result += (ind-j-1)
print(result)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input()[:-1])
li = list(map(int,input().split()))
li.sort()
ans = 0
for i in range(n-1,1,-1):
  for j in range(i-1,0,-1):
    l = 0
    r = j
    while r-l > 0:
      p = (r+l)//2
      if li[p] + li[j] > li[i]:
        r = p
      else:
        l = p + 1
    ans += j - r
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#提出 #8086687

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:
import bisect
N=int(input())
L=list(map(int,input().split()))
R=sorted(L)
point=0
for i in range(N-2):
  for j in range(i+1,N-1):
    p = bisect.bisect_right(R,R[i]+R[j]-1,j+1,N)
    point+=p-(j+1)
    
print(point)

Submission status: Accepted

Solution code:
import bisect


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

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import bisect

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):
        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()
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:
import bisect

N = int(input())
lst =list(map(int, input().split()))
lst.sort()
ret = 0
for i in range(N):
    for j in range(i + 1, N):
        n = lst[i] + lst[j]
        idx = bisect.bisect_left(lst, n, j + 1, N)
        ret += max(0, idx - j - 1)
print(ret)

Submission status: Accepted

Solution code:
import bisect

N = int(input())
lst =list(map(int, input().split()))
lst.sort()
ret = 0
for i in range(N):
    for j in range(i + 1, N):
        n = lst[i] + lst[j]
        idx = bisect.bisect_left(lst, n)
        ret += max(0, idx - j - 1)
print(ret)

Submission status: Accepted

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

count = 0
for i in range(n):
    for j in range(i+1, n):
        index = bisect.bisect_left(ls[j+1:], ls[i] + ls[j])
        count += index
print(count)

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):
    for j in range(N-i-1):
        a = L[i]
        b = L[i+j+1]
        c_right = bisect.bisect_left(L, b + a, i+j+2)
        ans += c_right - (i+j+2)
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(N-2):
    for j in range(N-i-1):
        a = L[i]
        b = L[i+j+1]
        c_right = bisect.bisect_left(L[i+j+2:], b + a)
        ans += c_right
print(ans)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
l = input().split(' ')
l = [int(i) for i in l]
l.sort()

cnt = 0
for i in range(n):
    for j in range(i+1, n):
        num = l[i] + l[j]
        index = bisect.bisect_left(l, num)
        # print(index, j)
        # print(index-2)
        cnt += (index-j-1)

print(cnt)

Submission status: Accepted

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

from bisect import bisect_right

L = sorted(L, reverse=True)
L = [-L[i] for i in range(N)]

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split(" ")))
l.sort()
cnt = 0 
def search(arr,target,start,last):
    while last > start:
        mid = (last + start)//2
        if arr[mid] < target:
            start = mid + 1 
        else:
            last = mid 
    return start

for i in range(0,n-2):
    a = l[i]
    for j in range(i+1,n-1):
        b = l[j]
        target = a + b 
        f1 = search(l,target,j+1,n)
        cnt+= f1 - j -1
print(cnt)

Submission status: Accepted

Solution code:
from bisect import *

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

a.sort()

cn = 0

for i in range(n):
    for j in range(i+1,n):
        
        t = a[i]+a[j]
        ind = bisect_left(a, t)
        cn += len(a)-ind
        # print(cn,ind,i,j)
        
d = n*(n-1)*(n-2)//6

print(d-cn)

Submission status: Accepted

Solution code:
#coding:utf-8
import sys
import bisect
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]

    N = II()
    L = LMIIS()
    L.sort()
    count = 0
    for i in range(N):
        a = L[i]
        k = i+2
        for j in range(i+1,N):
            b = L[j]
            while k < N and a+b > L[k]:
                k += 1
            count += k - 1 - j
    print(count)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())

n = readi()
t = readl()
t = [t[i] * -1 for i in range(n)]
t.sort()
s = 0
while(n >= 3):
    for j in range(1, n):
        p = t[0] - t[j] - 1
        i = bisect.bisect_right(t, p)
        i -= 1
        if i >= j + 1 and t[i] <= p:
            s += i - j
    n -= 1
    t.pop(0)
print(s)

Submission status: Accepted

Solution code:
import bisect
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):
        t = l[a]+l[b]
        cc = bisect.bisect_left(l,t)
        ans += max(0,cc-b-1)
print(ans)

Submission status: Accepted

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

def binary(L):
  left = -1
  right = len(L)
  t = (left+right)//2
  while right-left != 1:
    if L[t] > max_len - next_len:
      left = t
    else:
      right = t
    t = (left + right) // 2
  return left + 1
  
  
for i in range(N-2):
  max_len = L[i]
  for j in range(i+1,N-1):
    next_len = L[j]
    ans += binary(L[j+1:])
print(ans)

Submission status: Accepted

Solution code:

import bisect

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

L.sort()

cnt = 0
for i, b in enumerate(L):
    for a in L[:i]:
        idx = bisect.bisect_left(L, a+b)
        cnt += idx - (i + 1)

print(cnt)

Submission status: Accepted

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

ans = 0
from bisect import bisect_left
for i in range(N):
    for j in range(i+1,N):
        t = bisect_left(L,L[i]+L[j])
        ans += max(0, t-(j+1))
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
N = int ( input() )
t_list = list ( map ( int, input().split() ) )
t_list.sort()
ans = 0

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

Submission status: Accepted

Solution code:
import bisect
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):
		maxk=bisect.bisect_left(l,l[i]+l[j],j+1,n)-1
		count+=maxk-j
print(count)

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        ans += max(0, bisect_left(L, L[i]+L[j]) - j - 1)
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(N-2):
    for j in range(i+1, N-1):
        ans += max(0, bisect.bisect_left(L, L[i]+L[j]) - j - 1)
print(ans)

Submission status: Accepted

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


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

        ans += max(0, c-j-1)

        # print(i, j, c, c-j-1, ans)
print(ans)

Submission status: Accepted

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

from bisect import bisect_left

L.sort()

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

print(count)

Submission status: Accepted

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

count = 0        
for i in range(n-2):
  a = L[i]
  for k in range(i+1,n-1):
    b = L[k]

    count += bisect.bisect_left(L, a + b) - k -1
print(count)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
l2=sorted(l)
box=[0] #各長さの棒が何本あるか　最初の０は数合わせ
ans=0

num=1
while num<l2[0]: #最小の棒までは０をappend
    box.append(0)
    num+=1
    
now=l2[0]
bounum=1
for i in range(1,n):
    if l2[i]!=now:
        box.append(bounum)
        next=now+1
        while next<l2[i]:
            box.append(bounum)
            next+=1 
        now=l2[i]
        bounum+=1
    else:
        bounum+=1
if len(box)!=l2[-1]+1:
    box.append(bounum) #boxの完成

for i in range(n-2):
    for j in range(i+1,n-1):
        c=l2[i]+l2[j]-1
        if c>l2[-1]:
            c=l2[-1]
        add=box[c]-(j+1)
        if add<0:
            add=0
        ans+=add

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

count = 0

for i in range(n - 2):
  a = ls[i]
  for j in range(i + 1, n - 1):
    b = ls[j]
    index = bisect.bisect_left(ls, a + b)
    count += index - j - 1
print(count)

Submission status: Accepted

Solution code:
#import pysnooper
#import numpy
#import os,re,sys,operator
#from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement,permutations
from sys import stdin,setrecursionlimit
from bisect import bisect_left
#from copy import deepcopy
#import heapq
#import math
#import string

setrecursionlimit(10**6)
input=stdin.readline

n=int(input().rstrip())
l=sorted([int(i) for i in input().split()])
ans=0
for i in range(n-2):
    for j in range(i+1,n-1):
        m=bisect_left(l,l[i]+l[j])
        ans+=m-j-1
        #print(m)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


## Input
N = int(input())
Ln = list(map(int, input().split()))
Ln = sorted(Ln)

## Solve
ret = 0
for a in range(2,N):
  c = 0
  for b in range(a-1,0,-1):
    if c >= b:
      continue
    else:
      while (c<b and Ln[a]>=Ln[b]+Ln[c]):
        c += 1
      ret += max(b-c,0)
print(ret)

Submission status: Accepted

Solution code:
import bisect
import copy

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

Submission status: Accepted

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

ans = 0
for i in range(N):
    for j in range(i, N):
        if i == j: continue
        ab = L[i] + L[j]
        to = bisect.bisect_left(L, ab, j, N)
        ans += to - 1 - j
print(ans)

Submission status: Accepted

Solution code:
import bisect
import copy

#bisect2回使用する計算はアウト

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):
        Max = bisect.bisect_left(L,L[i]+L[j])
        ans += max(Max-(j+1),0)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
n = int(input())

li = list(map(int, input().split()))
li.sort()

count = 0

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    L=list(map(int,input().split()))
    L.sort()
    ans=0
    for b in range(n):
        c=b+1
        for a in range(b):
            while(1):
                if(c>=n or L[a]+L[b]<=L[c]): break
                c+=1
            ans+=c-(b+1)
    print(ans)
resolve()

Submission status: Accepted

Solution code:
from sys import stdin
import bisect

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

li.sort()

point = 0

for i in range(n-2):
    for j in range(i+1,n-1):
        bi = bisect.bisect_left(li,li[i]+li[j])
        point += bi-j-1
print(point)

Submission status: Accepted

Solution code:
N = int(input())
l = [int(x) for x in 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:
from bisect import bisect_right
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for i in range(N):
    for j in range(i):
        m = L[i]-L[j]
        k = bisect_right(L, m)
        n = max(0, j-k)
        ans+=n
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(1, N-1):
    for j in range(i+1, N):
        ans += i - bisect.bisect_right(L[:i], L[j]-L[i])
print(ans)

Submission status: Accepted

Solution code:
def solve():
    """
    基本方針:
        a <= b <= c で三角形を作る.
        a, b を決定して取りうる c を計算で求める(O(N ** 2)).
    """
    MAX_LENGTH = 10 ** 3
    N = input()
    L = [int(i) for i in input().split()]
    L.sort()

    # 移動和を求められるようにしておく
    O = [0] * (MAX_LENGTH + 1)
    for l in L:
        O[l] += 1
    O2 = [0] * (MAX_LENGTH + 1)
    for i in range(1, MAX_LENGTH + 1):
        O2[i] = O2[i - 1] + O[i]

    # △カウント
    sum_count = 0
    for i in range(len(L)):
        a = L[i]
        for j in range(i + 1, len(L)):
            b = L[j]
            # c の上限の決定
            c_high = a + b - 1
            if c_high > MAX_LENGTH:
                c_high = MAX_LENGTH
            # △の数加算
            sum_count += O2[c_high] - (j + 1)  # b <= c なので 1 ~ b までの辺を除外
            # print(i, j , O2[c_high] - (j + 1))

    print(sum_count)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
from bisect import bisect_left as bi
n = int(input())
x = [int(i) for i in input().split()]
x,ans = sorted(x),0
for i in range(2,n):
    for j in range(i-1,0,-1):
        b = bi(x,x[i]-x[j]+1)
        if b<j: ans+=j-b
        else: break
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
L_lst=sorted(list(map(int,input().split())))
cnt=0
for j in range(n-1,1,-1):
    i=j-1
    x=0
    while x<i:
        if L_lst[i]+L_lst[x]>L_lst[j]:
            cnt+=i-x
            i-=1
        else:
            x+=1
print(cnt)

Submission status: Accepted

Solution code:
from sys import stdin
import bisect

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

L.sort()

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

print(ans)

Submission status: Accepted

Solution code:
from _bisect import bisect_left
from _bisect import bisect_right

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

cnt = 0

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

Submission status: Accepted

Solution code:
import bisect

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

    cnt = 0
    for i in range(2,N):
        for j in range(i):
            shortest_len = L[i] - L[j] + 1
            index = bisect.bisect_left(L, shortest_len)
            if index < j:
                cnt += j - index
    
    print(cnt)

if __name__ == '__main__':
    solve()

Submission status: Accepted

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


def main():
    from bisect import bisect

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

    # KeyInsight
    # a < b < c かつ a + b < cとなる組み合わせ
    # 2つの値a, bを決め打ち
    # See:
    # https://www.youtube.com/watch?v=3U_N7zelnMM
    for i in range(n):
        for j in range(i + 1, n):
            ab = l[i] + l[j]
            right = bisect(l, ab - 1)
            left = j + 1
            ans += right - left

    print(ans)


if __name__ == '__main__':
    main()