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
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
 
n = int(input())
l = [int(item) for item in input().split()]
l.sort()
 
ans = 0
for i, a in enumerate(l):
    for j in range(i+1, n):
        b = l[j]
        lower = max(a-b, b-a)
        upper = a + b
        d = bisect.bisect_right(l, lower)
        u = bisect.bisect_left(l, upper)
        double = 0
        if d <= i < u:
            double += 1
        if d <= j < u:
            double += 1
        ans += max(0, u - d - double)
 
print(ans // 3)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# 長さじゃなくて、棒自体で区別してるやんけー！

import numpy as np

N = int(readline())
L = np.array(read().split(),np.int32)

L.sort()

U=1000
counter = np.bincount(L,minlength=U+U+10)
cum = counter.cumsum()

add = L[:,None] + L[None,:]
max_c = cum[add-1]
cnt_c = max_c - np.arange(len(L)) - 1

answer = np.triu(cnt_c,1).sum()
print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
N = int(input())
#ソートしておく
L = sorted(list(map(int, input().split())))
ans = 0
 
#短い2辺を総当たりで選択
for i in range(N - 2):
  for j in range(i + 1, N - 1):
    #小さい順に並べた時の成立条件
    # l1 + l2 > l3
    l12 = L[i] + L[j]
    #l2の隣から　l12以下のところまで全部が対象、成立しない場合
    #ans += max(bisect.bisect_left(L, l12) - j - 1, 0)
    ans += bisect.bisect_left(L, l12) - j - 1
print(ans)

Submission status: Accepted

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

    ans = 0
    for min_index in range(N - 2):
        for second_index in range(min_index + 1, N - 1):
            a, b = L[min_index], L[second_index]
            max_index = bisect_left(L, a + b)
            ans += max_index - (second_index + 1)
    return ans

print(d_triangles())

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
import sys

input = sys.stdin.readline

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

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

print(cnt)

Submission status: Accepted

Solution code:
import bisect
import itertools
import sys

input = sys.stdin.readline


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

        ans = 0
        L.sort()

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

        print(ans)


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Accepted

Solution code:
import bisect
import itertools
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        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):
                ind = bisect.bisect_left(L, L[i] + L[j])
                ans += ind - j - 1

        print(ans)


# Run main
if __name__ == '__main__':
    AtCoder().main()

Submission status: Accepted

Solution code:
import bisect
N = int(input())
#ソートしておく
L = sorted(list(map(int, input().split())))
ans = 0

#短い2辺を総当たりで選択
for i in range(N - 2):
  for j in range(i + 1, N - 1):
    #小さい順に並べた時の成立条件
    # l1 + l2 > l3
    l12 = L[i] + L[j]
    #l2の隣から　l12以下のところまで全部が対象、成立しない場合
    #ans += max(bisect.bisect_left(L, l12) - j - 1, 0)
    ans += bisect.bisect_left(L, l12) - j - 1
print(ans)

Submission status: Accepted

Solution code:
import sys
import bisect

N = int(sys.stdin.readline())
L = [int(x) for x in sys.stdin.readline().split()]
L.sort()

answer = 0

for i in range(len(L)-2):
    for j in range(i+1, len(L)-1):
        answer += bisect.bisect_left(L, L[i]+L[j])-j-1

print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
import random

def test(L):
    lst = []
    N = len(L)
    for a in range(N):
        for b in range(a):
            for c in range(b):
                if L[a] < L[b] + L[c]:
                    lst.append((c, b, a))
    return lst

def test_len(L):
    return len(test(L))

def test2(L):
    return len(set((l[0], l[2]) for l in test(L)))

N = int(input())
L = sorted(map(int, input().split()))
# L = [1, 1, 1000]
# L = [218, 233, 389, 645, 704, 728, 786]
# N = 10
# L = sorted([random.randint(0, 1000) for _ in range(N)])
# L = [150, 190, 192, 281, 294, 374, 501, 686, 769, 954]
# print(L)
# N = len(L)
S = 0
for a in range(2, N):
    c = bisect.bisect_left(L, (L[a] + 2) // 2)
    while c >= 0:
        if L[a] < L[c] * 2:
            c -= 1
        else:
            break
    b = c + 1
    assert L[a] < L[b] * 2
    if b == 0:
        # print("b0", b, a, (a - 1) * (a) // 2)
        S += (a - 1) * (a) // 2
        continue
    if c >= a - 1:
        continue
    assert L[a] < L[b] + L[b]
    assert L[a] >= L[c] + L[c], (a, c)
    S += (a - b) * (a - b - 1) // 2
    while c >= 0:
        while L[a] >= L[b] + L[c]:
            b += 1
        assert L[a] < L[b] + L[c]
        assert L[a] >= L[b - 1] + L[c]
        # print(c, b, a)
        S += a - b
        c -= 1
print(S)
# print(test_len(L))

Submission status: Accepted

Solution code:
N = int(input())

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

count = 0 
L.sort()

for i in range(N-2):
    k = i + 2
    for j in range(i+1,N):
        while (k<N and L[i] + L[j] > L[k]):
            k += 1

        if k > j:
            count += k - j - 1


print(count)

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]"):
    ans = 0
    L.sort()

    for x in range(N):
        for y in range(x+1,N):
            ze = bisect_left(L, L[x]+L[y])            
            ans += (ze-y-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:
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))

from bisect import bisect_left

def main():
    n = ni()
    l = sorted(na())
    
    ans = 0
    for i in range(n):
        for j in range(i+1, n):
            t = l[i] + l[j]
            ind = bisect_left(l, t)
            ans += max(0, ind - j - 1)
    print(ans)

main()

Submission status: Accepted

Solution code:
n=int(input())
da=list(map(int,input().split()))
sum=[0 for i in range(2001)]
ans=0
da=sorted(da)
for i in range(n):
    for j in range(2001):
        if j > da[i]:
            ans+=sum[j]
    for j in range(i):
        sum[da[i]+da[j]]+=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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):
    m = bisect.bisect_right(L,L[j]-L[i])
    if i>m:
      ans += i-m
print(ans)

Submission status: Accepted

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

import sys
from bisect import bisect_left

n = int(sys.stdin.readline().strip())
l = list(map(int,sys.stdin.readline().strip().split()))
    
l.sort()
c = 0
n1 = n - 1
for i in range(n-2):
    li = l[i]
    for j in range(i+1,n1):
        k = bisect_left(l,l[j]+li,lo=j)
        c += k-j-1
#
print(c)

Submission status: Accepted

Solution code:
import copy
import bisect

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

triangles = 0

for i in range(N - 2):
    P = L[i]
    for j in range(i + 1, N - 1):
        Q = L[j]
        if P + Q <= L[j + 1]:
            continue
        maxR = P + Q - 1
        triangles += bisect.bisect_right(L, maxR) - (j + 1)

print(triangles)

Submission status: Accepted

Solution code:
import bisect

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

L.sort()

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

print(num_triangles)

Submission status: Accepted

Solution code:
def main():
    from bisect import bisect_left
    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):
            ab = L[i] + L[j]
            ri = bisect_left(L, ab)
            le = j + 1
            ans += ri - le

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
l=list(map(int,input().split()))
l.sort()
import bisect
ans=0
for i in range(n):
    for j in range(i+1,n):
        x=bisect.bisect_left(l,l[i]+l[j],lo=j+1)
        ans+=x-j-1
print(ans)

Submission status: Accepted

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

    ans = 0
    for max_index in range(2, N):
        for second_index in range(1, max_index):
            b, c = L[second_index], L[max_index]
            # b <= c となるように辺を選ぶ (ソートしておけば簡単)。
            # 残りの辺の長さを a として、三角不等式は c < a + b ⇔ a > c - b である。
            # よって二分探索で長さが c - b より大きく b より小さな辺を求めればよい。
            third_index = bisect_right(L, c - b)
            if third_index < second_index:
                ans += second_index - third_index
    return ans

print(d_triangles())

Submission status: Accepted

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

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

    for i in range(N-2):
        k = 0
        for j in range(i+1,N-1):
            tansum = L[i] + L[j]
            while k < len(L) and L[k] < tansum: k += 1
            if tansum <= L[j+1]:
                continue
#            sum=sum+bi.bisect_left(L[j+1:N+1],tansum)
            sum = sum + k - j - 1
    print(sum)
    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:
import bisect
n = int(input())
L = list(map(int, input().split()))
L.sort()

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

print(count)

Submission status: Accepted

Solution code:
from bisect import bisect_right
N = int(input())
D = list(map(int, input().split()))
D.sort(reverse=False)

count = 0

for i in range(N - 2):
    for j in range(i + 1, N - 1):

        ind = bisect_right(D, D[i] + D[j] - 1)
        count += max(ind - j - 1, 0)

print(count)

Submission status: Accepted

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

ans = 0
for i in range(len(l)):
    right = i+1
    for left in range(i+1, len(l)):
        while right+1 < len(l) and l[right+1] < l[left] + l[i]:
            right += 1
        ans += right - left
        if right == left:
            right += 1

print(ans)

Submission status: Accepted

Solution code:
import bisect
N = int(input())
L = list(int(l) for l in input().split())
L.sort()
num=0

for i in range(N-2):
    for j in range(i+1,N-1):
        a = L[i]
        b = L[j]
        idx = bisect.bisect_left(L, a+b)
#        print("(a,b)=(%d,%d) : idx=%d : (i,j)=(%d,%d)"%(a,b,idx-(j+1),i,j))
        num += idx-(j+1)
print(num)

Submission status: Accepted

Solution code:
from bisect import bisect_right
N = int(input())
D = list(map(int, input().split()))
D.sort(reverse=False)

count = 0

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        # target = int(D[N - i - 1]) - int(D[N - j - 1])
        # l = D[:N - j - 1]
        # count += len(l) - bisect_right(l, target)
        ind = bisect_right(D, D[i] + D[j] - 1)
        count += max(ind - j - 1, 0)

print(count)

Submission status: Accepted

Solution code:
def main():
    from bisect import bisect
    N = int(input())
    L = list(map(int,input().split()))
    L.sort()
    cnt = 0

    for i in range(N-1,1,-1):
        for j in range(i-1,0,-1):
            temp = L[i] - L[j]
            l = bisect(L,temp,lo=0,hi=j)
            cnt += j - l
    print(cnt)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    L = [int(i) for i in input().split()]
    L.sort()
    L_pair = []
    for i in range(N):
        for j in range(i+1, N):
            L_pair.append((i, j))

    def is_ok(mid, i, j):
        if L[mid] < L[i] + L[j]:
            return True
        else:
            return False

    def binary_search_meguru(i, j):
        left = j
        right = N
        while right - left > 1:
            mid = left + ((right - left) // 2)
            if is_ok(mid, i, j):
                left = mid
            else:
                right = mid
        return right

    ans = 0
    for i, j in L_pair:
        r = binary_search_meguru(i, j)
        ans += r - (j + 1)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import bisect
    n = int(input())
    l = list(map(int, input().split()))
    l.sort()
    ans = 0
    # a < b < c とすると、2式は必ず成立し、c < a + b のみ考慮すれば良い
    # つまり　b < c < a + b
    # c < a + b の境界部の発見に2分探索を利用する
    for i in range(n - 1):
        for j in range(i + 1, n):
            k = bisect.bisect_left(l, l[i] + l[j])
            ans += k - (j + 1)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect

n = int(input())
li = list(map(int,input().split()))
count = 0
li.sort()
for i in range(n - 2):
  for j in range(i + 1, n - 1):
    ab = li[i] + li[j]
    x = bisect.bisect_left(li, ab, lo=0, hi=n)
    count += (x - j - 1)
 
print(count)

Submission status: Accepted

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

v = L[:]
v.sort()

res = 0

import bisect

for i in range(N-2):
    a = v[i]
    for j in range(i+1,N-1):
        b = v[j]
        sup = a + b
        #inf = max(a-b,b-a)
        iterS = bisect.bisect_left(v,sup)
        #iterI = bisect.bisect_left(v,inf+1)
        #print(a,b,sup,inf,iterS,iterI)
        res += iterS - (j+1)

print(res)

Submission status: Accepted

Solution code:
from bisect import*
n,*l=map(int,open(0).read().split())
l.sort()
#print(l)
ans=0
for i in range(n):
    for j in range(i+1,n):
#        print(max(bisect_left(l,l[i]+l[j])-j-1,0),l[i]+l[j])
        ans+=max(bisect_left(l,l[i]+l[j])-j-1,0)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = 0
# a < b < c とすると、2式は必ず成立し、c < a + b のみ考慮すれば良い
# つまり　b < c < a + b
# c < a + b の境界部の発見に2分探索を利用する
for i in range(n - 1):
    for j in range(i + 1, n):
        k = bisect.bisect_left(l, l[i] + l[j])
        ans += k - (j + 1)
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
n = int(input())
l = list(map(int, input().split()))
l.sort()
# print(l)

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

print(ans)

Submission status: Accepted

Solution code:
import bisect


N = int(input())
SL = [int(i) for i in input().split()]
SL.sort()
ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        ab = SL[i] + SL[j]
        r = bisect.bisect_left(SL, ab)
        l = j + 1
        ans += max(0, r - l)
print(ans)

Submission status: Accepted

Solution code:
import bisect

def f():
    N = int(input())
    SL = [int(i) for i in input().split()]
    SL.sort()
    ans = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            ab = SL[i] + SL[j]
            r = bisect.bisect_left(SL, ab)
            l = j + 1
            ans += max(0, r - l)
    print(ans)

if __name__ == "__main__":
    f()

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

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

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

Submission status: Accepted

Solution code:
import bisect

def solve():
    N = int(input())
    L = [int(i) for i in input().split()]
    L.sort()
    total = 0
    for s in range(N-2):
        for m in range(s+1, N-1):
            l = bisect.bisect_left(L, L[s] + L[m], 0, N)
            if l > m: total += l - m - 1
    print(total)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
# D - Triangles

import bisect

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

sum = 0

length.sort()

for i in range(n):
    for j in range(n):

            a = length[i]
            b = length[j]

            if i < j:
                k = bisect.bisect_left(length, a + b)
                sum += k - j - 1

print(max(0, sum))

Submission status: Accepted

Solution code:
N = int(input())
# L = map(int, input().split())
SL = [int(i) for i in input().split()]
SL.sort()
# SL = sorted(list(L))
# SL = L.sort()
# print('SL', SL)
import bisect

ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        ab = SL[i] + SL[j]
        # print(i, j)
        # print(SL[i], SL[j])
        r = bisect.bisect_left(SL, ab)
        l = j + 1
        # print('r', r)
        # print('l', l)
        # print('---')
        ans += max(0, r - l)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
L = map(int, input().split())
SL = sorted(list(L))
# print('SL', SL)
import bisect

ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        ab = SL[i] + SL[j]
        # print(i, j)
        # print(SL[i], SL[j])
        r = bisect.bisect_left(SL, ab)
        l = j + 1
        # print('r', r)
        # print('l', l)
        # print('---')
        ans += max(0, r - l)

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

Submission status: Accepted

Solution code:
from bisect import bisect_right
N = int(input())
L = sorted(list(map(int, input().split())))
L = [(l, i) for i, l in enumerate(L)]

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        l1_value, l1_index = L[i]
        l2_value, l2_index = L[j]
        ans += max(0, (bisect_right(L, (l1_value + l2_value, l2_index))) - j - 1)

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right

if __name__ == "__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):
            k = bisect_right(L, L[i] + L[j] - 1)
            ans += max(k - j - 1, 0)

    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
import bisect 

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

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

print(ans)

Submission status: Accepted

Solution code:
import bisect
N=int(input())
L=sorted(list(map(int,input().split())))
count=0
for d1 in range(N-2):
    for d2 in range(d1+1,N-1):
        m=bisect.bisect_left(L,L[d1]+L[d2])
        if m>d2+1:count+=m-(d2+1)
print(count)

Submission status: Accepted

Solution code:
import bisect
n = int(input())
L = sorted([int(i) for i in input().split()])
ans = 0
for b in range(n):
    for a in range(b):
        ab = L[a] + L[b]
        r = bisect.bisect_left(L,ab)
        l = b+1
        ans += max(0,r-l)
print(ans)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
#length = list(map(lambda x:int(x), input().split()))
length = list(map(int, input().split()))
length.sort()
ans = 0
for i in range(n-2):  
    a = length[i]
    for j in range(i+1,n-1):
        b = length[j]
        if i >= j:
            continue
        else:
            max_bar = bisect.bisect_left(length,a+b)
            ans += max_bar - j - 1
            
print(ans)

Submission status: Accepted

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

def main():
    N = int(input())
    L = [int(x) for x in input().split()]
    ans = 0
    L_sorted = sorted(L)
    for i in range(N - 2):
        a = L_sorted[i]
        for j in range(i + 1, N - 1):
            b = L_sorted[j]
            r = bisect.bisect_left(L_sorted, a + b, lo=j + 1)
            ans += r - j - 1

    print(ans)



    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect

N = int(input())
L = list(map(int, input().split(" ")))
L.sort() #小さい順
counter = 0

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

Submission status: Accepted

Solution code:
from bisect import bisect_left

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

ans = 0
l.sort()

for i in range(n):
    for j in range(i + 1, n):
        a = l[i]
        b = l[j]
        idx = bisect_left(l, a + b)
        # print(a, b, idx)

        ans += idx - (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()
count = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    count+=bisect_left(l,l[j]+l[i])-j-1
print(count)

Submission status: Accepted

Solution code:
from itertools import combinations
from bisect import bisect_left

N = int(input())
*L, = map(int, input().split())
L = sorted(L)
ans = 0
for i, j in combinations(range(N), 2): # i < j
    idx = bisect_left(L, L[i] + L[j])
    ans += idx - 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()
#print(L)
ans = 0
for i in range(N - 2):
    a = L[i]
    for j in range(i + 1, N - 1):
        ans += bisect_left(L, a + L[j])  - j - 1
        #print(ans, end=",")
print(ans)

Submission status: Accepted

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

lptr = [-1] * 1001
lptr[0] = n - 1
for i in range(1, 1001):
  maxptr = n -1
  minptr = 0
  fixed = [-1] * n
  cptr = (maxptr + minptr + 1) // 2

  while fixed[cptr] == -1:

#    print(str(cptr)+','+str(l[cptr]))
    if l[cptr] > i:
      fixed[cptr] = 0
      minptr = cptr
    else:
      fixed[cptr] = 1
      maxptr = cptr
    cptr = (maxptr + minptr + 1) // 2
      
  lptr[i] = cptr - fixed[cptr]

#print(lptr)

    
cnt = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    tgt = l[i] - l[j]
    if lptr[tgt] > j:
      cnt += lptr[tgt] - j

print(cnt)

Submission status: Accepted

Solution code:
import math
import collections
import itertools
import sys
import bisect
from heapq import heappop,heappush,heapify
sys.setrecursionlimit(10**6)
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7


n = INT()
l = MAP()

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

print(ans)

Submission status: Accepted

Solution code:
import bisect
def main():
 
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
 
    # print(L)
    ans = 0
    for i in range(N-2):
        for j in range(i+2, N):
            a = L[i]
            c = L[j]
            k = bisect.bisect_left(L, c-a+1)
            if k <= i:
                ans += j-i-1
            else:
                ans += j-k
    return ans
 
if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
inputs = sys.stdin.readlines()
n = int(inputs[0])
L = list(map(int, inputs[1].split()))
A = sorted(L)
 
ans = 0
for i in range(n-1, 0, -1):
    l, r = 0, i-1
    while l < r:
        if A[i] < A[l] + A[r]:
            ans += r-l
            r -= 1
        else:
            l += 1

print(ans)

Submission status: Accepted

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

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

count = 0

# for a, b, c in combinations(ls, 3):
#     if a < b + c and b < c + a and c < a + b:
#         count += 1

# for j in range(n - 1, 1, -1):
#     last = ls[j]
#     for a, b in combinations(ls[:j], 2):
#         if a + b > last:
#             count += 1

for i in range(n - 1, 1, -1):
    for j in range(i - 1, 0, -1):
        idx = bisect_left(ls[:j], ls[i] - ls[j] + 1)
        if idx < j:
            count += j - idx

print(count)

Submission status: Accepted

Solution code:
from bisect import bisect_left


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

    ans = N * (N - 1) * (N - 2) // 6
    L.sort()

    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            b = bisect_left(L, L[i] + L[j])
            if b != N:
                ans -= N - b

    print(ans)


if __name__ == '__main__':
    p_d()

Submission status: Accepted

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

cnt = 0
for i in range(N):
    a = l[i]
    for j in range(i+1, N):
        b = l[j]
        cnt += bisect_left(l[j+1:], a+b)
print(cnt)

Submission status: Accepted

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

L.sort()
#print(L)

from bisect import bisect_right,bisect_left

ans = 0
for i, a in enumerate(L[:-2]):
    for j, b in enumerate(L[i+1:-1]):
        j=i+1+j
        #print(a+b, abs(a-b), i, j)
        #for k, c in enumerate(L[j+1:]):
        #    if abs(a-b) < c and c < a+b:
        #        ans += 1
        #    if c >= a+b:
        #        break
        l = bisect_left(L, abs(a-b),j+1)
        r = bisect_left(L, a+b,j+1)
        #print(r-l)
        ans += r-l
print(ans)

Submission status: Accepted

Solution code:
import bisect
import copy
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))


N=one_int()
L=many_int()

L=sorted(L)

sankaku_dict={}
count=0
for i in range(len(L)-1):
    low=L[i]
    for j in range(i+1, len(L)):
        #すでに探索済みか判定
        high = L[j]
        
        #上限を取得
        high_index = bisect.bisect_left(L[j+1:], high+low)
        count+=high_index#len(L[j+1:high_index])

# len(sankaku_dict)
print(count)
# print(len(sankaku_dict.keys()))