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:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

N = inputI()
L = inputIL()

L.sort()

aa = [0 for i in range(1001)]
for l in L:
  aa[l] += 1

s = list(set(L))
s.sort()

count = 0
for a in range(len(s)):
  for b in range(a,len(s)):
    for c in range(b,len(s)):
      if s[a]+s[b]>s[c] and s[b]+s[c]>s[a] and s[c]+s[a]>s[b] :
        count += aa[s[a]]*aa[s[b]]*aa[s[c]]
print(count)

Submission status: Wrong Answer

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

N = inputI()
L = inputIL()

L.sort()

aa = [0 for i in range(1001)]
for l in L:
  aa[l] += 1

s = list(set(L))
s.sort()

count = 0
for a in range(len(s)):
  for b in range(a+1,len(s)):
    for c in range(b+1,len(s)):
      if s[a]+s[b]>s[c] and s[b]+s[c]>s[a] and s[c]+s[a]>s[b] :
        count += aa[s[a]]*aa[s[b]]*aa[s[c]]
print(count)

Submission status: Wrong Answer

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

N = inputI()
L = inputIL()

L.sort()

aa = [0 for i in range(1001)]
for l in L:
  aa[l] += 1

s = list(set(L))
s.sort()

count = 0
for a in range(len(s)):
  for b in range(a+1,len(s)):
    for c in range(b+1,len(s)):
      if s[a]+s[b]>s[c]:
        count += aa[s[a]]*aa[s[b]]*aa[s[c]]
print(count)

Submission status: Wrong Answer

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

N = inputI()
L = inputIL()

L.sort()

a = [0 for i in range(2010)]
for l in L:
  a[l] += 1


count = 0
for l in L:
  for q in L:
    if q>l:
      count += sum(a[q+1:q+l])
    
print(count)

Submission status: Wrong Answer

Solution code:
import itertools

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

l.sort()

nag = [0]*(10**3+1)

for i in range(n):
    nag[l[i]]+=1
#print(nag[:10])

res = 0
for ai in range(1,max(l)-1):
    for bi in range(ai+1,max(l)):
        if nag[ai] == 0 or nag[bi] == 0:
            continue
        cmax =  ai+bi-1
        cmin = bi+1
        if (cmin <= cmax) :
#            print(ai,bi,cmin,cmax,nag[cmin:cmax+1])
            res += sum(nag[cmin:cmax+1])

print(res)

Submission status: Wrong Answer

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools

# import copy
# import heapq
# from collections import deque
# import decimal

# sys.setrecursionlimit(100001)
INF = sys.maxsize
# MOD = 10 ** 9 + 7

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


# ===CODE===


def main():
    n = ni()
    d = na()

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
l = [int(_) for _ in input().split()]

M = 10**3+1

le = [0] * M
for i in range(n):
    le[l[i]] += 1

ans = 0
for a in range(1, M-3):
    for b in range(a+1, M-2):
        for c in range(b+1, min(a+b, M-1)):
            ans += (le[a] * le[b] * le[c])
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = [int(_) for _ in input().split()]

M = 10**3+1

le = [0] * M
for i in range(n):
    le[l[i]] += 1

ans = 0
for a in range(1, M-2):
    for b in range(a+1, M-1):
        for c in range(b+1, min(a+b, M)):
            ans += (le[a] * le[b] * le[c])
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = [int(_) for _ in input().split()]

M = 10**3+1

le = [0] * M
for i in range(n):
    le[l[i]] += 1

ans = 0
for a in range(1, M-2):
    for b in range(a, M-1):
        for c in range(b, min(a+b, M)):
            ans += (le[a] * le[b] * le[c])
print(ans)

Submission status: Wrong Answer

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

def binary_search3(A,x):
    OK=-1
    NG=len(A)
    DIV=(OK+NG)//2
    while OK+1<NG:
        if x>=A[DIV]:
            OK=DIV
        else:
            NG=DIV
        DIV=(OK+NG)//2
    return OK 

count=0
for a in range(len(L)):
    for b in range(a+1,len(L)):
        MAX=binary_search3(sort_L,L[a]+L[b]-1)
        MIN=binary_search3(sort_L,max(L[a],L[b]))
        print(L[a],L[b],MIN,MAX)
        count+=max(0,MAX-MIN)

print(count)

Submission status: Wrong Answer

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

def binary_search2(A,n):
    find=False
    start=0
    end=len(A)
    if A[0]==n:
        find=True
    else:
        while find==False and start<end-1:
            middle=(start+end)//2
            if n>A[middle]:
                start=middle
            elif n<A[middle]:
                end=middle
            else:
                find=True
    if find==True:
        value=A.index(n)
    else:
        value=start
    return value

count=0
for a in range(len(L)):
    for b in range(a+1,len(L)):
        MAX=binary_search2(sort_L,L[a]+L[b]-1)
        MIN=binary_search2(sort_L,max(L[a],L[b]))
        print(L[a],L[b],MAX,MIN)
        count+=max(0,MAX-MIN)

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):
  for j in range(i+1,N):
    summation = L[i] + L[j]
    subtract  = L[j] - L[i]
    D=L.copy()
    E=[ D[k] for k in range(N) if k!= i and k!= j ]
    ##summationより小さいかを二分探索
    high = N-3
    low = 0
    if E[0] >= summation:
      upper = 0 #1つも満たさない
      #print(L[i],L[j],E,upper)
    elif E[-1] < summation:
      upper = N-2 #N-2個すべてOK
    else:
      while high > low+1:
        mid =(high + low) // 2
        if E[mid] >= summation: #大きすぎ
          high = mid
          # mid = low
        else: #小さすぎかちょうど
          low = mid
          # mid =high 
      upper = high
    #print("summation",summation,E,upper)
    ##subtractより大きいかを二分探索
    high = N-3
    low = 0
    if E[0] > subtract:
      lower = 0 #N-2個すべてOK
    elif E[-1] <= subtract:
      lower = N-2 #1つも満たさない
    else:
      while high > low+1:
        mid =(high + low) // 2
        if E[mid] > subtract: #大きすぎ
          high = mid
          # mid = low
        else: #小さすぎかちょうど
          low = mid 
          # mid =high 
      lower = high
    #print("subtract",subtract,E,lower)
    count += upper - lower
print(count/3)

Submission status: Wrong Answer

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

# N = int(input())
# L = sorted(list(map(int, input().split())))
A = readlines()
print(A)
exit()


def f(v):
    left = 0
    right = N
    while left + 1 < right:
        x = (left + right) // 2
        if v > L[x]:
            left = x
        else:
            right = x
    return right


ans = 0
for i in range(N):
    for j in range(i + 1, N):
        idx = f(L[i] + L[j])
        # print('a, b, idx', a, b, idx)
        # idx = np.searchsorted(L, a + b, sorter=None)
        ans += idx - 1 - j
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations
n = int(input())
l_ls = list(map(int, input().split()))
l_ls.sort()

def isOK(index, key, l):
    if l[index] >= key:
        return True
    else:
        return False
def meguru_search(l, key):
    '''
    key以上の値を持つ最小のインデックスを返す
    '''
    left = -1
    right = len(l)

    while (right - left) > 1:
        mid = left + (right - left) // 2
        # 直感的に記憶　
            # 「含まれている」んだったら「含むを許容（等号付き）する右側」はmidまで動かして問題ない
                # 新しいrightの位置がkeyの位置だとしても、「含む」は許容されてる
            # 「含まれていない」んだったら「含まない(<)左の壁」をそこまで動かしてもkeyを通り過ぎることはない
        if isOK(mid, key, l):
            right = mid
        else:
            left = mid
    
    # whileを抜ける時、rightとleftは一個差で、かつkeyがrightの位置にある
    return right

count = 0
for a,b in combinations(l_ls,2):
    diff = abs(a-b)
    Sum = a + b
    Max = Sum - 1
    Min = diff + 1
    maxind = meguru_search(l_ls, Max)
    minind = meguru_search(l_ls, Min)
    count += maxind - minind + 1
    if a > diff and a < Sum:
            count -= 1
    if b > diff and a < Sum:
            count -= 1
print(count)

Submission status: Wrong Answer

Solution code:
import bisect
n = int(input())
l = [int(x) for x in input().split()]

l.sort()

ans = 0
for a in range(n - 1):
    for b in range(a + 1, n):
        c = bisect.bisect(l, l[a] + l[b])
        if b < c:
            ans += c - (b + 1)

print(ans)

Submission status: Wrong Answer

Solution code:
def binary_search(l, x):
    low = 0
    high = len(l) - 1
    while low != high:
        middle = (low + high) // 2
        if l[middle] < x and x <= l[middle + 1]:
            return middle + 1
        elif x < l[middle]:
            high = middle
        else:
            low = middle + 1
        
    if low == 0:
        return 0
    else:
        return high + 1

n = int(input())
l = [int(x) for x in input().split()]

l.sort()

ans = 0
for a in range(n - 1):
    for b in range(a + 1, n):
        num = binary_search(l, l[a] + l[b])
        if 0 < num - (b + 1):
            #print(l[a], l[b], num)
            ans += num - (b + 1)

print(ans)

Submission status: Wrong Answer

Solution code:
def binary_search(l, x):
    low = 0
    high = len(l) - 1
    while low != high:
        middle = (low + high) // 2
        if l[middle] < x and x <= l[middle + 1]:
            return middle + 1
        elif x < l[middle]:
            high = middle
        else:
            low = middle + 1
        
    if x < l[low]:
        return low
    else:
        return low + 1

n = int(input())
l = [int(x) for x in input().split()]

l.sort()

ans = 0
for a in range(n - 1):
    for b in range(a + 1, n):
        num = binary_search(l, l[a] + l[b])
        if 0 < num - (b + 1):
            #print(a, b, num)
            ans += num - (b + 1)

print(ans)

Submission status: Wrong Answer

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


ml = max(l)
s = [0 for _ in range(ml+1)]

d = []

for r in l:
    s[r] += 1
    if(r not in d):d.append(r)
d.sort()
c = 0

for i in d:
    x = s[i]
    #3
    if(x >= 3):
        c += x * (x-1) * (x-2) / 3 / 2
    #2
    if(x >= 2):
        t = x * (x-1) / 2
        for j in d:
            if(i <= j):continue
            if(j >= 2*x):break
            c += t * s[j]

    #1
    if(x >= 1):
        t = x
        for j in d:
            if(i <= j):continue
            y = s[j]
            t = x * y
            for k in d:
                if(i <= k or j <= k):continue
                if(k >= i+j):break
                if(k <= abs(i-j)):continue
                c += t * s[k]
print(c)

Submission status: Wrong Answer

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


ml = max(l)
s = [0 for _ in range(ml+1)]

d = []

for r in l:
    s[r] += 1
    if(r not in d):d.append(r)

c = 0

for i in d:
    x = s[i]
    #3
    if(x >= 3):
        c += x * (x-1) * (x-2) / 3 / 2
    #2
    if(x >= 2):
        t = x * (x-1) / 2
        for j in d:
            if(i <= j):continue
            if(j >= 2*x):break
            c += t * s[j]

    #1
    if(x >= 1):
        t = x
        for j in d:
            if(i <= j):continue
            y = s[j]
            t = x * y
            for k in d:
                if(i <= k or j <= k):continue
                if(k >= i+j):break
                if(k <= abs(i-j)):continue
                c += t * s[k]
print(c)

Submission status: Wrong Answer

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

Edges=sorted(Edges)
cnt =0
# for i in c:
#     e1=(i[1] + i[2])
#     e2=(i[0] + i[2])
#     e3=(i[0] + i[1])

#     if i[0] < e1 and i[1] < e2 and i[2] < e3:
#         cnt+=1
print(Edges)

import bisect

for a in range(N-2):
    for b in range(a+1,N-1):
        ab = Edges[a] + Edges[b]

        idx = bisect.bisect_left(Edges,ab)

        cnt += max(0,idx-b-1)


print(cnt)

Submission status: Wrong Answer

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

for i in range(N-1):
    l = 0
    r = i -1
    while(l< r):
        if (A[l] + A[r] >A[i]):
            count += (r-l)
            r -= 1
        else:
            l += 1
print(count)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc143/tasks/abc143_d

import bisect

N = int(input())
L_list = list(map(int, input().split()))
L_list = sorted(L_list)
count = 0

for a in range(N-2):
    for b in range(a + 1, N-1):
        c_min = max(L_list[a] - L_list[b], L_list[b] - L_list[a], L_list[b])
        c_max = L_list[a] + L_list[b]
        count_c = bisect.bisect_right(L_list, c_max) - bisect.bisect_right(L_list, c_min)
        count += count_c

print(count)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
import collections
import itertools
import bisect
mod = 10 ** 9 + 7
N = int(input())
L = list(map(int, input().split()))
L = sorted(L)
print(L)
count = 0
for i in range(len(L)):
    for j in range(i+1, len(L)):
        a = L[i]
        b = L[j]
        idx = insert_index = bisect.bisect_left(L, a+b)
        count += max(0, idx-j-1)


print(count)

Submission status: Wrong Answer

Solution code:
def binary_search(list_a, item):
    low = 0
    high = len(list_a) - 1
    while low != high:
        middle = (low + high) // 2
        if list_a[middle] < item and item <= list_a[middle + 1]:
            return middle
        
        if item < list_a[middle]:
            high = middle
        else:
            low = middle + 1
        
    if middle == 0:
        return -1
    else:
        return len(list_a) - 1
    
n = int(input())
l = [int(x) for x in input().split()]

l.sort()

ans = 0
for a in range(n - 1):
    for b in range(a + 1, n):
        c = binary_search(l, l[a] + l[b])
        
        if c < 0:
            continue
        
        ans += c - b
        
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

SUM = 0
for i,a in enumerate(A):
    for j,b in enumerate(A[i+1:]):
        print(a,b,A[j+i+2:])
        print(bisect.bisect_left(A[i+j+2:], a+b))
        SUM += bisect.bisect_left(A[i+j+2:], a+b)

print(SUM)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

A = [0] * 2010 #どのサイズの木があるか

B = [0] * 2010 #indexより小さい木はいくつあるか

for i in L:
    A[i] += 1

t = 0
for i in range(2010):
    if A[i] != 0:
        B[i] = t
        t += 1
    else:
        B[i] = t
# print(A, B)
ans = 0
for i in range(1, len(A)):
    if A[i] == 0:
        continue
    if A[i] > 1:
        a = 0
    else:
        a = 1
    for j in range(i + a, len(A)):
        if A[j] == 0:
            continue
        # print(i + j, max(i, j), end=" ")
        ans += (B[i + j] - B[max(i, j) + 1])
        # print(B[i + j] - B[max(i, j) + 1])
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

A = [0] * 2010 #どのサイズの木があるか

B = [0] * 2010 #indexより小さい木はいくつあるか

for i in L:
    A[i] += 1

t = 0
for i in range(2010):
    if A[i] != 0:
        B[i] = t
        t += 1
    else:
        B[i] = t
# print(A, B)
ans = 0
for i in range(1, len(A)):
    if A[i] == 0:
        continue
    if A[i] > 1:
        a = 0
    else:
        a = 1
    for j in range(i + a, len(A)):
        if A[j] == 0:
            continue
        # print(i + j, max(i, j), end=" ")
        ans += (B[i + j] - B[max(i, j)] - 1)
        # print(B[i + j] - B[max(i, j) + 1])
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

A = [0] * 2010 #どのサイズの木があるか

B = [0] * 2010 #indexより小さい木はいくつあるか

for i in L:
    A[i] += 1

t = 0
for i in range(2010):
    if A[i] != 0:
        B[i] = t
        t += 1
    else:
        B[i] = t
# print(A, B)
ans = 0
for i in range(1, len(A)):
    if A[i] == 0:
        continue
    if A[i] > 1:
        a = 0
    else:
        a = 1
    for j in range(i + a, len(A)):
        if A[j] == 0:
            continue
        # print(i + j, max(i, j), end=" ")
        ans += (B[i + j] - B[max(i, j)] - 1)
        # print(B[i + j] - B[max(i, j)] - 1)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
l = list(map(int,input().split()))
r = [0 for _ in range(1001)]
for i in range(n):
    r[l[i]] += 1
f,g = [],[]
for i in range(len(r)):
    if r[i]:
        f.append(i)
        g.append(r[i])
count = 0
t = list(map(list,itertools.combinations([i for i in range(len(f))],3)))
for i in range(len(t)):
    if f[t[i][0]] < (f[t[i][1]] + f[t[i][2]]) and f[t[i][1]] < (f[t[i][0]] + f[t[i][2]]) and f[t[i][2]] < (f[t[i][0]] + f[t[i][1]]):
        count += (g[t[i][0]] * g[t[i][1]] * g[t[i][2]])
print(count)

Submission status: Wrong Answer

Solution code:
import itertools
n = int(input())
l = list(map(int,input().split()))
r = [0 for _ in range(1001)]
for i in range(n):
    r[l[i]] += 1
f,g = [],[]
for i in range(len(r)):
    if r[i]:
        f.append(i)
        g.append(r[i])
count = 0
t = list(map(list,itertools.combinations(f,3)))
for i in range(len(t)):
    if t[i][0] < (t[i][1] + t[i][2]) and t[i][1] < (t[i][0] + t[i][2]) and t[i][2] < (t[i][0] + t[i][1]):
        count += (g[f.index(t[i][0])] * g[f.index(t[i][1])] * g[f.index(t[i][2])])
print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
L = list(map(int,input().split()))
cnt = 0
L.sort()
L = L[::-1]

def nibutan(L,i,j,N):
    lower = N-1
    upper = j
    middle = int(np.round(upper+lower)/2)
    if upper> lower:
        return 0

    for cnt in range(11):
        if L[i] < L[j] + L[middle]:
            upper = middle
            middle = int(np.round((upper+lower)/2))
        else:
            lower = middle
            middle = int(np.round((upper+lower)/2))-1
#         print(middle)
    return max([middle - j,0])

# print('L',L)
for i in range(N):
    for j in range(i+1,N):
#         print('test',L[i],L[j])
        cnt += nibutan(L,i,j,N)
print(cnt)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
L = list(map(int,input().split()))
cnt = 0
L.sort()
L = L[::-1]

def nibutan(L,i,j,N):
    lower = N-1
    upper = j
    middle = int(np.round(upper+lower)/2)
    if upper> lower:
        return 0

    for cnt in range(16):
        if L[i] < L[j] + L[middle]:
            upper = middle
            middle = int(np.round((upper+lower)/2))
        else:
            lower = middle
            middle = int(np.round((upper+lower)/2))-1
#         print(middle)
    return max([middle - j,0])

# print('L',L)
for i in range(N):
    for j in range(i+1,N):
#         print('test',L[i],L[j])
        cnt += nibutan(L,i,j,N)
print(cnt)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
L = list(map(int,input().split()))
cnt = 0
L.sort()
L = L[::-1]

def nibutan(L,i,j,N):
    lower = N-1
    upper = j+1
    middle = int(np.round(upper+lower)/2)
    if upper> lower:
        return 0

    for cnt in range(16):
        if L[i] < L[j] + L[middle]:
            upper = middle
            middle = int(np.round((upper+lower)/2))
        else:
            lower = middle
            middle = int(np.round((upper+lower)/2))-1
#         print(middle)
    return max([middle - j,0])

# print('L',L)
for i in range(N):
    for j in range(i+1,N):
#         print('test',L[i],L[j])
        cnt += nibutan(L,i,j,N)
print(cnt)

Submission status: Wrong Answer

Solution code:
from copy import copy
from collections import deque
import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort()
first = deque(L)
count = 0
for i in range(N-2):
    l = first.pop()
    second = copy(first)
    for j in range(1,N-i-1):
        m = second.pop()
        n = len(second) - bisect.bisect_left(second,l-m)
        if n > 0:
            count += n
        else:
            break
print(count)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(input().split())
L=[]
for a in A:
  L.append(int(a))
L.sort(reverse=True)
count=0
for i in range(N):
  for j in range(1,N):
    for k in range(2,N):
      if i<j and j<k:
        a=L[i]
        b=L[j]
        c=L[k]
        cos=((b**2)+(c**2)-(a**2))/(2*b*c)
        if -1<=cos<=1:
          count+=1
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)
L_ = []
for i in range(N):
    L_ += [L[i]*(10**3) + i]
# length_list[m] m 以下の長さの辺の個数
length_list = [0] * (2* 10**6 + 1)
for i in range(N):
    length_list[L_[i]] += 1
for i in range(2* 10**6):
    length_list[i+1] += length_list[i]
    
ans = 0
for i in range(N):
    for j in range(i+1,N):
        ans += max(0, length_list[L_[j]-1] - length_list[(L_[i]//1000- L_[j]//1000) *1000+999] )
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)
L_ = []
for i in range(N):
    L_ += [L[i]*(10**3) + i]
# length_list[m] m 以下の長さの辺の個数
length_list = [0] * 10**7
for i in range(N):
    length_list[L_[i]] += 1
for i in range(10**7-1):
    length_list[i+1] += length_list[i]
    
ans = 0
for i in range(N):
    for j in range(i+1,N):
        ans += max(0, length_list[L_[j]-1] - length_list[L_[i]- L_[j]-1000] )
print(ans)

Submission status: Wrong Answer

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

import bisect
L=sorted(L)
comb = 0
for a in range(N-2):
    for b in range(1+a,N-1):
        posIndex = bisect.bisect_left(L, L[a] + L[b])
        comb += N - posIndex if posIndex != 0 else 0
print(comb)

Submission status: Wrong Answer

Solution code:
import bisect

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

L = sorted(L)

for c in reversed(range(2, N)):
	for b in reversed(range(1, c)):
		a = bisect.bisect_left(L, L[c]-L[b])
		if a<b:
			s += b-a
print(s)

Submission status: Wrong Answer

Solution code:
import bisect

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

L = sorted(L)
print(L)
for c in reversed(range(2, N)):
	for b in reversed(range(1, c)):
		a = bisect.bisect_left(L, L[c]-L[b])
		if a<b:
			s += b-a
print(s)