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())
Ls = sorted(list(map(int, input().split())))

cum_sum = [0] * (Ls[-1]+1)
ind = 0
cnt = 0
for i in range(Ls[-1]+1):
    if Ls[ind] == i:
        cnt += 1
        ind += 1
    cum_sum[i] = cnt
    
maxi = len(cum_sum)-1
cnt = 0

for i in range(N):
    l1 = Ls[i]
    prevcnt = 0
    for j in range(i+1, N):
        l2 = Ls[j]
        if Ls[j] == Ls[j-1]:
            prevcnt += 1
        else:
            prencnt = 0
        delta = max(0, cum_sum[min(maxi,l1+l2-1)]) - cum_sum[l2]
        cnt += delta + prevcnt
print(cnt)

Submission status: Wrong Answer

Solution code:
def judge(a, b, c):
    if(a < b + c):
        if(b < c + a):
            if(c < a + b):
                return 1
    return 0

n = int(input())
l = [int(e) for e in input().split(" ")]
i = 0; j = 1; k = 0; ans = 0;
while(i < n):
    j = i + 1
    k = j + 1
    while(j < n):
        while(k < n):
            ans += judge(l[i], l[j], l[k])
            #print(f"ans={ans}, l[{i}]={l[i]}, l[{j}]={l[j]}, l[{k}]={l[k]}")
            k += 1
        j += 1
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted(map(int, input().split()))
r = 0
for i in range(2, n):
    j, k = 0, i - 1
    while j <= k:
        while j <= k and a[j] + a[k] > a[i]: k -= 1
        r += i - k - 1
        j += 1
print(r)

Submission status: Wrong Answer

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

import sys
from bisect import bisect_left

n = int(sys.stdin.readline().strip())
l = [ int(x) for x in sys.stdin.readline().strip().split() ]

c = 0
for i in range(1,n):
    for j in range(i+1,n):
        k = bisect_left(l,l[j]-l[i]+1,hi=i)
        if k==i: continue
        c += i-k
#
print(c)

Submission status: Wrong Answer

Solution code:
N = int(input())
nums = [int(i) for i in input().split()]
nums.sort()
ans = 0
print(nums)

for i in range(N-1,1,-1):
  for j in range(i-1,0,-1):
    point = int(j/2)
    f = -1
    if nums[point]+nums[j]-nums[i] >= 0:
      f = 1
    while True:
      s = nums[point]+nums[j]-nums[i]
      if s <= 0:
        if f == 1 or point == j:
          ans += j-point-1
          if point == j:
            ans+=1
          break
        else:
          point += 1
      else:
        if f == -1 or point == 0:
          ans += j-point
          break
        else:
          point -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted(map(int, input().split()))
r = 0
for i in range(2, n):
    j, k = 0, i - 1
    while j < k:
        while j < k and a[j] + a[k] > a[i]: k -= 1
        r += i - k - 1
        j += 1
print(r)

Submission status: Wrong Answer

Solution code:
import bisect

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

L=sorted(L)

#重複削除用探索リスト
setL = sorted(list(set(L)))

sankaku_dict={}
count=0
for i in range(len(L)-1):
    low=L[i]
    for j in range(i+1, len(L)):
        high = L[j]
        
        #下限のインデックスを取得
        low_index = bisect.bisect_right(L, high)
        
        # 2本の棒の和よりも小さい棒の数を探索
        high_index = bisect.bisect_left(L, high+low)
        count+=max(high_index-low_index, 0)

print(count)

Submission status: Wrong Answer

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

cum_sum = [0] * (Ls[-1]+1)
ind = 0
cnt = 0
for i in range(Ls[-1]+1):
    if Ls[ind] == i:
        cnt += 1
        ind += 1
    cum_sum[i] = cnt
    
maxi = len(cum_sum)-1
cnt = 0
for i in range(N):
    l1 = Ls[i]
    for j in range(i+1, N):
        l2 = Ls[j]
        delta = max(0, cum_sum[min(maxi,l1+l2-1)]) - cum_sum[l2]
        cnt += delta
print(cnt)

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

Submission status: Wrong Answer

Solution code:
import bisect

n = int(input())
l = sorted(map(int, input().split())) + [float('INF')]

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted(map(int, input().split()))
r = 0
for i in range(2, n):
    j, k = 0, i
    while j < k:
        while j < k and a[j] + a[k] > a[i]: k -= 1
        r += i - k - 1
        j += 1
print(r)

Submission status: Wrong Answer

Solution code:
import copy
import bisect

def main(N, L):
    L.sort(reverse=True)

    triangles = 0

    for i, P in enumerate(L):
        Q_list = copy.copy(L)
        del Q_list[:i + 1]
        for j, Q in enumerate(Q_list):
            # if Q < P / 2:
            #     break
            minR = P - Q + 1
            R_list = copy.copy(Q_list)
            del R_list[:j + 1]
            triangles += bisect.bisect_right(R_list, minR)
            
            # for R in R_list:
            #     if R >= minR:
            #         triangles += 1
            #     else:
            #         break

    print(triangles)

if __name__ == '__main__':
    N = int(input())
    L = list(map(int, input().split()))
    main(N, L)

Submission status: Wrong Answer

Solution code:
import bisect

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

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

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
#import math
#import time
#import random  # randome is not available at Codeforces
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
#eps=10**(-10)
#ts=time.time()
#sys.setrecursionlimit(10**6)
input=lambda: sys.stdin.readline().rstrip()

show_flg=False
show_flg=True

n=I()
a=sorted(LI())[::-1]

s=set()
for i in a:
    s.add(i)
s=sorted(list(s))[::-1]
L=1080
#L=10000
k=[0]*(L)
ak=[0]
ans=0

for i in a:
    k[i]+=1
for i in range(L):
    ak.append(ak[-1]+k[i])

un=len(s)
for i in range(un):
    c=s[i]
    if k[c]>=3:
        ans+=k[c]*(k[c]-1)*(k[c]-2)//6
    if k[c]>=2:
        ans+=ak[c]*k[c]*(k[c]-1)//2
    for j in range(i+1,un):
        #show('i,j,s[i],s[j],mm',i,j,'|',s[i],s[j],s[i]-s[j]+1,'|',ak[s[i]-s[j]],ans)
        
        sc=s[j]
        if ak[sc]==0:
            break
        mm=c-sc+1
        if mm<0:
            continue
        elif mm>sc:
            continue # break
        else:
            ans+=k[c]*k[sc]*((1 if 2*k[sc]>k[c] else 0)*(k[sc]-1)//2+ak[sc]-ak[mm])
        #show('ans+=',k[sc]*(k[sc]-1)//2+ak[sc]-ak[mm])
    
print(ans)

Submission status: Wrong Answer

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

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

def one_str():
    return input()

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

N=one_int()
L=many_int()

L=sorted(L)

#重複削除用探索
setL = sorted(list(set(L)))

sankaku_dict={}
count=0
for i in range(len(L)-1):
    low=L[i]
    for j in range(i+1, len(L)):
        high = L[j]
        
        #上限を取得
        low_index = bisect.bisect_right(setL, high)

        high_index = bisect.bisect_left(setL, high+low)
        count+=max(high_index-low_index, 0)

print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

def one_str():
    return input()

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

N=one_int()
L=many_int()

L=sorted(L)

#重複削除用探索
setL = sorted(list(set(L)))

sankaku_dict={}
count=0
for i in range(len(L)-1):
    low=L[i]
    for j in range(i+1, len(L)):
        high = L[j]
        
        #上限を取得
        low_index = bisect.bisect_right(setL, high)

        high_index = bisect.bisect_left(setL, high+low)
        count+=(high_index-low_index)

print(count)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
#import math
#import time
#import random  # randome is not available at Codeforces
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
#eps=10**(-10)
#ts=time.time()
#sys.setrecursionlimit(10**6)
input=lambda: sys.stdin.readline().rstrip()

show_flg=False
show_flg=True

n=I()
a=sorted(LI())[::-1]

s=set()
for i in a:
    s.add(i)
s=sorted(list(s))[::-1]
L=1080
#L=10000
k=[0]*(L)
ak=[0]
ans=0

for i in a:
    k[i]+=1
for i in range(L):
    ak.append(ak[-1]+k[i])

un=len(s)
for i in range(un):
    c=s[i]
    if k[c]>=3:
        ans+=k[c]*(k[c]-1)*(k[c]-2)//6
    if k[c]>=2:
        ans+=ak[c]*k[c]*(k[c]-1)//2
    for j in range(i+1,un):
        #show('i,j,s[i],s[j],mm',i,j,'|',s[i],s[j],s[i]-s[j]+1,'|',ak[s[i]-s[j]],ans)
        
        sc=s[j]
        if ak[sc]==0:
            break
        mm=c-sc+1
        if mm<0:
            continue
        elif mm>sc:
            continue # break
        else:
            ans+=k[sc]*(k[sc]*(k[sc]-1)//2+ak[sc]-ak[mm])
        #show('ans+=',k[sc]*(k[sc]-1)//2+ak[sc]-ak[mm])
    
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
#import math
#import time
#import random  # randome is not available at Codeforces
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
#eps=10**(-10)
#ts=time.time()
#sys.setrecursionlimit(10**6)
input=lambda: sys.stdin.readline().rstrip()

show_flg=False
show_flg=True

n=I()
a=sorted(LI())[::-1]

s=set()
for i in a:
    s.add(i)
s=sorted(list(s))[::-1]
L=1080
#L=10000
k=[0]*(L)
ak=[0]
ans=0

for i in a:
    k[i]+=1
for i in range(L):
    ak.append(ak[-1]+k[i])

un=len(s)
for i in range(un):
    c=s[i]
    if k[c]>=3:
        ans+=k[c]*(k[c]-1)*(k[c]-2)//6
    if k[c]>=2:
        ans+=ak[c]*k[c]*(k[c]-1)//2
    for j in range(i+1,un):
        #show('i,j,s[i],s[j],mm',i,j,'|',s[i],s[j],s[i]-s[j]+1,'|',ak[s[i]-s[j]],ans)
        
        sc=s[j]
        if ak[sc]==0:
            break
        mm=c-sc+1
        if mm<0:
            continue
        elif mm>sc:
            continue # break
        else:
            ans+=k[c]*(k[sc]*(k[sc]-1)//2+ak[sc]-ak[mm])
        #show('ans+=',k[sc]*(k[sc]-1)//2+ak[sc]-ak[mm])
    
print(ans)

Submission status: Wrong Answer

Solution code:
if __name__ == "__main__":
    N = int(input())
    L = list(map(int, input().split()))

    L.sort()
    L.reverse()

    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            for k in range(j+1, N):
                if i != j and j != k and k != i:
                    if L[i] < L[j] + L[k]:
                        ans += N - k
                        break

    print(ans)

Submission status: Wrong Answer

Solution code:

N = int(input())
D = input().split(' ')

D.sort(reverse=True)

count = 0

def search(num, l, target):
    if l == []:
        return num
    if int(l[len(l) // 2]) <  target:
        return search(num, l[:len(l) // 2], target)
    elif int(l[len(l) // 2]) > target:
        return search(num + len(l) // 2 + 1, l[len(l) // 2 + 1:], target)
    return num + len(l) // 2

for i in range(N):
    for j in range(i + 1, N - 1):
        target = int(D[i]) - int(D[j])
        l = D[j + 1:]
        count += search(0, l, target)

print(count)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
#import math
#import time
#import random  # randome is not available at Codeforces
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
#eps=10**(-10)
#ts=time.time()
#sys.setrecursionlimit(10**6)
input=lambda: sys.stdin.readline().rstrip()

show_flg=False
show_flg=True

n=I()
a=sorted(LI())[::-1]

s=set()
for i in a:
    s.add(i)
s=sorted(list(s))[::-1]
L=1080
#L=10000
k=[0]*(L)
ak=[0]
ans=0

for i in a:
    k[i]+=1
for i in range(L):
    ak.append(ak[-1]+k[i])

un=len(s)
for i in range(un):
    c=s[i]
    if k[c]>=3:
        ans+=k[c]*(k[c]-1)*(k[c]-2)//6
    elif k[c]==2:
        ans+=ak[c]
    for j in range(i+1,un):
        #show('i,j,s[i],s[j],mm',i,j,'|',s[i],s[j],s[i]-s[j]+1,'|',ak[s[i]-s[j]],ans)
        
        sc=s[j]
        if ak[sc]==0:
            break
        mm=c-sc+1
        if mm<0:
            continue
        elif mm>sc:
            continue # break
        else:
            ans+=k[c]*(k[sc]*(k[sc]-1)//2+ak[sc]-ak[mm])
        #show('ans+=',k[sc]*(k[sc]-1)//2+ak[sc]-ak[mm])
    
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N=int(input())
L=sorted(list(map(int,input().split())))
L=list(set(L))
N=len(L)
print(L)
exit()
count=0
d2breakFlag=False
for d1 in range(N-2):
    for d2 in range(d1+1,N-1):
        Temp=L[d1]+L[d2]

        """for d3 in range(d2+1,N):
            if L[d1]+L[d2]>L[d3]:count+=1
            else:break
print(count)"""

Submission status: Wrong Answer

Solution code:
N=int(input())
L=sorted(list(map(int,input().split())))
L=[3,1,5,1,2,3,4,1,5,2,1,3,]
L=list(set(L))
N=len(L)
print(L)
exit()
count=0
d2breakFlag=False
for d1 in range(N-2):
    for d2 in range(d1+1,N-1):
        Temp=L[d1]+L[d2]

        """for d3 in range(d2+1,N):
            if L[d1]+L[d2]>L[d3]:count+=1
            else:break
print(count)"""

Submission status: Wrong Answer

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

ind_list = [-1] * 1024

last_ind = -1
for i in range(1001):
    if i in L:
        last_ind = L.index(i)
        ind_list[i] = last_ind
    else:
        ind_list[i] = last_ind + 0.5

triangles = []

for i in range(N-1):
    for j in range(i+1, N):
        a = L[i]
        b = L[j]
        min_c = max(abs(a - b) + 1, b)
        max_c = min(a + b - 1, 1000)

        min_ind = ind_list[min_c]
        if isinstance(min_ind, float):
            min_ind = int(min_ind + 1)
        max_ind = int(ind_list[max_c])

        triangles += ['{}_{}_{}'.format(a, b, c) for c in L[min_ind:max_ind]]

print(len(set(triangles)))

Submission status: Wrong Answer

Solution code:
N=int(input())
L=sorted(list(map(int,input().split())))
count=0
d2breakFlag=False
for d1 in range(N-2):
    for d2 in range(d1+1,N-1):
        Temp=L[d1]+L[d2]

        """for d3 in range(d2+1,N):
            if L[d1]+L[d2]>L[d3]:count+=1
            else:break
print(count)"""

Submission status: Wrong Answer

Solution code:
import sys
import bisect
N = int(input())
L = [i for i in map(int, input().split())]
ans = 0

L.sort()
#print(L)
if N == 3:
    if L[0] + L[1] > L[2]:
        print("1")
        sys.exit()

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

dp = [[-1 for i in range(1001)] for j in range(n)]
lptr = [-1] * 1001

cnt = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    tgt = l[i] - l[j]
    if lptr[tgt] != -1:
      if lptr[tgt] >= j:
        cnt += lptr[tgt] - j
#    elif dp[j+1][tgt] != -1:
#      cnt += dp[j+1][tgt]
    else:
      cptr = ((n-1) + (j+1)) // 2
      fixed = [-1] * n
      while fixed[cptr] == -1:
        if l[cptr] > tgt:
          fixed[cptr] = 0
          cptr = ((n-1) + (cptr+1)) // 2
        else:
          fixed[cptr] = 1
          cptr = ((cptr) + (j+1)) // 2
      inc = cptr - fixed[cptr] - j 
      cnt += inc
#      dp[j+1][tgt] = inc
      lptr[tgt] = cptr - fixed[cptr]
#      print('J='+str(j)+', CPTR='+str(cptr)+', CNT='+str(cnt))

print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
L0 = [int(x) for x in input().split()]

cnt=0

L=sorted(L0)

for a in range(N-2):
    for b in range(a+1,N-1):
        for c in range(b+1,N):
            if L[c]<L[a]+L[b]:
                  cnt+=1
            else:
                  break

Submission status: Wrong Answer

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

ind_list = [-1] * 1024

last_ind = -1
for i in range(1001):
    if i in L:
        last_ind = L.index(i)
        ind_list[i] = last_ind
    else:
        ind_list[i] = last_ind + 0.5

ans = 0

for i in range(N-1):
    for j in range(i+1, N):
        a = L[i]
        b = L[j]
        min_c = max(abs(a - b) + 1, b + 1)
        max_c = min(a + b - 1, 1000)

        min_ind = ind_list[min_c]
        if isinstance(min_ind, float):
            min_ind = int(min_ind + 1)
        max_ind = int(ind_list[max_c])

        ans += max(max_ind - min_ind + 1, 0)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

N = int(input())

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

d.sort()

ans = 0

for i in range(N-2):
    a = d[i]
    for j in range(i+1,N-1):
        ans += bisect.bisect_left(d,a+d[j])
print(ans)

Submission status: Wrong Answer

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

ind_list = [-1] * 1024

last_ind = -1
for i in range(1001):
    if i in L:
        last_ind = L.index(i)
        ind_list[i] = last_ind
    else:
        ind_list[i] = last_ind + 0.5

ans = 0

for i in range(N-1):
    for j in range(i+1, N):
        a = L[i]
        b = L[j]
        min_c = max(abs(a - b) + 1, b + 1)
        max_c = min(a + b - 1, 1000)

        min_ind = ind_list[min_c]
        if isinstance(min_ind, float):
            print(min_ind)
            min_ind = int(min_ind + 1)
        max_ind = int(ind_list[max_c])

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

Submission status: Wrong Answer

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

ind_list = [-1] * 1024

last_ind = -1
for i in range(1001):
    if i in L:
        last_ind = L.index(i)
        ind_list[i] = last_ind
    else:
        ind_list[i] = last_ind + 0.5

ans = 0

for i in range(N-1):
    for j in range(i+1, N):
        a = L[i]
        b = L[j]
        min_c = max(abs(a - b) + 1, b + 1)
        max_c = min(a + b - 1, 1000)

        min_ind = ind_list[min_c]
        if isinstance(min_ind, float):
            min_ind = int(min_ind + 1)
        max_ind = int(ind_list[max_c])

        ans += max_ind - min_ind + 1

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    number_of_bars = int(input())
    bar_list = list(map(int,input().split()))
    print(len(traiangle_maker(bar_list))//3)
    # print(traiangle_maker(bar_list))


def traiangle_maker(bar_list):
    bar_list.sort()
    result_list = []
    for i in range(len(bar_list)):
        for j in range(len(bar_list)):
            if i == j or bar_list[i] < bar_list[j]:
                continue
            minimum_length = bar_list[i] - bar_list[j]
            max_length = bar_list[i] + bar_list[j]
            for k in bar_list:
                if k < minimum_length:
                    break
                if k > minimum_length and k < max_length and k != bar_list[i] and k != bar_list[j]:
                    traiangle = [bar_list[i],bar_list[j],k]
                    result_list.append(traiangle)

    return result_list


main()

Submission status: Wrong Answer

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

dp = [[-1 for i in range(1001)] for j in range(n)]
lptr = [-1] * 1001

cnt = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    tgt = l[i] - l[j]
    if lptr[tgt] != -1:
      cnt += lptr[tgt] - j
#    elif dp[j+1][tgt] != -1:
#      cnt += dp[j+1][tgt]
    else:
      cptr = ((n-1) + (j+1)) // 2
      fixed = [-1] * n
      while fixed[cptr] == -1:
        if l[cptr] > tgt:
          fixed[cptr] = 0
          cptr = ((n-1) + (cptr+1)) // 2
        else:
          fixed[cptr] = 1
          cptr = ((cptr) + (j+1)) // 2
      inc = cptr - fixed[cptr] - j 
      cnt += inc
#      dp[j+1][tgt] = inc
      lptr[tgt] = cptr - fixed[cptr]
#      print('J='+str(j)+', CPTR='+str(cptr)+', CNT='+str(cnt))

print(cnt)

Submission status: Wrong Answer

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

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

    L = sorted(L)
    ans = 0
    for a, b in combinations(L, 2):
        c_max = min(a, b)
        c_min = abs(a-b)

        left = bisect_right(L, c_min)
        right = bisect_right(L, c_max)

        if a == b:
            right -= 2
        else:
            right -= 1
        
        if left > right:
            continue
        ans += right - left

    return ans

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

Submission status: Wrong Answer

Solution code:
n = int(input())
l = [int(i) for i in input().split()]
l.sort()
ans = 1
for i in range(n):
    for j in range(i + 1, n):
        ans += sum([l[k] < l[i] + l[j] for k in range(j + 1, n)])
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = [int(i) for i in input().split()]
l.sort()
ans = 1
for i in range(n):
    for j in range(i + 1, n):
        for k in range(j + 1, n):
            if l[k] < l[i] + l[j]:
                ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right


def solve_d(n, l_list):
    res = 0
    l_list_s = sorted(l_list)
    for i in range(1, n - 1):
        b = l_list_s[i]
        for j in range(0, i):
            a = l_list_s[j]
            res += bisect_left(l_list_s, a + b) - bisect_right(l_list_s, b)

    return res


def solve_d_(n, l_list):
    res = 0
    l_list_s = sorted(l_list)
    h = []
    for i in range(1, n - 1):
        for j in range(0, i):
            heappush(h, l_list_s[i] + l_list_s[j])
        while len(h) > 0:
            a = heappop(h)
            if a > l_list_s[i + 1]:
                heappush(h, a)
                break
        res += len(h)

    return res


def main():
    n = int(input())
    l_list = list(map(int, input().split()))
    res = solve_d(n, l_list)
    print(res)


def test():
    assert solve_d(4, [3, 4, 2, 1]) == 1
    assert solve_d(3, [1, 1000, 1]) == 0
    assert solve_d(7, [218, 786, 704, 233, 645, 728, 389]) == 23


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

Submission status: Wrong Answer

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

l.sort(reverse=True)
ans = 0
for i in range(n-2):
    j = i+1
    k = n-1
    while (j < k):
        if l[i] - l[j] - l[k] <= 0:
            ans += k - j
            j += 1
        else:
            k -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = [int(i) for i in input().split()]
l.sort()
ans = 1
for i in range(n):
    a = l[i]
    for j in range(i + 1, n):
        b = l[j]
        c = a+b
        for k in range(j + 1, n):
            if l[k] < c:
                ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left#(sorted_list,x)で昇順ソート位置

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

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

Submission status: Wrong Answer

Solution code:
import sys

#+++++

def is_tr(a,b,c):
	if a < b+c:
		return True
	return False


def main():
	n = int(input())
	ll = list(map(int, input().split()))
	ll.sort(reverse = True)
	pa(ll)
	
	ret = 0
	for i,l1 in enumerate(ll[:-2]):
		for j, l2 in enumerate(ll[i+1:-1]):
			pa(ret)
			if not is_tr(l1, l2, ll[j+1]):
				break
			if is_tr(l1,l2,ll[-1]):
				pa('ggg')
				ret += n-j
				continue
			ok=i+j+1
			ng=n-1
			while ng-ok > 1:
				mid = (ok+ng)//2
				if is_tr(l1,l2,ll[mid]):
					ok =mid
				else:
					ng = mid
			pa((ok,j+i+1))
			ret += (ok - (i+j+1))
	
	print(ret)
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


def solve(N: int, L: "List[int]"):
    L.sort()
    ans = 0
    ans = sum([bisect.bisect_left(L, sum(way2)) - bisect.bisect_right(L, max(way2))
               for way2 in itertools.combinations(L[:-1], 2)])
    print(ans)
    return


def main():
    def iterate_tokens():
        for sent in sys.stdin:
            for token in sent.split():
                yield token
    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:
import bisect
n=int(input())
l=list(map(int,input().split()))
cnt=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)
    cnt+=(k-j-1)
print(cnt)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
N = int(input())
L = sorted(list(map(int, input().split())))


acc = [0] * 1001
for l in L:
    acc[l] += 1
acc = list(accumulate(acc))


ans = 0
for i in range(N):
    for j in range(i + 1, N):
        l1, l2 = L[i], L[j]
        if l1 == l2:
            ans += acc[min(1000, l1 + l2 - 1)] - acc[l2 - 1] - 2
        else:
            ans += acc[min(1000, l1 + l2 - 1)] - acc[l2 - 1] - 1

print(ans)