Problem:


Score : 500 points

Problem Statement

Takahashi has come to a party as a special guest.
There are N ordinary guests at the party. The i-th ordinary guest has a power of A_i.

Takahashi has decided to perform M handshakes to increase the happiness of the party (let the current happiness be 0).
A handshake will be performed as follows:

Takahashi chooses one (ordinary) guest x for his left hand and another guest y for his right hand (x and y can be the same).

Then, he shakes the left hand of Guest x and the right hand of Guest y simultaneously to increase the happiness by A_x+A_y.

However, Takahashi should not perform the same handshake more than once. Formally, the following condition must hold:

Assume that, in the k-th handshake, Takahashi shakes the left hand of Guest x_k and the right hand of Guest y_k. Then, there is no pair p, q (1 \leq p < q \leq M) such that (x_p,y_p)=(x_q,y_q).

What is the maximum possible happiness after M handshakes?

Constraints

1 \leq N \leq 10^5

1 \leq M \leq N^2

1 \leq A_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the maximum possible happiness after M handshakes.

Sample Input 1

5 3
10 14 19 34 33

Sample Output 1

202

Let us say that Takahashi performs the following handshakes:

In the first handshake, Takahashi shakes the left hand of Guest 4 and the right hand of Guest 4.

In the second handshake, Takahashi shakes the left hand of Guest 4 and the right hand of Guest 5.

In the third handshake, Takahashi shakes the left hand of Guest 5 and the right hand of Guest 4.

Then, we will have the happiness of (34+34)+(34+33)+(33+34)=202.

We cannot achieve the happiness of 203 or greater, so the answer is 202.

Sample Input 2

9 14
1 3 5 110 24 21 34 5 3

Sample Output 2

1837

Sample Input 3

9 73
67597 52981 5828 66249 75177 64141 40773 79105 16076

Sample Output 3

8128170



Sample input:
5 3
10 14 19 34 33


Sample output:
202

Submission status: Wrong Answer

Solution code:
import sys
from math import factorial
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
MOD = 10**9 + 7

n, m = MAP()
a = LIST()
a = sorted(a, reverse = True)
res = 0
q = []

heapq.heappush(q, [0, 0, -1*a[0]*2])
hantei = [[False]*n for i in range(n)]
hantei[0][0] = True
count = 0
res = 0
while True:
    if count == m:
        break
    temp = heapq.heappop(q)
    res += temp[2]*-1
    count += 1
    i = temp[0]
    j = temp[1]
    if count != m and i != j:
        res += temp[2]*-1
        count += 1
    if i != j and not hantei[i+1][j]:
        heapq.heappush(q, [i+1, j, -1*(a[i+1]+a[j])])
        hantei[i+1][j] = True
    if j != n-1 and not hantei[i][j+1]:
        heapq.heappush(q, [i, j+1, -1*(a[i]+a[j+1])])
        hantei[i][j+1] = True

print(res)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort(reverse=True)
#print(A)
AA = []
ans = []
index = [i+1 for i in range(N)]

for a in A:
    AA.append(a+a)
#print(AA)

for i in range(len(AA)-1):
    #print(ans)
    ans.append(AA[i])
    if len(ans) == M:
        print(sum(ans))
        exit()
    top = AA[i]
    buttom = AA[i+1]
    for j in range(i+1):
        start = index[j]
        for k in range(start,len(AA)):
            if A[j] * A[k] >= buttom:
                ans.append(A[j] + A[k])
                ans.append(A[j] + A[k])
                if len(ans) >= M:
                    anstrue = 0
                    for i in range(M):
                        anstrue += ans[i]
                    #print(ans)
                    print(anstrue)
                    exit()
            else:
                index[j] = k
                break
            index[j] = k + 1
#print(sum(ans))
#print(ans)

Submission status: Wrong Answer

Solution code:
#幸福度の最小値で二分探索

import bisect
n,m = map(int, input().split())
a = list(map(int, input().split( )))
a.sort()

a_ = []

for i in range(n):
    a_.append(a[i])
    a_.append(a[i])
mn = 0
mx = a[-1]*2
md = (mx+mn)//2
while mx-mn>1:
    md = (mx+mn)//2
    cnt = 0
    for i in range(n):
        tmp = md-a[i]
        idx = bisect.bisect_right(a_, tmp)
        if i>idx:
            cnt += (2*i-1-idx)
        elif i == idx:
            cnt += 1
        else:
            break
    if cnt > m:
        mn = md
    else:
        mx = md

ans = 0
cnt2 = m

acc = []
sm = 0
for i in range(2*n-1,-1,-1):
    sm += a_[i]
    acc.append(sm)

for i in range(n-1,-1,-1):
    idx = bisect.bisect_left(a_,md-a[i])
    if idx > 2*i-1:
        break
    tmp2 = 2*i-1-idx
    if cnt2>= tmp2:
        ans += a[i]* tmp2
        ans += acc[2*i-2]- acc[idx-1]
        cnt2-=  tmp2
    else :
        ans += a[i]*cnt2 + acc[2*i-2] -acc[2*i-2-cnt2]
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import bisect
input = sys.stdin.readline

class Bisect(object):
    def bisect_max(self, reva, func,M):
        ok = 0 # exist
        ng = 10**9 # not exist
        while abs(ok-ng) > 1:
            cnt = (ok + ng) // 2
            if func(cnt,reva,M):
                ok = cnt
            else:
                ng = cnt
        return ok

    def bisect_min(self, reva, func,M):
        ok = 10**9 # exist
        ng = 0 # not exist
        while abs(ok-ng) > 1:
            cnt = (ok + ng) // 2
            if func(cnt,reva,M):
                ok = cnt
            else:
                ng = cnt
        return ok

def solve1(tgt,reva,M):
    res=0
    n = len(reva)
    for i in range(n):
        tmp = bisect.bisect_left(reva,tgt-reva[i])
        tmp = n - tmp
        res += tmp
    if M <= res:
        return True
    else:
        return False

def solve2(tgt):
    print(tgt)
    if tgt > 12:
        return True
    else:
        return False
N,M = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse=True)
reva = a[::-1]
bs = Bisect()
Kmax = (bs.bisect_max(reva,solve1,M))

r=[0]
for i in range(N):
    r.append(r[i]+a[i])

res = 0
for i in range(N):
    tmp = bisect.bisect_left(reva,Kmax-reva[N-i-1])
    tmp = N - tmp
    res += tmp*a[i]+r[tmp]
print(res)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split(" "))
POWERS = sorted([int(i) for i in input().split(" ")])

i = 0
score = 0
remaining = POWERS
while i < M:
    if N - i >= len(remaining) * 2 + 1:
        score += remaining[-1] * 2 + remaining[-1] * (len(remaining) - 1) * sum(remaining[:-1]) * 2
        i += len(remaining) * 2 + 1
        remaining = remaining[:-1]
    else:
        score += remaining[-1] * 2
        i += 1
        if i >= M:
            break
        for lesser in reversed(remaining[:-1]):
            score += remaining[-1] + lesser
            i += 1
            if i >= M:
                break
            score += remaining[-1] + lesser
            i += 1
            if i >= M:
                break
        else:
            remaining = remaining[:-1]
            continue
        break


print(score)

Submission status: Wrong Answer

Solution code:
from bisect import *
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
a
left = 0
right = 200001

while right - left > 1:
    mid = left + (right - left) // 2
    count = 0
    for i in range(n-1, -1, -1):
        count += n - bisect_left(a, mid - a[i])
        if count >= m:
            break


    if m <= count:
        left = mid
    else:
        right = mid

    #print(mid, count)

ans = 0
a_accum = [0]
for i in range(n):
    a_accum.append(a_accum[i] + a[-1-i])


for i in range(n-1, -1, -1):
    count = n - bisect_left(a, left - a[i])
    ans += a_accum[count] + a[i] * count
    m -= count
    if m <= 0:
        break

ans += left * m
print(ans)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
hands = map(int, input().split())

hands = sorted(hands)

ans = 0
person = -1
nextPerson = True
i = 0
while i < m:
    if (-person) % (n+1) == 0:
        del(hands[-1])
        n -= 1
        person = -1
        nextPerson = True
        if n == 0:
            break

    add = hands[-1] + hands[person]
    ans += add if person == -1 else add*2
    i += 1 if person == -1 else 2
    person -= 1


print(ans)

Submission status: Wrong Answer

Solution code:
x, y = [int(x) for x in input().split()] 
a = list(map(int,input().strip().split()))[:x]

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
al = list(map(int,input().split()))
al = sorted(al)
ans = 0
while True:
   if m - 2*n + 1 > 0:
      ans += sum(al) * 2 + 2*al[-1]*(n-1)
      al.pop()
      m = m - 2*n +1
      n -= 1
   else:
      l = []
      for a in al:
         l.append(a+al[-1])
         l.append(a+al[-1])
      l.pop()
      l = l[-m:]
      ans += sum(l)
      break

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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


left = 1
right = a[-1] * 2 + 1
mid = (left + right) // 2
while right - left > 1:
    mid = (left + right) // 2
    count = 0
    for i in range(n):
        j = bisect.bisect_left(a, mid - a[i])
        count += n - j

    if count == m:
        break
    elif count < m:
        right = mid
    else:
        left = mid

# left 以上のものを採用すれば良い。

acum = [0] * (n+1)
for i in range(n):
    acum[i+1] = acum[i] + a[i]

ans = 0
for i in range(n):
    j = bisect.bisect_left(a, mid - a[i])
    ans += acum[n] - acum[j] + a[i] * (n-j)

print(ans)

Submission status: Wrong Answer

Solution code:
def handshake(n):  
    if (n == 0): 
        return 0
    else: 
        return (n - 1) + handshake(n - 1)  

n = 9
print(handshake(n))

Submission status: Wrong Answer

Solution code:
import bisect
import sys
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
L = A[:]
R = A[:]
# print(len(A))
for i in range(N-1):
    # print(i)
    L[i+1] += L[i]
    R[-i-2] += R[-i-1]

def func(l,r):
    if r-l == 1:
        return l
    m = (l+r)//2
    count = 0
    for i in A:
        count += N-bisect.bisect_left(A,m-i)
    if count >= M:
        return func(m,r)
    return func(l,m)

f = func(0,10**6)
ans = 0
count = 0
p = 0
while count < M:
    x = N-bisect.bisect_left(A,f-A[p])
    if x == 0:
        p += 1
        continue
    ans += A[p]*x + R[-x]
    count += x
    p += 1
ans -= (count-M)*f
print(ans)
if count < M:
    print(0/0)

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect, bisect_left
import heapq

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    A = [int(a) for a in input().split()]
    A.sort()
    S = [0] * (N + 1)
    for i, a in enumerate(A): S[i + 1] = a + S[i]
 
    l, h = 1, A[N-1] * 2 + 1
    while h - l > 1: #最小値がｍ以上の要素をM個用意できるか
        m = (h + l) // 2
        count = 0
        count_l = 0
        for i, a in enumerate(A): count += N - bisect_left(A, m - a)
        if count >= M: 
            l = m
            count_l = count
        else: h = m

    total = 0
    count = 0
    Q = []
    heapq.heapify(Q)
    #print(A)
    for i, a in enumerate(A):
        j = bisect_left(A, l - a)
        double = bisect_left(A, h - a)
        total += a * (N - j) + (S[N] - S[j])
        for k in range(j, min(N - 1, double + 1)):
            heapq.heappush(Q, a + A[k])
        #print(a, j)

    if count_l > M:
        for _ in range(count_l - M):
            minus = heapq.heappop(Q)
            total -= minus

    print(total)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
A = sorted(list(map(int,input().split())),reverse=1)
ans = 0
if M == N**2:
    ans = sum(A)*2*N
print(ans)

Submission status: Wrong Answer

Solution code:
print("hellow")

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
kouho=[]
for i in range(n-1):
    kouho.append(a[i]+a[i])
    for j in range(i+1,n):
        kouho.append(a[i]+a[j])
        kouho.append(a[i]+a[j])
kouho.sort(reverse=True)
print(sum(kouho[0:m]))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
al = list(map(int,input().split()))
al = sorted(al)
ans = 0
while True:
   if m - 2*n + 1 > 0:
      ans += sum(al) * 2 + 2*al[-1]*(n-1)
      al.pop()
      m = m - 2*n +1
      n -= 1
   else:
      l = [a + al[-1] for a in al] + [a + al[-1] for a in al]
      l.remove(al[-1]+al[-1])
      l = sorted(l)
      l = l[-m:]
      ans += sum(l)
      break

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
import sys
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
L = A[:]
R = A[:]
# print(len(A))
for i in range(N-1):
    # print(i)
    L[i+1] += L[i]
    R[-i-2] += R[-i-1]

def func(l,r):
    if r-l == 1:
        return l
    m = (l+r)//2
    count = 0
    for i in A:
        count += N-bisect.bisect_left(A,m-i)
    if count >= M:
        return func(m,r)
    return func(l,m)

f = func(0,10**6)
ans = 0
count = 0
p = 0
while count < M:
    x = N-bisect.bisect_left(A,f-A[p])
    if x == 0:
        p += 1
        continue
    ans += A[p]*x + R[-x]
    count += x
    p += 1
ans -= (count-M)*f
print(ans)

Submission status: Wrong Answer

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

cnt = 0
table.sort(reverse=True)
goukei = 0


def plus(x, y):
    global goukei
    global cnt

    goukei += x + y
    cnt += 1
    if cnt == m:
        return True
    else:
        return False


if m == 0:
    print(0)
    quit()
for x in range(0, n):
    if plus(table[x], table[x]):
        print(goukei)
        quit()

    if m > cnt + (n - x) * (n - x - 1) + 2:
        goukei += sum([table[x] + table[y] * 2 for y in range(x + 1, n)])
        cnt += (n - x) * (n - x - 1) + 2
        if cnt >= m:
            print(goukei)
            quit()
    else:
        for y in range(x + 1, n):
            if plus(table[x], table[y]):
                print(goukei)
                quit()
            if plus(table[x], table[y]):
                print(goukei)
                quit()

print(goukei)

Submission status: Wrong Answer

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

cnt = 0
table.sort(reverse=True)
goukei = 0


def plus(x, y):
    global goukei
    global cnt

    goukei += x + y
    cnt += 1
    if cnt == m:
        return True
    else:
        return False

if m == 0:
    print(0)
    quit()
for x in range(0, n):
    if plus(table[x], table[x]):
        print(goukei)
        quit()

    if m > cnt + (n - x) * (n - x - 1) + 2:
        goukei += sum([table[x] + table[y] * 2 for y in range(x + 1, n)])
        print(goukei)
        cnt += (n - x) * (n - x - 1) + 2
    else:
        for y in range(x + 1, n):
            if plus(table[x], table[y]):
                print(goukei)
                quit()
            if plus(table[x], table[y]):
                print(goukei)
                quit()

print(goukei)

Submission status: Wrong Answer

Solution code:
n, m = list(map(int, input().split()))
_a = list(map(int, input().split()))
a = sorted(_a, key=lambda x: -x)
p = a[0] * 2
m -= 1
t = 0
r1 = 0
r2 = 1
while m > 0:
    if r2 >= n:
        break
    if m > 2:
        t = 2
    else:
        t = m
    p += a[r1] * t + a[r2] * t
    #print("m:{} r1:{} r2:{} t:{} p:{}".format(m, a[r1], a[r2], t, p))
    m -= t
    r2 += 1
    if r2 >= n:
        r1 += 1
        r2 = r1 + 1
        if r1 < n:
            if m > 0:
                p += a[r1] * 2
                m -= 1
print(p)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
An = list(map(int, input().split()))
aaa = []
cnt = 0
flg = False
An.sort(reverse=True)
for i in range(N):
  for j in range(N):
    aaa.append(An[i]+An[j])
    cnt += 1
    if cnt > M :
      flg = True
    if flg:
      break
  if flg:
    break
aaa.sort(reverse=True)
ans = sum(aaa[0:M])
print(ans)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left as bl
from itertools import accumulate as ac
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
s=[0]+list(ac(a[::-1]))
l,r=2*a[-1]*(n**2)+1,0
while l-r>1:
    t=(l+r)//2
    p=0
    for i in range(n):
        if a[i]+a[-1]<=t:
            x=bl(a,t-a[i])
            p+=n-x
    if p<m:
        l=t
    else:
        r=t
c,d=0,0
w=0
for i in range(n):
    if a[i]+a[-1]<=t:
        g=bl(a,r-a[i])
        x=n-g
        c+=s[x]+x*a[i]
        w+=x
print(c-r*max(0,(w-m)))

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect, bisect_left

def solve():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    A = [int(a) for a in input().split()]
    A.sort()
    S = [0] * (N + 1)
    for i, a in enumerate(A): S[i + 1] = a + S[i]
 
    l, h = 1, A[N-1] * 2 + 1
    while h - l > 1: #最小値がｍ以上の要素をM個用意できるか
        m = (h + l) // 2
        count = 0
        for i, a in enumerate(A): count += N - bisect_left(A, m - a)
        if count >= M: l = m
        else: h = m

    total = 0
    #print(A)
    for i, a in enumerate(A):
        j = bisect_left(A, l - a)
        total += a * (N - j) + (S[N] - S[j])
        #print(a, j)

    print(total)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
L = [int(i) for i in input().split()]
L.sort(reverse=True)
#print(L)
out = 0

while(1):
  if (2*n-1) >= m:
    break
  out += (2*sum(L) + 2*(n-1)*L[0])
  L.remove(L[0])
  m -= (2*n - 1)
  n -= 1
    


mm = m - 1
ln = mm//2
for i in range(ln):
  out += 2*L[i+1]
if mm%2 == 1:
  out += L[ln+1]
out += (L[0]*(m+1))


print(out)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
hands = map(int, input().split())

hands = sorted(hands)

ans = 0
person = -1
nextPerson = True
for i in range(m):
    if (-person) % (n+1) == 0:
        del(hands[-1])
        n -= 1
        person = -1
        nextPerson = True
        if n == 0:
            break

    add = hands[-1] + hands[person]
    ans += add

    if nextPerson:
        person -= 1
        nextPerson = False
    else:
        nextPerson = True


print(ans)

Submission status: Wrong Answer

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

a = sorted(a)[::-1]

s = [0]
for i in range(n):
    s.append(s[-1] + a[i])

ans = 0
for i in range(n):
    # print(i, m)
    if m < 2 * (n - i):
        ans += a[i] * (m - 1)
        # ans += sum(a[i: i + m // 2]) * 2
        ans += (s[i + m // 2] - s[i]) * 2
        if m % 2 == 0:
            ans += a[i + m // 2]
        else:
            ans += a[i + m // 2] * 2
        break
    else:
        ans += a[i] * (n - i - 1) * 2
        # ans += sum(a[i:]) * 2
        ans += (s[-1] - s[i]) * 2
        m -= (n - i) * 2 - 1

print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
A = sorted([int(a) for a in input().split()])
C = [0] * 101010
S = [0] * 101010

for a in A[::-1]:
    C[a] += 1
    S[a] += a
for i in range(101000)[::-1]:
    C[i] += C[i+1]
    S[i] += S[i+1]

def cnt(n):
    re = 0
    for a in A:
        re += C[max(n-a, 0)] - (2*a >= n) * 0
    return re
def calc(n):
    re = 0
    for a in A:
        re += S[max(n-a, 0)]  + C[max(n-a, 0)] * a
    return re

l, r = 0, 101000
while r - l > 1:
    m = (l+r) // 2
    if cnt(m) >= M:
        l = m
    else:
        r = m

print(calc(l) - (cnt(l) - M) * l)

Submission status: Wrong Answer

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

a = sorted(a)[::-1]

s = [0]
for i in range(n):
    s.append(s[-1] + a[i])

ans = 0
for i in range(n):
    print(i, m)
    if m < 2 * (n - i):
        ans += a[i] * (m - 1)
        # ans += sum(a[i: i + m // 2]) * 2
        ans += (s[i + m // 2] - s[i]) * 2
        if m % 2 == 0:
            ans += a[i + m // 2]
        else:
            ans += a[i + m // 2] * 2
        break
    else:
        ans += a[i] * (n - i - 1) * 2
        # ans += sum(a[i:]) * 2
        ans += (s[-1] - s[i]) * 2
        m -= (n - i) * 2 - 1

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
import sys
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
L = A[:]
R = A[:]

for i in range(N-1):
    L[i+1] += L[i]
    R[-i-2] += R[-i-1]

def func(l,r):
    if r-l == 1:
        return l
    m = (l+r)//2
    count = 0
    for i in A:
        count += N-bisect.bisect_left(A,m-i)
    if count >= M:
        return func(m,r)
    return func(l,m)

f = func(0,10**5)
ans = 0
count = 0
p = 0
while count < M:
    x = N-bisect.bisect_left(A,f-A[p])
    if x == 0:
        p += 1
        continue
    ans += A[p]*x + R[-x]
    count += x
    p += 1
ans -= (count-M)*f
print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort(reverse=True)
z=a[0:round(m**0.5+1)]
x=[]
for i in range(len(z)):
  for j in range(len(z)):
    x.append(z[i]+z[j])
x.sort(reverse=True)
print(sum(x[0:m]))

Submission status: Wrong Answer

Solution code:
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,M=two_int()
A=many_int()

min_i=0
for i in range(1,N+1):
    if i**2-i >=M:
        min_i=i
        break
if min_i==0:
    min_i=N

import itertools

AA=sorted(A,reverse=True)[:min_i+5]



sums = [sum(i) for i in itertools.product(AA, AA)]

print(sum(sorted(sums,reverse=True)[:M]))

Submission status: Wrong Answer

Solution code:
def main():
    n,m = map(int,input().split())
    a = sorted(list(map(int,input().split())))
    ans = 0
    sa = sum(a)-a[-1]
    while True:
        if m == 0:
            break
        r = a[-1]
        if m >= 2*n-1:
            ans += sa*2+r*2*n
        elif m == 1:
            ans += r*2
            break
        else:
            ans += r*2
            del a[-1]
            m -= 1
            for i in range(m//2):
                ans += a.pop(-1)*2+r*2
            if m%2 == 1:
                ans += a.pop(-1)+r
            break
        m -= 2*n-1
        n -= 1
        if n == 0:
            break
        del a[-1]
        sa -= a[-1]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!python3

def resolve():
    N, M = map(int, input().rstrip().split())
    A = list(map(int, input().rstrip().split()))

    A.sort(reverse=True)

    s = 0
    i = 0
    x, y = 0, 0
    for x in range(N):
        for y in range(x, N):
            s += A[x] + A[y]
            i += 1
            if i >= M:
                print(s)
                return
            elif x == y:
                continue

            s += A[x] + A[y]
            i += 1
            if i >= M:
                print(s)
                return


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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

for i in range(n):
    for j in range(i,n):
        l.append(a[i]+a[j])
        if i != j:
            l.append(a[i]+a[j])
l.sort(reverse=True)

sum(l[:m])

Submission status: Wrong Answer

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

A.sort()

OK=0
NG=max(A)*2+1

import bisect

while NG-OK>2: #N*N個の中で,mid以上の個数がM個以上あればOK
    mid=(OK+NG)//2

    score=0

    for a in A:
        x=bisect.bisect_left(A,mid-a)
        score+=N-x
        if score>=M:
            OK=mid
            break
    else:
        NG=mid
        
from itertools import accumulate
S=list(accumulate(A))

ANS=0
ups=0
for a in A:
    x=bisect.bisect_right(A,OK-a)
    if x!=0:
        ANS+=(N-x)*a+(S[-1]-S[x-1])
    else:
        ANS+=(N-x)*a+S[-1]
        
    #print(x,ANS)
    ups+=N-x
#print(ANS)
ANS+=(M-ups)*OK
print(ANS)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
n,m=[int(x) for x in input().split(" ")]
t = input()
a = [int(x) for x in t.split(" ")] + [int(x) for x in t.split(" ")]
a.sort(reverse=True)
# print(a)
def f():
    k = 0
    s = 0
    c = 0
    while True:
        for i in range(k + 1,2*n):
            s += a[k] + a[i]
            c += 1
            if c == m:
                return s
        k += 2
print(f())

Submission status: Wrong Answer

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

a = sorted(a)[::-1]

s = [0]
for i in range(n):
    s.append(s[-1] + a[i])

import math
ans = 0
for i in range(n):
    # print(i)
    if m < 2 * (n - i):
        ans += a[i] * (m - 1)
        # ans += sum(a[i: i + m // 2]) * 2
        ans += (s[i + m // 2] - s[i]) * 2
        if m % 2 == 0:
            ans += a[i + m // 2]
        else:
            ans += a[i + m // 2] * 2
        break
    else:
        ans += a[i] * (n - i - 1) * 2
        # ans += sum(a[i:]) * 2
        ans += (s[-1] - s[i]) * 2
        m -= (n - i) * 2 - 1

print(ans)

Submission status: Wrong Answer

Solution code:

n, m = map(int, input().split())
table = list(map(int, input().split()))

cnt = 0
table.sort(reverse=True)
sum = 0

def plus(x,y):
    global sum
    global cnt

    sum += x + y
    cnt += 1
    if cnt == m:
        return True
    else:
        return False


for x in range(0, n):
    if plus(table[x],table[x]):
        print(sum)
        quit()
    for y in range(x + 1, n):
        if plus(table[x], table[y]):
            print(sum)
            quit()
        if plus(table[x], table[y]):
            print(sum)
            quit()


print(sum)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
al=sorted(list(map(int,input().split())),reverse=True)
ans=0
cnt=0
for i in range(n):
    ans+=al[i]*2
    cnt+=1
    if cnt>=m:
        break
    for j in range(i+1,n):
        ans+=al[i]+al[j]
        cnt+=1
        if cnt>=m:
            break
        ans+=al[i]+al[j]
        cnt+=1
        if cnt>=m:
            break
    else:
        continue
    break
print(ans)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,m = LI()
    a = LI()
    a.sort()
    l = 0
    r = 2*a[-1]
    while r-l > 1:
        x = (l+r) >> 1
        s = 0
        for i in a:
            j = bisect.bisect_left(a,x-i)
            s += n-j
        if s <= m:
            r = x
        else:
            l = x
    s = [0]*n
    s[-1] = a[-1]
    for i in range(n-1)[::-1]:
        s[i] = s[i+1]+a[i]
    res = 0
    ans = 0
    for i in a:
        j = bisect.bisect_left(a,r-i)
        if j < n:
            ans += (n-j)*i
            res += n-j
            ans += s[j]
    res = m-res
    print(ans+res*l)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
from heapq import heappop as hpop
from heapq import heappush as hpush
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort(reverse=1)
print(*a)
a+=[-99999999999999]
h=[]
for i in range(n):hpush(h,[-a[i]*2,i])
count=m
ans=0
while count!=0:
  x,i=hpop(h)
  x=-x
  c=min(count,1+(x!=a[i]*2))
  count-=c
  ans+=c*x
  x-=a[i]
  x+=a[i+1]
  i+=1
  hpush(h,[-x,i])
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
import math
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse=True)
l = []
r = math.ceil(math.sqrt(m))
heapq.heapify(l)
for x in range(r):
    for y in range(r):
        heapq.heappush(l, (a[x]+a[y])*-1)
z = 0
for i in range(m):
    z = z + (heapq.heappop(l)*-1)
print(z)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort(reverse=True)
S = []
for i in range(min(100,N)):
    S+=[A[i]*2]
    for j in range(i+1,N):
        S+=[A[i]+A[j]]*2
S.sort(reverse=True)
print(sum(S[:M]))