Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
A=list(map(int,input().split()))

A.sort(reverse=True)

l=1
r=200000+1

while l+1<r:
    mid=(l+r)//2
    cnt=0
    C=0
    S=0
    for la in A:
        ll=-1
        rr=N
        while ll+1<rr:
            mi=(ll+rr)//2
            if A[mi]<=mid-la:
                rr=mi
            else:
                ll=mi
        if rr==0:
            break
        cnt+=rr
    if cnt<M:
        r=mid
    else:
        l=mid

AN=r

S=[0]*(N+1)

for i in range(N):
    S[i+1]=S[i]+A[i]

ans1=0
c=0
for la in A:
    ll = -1
    rr = N
    while ll + 1 < rr:
        mi = (ll + rr) // 2
        if A[mi] <= AN - la:
            rr = mi
        else:
            ll = mi
    if rr == 0:
        break
    c+=rr
    ans1=ans1+la*rr+S[rr]

ans=ans1+(M-c)*AN
print(ans)




Fixed submission:
from bisect import bisect_left

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

A.sort()

l=1
r=200000+1

def chk(x):
    c=0
    for la in A:
        y=bisect_left(A,x-la)
        c+=N-y
    if c>=M:
        return True
    else:
        return False



while l+1<r:
    mid=(l+r)//2
    if chk(mid):
        l=mid
    else:
        r=mid

AN=l

S=[0]*(N+1)

for i in range(N):
    S[i+1]=S[i]+A[i]

ans1=0
c=0
for la in A:
    ll = -1
    rr = N
    while ll + 1 < rr:
        mi = (ll + rr) // 2
        if A[mi] > AN - la:
            rr = mi
        else:
            ll = mi
    c+=N-rr
    ans1=ans1+la*(N-rr)+S[N]-S[rr]

ans=ans1+(M-c)*AN
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
A=list(map(int,input().split()))

A.sort(reverse=True)

l=1
r=200000+1

while l+1<r:
    mid=(l+r)//2


    cnt=0
    C=0
    S=0

    for la in A:
        for ra in A:
            if la+ra>mid:
                S+=(la+ra)
                cnt+=1
                C+=1
            else:break
    if cnt<M:
       
        r=mid
    else:
        l=mid

AN=r
ans=S+(M-C)*AN
print(ans)




Fixed submission:
from bisect import bisect_left

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

A.sort()

l=1
r=200000+1

def chk(x):
    c=0
    for la in A:
        y=bisect_left(A,x-la)
        c+=N-y
    if c>=M:
        return True
    else:
        return False



while l+1<r:
    mid=(l+r)//2
    if chk(mid):
        l=mid
    else:
        r=mid

AN=l

S=[0]*(N+1)

for i in range(N):
    S[i+1]=S[i]+A[i]

ans1=0
c=0
for la in A:
    ll = -1
    rr = N
    while ll + 1 < rr:
        mi = (ll + rr) // 2
        if A[mi] > AN - la:
            rr = mi
        else:
            ll = mi
    c+=N-rr
    ans1=ans1+la*(N-rr)+S[N]-S[rr]

ans=ans1+(M-c)*AN
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
A=list(map(int,input().split()))

A.sort(reverse=True)

l=1
r=200000+1

while l+1<r:
    mid=(l+r)//2


    cnt=0
    C=0
    S=0

    for la in A:
        for ra in A:
            if la+ra>mid:
                S+=(la+ra)
                cnt+=1
                C+=1
            else:break
    if cnt<M:
       
        r=mid
    else:
        l=mid

AN=r
ans=S+(M-C)*AN
print(ans)




Fixed submission:
from bisect import bisect_left

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

A.sort()

l=1
r=200000+1

def chk(x):
    c=0
    for la in A:
        y=bisect_left(A,x-la)
        c+=N-y
    if c>=M:
        return True
    else:
        return False



while l+1<r:
    mid=(l+r)//2
    if chk(mid):
        l=mid
    else:
        r=mid

AN=l

S=[0]*(N+1)

for i in range(N):
    S[i+1]=S[i]+A[i]

ans1=0
c=0
for la in A:
    ll = -1
    rr = N
    while ll + 1 < rr:
        mi = (ll + rr) // 2
        if A[mi] > AN - la:
            rr = mi
        else:
            ll = mi
    c+=N-rr
    ans1=ans1+la*(N-rr)+S[N]-S[rr]

ans=ans1+(M-c)*AN
print(ans)