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: Runtime Error

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

n, m = map(int, input().split())
A_list = sorted(list( int(i) for i in input().split()), reverse = True)


def judge(lst, i):
  for j in range(i+1,n):
    min_lst = heapq.heappop(lst)
    if min_lst < A_list[i]+A_list[j]:
      heapq.heappush(lst, A_list[i]+A_list[j])
    else:
      heapq.heappush(lst, min_lst)
      break


    min_lst = heapq.heappop(lst)
    if min_lst < A_list[i]+A_list[j]:
      heapq.heappush(lst, A_list[i]+A_list[j])
    else:
      heapq.heappush(lst, min_lst)
      break
  return lst


B_list = [0] * m
heapq.heapify(B_list)
for i in range(n):
  min_b = heapq.heappop(B_list)
  if min_b < A_list[i]*2:
    heapq.heappush(B_list, A_list[i]*2)
    B_list = judge(B_list, i)
  else:
    heapq.heappush(B_list, min_b)
    break
    
    
print(sum(B_list))

Submission status: Runtime Error

Solution code:
import heapq

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

a.sort(reverse=True)
lr = [0]*m
ans = 0


for ai in a:
    for aj in a:
        power1 = heapq.heappop(lr)
        power2 = ai+aj
        if (power1 > power2):
            heapq.heappush(lr, power1)
            break
        else:
            heapq.heappush(lr, power2)
            
print(sum(lr))

Submission status: Runtime Error

Solution code:
N,M=[int(i) for i in input().split()]
A=sorted([int(i) for i in input().split()])
A=A[::-1]

T=[]
for i in A:
    for j in A:
        T.append([i,j])

T.sort(key=lambda x: x[0]+x[1],reverse=True)
T=T[:M]
s=sum([*map(sum,T)])
print(s)

Submission status: Runtime Error

Solution code:
mport numpy as np

n,m = map(int,input().split())
l =list(map(int,input().split()))
v=np.array(l)
vv=np.zeros((n,n))
for i in range(n):
    vv[i] += v
for j in range(n):
    vv[:,j] += v.T
vv.flatten
np.sort(vv)
ans=np.sum(vv[n-m:n])
print(ans)

Submission status: Runtime Error

Solution code:
#E - Handshake
N,M = list(map(int, input().split()))
A=[int(input()) for i in range(N)]

pattern = []
for f1 in A:
    for f2 in A:
        pattern.append(f1 + f2)
        
pattern2 = sorted(pattern, reverse=True)

sum = 0
for i in range(0,M):
    sum = sum + pattern2[i]
print(sum)

Submission status: Runtime Error

Solution code:
import numpy as np
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=[]
a=sorted(a,reverse=True)
for i in range(min(len(a),np.sqrt(m)+1)):#int(np.sqrt(m))+1):
    for j in range(len(a)):
        b.append(a[i]+a[j])
b=sorted(b,reverse=True)
ct=0
for i in range(m):
    ct+=b[i]
print(ct)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
A = list(map(int,input().split()))
h=0
sorted(A, reverse=True)
h+= A[0]+A[0]
h+= (A[0]+A[1])*2


for i in range(1,M):
  h+= A[i]+A[i]
  h+= (A[i]+A[i+1])*2
  
print(h)

Submission status: Runtime Error

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

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

ans=0
inx=-1
while m>=0:
 m-=2*n-1
 ans+=a[inx]*(2*n-1)+b[inx-1]*2
 if m<0:
  m=abs(m)
  if m%2==0:
#   ans-=a[inx]*(m//2)+b[m//2+1]
   ans-=a[inx]*(m-1)+b[m//2]*2
  else:
#   ans-=a[inx]*(m//2)+b[m//2+1]+(a[inx]+a[m//2+1])
   ans-=a[inx]*(m-1)+b[m//2]*2+a[m//2]
  print(ans)
  exit()

 inx-=1

Submission status: Runtime Error

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
    count_l = 0
    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
            count_l = count
        else: h = m

    total = 0
    count = 0
    Q = []
    heapq.heapify(Q)
    min_out = 10000000
    for i, a in enumerate(A):
        j = bisect_left(A, l - a)  
        total += a * (N - j) + (S[N] - S[j])
        count += N - j
        min_out = min(min_out, a + A[j])

    if count > M:
        total -= min_out * (count - M)

    print(total)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
import sys
import itertools

n,m=input().strip().split()
a=input().strip().split()

n=int(n)
m=int(m)

for i in range(len(a)):
    a[i]=int(a[i])

a.sort(reverse=True)

bef=-1
pw=0
cnt=0

for i in itertools.permutations(a,2):
    print(i)
    if bef!=i[0]:
        pw+=i[0]*2
        bef=i[0]
        cnt+=1
    
    if cnt==m:
      print(pw)
      sys.exit()
    
    pw+=(i[0]+i[1])
    cnt+=1

    if cnt==m:
      print(pw)
        pw+=(i[0]+i[1])
    cnt+=1

    if cnt==m:
      print(pw)
      
      sys.exit()

Submission status: Runtime Error

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

l = sorted(a, reverse=True)
print(l)
total = 0
M = m - (m//3)*3
i = 0
while i < m//3:
    total += l[i]+l[i] + l[i]+l[i+1] + l[i+1]+l[i]
    i += 1
if M == 1:
    total += l[i+1]+l[i+1]
elif M == 2:
    total += l[i]+l[i] + l[i]+l[i+1]
print(total)

Submission status: Runtime Error

Solution code:
import sys
n, m = map(int, input().split())
a = list(map(int, sys.stdin.readline().split())).sort()
a = a[m//2:m+1]
pow = []
res = 0
pow = [a[i] + a[j] for i in range(n) for j in range(n)]
pow.sort(reverse=True)
for i in range(m):
    res += pow[i]
print(res)

Submission status: Runtime Error

Solution code:
import sys
import bisect
import itertools


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, m = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a.sort()
    t = 0
    c = 0
    for i in range(a[-1] * 2, a[1] - 1, -1):
        for j in range(n - 1, -1, -1):
            pl = bisect.bisect_left(a, i - a[j])
            pr = bisect.bisect_right(a, i - a[j])
            if pl != pr:
                if pr - pl == 1:
                    c += (pr - pl)
                    t += (pr - pl) * i
                else:
                    c += 2 ** (pr - pl - 1)
                    t += 2 ** (pr - pl - 1) * i
            if c > m:
                t -= (c - m) * i
                print(t)
                exit()


if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
N,M = map(int, input().split())
A = list(map(int,input().split()))
SA = sorted(A,reverse=True)
cnt=0
score = 0
for i in range(N):
    for j in range(i,N,1):
        if A[i]+A[j]>A[i+1]+A[i+2]:
            score =A[i]+A[j]
            cnt +=1
            if cnt == M:
                print(score)        
        else:
            break

Submission status: Runtime Error

Solution code:
import numpy as np

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

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


A_arr = np.sort(np.array(A))[::-1]
A_arr = A_arr.reshape([N, 1])

A_mat = A_arr.dot(np.ones((1, N)))

A_mat = A_mat + A_mat.T

A_flat = A_mat.flatten()

A_flat = np.sort(A_flat)[::-1]

print(int(np.sum(A_flat[:M])))

Submission status: Runtime Error

Solution code:
import heapq

n,m = map(int, input().split())
A = sorted(list(map(int, input().split())), reversed=True)

cnt = 0
t = 1
cond = True
lim = (1+n)*n//2
while True:
	if cnt + t < m:
		break
	elif cnt + t == m:
		cnt += t
		t += 1
		break

	cnt += t

	if t == n:
		cond = False

	if cond:
		t += 1
	else:
		t -= 1



res = m-cnt

ans = 0

if cond:
	for i in range(t-1):
		ans += A[i] * (2*(t-1-i))

	q = []

	for i in range(t):
		heapq.heappush(-(A[i]+A[t-1-i]), q)
	for i in range(res):
		ans += heapq.heappop(q)*(-1)

else:
	mmm = 0
	for i in range(t-1):
		mmm += A[-(i+1)] * (2*(t-i-1))
		ans = sum(A)*n*2 - mmm
	for i in range(t):
		for i in range(t):
			heapq.heappush(-(A[i]+A[t-1-i]), q)
	for i in range(res):
		ans += heapq.heappop(q)*(-1)

print(ans)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
import numpy as np
A = sorted(np.array(list(map(int,input().split()))),reverse=True)
ans = 0
while M>0:
  a = A.pop(0)
  ans += a*2
  M-=1
  print(a)
  if (M>0)and(a>=A[0]*2):
    for i in A:
      ans+=(i+a)*2
      M-=2
      if M==0:
        break
      elif M<0:
        ans-=(i+a)
        break
        
  elif (M>0)and(a<A[0]*2):
    b=A[0]*2-a
    for i in A:
      if i>=b:
        ans+=(i+a)*2
        M-=2
        if M==0:
          break
        elif M<0:
          ans-=(i+a)
          break
      else:
        break
print(ans)

Submission status: Runtime Error

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

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

import itertools
l = A
p = itertools.permutations(A,2)

permlist = []
for perm in p:
    permlist.append(perm)

permsumlist = []

for perm in permlist:
    permsumlist.append(sum(perm))

new_list_reverse = sorted(permsumlist, reverse=True)

answer = max(A) + max(A)

for i in range(m-1):
    answer += new_list_reverse[i]

print(answer)

Submission status: Runtime Error

Solution code:
N,M=[int(i) for i in input().split()]
A=sorted([int(i) for i in input().split()])
A=A[::-1]

T=[]
for i in A:
    for j in A:
        T.append([i,j])

T.sort(key=lambda x: x[0]+x[1],reverse=True)
T=T[:M]
print(sum([*map(sum,T)]))

Submission status: Runtime Error

Solution code:
import sys
n, m = map(int, input().split())
a = list(map(int, sys.stdin.readline().split())).sort()
a = a[m//2 - 3:m+1]
pow = []
res = 0
pow = [a[i] + a[j] for i in range(n) for j in range(n)]
pow.sort(reverse=True)
for i in range(m):
    res += pow[i]
print(res)

Submission status: Runtime Error

Solution code:
import numpy as np
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=[]
a=sorted(a,reverse=True)
for i in range(int(m/2)+1):#int(np.sqrt(m))+1):
    for j in range(len(a)):
        b.append(a[i]+a[j])
b=sorted(b,reverse=True)
ct=0
for i in range(m):
    ct+=b[i]
print(ct)

Submission status: Runtime Error

Solution code:
import math


def main():
    n, m = [int(x) for x in input().split()]
    powers = [int(x) for x in input().split()]

    powers.sort(reverse=True)
    powers = powers[0:-1]

    scores = []
    for i in range(n):
        for j in range(n):
            scores.append(powers[i] + powers[j])

    scores.sort(reverse=True)
    print(sum(scores[0:m]))


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N,M = map(int, input().split())
A = list(map(int,input().split()))
SA = sort(A,revers=True)
cnt=0
score = 0
for i in range(N):
    for j in range(i,N,1):
        if A[i]+A[j]>A[i+1]+A[i+2]:
            score =A[i]+A[j]
            cnt +=1
            if cnt == M:
                print(score)        
        else:
            break

Submission status: Runtime Error

Solution code:
N,M=[int(i) for i in input().split()]
A=sorted([int(i) for i in input().split()])[::-1]

T=[]
for i in A:
    for j in A:
        T.append([i,j])

T.sort(key=lambda x: x[0]+x[1],reverse=True)
T=T[:M]
print(sum([*map(sum,T)]))

Submission status: Runtime Error

Solution code:
import math

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

nums.sort(reverse=True)

output = 0
if m <= 2:
    output = nums[0]*m
else:
    output = (nums[0]*2)
    if (m-1) % 2 == 0:
        for i in range(int((m-1)/2)):
            output = output + ((nums[0] + nums[i+1]) * 2)
    else:
        last_i = 0
        for i in range(int(math.floor((m-1)/2))):
            output = output + ((nums[0] + nums[i+1]) * 2)
            last_i = i
        output = output - (nums[0] + nums[last_i])
print(output)

Submission status: Runtime Error

Solution code:
import sys

nm = list(input().split(" "))
N = int(nm[0])
M = int(nm[1])
A = list(input().split(" "))
A.sort()
A.reverse()

ans = 0

start ,counter  = 0 , 0
flag1 = 0
flag2 = 0 # repete
flag3 = 0 # next step
for i in range(0,M):
    if flag1 == 0:
        ans += int(A[start]) + int(A[counter])
        counter += 1
        flag1 = 1
    elif flag2 == 0:
        ans += int(A[start]) + int(A[counter])
        flag2 = 1
    else:
        ans += int(A[start]) + int(A[counter])
        flag2 = 0
        if flag3 == 0:
            counter += 1
            if counter == N-1:
                flag3 = 1
        else:
            flag1 = 0
            flag3 = 0
            start += 1
            counter = start

print(ans)

Submission status: Runtime Error

Solution code:
n,m = map(int, input().strip().split())
A = list(map(int, input().strip().split()))
comb=[0]*n**2
for i in range(n):
    for j in range(n):
        comb[i+j*n]=A[i]+A[j]
comb.sort(reverse=True)
print(sum(comb[:m]))

Submission status: Runtime Error

Solution code:
import sys
import bisect
import itertools


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, m = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a.sort()
    acm = list(itertools.accumulate(a))
    t = 0
    c = 0
    for i in range(a[-1] * 2, a[1] - 1, -1):
        for j in range(n - 1, -1, -1):
            pl = bisect.bisect_left(a, i - a[j])
            pr = bisect.bisect_right(a, i - a[j])
            if pl != pr:
                c += (pr - pl)
                t += (pr - pl) * i
            if c > m:
                t -= (c - m) * i
                print(t)
                exit()


if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
import numpy as np
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=[]
a=sorted(a,reverse=True)
for i in range(int(np.sqrt(m))+1):
    for j in range(len(a)):
        b.append(a[i]+a[j])
b=sorted(b,reverse=True)
ct=0
for i in range(m):
    ct+=b[i]
print(ct)

Submission status: Runtime Error

Solution code:
import numpy as np
import heapq

N, M = map(int, input().split())
A = np.array(input().split(), np.int32)
A2 = np.tile(A, (N, 1))
s = (A2.T + A2).reshape(-1)

#print(s)
print(sum(heapq.nlargest(M, s)))

Submission status: Runtime Error

Solution code:
N,M=[int(i) for i in input().split()]
A=sorted([int(i) for i in input().split()])[::-1]

done=[]
point=0
count=0
T=[]
for i in A:
    for j in A:
        T.append([i,j])

T.sort(key=lambda x: x[0]+x[1])
T=T[-M:]
print(sum([*map(sum,T)]))

Submission status: Runtime Error

Solution code:
n,m=map(int, input().split())
A=list(map(int, input().split()))
A.sort(reverse=True)
#print(A)


place=[[0,0],[1,1]]

count=0
mcount=0
#for _ in range(m):

while mcount<m:
    v1=A[place[0][0]]+A[place[0][1]]
    v2=A[place[1][0]]+A[place[1][1]]
    if v1>=v2:
        if  place[0][0]==place[0][1]:
            count+=v1
            mcount+=1
        else:
            if mcount+1==m:
                count+=v1
                mcount+=1
            else:
                count+=v1*2
                mcount+=2

        if place[0][1]==len(A)-1:
            #ラストのケース
            if place[1][0]==len(A)-1:
                place[0][0]=place[1][0]
                place[0][1]=place[1][0]
            else:
                place[0][0]=place[1][0]+1
                place[0][1]=place[1][0]+1
        else:
            place[0][1]+=1

    else:
        if  place[1][0]==place[1][1]:
            count+=v2
            mcount+=1
        else:
            if mcount+1==m:
                count+=v2
                mcount+=1
            else:
                count+=v2*2
                mcount+=2

        if place[1][1]==len(A)-1:
            if place[0][0]==len(A)-1:
                place[1][0]=place[0][0]
                place[1][1]=place[0][0]
            else:
                place[1][0]=place[0][0]+1
                place[1][1]=place[0][0]+1
        else:
            place[1][1]+=1
    #print(place)

print(count)

Submission status: Runtime Error

Solution code:
import sys
import heapq
readline = sys.stdin.buffer.readline
import itertools
n,m=map(int,readline().split())
alst=[int(i) for i in readline().split()]
alst.sort(reverse=True)
a=0
c=[0]*m
heapq.heapify(c)

for x,y in itertools.product(alst, repeat = 2):
  ta = x+y
  if ta > c[0]:
    heapq.heappushpop(c, ta)
print(sum(c))

Submission status: Runtime Error

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

b = []
for i in range(n):
  for j in range(n):
    b.append(a[i] + a[j])
    if a[i] + a[j] == 115:
      print(str(i) + "," 

b.sort(reverse=True)

print(sum(b[:m]))

Submission status: Runtime Error

Solution code:
#coding:utf-8
import heapq
from itertools import combinations
import numpy as np
N,M = map(int,input().split())
A = list(map(int,input().split()))
S = sorted(A)[::-1]
S = np.array(S)
S = S.reshape(-1,1)
array =  S + S.T


h = []
heapq.heappush(h,(-array[0][0],0,0))
visited = np.zeros_like(array)
ans = 0
for m in range(M):
    while(1):
        v = heapq.heappop(h)
        if (visited[v[1],v[2]]==0):
            visited[v[1],v[2]] = 1
            break
    ans -= v[0]
    if v[1] < N-1 and visited[v[1]+1,v[2]]==0:
        heapq.heappush(h,(-array[v[1]+1,v[2]],v[1]+1,v[2]))
    if v[2] < N-1 and visited[v[1],v[2]+1]==0:
        heapq.heappush(h,(-array[v[1],v[2]+1],v[1],v[2]+1))
print (ans)

Submission status: Runtime Error

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
        for i, a in enumerate(A): count += N - bisect_left(A, m - a)
        if count >= M: l = m
        else: h = m

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

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

    print(total)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
import numpy as np

N, M = map(int, input().split())
A = np.array(list(map(int, input().split())), dtype='int')

A.sort()
A = A[::-1]
xx, yy = np.meshgrid(A, A)
arr = (xx + yy).flatten()
arr.sort()
arr = arr[::-1]
print(arr[:M].sum())

Submission status: Runtime Error

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
        for i, a in enumerate(A): count += N - bisect_left(A, m - a)
        if count >= M: l = m
        else: h = m

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

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

    print(total)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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: Runtime Error

Solution code:
import numpy as np
import itertools

n, m = map(int, input().split())
arr = np.array(list(map(int, input().split())))
s_arr = np.sort(arr)[::-1]
l = len(s_arr)
zeros = np.zeros((l,l))
s1 = zeros + s_arr
s2 = s1.T + s_arr
s3 = np.hstack(s2)
s4 = np.sort(s3)[::-1]
print(int(s4[:m].sum()))

Submission status: Runtime Error

Solution code:
#coding:utf-8
import heapq
from itertools import combinations
import numpy as np
N,M = map(int,input().split())
A = list(map(int,input().split()))
S = sorted(A)[::-1]
S = np.array(S)
S = S.reshape(-1,1)
array =  S + S.T


h = []
heapq.heappush(h,(-array[0][0],0,0))
visited = np.zeros_like(array)
ans = 0
for m in range(M):
    #print (h)
    while(1):
        #print ("#",h)
        v = heapq.heappop(h)
        #print (v)
        #print (h)
        if (visited[v[1],v[2]]==0):
            visited[v[1],v[2]] = 1
            break
    #print (v[0])
    ans -= v[0]
    if v[1] < N-1:
        heapq.heappush(h,(-array[v[1]+1,v[2]],v[1]+1,v[2]))
    if v[2] < N-1:
        heapq.heappush(h,(-array[v[1],v[2]+1],v[1],v[2]+1))
print (ans)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort(reverse=True)
cnt=0
x=0
j=0
for i in range(m):
    cnt=cnt+a[i]+a[j]
    x=x+1
    if(x>=m):
        print(cnt)
        exit()
    cnt=cnt+a[j]+a[i]
    x=x+1
    j=j+1
    if(x>=m):
        print(cnt)
        exit()

Submission status: Runtime Error

Solution code:
import numpy as np
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
B = np.tile(np.array(A),(N,1))
C = B+B.T
D = list(np.ravel(C))
D.sort(reverse=True)
print(sum(D[:M]))

Submission status: Runtime Error

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

list.sort(a, reverse = True)
b = []

for i in range(m):
  for j in range(m):
    b.append(a[i]+a[j])
  
list.sort(b, reverse = True)

ans = 0
for i in range(m):
  ans += b[i]
  
print(ans)

Submission status: Runtime Error

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

import heapq
import numpy as np
B = np.zeros((N, N))
for i in range(N):
  for j in range(N):
    B[i, j] = A[i] + A[j]
B = B.flatten()*(-1)
B = B.tolist()
point = 0
heapq.heapify(B)
for i in range(M):
  point += heapq.heappop(B)
print(int(-1*point))

Submission status: Runtime Error

Solution code:
n,m=map(int, input().split())
A=list(map(int, input().split()))
A.sort(reverse=True)
#print(A)


place=[[0,0],[1,1]]

count=0
mcount=0
#for _ in range(m):
while mcount<m:
    v1=A[place[0][0]]+A[place[0][1]]
    v2=A[place[1][0]]+A[place[1][1]]
    if v1>=v2:
        if  place[0][0]==place[0][1]:
            count+=v1
            mcount+=1
        else:
            if mcount+1==m:
                count+=v1
                mcount+=1
            else:
                count+=v1*2
                mcount+=2

        if place[0][1]==len(A)-1:
            place[0][0]=place[1][0]+1
            place[0][1]=place[1][0]+1
        else:
            place[0][1]+=1

    else:
        if  place[1][0]==place[1][1]:
            count+=v2
            mcount+=1
        else:
            if mcount+1==m:
                count+=v2
                mcount+=1
            else:
                count+=v2*2
                mcount+=2

        if place[1][1]==len(A)-1:
            place[1][0]=place[0][0]+1
            place[1][1]=place[0][0]+1
        else:
            place[1][1]+=1
    #print(place)

print(count)

Submission status: Runtime Error

Solution code:
import numpy as np

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

A.sort()
A = A[::-1]
xx, yy = np.meshgrid(A, A)
arr = (xx + yy).flatten()
arr.sort()
arr = arr[::-1]
print(arr[:M].sum())

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
import numpy as np
B = np.tile(np.array(A),(N,1))
C = B+B.T
D = list(np.ravel(C))
D.sort(reverse=True)
print(sum(D[:M]))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=[]
for i in range(len(a)):
    for j in range(i,len(a)):
        b.append(a[i]+a[j])
b=sorted(b,reverse=True)
ct=0
for i in range(m):
    ct+=b[i]
print(ct)

Submission status: Runtime Error

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)]
    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, 200001
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: Runtime Error

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
        for i, a in enumerate(A): count += N - bisect_left(A, m - a)
        if count >= M: l = m
        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)
        total += a * (N - j) + (S[N] - S[j])
        count += (N - j)
        for k in range(min(3, N - j)):
            heapq.heappush(Q, a + A[j + k])
        #print(a, j)

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

    print(total)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
hslist = list(map(int,input().split()))
hslist.sort(reverse=True)
happylist = [0]*n**2
count=0
for i in range(n):
    for j in range(i,n):
        if i == j:
            happylist[count] = hslist[i]+hslist[j]
            count += 1
        else:
            happylist[count] = hslist[i]+hslist[j]
            count += 1
            happylist[count] = hslist[i]+hslist[j]
            count += 1

happylist.sort(reverse=True)

print(sum(happylist[0:m]))

Submission status: Runtime Error

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

lis = []
for i in range(n-1):
    for j in range(i+1,n):
        c = a[i]+a[j]
        if c < a[i+1]:
            break
        lis.append(c)
lis.sort()
lis.reverse()
temp = lis[:m//2]*2
for i in range(n):
    if 2 * a[i] < min(temp):
        break
    temp.append(2*a[i])
temp.sort()
temp.reverse()
ans = temp[:m]
print(sum(ans))

Submission status: Runtime Error

Solution code:
# E
# 全列挙は多分時間が足りない

import numpy as np
N, M = map(int, input().split())
A = np.array(input().split(), dtype=int)
A = np.sort(A)[::-1] # ソートしても変わらない

ret = np.sort((A.reshape(-1,1) + A.reshape(1,-1)).reshape(-1))[-M:].sum()
print(ret)

Submission status: Runtime Error

Solution code:
# K回前と現在の問いが同じだったら0点

str1 = input().split(" ")
str2 = input().split(" ")
t = input()

n = int(str1[0])
k = int(str1[1])

r = int(str2[0])
s = int(str2[1])
p = int(str2[2])

skill = {"r":p,"s":r,"p":s}

result = 0
douchilist = []
for i in range(0,n):
    if i >= k:
        if t[i] == t[i-k] and i-k not in douchilist:
            result += 0
            douchilist.append(i)
            continue

    result += skill[t[i]]
    continue


print(result)

Submission status: Runtime Error

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

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

B = [0]*(N*N)
k = 0
for i in range(N):
    for j in range(i, N):
        if i==j:
            B[k] = (A[i]+A[j])
            k += 1
        else:
            B[k] = (A[i]+A[j])
            B[k+1] = (A[i]+A[j])
            k += 2

B.sort(reverse=True)

print(sum(B[:M]))

Submission status: Runtime Error

Solution code:
from heapq import heappush, heappop


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

same_num = []
for a in A:
    heappush(same_num, -2 * a)
diff_num = []
for i in range(N):
    for j in range(i+1, N):
        heappush(diff_num, -A[i] - A[j])

while True:
    s = -1 * heappop(same_num)
    d = -1 * heappop(diff_num)
    if s > d:
        ans += s
        M -= 1
        heappush(diff_num, -d)
    else:
        ans += 2 * d
        M -= 2
        heappush(same_num, -s)
    if M <= 0:
        break
if M == -1:
    ans -= d
print(ans)

Submission status: Runtime Error

Solution code:
import copy
import heapq, math
n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort(reverse=True)
heap = []
temp = n//m
for i in range(n):
    for k in range(n):
        cur = a[i] + a[k]
        if cur < a[temp]:
            break
        heapq.heappush(heap,-(a[i] + a[k]))
ans = 0
for _ in range(m):
    ans += heapq.heappop(heap)
print(-ans)

Submission status: Runtime Error

Solution code:
import math
testcase = int(input())
for x in range(testcase):
    x = int(input())
    print(int(x*(x-1)/2))

Submission status: Runtime Error

Solution code:
import heapq, math
n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a.sort(reverse=True)
heap = []
temp = m//n
for i in range(n):
    for k in range(n):
        cur = a[i] + a[k]
        if cur < a[temp]:
            break
        heapq.heappush(heap,-(a[i] + a[k]))
ans = 0
for _ in range(m):
    ans += heapq.heappop(heap)
print(-ans)