Problem:


Score : 400 points

Problem Statement

Takahashi has N sticks that are distinguishable from each other. The length of the i-th stick is L_i.

He is going to form a triangle using three of these sticks. Let a, b, and c be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:

a < b + c

b < c + a

c < a + b

How many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.

Constraints

All values in input are integers.

3 \leq N \leq 2 \times 10^3

1 \leq L_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Constraints

Print the number of different triangles that can be formed.

Sample Input 1

4
3 4 2 1

Sample Output 1

1

Only one triangle can be formed: the triangle formed by the first, second, and third sticks.

Sample Input 2

3
1 1000 1

Sample Output 2

0

No triangles can be formed.

Sample Input 3

7
218 786 704 233 645 728 389

Sample Output 3

23



Sample input:
4
3 4 2 1


Sample output:
1

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left, bisect_right

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *L = map(int, read().split())

    L.sort()

    ans = 0
    for i in range(N):
        a = L[i]
        for j in range(i + 1, N):
            b = L[j]
            left = bisect_right(L, b)
            right = bisect_left(L, a + b)
            ans += right - left

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

for i in range(N):
    for j in range(i + 1, N):
        for k in range(j + 1, N):
            cnt += (L[k] < L[i] + L[j])
print(cnt)

Submission status: Wrong Answer

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

X = [0]*1005
Y = [0]*1005
Z = [0]*1005
equal = [0]*1005
for i in L:
    X[i] += 1
    if Y[i]==0:
        Y[i] += 1
cum = 0
for i in range(1005):
    cum += Y[i]
    Z[i] += cum
    
for i in range(1005):
    if X[i]>=2:
        equal[i] += 1

import itertools
L.sort()
ab = set(list(itertools.combinations(L,2)))

ans= 0
for i,j in ab:
    ans += Z[min(1000,i+j-1)] - Z[j]
    if i!=j and X[j]>=2:
        ans += 1
    elif i==j and X[j]>=3:
        ans += 1

ans -= sum(equal)*(sum(equal)-1)//2

            
print(ans)

Submission status: Wrong Answer

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

X = [0]*1005
Y = [0]*1005
Z = [0]*1005
for i in L:
    X[i] += 1
    if Y[i]==0:
        Y[i] += 1
cum = 0
for i in range(1005):
    cum += Y[i]
    Z[i] += cum

import itertools
L.sort()
ab = set(list(itertools.combinations(L,2)))

ans= 0
for i,j in ab:
    ans += Z[min(1000,i+j-1)] - Z[j]
    if i!=j and X[j]>=2:
        ans += 1
    elif i==j and X[j]>=3:
        ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int, input().split()))
X = [0]*1005
Y = [0]*1005
for i in L:
    X[i] += 1

cum = 0
for i in range(1005):
    cum += X[i]
    Y[i] += cum

ans = 0
for i in range(1,1001):
    if X[i]!=0:
        for j in range(i,1001):
            if j == i and X[j]>=2:
                ans += Y[min(1000,i+j-1)] - Y[j-1]-2                
            
            elif j != i and X[j]>=1:
                ans += Y[min(1000,i+j-1)] - Y[j-1]-1

print(ans)

Submission status: Wrong Answer

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

#
lim = 10**3 * 2
nums = [0 for _ in range(lim + 1)]
for l in L:
    nums[l] += 1
for i in range(1, lim + 1):
    nums[i] += nums[i-1]

L.sort()

ans = 0
for i in range(N):
    l1 = L[i]
    for j in range(i+1, N):
        l2 = L[j]
        l3_max = (l1 + l2) - 1
        ans += nums[l3_max] - nums[l2]

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from itertools import combinations

def triangle_exists(a, b, c):
    return ((a < b + c) and 
            (b < c + a) and
            (c < a + b))

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

    L_dict = {}
    for l in L:
        if l in L_dict:
            L_dict[l] += 1
        else:
            L_dict[l] = 1

    L_list = list(L_dict.keys())

    ans = 0
    for a,b,c in combinations(L_list, 3):
        if triangle_exists(a, b, c):
            ans += L_dict[a] * L_dict[b] * L_dict[c]
    print(ans)

main()

Submission status: Wrong Answer

Solution code:
import bisect

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

ans = 0
# a + b < c
for i in range(n-2):
    a = ll[i]
    for j in range(i+1, n-1):
        b = ll[j]
        # a+b未満 の要素の個数 (ll[j+1:] を探索すると遅くなるので注意)
        c_cnt = bisect.bisect_left(ll, a+b, lo=j+1)
        # cnt -= (j+1)
        ans += max(c_cnt,0)

print(ans)

Submission status: Wrong Answer

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):
        right = bisect.bisect_left(L, L[a]+L[b])
        left = bisect.bisect_left(L, L[b]+1)
        ans += max((right - left, 0))

print(ans)

Submission status: Wrong Answer

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

def binary_search(data, value):
    left = 0
    right = len(data)

    while left < right:
        mid = (left + right) // 2
        if data[mid] == value:
            return mid
        elif data[mid] < value:
            left = mid + 1
        else:
            right = mid -1
    return -1
        
            
for a in range(n):
    for b in range(a+1, n):
        c = binary_search(sort_l, sort_l[a] + sort_l[b])
        if c > b:
            count += c - b - 1
        else:
            pass     
        

print(count)

Submission status: Wrong Answer

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

def binary_search(data, value):
    left = value + 1
    right = len(data)-1

    while left < right:
        mid = (left + right) // 2
        if data[mid] == value:
            return mid
        elif data[mid] < value:
            left = mid + 1
        else:
            right = mid -1
    return -1
        
            
for a in range(n):
    for b in range(a+1, n):
        c = binary_search(sort_l, sort_l[a] + sort_l[b])
        if c > b:
            count += c - b -1
        else:
            pass     
        

print(count)

Submission status: Wrong Answer

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

def binary_search(data, value):
    left = value + 1
    right = len(data)-1

    while left <= right:
        mid = (left + right) // 2
        if data[mid] == value:
            return mid
        elif data[mid] < value:
            left = mid + 1
        else:
            right = mid -1
    return -1
        
            
for a in range(n):
    for b in range(a+1, n):
        c = binary_search(sort_l, sort_l[a] + sort_l[b])
        if c > b:
            count += c - b -1
        else:
            pass     
        

print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import itertools
import bisect

n = int(input())

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

A = sorted(L)

ans = 0

for a,b in itertools.combinations(A,2):
    total = a + b
    ans += bisect.bisect_left(A,total) - bisect.bisect_right(A,max(a,b))

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
import bisect

n = int(input())

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

A = sorted(L)

ans = 0

for a,b in itertools.combinations(A,2):
    total = a + b
    l = bisect.bisect(A,max(a,b))
    r = bisect.bisect_left(A,total) 
    ans += max(0,r-l)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
length = [0]*(10**3+1)
lengthSame = [-1]*(10**3+1)

for i in range(n):
  length[l[i]] += 1
  lengthSame[l[i]] += 1
  
for i in range(10**3+1):
  length[i] += length[i-1]

ans = 0
for i in range(n):
  for j in range(i+1, n):
    ans += length[min(10**3, l[i]+l[j]-1)] - length[l[j]] + lengthSame[l[j]]
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
length = [0]*(10**3+1)

for i in range(n):
  length[l[i]] += 1
  
for i in range(10**3+1):
  length[i] += length[i-1]

ans = 0
for i in range(n):
  for j in range(i+1, n):
    ans += length[min(10**3, l[i]+l[j]-1)] - length[l[j]]
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import bisect

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

# triples = set()

total = 0

for i in range(len(L)):
    for j in range(i):
        a = L[i]
        b = L[j]

        right_index = bisect.bisect_left(L, a + b)
        left_index = bisect.bisect_right(L, abs(a - b))

        total += right_index - left_index
        
        # for k in range(left_index, right_index):
        #     triple = frozenset([i,j,k])
        #     if len(triple) == 3:
        #         triples.add(frozenset([i,j,k]))

# print(len(triples))

Submission status: Wrong Answer

Solution code:
import bisect

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

# triples = set()

total = 0

for i in range(len(L)):
    for j in range(i):
        a = L[i]
        b = L[j]

        right_index = bisect.bisect_left(L, a + b)
        left_index = bisect.bisect_right(L, abs(a - b))

        total += right_index - left_index
        
        # for k in range(left_index, right_index):
        #     triple = frozenset([i,j,k])
        #     if len(triple) == 3:
        #         triples.add(frozenset([i,j,k]))

# print(len(triples))

Submission status: Wrong Answer

Solution code:
import sys
from io import StringIO
import unittest
import os
import bisect

# 検索用タグ、尺取り、3つの内2つを固定
# https://drken1215.hatenablog.com/entry/2019/10/20/032700

# 実装を行う関数
def resolve():
    n = int(input())
    ls = list(map(int, input().split()))

    ls.sort()

    ans = 0
    # 尺取り
    for i in range(n - 2):
        a = ls[i]
        for j in range(i + 1, n - 1, 1):
            b = ls[j]
            # 以下がサンプル通りだが、TLE。
            # for k in range(j + 1, n, 1):
            #     c = ls[k]
            #     if not c < a + b:
            #         break
            #     ans += 1
            cnt = bisect.bisect_left(ls, a + b, j)
            if cnt >= n:
                continue
            ans += cnt - j - 1


    print(ans)


# テストクラス
class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """4
3 4 2 1"""
        output = """1"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """3
1 1000 1"""
        output = """0"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """7
218 786 704 233 645 728 389"""
        output = """23"""
        self.assertIO(test_input, output)

    # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()

Submission status: Wrong Answer

Solution code:
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):
        for k in range(j + 1, n):
            if l[i] + l[j] + 1 > l[k]:
                ans += 1
            else:
                break

print(ans)

Submission status: Wrong Answer

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

L.sort()
count = 0
for i in range(N-2):
  a = L[i]
  for j in range(i+1, N-1):
    for k in range(j+1, N):
      b = L[j]
      c = L[k]
      b2 = b/a
      c2 = c/a
      if b2+c2 > 1 and abs(b2-c2) < 1:
        count += 1
      else:
        break
print(count)

Submission status: Wrong Answer

Solution code:
import os, sys, re, math

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

counts = [0] * 1001
for i in range(1001):
    counts[i] = len([x for x in L if x == i])

L = sorted(list(set(L)))

memo = [-1] * 2001
triangles = 0

for l in L:
    if counts[l] >= 3:
        triangles += math.factorial(counts[l]) // math.factorial(3)

for i in range(len(L) - 2):
    for j in range(i + 1, len(L) - 1):
        Lij = L[i] + L[j]
        if memo[Lij] < 0:
            t = 0
            for k in range(j + 1, len(L)):
                if L[i] + L[j] > L[k]:
                    triangles += counts[L[i]] * counts[L[j]]
                else:
                    break
            memo[Lij] = t
        triangles += memo[Lij]

print(triangles)

Submission status: Wrong Answer

Solution code:
import os, sys, re, math

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

counts = [0] * 1001
for i in range(1001):
    counts[i] = len([x for x in L if x == i])

L = sorted(list(set(L)))

memo = [-1] * 2001
triangles = 0
for i in range(len(L) - 2):
    for j in range(i + 1, len(L) - 1):
        Lij = L[i] + L[j]
        if memo[Lij] < 0:
            t = 0
            for k in range(j + 1, len(L)):
                if L[i] + L[j] > L[k]:
                    triangles += counts[L[i]] * counts[L[j]]
                else:
                    break
            memo[Lij] = t
        triangles += memo[Lij]

print(triangles)

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
L = [int(s) for s in input().split(' ')]
L_sorted = sorted(L)
print(L_sorted)
ret = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        for k in range(j + 1, N):        
            a = L[i]
            b = L[j]
            c = L[k]
            # c < a + b
            # c > a - b
            # c > b - a 
            if a < b + c and b < c + a and c < a + b:
                ret += 1

print(ret)

Submission status: Wrong Answer

Solution code:
import os, sys, re, math

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

counts = [0] * 1001
for i in range(1001):
    counts[i] = len([x for x in L if x == i])

L = sorted(list(set(L)))

triangles = 0
for i in range(len(L) - 2):
    for j in range(i + 1, len(L) - 1):
        for k in range(j + 1, len(L)):
            if L[i] + L[j] > L[k]:
                triangles += counts[L[i]] * counts[L[j]]
            else:
                break

print(triangles)

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
L = [int(s) for s in input().split(' ')]
L_sorted = sorted(L)
print(L_sorted)
ret = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        for k in range(j + 1, N):        
            a = L[i]
            b = L[j]
            c = L[k]
            # c < a + b
            # c > a - b
            # c > b - a 
            if a < b + c and b < c + a and c < a + b:
                print((a,b,c))
                ret += 1

print(ret)

Submission status: Wrong Answer

Solution code:
import os, sys, re, math

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

counts = [0] * 1001
for i in range(1001):
    counts[i] = len([x for x in L if x == i])

L = sorted(list(set(L)))

triangles = 0
for i in range(len(L) - 2):
    for j in range(i + 1, len(L) - 1):
        for k in range(j + 1, len(L)):
            if L[i] + L[j] > L[k]:
                triangles += counts[L[i]] * counts[L[j]]

print(triangles)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *

N = INT()
L = LIST()

L.sort()

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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *

N = INT()
L = LIST()

L.sort()

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

print(ans)

Submission status: Wrong Answer

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

L_list = sorted(L_list)

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

print(count)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

cnt=0
for i in range(0,n-1):
    for j in range(i+1,n):
        k=bisect.bisect_left(l,l[j]+1)
        p=bisect.bisect_right(l,l[i]+l[j]-1)
        cnt+=max(p-k,0)
print(cnt)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import bisect


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

    ans = 0
    for i in range(n-1):
        for j in range(i+1, n):
            a = ls[i]
            b = ls[j]
            k = bisect.bisect_left(ls, a+b)
            if k > j:
                ans += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
D - Triangles
https://atcoder.jp/contests/abc143/tasks/abc143_d

"""
import sys

from itertools import combinations
from bisect import bisect_left, bisect_right

def solve(N, lines):
    ans = 0
    for x, y in combinations(lines, 2):
        i = bisect_left(lines, x)
        j = bisect_right(lines, y-x)
        ans += max(0, (i - j))
    return ans


def main(args):
    N = int(input())
    lines = sorted(map(int, input().split()))
    ans = solve(N, lines)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    l = sorted(list(map(int, input().split())), reverse=True)
    count = 0
    for i in range(n-2):
        a = l[i]
        for j in range(i+1,n-1):
            b = l[j]
            x = a-b
            z = a+b
            for k in range(j+1, n):
                c = l[k]
                if x >= c:
                    break
                if z > c:
                    count += n-k
                    break
    print(count)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
cnt = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        if l[i] + l[j] > l[n - 1]:
            cnt += n - j - 1
            continue
        le = j + 1
        ri = n - 1
        while ri - le > 0:
            m = (le + ri) // 2
            if l[m] < l[i] + l[j]:
                le = m + 1
            else:
                ri = m
            cnt += le - j - 1
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.sort()
cnt = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        if l[i] + l[j] > l[n - 1]:
            cnt += n - j - 1
            continue
        le = j + 1
        ri = n - 1
        while ri - le > 0:
            m = (le + ri) // 2
            if l[m] < l[i] + l[j]:
                le += m + 1
            else:
                ri = m
            cnt += le - j - 1
print(cnt)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right, bisect_left

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

l.sort()

ans = 0
for i, a in enumerate(l):
    for b in l[i+1:]:
        j = bisect_right(l, b)
        k = bisect_left(l, a + b)
        ans += k - j

print(ans)

Submission status: Wrong Answer

Solution code:
# abc143_d.py
def can_make(i, j, k):
    if L[i] < L[j] + L[k] and L[j] < L[k] + L[i] and L[k] < L[j] + L[i]:
        return True
    return False


def find(i, j, L):
    left = j
    right = N
    mid = (right + left)//2
    flg = False
    while right - left > 1:
        mid = (right + left)//2
        # print("left,mid,right", left, mid, right)
        if can_make(i, j, mid):
            # print("can make")
            left = mid
            flg = True
        else:
            right = mid
    # print("j,mid", j, mid)
    if flg:
        return mid-j
    else:
        return 0


N = int(input())
L = list(map(int, input().split()))
cnt = 0
L = sorted(L, reverse=True)
for i in range(N):
    for j in range(i+1, N):
        # print(L)
        # print("i,j", i, j, L[i], L[j])
        cnt += find(i, j, L)
print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

N = int(input())
L = sorted([int(i) for i in input().split()], reverse=True)
cnt = 0
for i in range(N):
    for j in range(i+1,N):
        if L[i]-L[j] > L[j]:
            continue
        cnt += N-bisect_left(L[::-1], L[i]-L[j])-j-1
print(cnt)

Submission status: Wrong Answer

Solution code:
import bisect

n = int(input())

in_ar = [0 for _ in range(n)]
in_ar = [int(s) for s in input().split()]
in_ar.sort()
res = 0
#print(in_ar)
for i in range(n):
    for j in range(i,n):
        if(i == j):
            continue

        a = in_ar[i]
        b = in_ar[j]
        st = bisect.bisect_left(in_ar, abs(a-b))
        en = bisect.bisect_right(in_ar, abs(a+b))
        #print(a,b,abs(a-b),abs(a+b),st,en)
        res += en - j - 1
        '''
        if(abs(a-b) < a):
            res -= 1
        if(abs(a-b) < b):
            res -= 1
        '''

print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import bisect

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

L.sort()
print(L)
ans = 0
for ai in range(N):
    for bi in range(ai+1, N):
        ci = bisect.bisect_left(L, L[ai]+L[bi])
        print(ai, bi, ci)
        if ci > bi:
            ans += ci - bi - 1
            print(ci - bi - 1)
        else:
            pass

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
L = sorted([int(i) for i in input().split()], reverse=True)
L = np.array(L)

ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        min_c = np.count_nonzero(L[j] > L[j+1:])
        max_c = np.count_nonzero(L[i] - L[j] >= L[j+1:])
        ans += min_c - max_c

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l_s = list(map(int,input().split()))
l_s.append(0)
l_s.sort()
ans = 0
#一番長い辺を選ぶ
#print(l_s)
for i in range(3,n+1):
    for j in range(2,i):
        OK = 0
        NG = j
        while (NG-OK) > 1:
            mid = (OK+NG)//2
            if l_s[i] < l_s[j] + l_s[mid]:
                OK = mid
            else:
                NG = mid
        ans += OK
print(ans)

Submission status: Wrong Answer

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n=n0()
L=n1()
L.sort()
m=L[-1]

d=[0]*(m+1) 
for i in range(n):
        d[L[i]]+=1
d2=list(itertools.accumulate(d))

ans=0
for i in range(n):
    for j in range(i+1,n):
        a,b=L[i],L[j]
        ans+=max(d2[a-1]-d2[b-a],0)
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

n = int(input())
L = sorted(map(int, input().split()))
ans = 0
for i in range(n):
    a = L[i]
    for j in range(i+1, n):
        b = L[j]
        k = bisect.bisect_left(L, a+b)
        if L[k-1] > b:
            ans += k - j - 1
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

n = int(input())
L = sorted(map(int, input().split()))
ans = 0
for i in range(n):
    a = L[i]
    for j in range(i+1, n):
        b = L[j]
        k = bisect.bisect_left(L, a+b)
        print(i, j, k)
        if L[k-1] > b:
            ans += k - j - 1
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

n = int(input())
L = sorted(map(int, input().split()))
ans = 0
for i in range(n-2):
    a = L[i]
    for j in range(i+1, n-1):
        b = L[j]
        k = bisect.bisect_left(L, a+b)
        if L[k - 1] > b:
            ans += k - j - 1
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

Submission status: Wrong Answer

Solution code:
from itertools import combinations
from collections import defaultdict

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

for l in L_:
    d[l] += 1
n = 0
for i in range(2001):
    n += d[i]
    d[i] += n

ans = 0

for a, b in combinations(L_, 2):
    n =  d[a+b-1] - d[abs(a-b)]
    if abs(a-b) < a:
        ans -= 1
    if abs(a-b) < b:
        ans -= 1
    ans += n
    
print(ans//3)

Submission status: Wrong Answer

Solution code:
def judge(a,b,c):
    r = [a,b,c]
    if max(r) < sum(r)-max(r):
        return True
    else:
        return False

n = int(input())
l = list(map(int,input().split()))
o = [0 for i in range(max(l))]
u = []
ans = 0

for i in range(n):
    o[l[i]-1] += 1

for i in range(max(l)):
    if o[i]:
        u.append([i+1,o[i]])

for i in range(len(u)-2):
    for j in range(i+1,len(u)-1):
        for k in range(j+1,len(u)):
            if judge(u[i][0],u[j][0],u[k][0]):
                ans += u[i][1]*u[j][1]*u[k][1]

print(ans)

Submission status: Wrong Answer

Solution code:
inf = 1001001001
n = int(input())
lis = list(map(int, input().split()))
lis.sort()
lis.append(inf)
ans = 0
for i in range(n-2):
    a = lis[i]
    for j in range(i + 1, n-1):
        b = lis[j]
        l = j
        r = n
        #while r > l+1:
        #    mid = (l+r) // 2
        #    if lis[mid] < a + b:
        #        l = mid
        #    else:
        #        r = mid
        #ans += l - j
print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left, bisect_right
from itertools import combinations_with_replacement

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

li = list(combinations_with_replacement(l, 2))
li = [x for x in li if x[0] != x[1] and x[1] != max(l)]

ans = 0
for small, mid in li:
    right = bisect_right(l, small + mid - 1)
    left = bisect_left(l, mid)
    ans += max(right - left - 1, 0)

print(ans)