Problem:


Score : 400 points

Problem Statement

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

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

a < b + c

b < c + a

c < a + b

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

Constraints

All values in input are integers.

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

1 \leq L_i \leq 10^3

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Constraints

Print the number of different triangles that can be formed.

Sample Input 1

4
3 4 2 1

Sample Output 1

1

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

Sample Input 2

3
1 1000 1

Sample Output 2

0

No triangles can be formed.

Sample Input 3

7
218 786 704 233 645 728 389

Sample Output 3

23



Sample input:
4
3 4 2 1


Sample output:
1

Submission status: Wrong Answer

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

counts = {}
x = v[0]
c = 0
for i in v:
    if i != x:
        counts[x] = c
        x = i
        c = 0
    c += 1
counts[x] = c

v1 = list(counts)

sums = [0]
for i in range(1, 1005):
    try:
        sums.append(v1.index(i))
    except ValueError:
        sums.append(sums[-1])


total = 0
for i, x in enumerate(v1[:-2]):
    if x == 1:
        continue
    for j, y in enumerate(v1[i+1:-1]):
        total += (sums[min(1004, x+y-1)] - sums[y])


for i, c in counts.items():
    if c >= 2:
        total += (sums[min(1004, 2*i-1)])




# v3
total += sum(True if c >= 3 else False for c in counts.values())


print(total)

Submission status: Wrong Answer

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

counts = {}
x = v[0]
c = 0
for i in v:
    if i != x:
        counts[x] = c
        x = i
        c = 0
    c += 1
counts[x] = c

v1 = list(counts)

sums = [0]
for i in range(1, 1005):
    try:
        sums.append(v1.index(i))
    except ValueError:
        sums.append(sums[-1])


total = 0
for i, x in enumerate(v1[:n-2]):
    if x == 1:
        continue
    for j, y in enumerate(v1[i+1:n-1]):
        total += (sums[min(1004, x+y-1)] - sums[y])


for i, c in counts.items():
    if c >= 2:
        total += (sums[min(1004, 2*i-1)])




# v3
total += sum(True if c >= 3 else False for c in counts.values())


print(total)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

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

cnt = 0
for i in range(0,N):
  for j in range(i+1,N):
    t = L[i]+L[j]
    idx = bisect_left(L,t,lo=j+1)
    print(L[i],L[j],idx)
    cnt += max(0,idx-j-1)
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
v = list(sorted(set(map(int, input().split()))))

sums = [v]

total = 0
for i, x in enumerate(v[:n-2]):
    if x == 1:
        continue
    for j, y in enumerate(v[i+1:n-1]):
        for k, z in enumerate(v[i+j+2:]):
            if z >= x + y:
                break
            total += 1



print(total)

Submission status: Wrong Answer

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


ans = 0
flag = 0
ma = 2

for left1 in range(N-2):
    for left2 in range(left1+1, N-1):
        for right in range(ma, N):
            if L[right] < L[left1] + L[left2]:
                flag = 1
                ma = right
            if L[right] >= L[left1] + L[left2]:
                if flag == 1:
                    ans += right - left2
                break
print(ans)

Submission status: Wrong Answer

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


ans = 0

print(L)
for left1 in range(N-2):
    for left2 in range(left1+1, N-1):
        for right in range(left2+1, N):
            if L[right] < L[left1] + L[left2]:
                ans += 1
            else:
                break
print(ans)

Submission status: Wrong Answer

Solution code:

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

L_sorted = sorted(L)

def bis(left, right, SrcList, value):
    if SrcList[left] >= value:
        return -1
    elif SrcList[right] < value:
        return right
    while (left + 1 < right):
        mid = (left + right) // 2
        if SrcList[mid] < value:
            left = mid
        else:
            right = mid
    return left


count = 0
for i in range(0, N-2):
    for j in range(i+1,N-1):
        r = bis(j+1, N-1, L_sorted, L_sorted[i] + L_sorted[j])
        if r != -1:
            print(i,j,r)
            count += r-j

print(count)

Submission status: Wrong Answer

Solution code:

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

L_sorted = sorted(L)

def bis(left, right, SrcList, value):
    if SrcList[left] > value:
        return -1
    elif SrcList[right] < value:
        return right
    while (left + 1 < right):
        mid = (left + right) // 2
        if SrcList[mid] < value:
            left = mid
        else:
            right = mid
    return left


count = 0
for i in range(0, N-2):
    for j in range(i+1,N-1):
        r = bis(j+1, N-1, L_sorted, L_sorted[i] + L_sorted[j])
        if r != -1:
            count += r-j

print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import bisect

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

		print(l[j],l[i],bisect.bisect_right(l,l[i]-l[j]),count)

Submission status: Wrong Answer

Solution code:
import math
from bisect import bisect_right
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):
        lg = L[i] + L[j] 
        st = L[j] - L[i] 
        m2 = bisect_left(L[j+1:],st)
        m1 = bisect_right(L[j+1:],lg)
        c += max(0,m1-m2)
print (c)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
print(l)
for i in range(n):
    for j in range(i+1,n):
        val = l[i]+l[j]
        ng = n
        ok = j
        while abs(ok - ng) > 1:
            mid = (ok+ng)//2
            if val > l[mid]:
                ok = mid
            else:
                ng = mid


        print(i,j,ok,ng)

        ans += ok - j


print(ans)

Submission status: Wrong Answer

Solution code:
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):
        val = i+j
        ng = n+1
        ok = j
        while abs(ok - ng) > 1:
            mid = (ok+ng)//2
            if val > mid:
                ok = mid
            else:
                ng = mid


        #print(ok)
        ans += ok - j


print(ans)

Submission status: Wrong Answer

Solution code:
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):
        val = i+j
        ng = j
        ok = n+1
        while ok - ng > 1:
            mid = (ok+ng)//2
            if val < mid:
                ok = mid
            else:
                ng = mid
        ans += n - ng


print(ans)

Submission status: Wrong Answer

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(i,j)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

def main():
    N=int(input())
    L=list(map(int,input().split()))
    long=[0 for i in range(0,1001)]
    for i in range(0,N):
        long[L[i]]+=1
    netlong=[]
    for i in range(1,1001):
        if long[i]>=1:
            netlong.append(i)
    X=list(accumulate(long))
    subans=[0 for i in range(0,2001)]
    ans=0
    for i in range(0,len(netlong)):
        a=netlong[i]
        for j in range(netlong[i]+1,2*netlong[i]-1):
            ans+=subans[j]*long[a]
        ans+=X[a-1]*long[a]*(long[a]-1)//2+(long[a]-1)*(long[a]-2)//6
        for j in range(0,i):
            subans[a+netlong[j]]+=long[a]*long[netlong[j]]
        subans[2*a]+=long[a]*(long[a]-1)//2
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

l = sorted( l )
cnt = 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 ]:
        break
      else:
        print( l[ i ], l[ j ], l[ k ] )
        cnt += 1
print( cnt )

Submission status: Wrong Answer

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

#a<=b<=c とする
#a,b 固定
for i in range(N):
  for j in range(i+1,N):
    a,b = L[i],L[j]
    k = bisect.bisect(L,a+b)
    count += k-j-1
 
print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
L=list(map(int,input().split()))
L.sort()
count=0
for i in range(n):
  if i!=1 and L[i]==L[i-1]:
    continue
  else:
    for j in range(i+1,n):
      if j!=i+1 and L[j]==L[i-1]:
        continue
      else:
        for k in range(j+1,n):
          if k!=j+1 and L[k]==L[k-1]:
            continue
          else:
            if L[i]+L[j]>L[k]:
              count+=1
            else:
              break
print(count)

Submission status: Wrong Answer

Solution code:
def check(a,b,c):
  if c<a+b:
    return True
  else:
    return False
n = int(input())
li = list(map(int,input().split()))
ans = 0
li.sort()
for k in range(0,n-2):
  for i in range(k+1,n-1):
    li_2 = li[i:]
    l = 0
    r = len(li_2)-1
    if check(li[k],li[i],li[-1]) == True:
      ans += len(li_2)-1
    else:
      while r<l:
        m = (r+l)//2
        if check(li[k],li[i],li_2[m]) == True:
          l=m
        else:
          r=m
      ans += len(li_2[1:l+1])
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
sticks = list(map(int, input().split()))
sticks.sort()
def binary_search(list, item):
    """
    itemの値以上を切り捨てたリストを返す
    """
    low_index = 0
    high_index = len(list) - 1

    while True:
        mid_index = (low_index + high_index) // 2
        guess = list[mid_index]
        if guess == item or (high_index - low_index) <= 1:
            return list[0: mid_index + 1]
        elif guess > item:
            high_index = mid_index
        else:
            low_index = mid_index
ans = 0
count = 0
while len(sticks) >= 3:
    c = sticks.pop()
    sticks_rest = sticks.copy()
    while len(sticks_rest) >=2 :
        b = sticks_rest.pop()
        num_of_a = binary_search(sticks_rest, c - b)
        ans += len(num_of_a)
        count += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

ans = 0

for i in range(N - 1, 1, -1):
    a = L[i]
    for ii in range(i - 1, 0, -1):
        b = L[ii]
        iii = bisect.bisect_right(L[:N - ii - 1], a - b)
        if 0 >= ii - iii:
            break
        ans += ii - iii
            
print(ans)

Submission status: Wrong Answer

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

for i in range(N-2):
    a = L[i]
    for j in range(i+1,N-1):
        b = L[j]
        c = bisect.bisect_right(L,a+b)
        ans += c-j-1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int, input().split()))
L.sort()
#print(L)
ans = 0
def find(x, arr):
  a = len(arr)//2
  cur = a
  while True:
    a //= 2
    if arr[cur] < x:
      if cur == len(arr)-1:
        return cur+1
      elif arr[cur+1] >=x:
        return cur+1
      else:
        cur += max(1, a)
    else:
      if cur == 0:
        return 0
      else:
        cur -= max(1, a)
for i in range(2, N):
  for j in range(1, i):
    ans += j - find(L[i] - L[j], L[:j])
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
nums = list(map(int, input().split(' ')))
nums.sort()
total = 0

for i in range(n-1, -1, -1):
    l = 0
    r = i - 1
    while l < r:
        if nums[l] + nums[r] > nums[i]:
            total += 1
            r -= 1
        else:
            l += 1
print(total)

Submission status: Wrong Answer

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


def solve(N: int, L: "List[int]"):
    ans = 0
    L = sorted(L,reverse=True)
    for i in range(N):
        a = L[i]
        flag = False
        for j in range(i+1,N):
            b = L[j]
            for k in range(j+1,N):
                c = L[k]
                if ( a < b + c ):
                    ans += 1
                else:
                    flag = True
                    break
            if flag:
                break
    print(ans)  

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

Solution code:
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-2):
  for j in range(i+1, n-1):
    ans += 1
print(ans)

Submission status: Wrong Answer

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

# Input
s = [input() for i in range(2)]

N = int(s[0])
S = sorted(list(s[1].split()))

# Proccess
TS = 0

for i in range(0, N):
	for j in range(i+1,N):
		for k in range(j+1,N):
			if(int(S[k]) < int(S[i]) + int(S[j])):
				TS += 1

# Output							
print(TS)

Submission status: Wrong Answer

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

print(l)

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

print(cnt)

Submission status: Wrong Answer

Solution code:
import itertools

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

# N = 2000
# L = list(range(N))

# 重複を消す
L = list(set(L))
N = len(L)

# Lを小さい順にソート
L = sorted(L)

# j=0〜max(L)について，L[i]<jを満たす最大のi: X[j] = i
X = []
for j in range(L[-1]+1):
    flag = False
    for i in range(N-1,-1,-1):
        if L[i] < j:
            X.append(i)
            flag = True
            break
    # 見つからなければ
    if not flag: X.append(None)

# j=0〜max(L)について，L[i]>jを満たす最小のi: Y[j] = i
Y = []
for j in range(L[-1]+1):
    flag = False
    for i in range(N):
        if L[i] > j:
            Y.append(i)
            flag = True
            break
    # 見つからなければ
    if not flag: Y.append(None)

#print(L)

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

        # print(b-a,a+b,Y[b-a],X[a+b])

        # cのindexの範囲 = Y[b-a]〜X[a+b]
        cmin = Y[b-a]
        if a+b <= L[-1]:
            cmax = X[a+b]
        else:
            cmax = N - 1

        # cminはjより大きい
        if cmin is not None and cmin <= j:
            cmin = j + 1

        #print(b-a,a+b,cmin,cmax)

        # if a == 2 and b == 3:
        #     print(b-a,a+b,cmin,cmax)
        #     exit()
        # print(a,b,cmin,cmax)
            
        # どちらかがNoneなら0
        if cmin is not None and cmax is not None and cmin <= cmax:
            ans += cmax - cmin + 1

        # # ind1を探す
        # # 前のind1よりも後ろを探せばよい
        # for k in range(ind1+1,N):
        #     if b-a < L[k]: ind1 = k
        # # ind2を探す
        # # 前のind2よりも前を探せばよい
        # for k in range(ind2,0,-1):
        #     if L[k] < a+b: ind2 = k

        # ans += ind2-ind1+1

print(ans)

Submission status: Wrong Answer

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

# Input
s = [input() for i in range(2)]

N = int(s[0])
S = list(s[1].split())

# Proccess
def triangle_OK(a,b,c):
	if (int(a) < int(b) + int(c)):
		if (int(b) < int(c) + int(a)):
			if (int(c) < int(a) + int(b)):
				return 1
			else:
				return 0
		else:
			return 0
	else:
		return 0

TS = 0

for i in range(0, N):
	for j in range(0,N):
		for k in range(0,N):
			if i == j or j == k or k == i:
				pass
			else:
				TS = TS + triangle_OK(S[i],S[j],S[k])
# Output
print(TS/6)

Submission status: Wrong Answer

Solution code:
N = int(input())
sticks = list(map(int, input().split()))
sticks.sort()
ans = 0
for a in range(N-2):
  A = sticks[a]
  for b in range(a+1, N-1):
    B = sticks[b]
    c = b+1
    while sticks[c] <= A + B:
      c += 1
      ans += 1
      if c == N:
        break
    
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

for i in range(N-1):
    a = L[i]
    for j in range(i+1, N):
        b = L[j]
        up_bound = bisect.bisect_right(L, a+b)
        ct += max(0, up_bound - (j+1))

print(ct)

Submission status: Wrong Answer

Solution code:
import bisect

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

for i in range(N-1):
    a = L[i]
    for j in range(i+1, N):
        b = L[j]
        up_bound = bisect.bisect_right(L, a+b)
        ct += up_bound - (j+1)

print(ct)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int, input().split()))
L = sorted(L)
lenL = len(L)
combi = set()
for i in range(lenL-2):
    a = L[i]
    for j in range(i+1, lenL-1):
        b = L[j]
        if a >= b: continue
        for k in range(j+1, lenL):
            c = L[k]
            if a < (b+c) and b < (c+a) and c < (a+b): combi.add((a,b,c))

print(len(combi))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left

N = int(input())
L = list(sorted(map(int, input().split()), key=lambda x: -x))

res = 0

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

        res += k - j - 1

print(res)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from itertools import groupby, permutations
import numpy as np



def main():
    N = int(input())
    L = np.sort(np.array([int(x) for x in input().split()]))

    ret = 0
    # print(L)
    for i in range(N-2):
        for j in range(i + 1, N-1):
            k = np.searchsorted(L, L[i] + L[j], side='right')
            # print(L[i], L[j], k, ret)
            ret += k - j - 1
    return ret


if __name__ == "__main__":
    print(main())

Submission status: Wrong Answer

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

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

count = 0
for i in range(n-3):
    for j in range(i+1, n-2):
        # j+1~n-1まで二分探索
        left = j+1
        right = n-1
        # print(left, right)
        while left <= right:
            mid = (left + right) // 2
            if l[i] + l[j] > l[mid] and l[j] + l[mid] > l[i] and l[mid] + l[i] > l[j]:
                left = mid + 1
                satisfied = mid
            else:
                right = mid - 1
        # print(j+1, right)
        count += right - j+1

print(count)

Submission status: Wrong Answer

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

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

count = 0
for i in range(n-3):
    for j in range(i+1, n-2):
        # j+1~n-1まで二分探索
        left = j+1
        right = n-1
        # print(left, right)
        while left <= right:
            mid = (left + right) // 2
            if l[i] + l[j] > l[mid] and l[j] + l[mid] > l[i] and l[mid] + l[i] > l[j]:
                left = mid + 1
                satisfied = mid
            else:
                right = mid - 1
        # print(j+1, right)
        count += right - j+1

print(count)

Submission status: Wrong Answer

Solution code:
from math import *
import sys

sys.stdin.readline()
L = list(set(list(map(int, sys.stdin.readline().split()))))
L = sorted(L)
l = len(L)


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

print(count)

Submission status: Wrong Answer

Solution code:
input()
L = list(set(list(map(int, input().split()))))
l = len(L)

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

print(count)

Submission status: Wrong Answer

Solution code:
input()
L = list(set(list(map(int, input().split()))))
l = len(L)

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

print(count)

Submission status: Wrong Answer

Solution code:
import bisect

N = int(input())
L = sorted(list(map(int,input().split())))
ans = 0
for bi in range(1,N-1):
  for ai in range(0,bi):
    ci = bisect.bisect(L,L[bi]+L[ai]) - 1
    if bi<ci:
      ans += ci-bi
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = (sorted(list(map(int, input().split()))))
l.reverse()
ans = 0
i = 0
#print(l)
while i+2 < n:
    if l[i] < l[i+1] + l[i+2]:
        ans+=1
        i+=3
    else: i+=1
print(ans)

Submission status: Wrong Answer

Solution code:
def lower_bound(lst: list, start: int, bound: int) -> int:
  
    def recur(low: int, high: int) -> int:
        if low < high:
            middle_index = (low + high) // 2
            middle = lst[middle_index]

            if lst[low] <= middle <= lst[high]:
                return middle_index
            elif middle < bound:
                return recur(middle_index + 1, high)
            else:
                return recur(low, middle_index - 1)

    result = recur(0, len(lst) - 1)
    if result is not None:
        return result
    else:
        return start - 1


def triangles(lst: list) -> int:
    sorted_list = sorted(lst)
    size = len(sorted_list)
    ans = 0
    for i in range(size):
        for j in range(i + 1, size):
            ab = sorted_list[i] + sorted_list[j]
            ans += j - lower_bound(sorted_list[j + 1:], j + 1, ab)
    return ans


if __name__ == '__main__':
    N = int(input())
    ls = list(map(int, input().split()))
    print(triangles(ls))

Submission status: Wrong Answer

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

import bisect
ans = 0
for i in range(1,N):
  for j in range(i):
    index = bisect.bisect(L,L[i]+L[j])
    if index <= i+1:
      continue
    ans += len(L[i+1:index])
    
print(ans)

Submission status: Wrong Answer

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

import bisect
ans = 0
for i in range(1,N):
  for j in range(i):
    index = bisect.bisect(L,L[i]+L[j])
    if index <= i+1:
      continue
    ans += len(L[i+1:index])
    
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    ll = input().split(" ")
    ll.sort()
    ll.reverse()
    L = []
    for l in ll:
        L.append(int(l))
    result = 0
    tmp = []
    for i in range(N):
        for j in range(i + 1, N):
            a = L[i]
            b = L[j]
            if a == b:
                continue
            tmp = list(set(L[j + 1:]))
            for k in range(len(tmp)):
                c = tmp[k]
                if (a < b + c) and (b < a + c) and (c < a + b):
                    result += 1
    print(result)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
l = list(map(int, input().split()))
l.sort()

acc = [0]*(10**3+1)
idx = 0
for i in range(10**3):
    if idx<n and l[idx]==i:
        acc[i] += 1
        idx += 1
    acc[i+1] += acc[i]

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

print(res)

Submission status: Wrong Answer

Solution code:
import math
import fractions

def makeDivisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def bitlist(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def makeNeighbor(es,n):
    nlist = [[] for i in range(n+1)]
    for i in es:
        nlist[i[0]].append(i[1])
        nlist[i[1]].append(i[0])
    return nlist
    
#a = list(map(int, input().split()))
# --------------------------------------------

n = int(input())
a = list(map(int, input().split()))
a.sort()
ct = [0 for i in range(1001)]
more = [0 for i in range(2001)]
dab = [0 for i in range(n)]
for i in range(n):
    ct[a[i]]+=1
    if(i>=1):
        if(a[i-1]==a[i]):
            dab[i]=dab[i-1]+1
for i in range(1000):
    more[1001-2-i] = more[1001-1-i] + ct[1001-2-i]
ans = 0
for i in range(n-1):
    for j in range(n-i-1):
        ans += more[a[i+j+1]+1] - more[a[i]+a[i+j+1]]
        ans += max(0, ct[a[i+j+1]]-dab[i+j+1]-1)
        #print(i,j,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import fractions

def makeDivisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def bitlist(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def makeNeighbor(es,n):
    nlist = [[] for i in range(n+1)]
    for i in es:
        nlist[i[0]].append(i[1])
        nlist[i[1]].append(i[0])
    return nlist
    
#a = list(map(int, input().split()))
# --------------------------------------------

n = int(input())
a = list(map(int, input().split()))
a.sort()
ct = [0 for i in range(1001)]
more = [0 for i in range(2001)]
dab = [0 for i in range(n)]
for i in range(n):
    ct[a[i]]+=1
    if(i>=1):
        if(a[i-1]==a[i]):
            dab[i]=dab[i-1]+1
for i in range(1000):
    more[1001-2-i] = more[1001-1-i] + ct[1001-2-i]
ans = 0
for i in range(n-1):
    for j in range(n-i-1):
        ans += more[a[i+j+1]+1] - more[a[i]+a[i+j+1]]
        ans += max(0, ct[a[i+j+1]]-dab[i+j+1]-1)
        print(i,j,ans)
print(ans)

Submission status: Wrong Answer

Solution code:

import math
import fractions

def makeDivisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def bitlist(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def makeNeighbor(es,n):
    nlist = [[] for i in range(n+1)]
    for i in es:
        nlist[i[0]].append(i[1])
        nlist[i[1]].append(i[0])
    return nlist
    
#a = list(map(int, input().split()))
# --------------------------------------------

n = int(input())
a = list(map(int, input().split()))
a.sort()
more = [0 for i in range(2001)]
for i in range(n):
    more[a[i]-1]+=1
for i in range(1000):
    more[1001-2-i] += more[1001-1-i]
ans = 0
for i in range(n-1):
    for j in range(n-i-1):
        ans+= more[a[i+j+1]+1] - more[a[i]+a[i+j+1]]
        #print(i,j,ans)
print(ans)

Submission status: Wrong Answer

Solution code:

import math
import fractions

def makeDivisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def bitlist(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def makeNeighbor(es,n):
    nlist = [[] for i in range(n+1)]
    for i in es:
        nlist[i[0]].append(i[1])
        nlist[i[1]].append(i[0])
    return nlist
    
#a = list(map(int, input().split()))
# --------------------------------------------

n = int(input())
a = list(map(int, input().split()))
a.sort()
more = [0 for i in range(2001)]
for i in range(n):
    more[a[i]]+=1
for i in range(1000):
    more[1001-2-i] += more[1001-1-i]
ans = 0
for i in range(n-1):
    for j in range(n-i-1):
        ans+= more[a[i+j+1]+1] - more[a[i]+a[i+j+1]]
        #print(i,j,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

N = int(input())
L = [int(l) for l in input().split()]
L.sort()
S = 0
n = 0
print(L)
S = 0
for i in range(N - 2):
    # print("i=", i)
    for j in range(i + 1, N - 1):
        # print("j=",j)
        l = L[i] + L[j]
        s = bisect.bisect_left(L, l)
        # print(s)
        S += bisect.bisect_left(L, L[i] + L[j]) - j - 1

print(S)

Submission status: Wrong Answer

Solution code:
import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for i in range(N - 2):
    L_i = L[i]
    for j in range(i + 1, N - 1):
        L_j = L[j]
        x_left = abs(L_i - L_j)
        x_right = L_i + L_j
        x = min(N, bisect.bisect_right(L, x_right)) - max(j + 1,bisect.bisect_left(L, x_left))
        print(x)
        if x <= 0:
            break
        ans += x
print(ans)

Submission status: Wrong Answer

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):
        index = bisect.bisect_right(l,l[i]+l[j],j)
       
        count += index-j-1
print(count)

Submission status: Wrong Answer

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