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: Time Limit Exceeded

Solution code:
import itertools

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

pair = list(itertools.product(A, repeat=2))
pair = sorted(pair, key=lambda x:sum(x), reverse=True)

score = sum(map(lambda x: sum(x), pair[0:M]))

print(score)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations_with_replacement, product

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

a = [int(x) for x in input().split(' ')]
a.sort(reverse=True)
ch = 0
pairs = sorted(product(a, repeat=2), key=lambda x: sum(x), reverse=True)


for i in range(m):
    ch += sum(pairs[i])



print(ch)

Submission status: Time Limit Exceeded

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),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: Time Limit Exceeded

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

from itertools import product
a.sort(reverse=True)
aa = list(product(a[:m], repeat=2))
print(sum(sorted(list(map(lambda x: x[0] + x[1], aa)), reverse=True)[:m]))

Submission status: Time Limit Exceeded

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

 
for i in range(N):
    for j in range(N):
        h.append(A[i]+A[j])
 
 
h.sort(reverse=True)

print(sum(h[:M]))

Submission status: Time Limit Exceeded

Solution code:
tmp = input().split()

N = int(tmp[0])
M = int(tmp[1])
DA = M
A = list(map(int, input().split()))

A.sort(reverse = True)
# print(A)

dada = []

for i in range(N):

    for j in range(N):

        dada.append(A[i] + A[j])
        # print(dada)
        if j == M-2:
            M -= 1
            break
    
    if i == M:
        break

dada.sort(reverse = True)
# print(dada)

ans = 0
for i in range(DA):
    ans += dada[i]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
A.reverse()
cnt_list = [0 for i in range(N)]
ans = 0
shake_cnt = M
max_index = None
max_shake = -1
ans = 0
for loop in range(shake_cnt):
    for i in range(N):
        if cnt_list[i]<=N-1:
            if max_shake<A[i]+A[cnt_list[i]]:
                max_shake = A[i]+A[cnt_list[i]]
                max_index = i
        if cnt_list[i]==0:
            break
    ans+=max_shake
    cnt_list[max_index]+=1
    max_shake = -1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
# a.sort(reverse=True)
p = []
for i in range(0,n):
    for j in range(0,n):
        p.append(a[i] + a[j])
p.sort(reverse=True)
res = 0
for i in range(0,m):
    res += p[i]
print(res)

Submission status: Time Limit Exceeded

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

length = N

B_n = [A_n[i]+A_n[j] for j in range(length) for i in range(length)]

B_n = sorted(B_n)

ans_list = [B_n.pop() for i in range(M)]

print(sum(ans_list))

Submission status: Time Limit Exceeded

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
cnt = 0
t = 0
for i in range(N):
    tmp = bisect.bisect_left(reva,Kmax-reva[N-i-1])
    tmp2 = bisect.bisect_right(reva,Kmax-reva[N-i-1])
    if tmp!=tmp2:
        t = 1
    tmp = N - tmp
    cnt += tmp
    res += tmp*a[i]+r[tmp]
if t==1:
    res -= (cnt-M)*Kmax
print(res)

Submission status: Time Limit Exceeded

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


b = []
for i in range(N-1):
    for j in range(i+1, N):
        b.append([A[i]+A[j],2])
b.extend([[A[i]*2,1] for i in range(N)])
b = sorted(b, key=lambda x:-x[0])
ans = 0
for one in b:
    if one[1]>1 and M==1:
        ans+=one[0]
        break
    M-=one[1]
    ans+=one[0]*one[1]
    if M==0:
        break
print(ans)

Submission status: Time Limit Exceeded

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

from itertools import product
aa = list(product(a, repeat=2))
print(sum(sorted(list(map(lambda x: x[0] + x[1], aa)), reverse=True)[:m]))

Submission status: Time Limit Exceeded

Solution code:
def merge_sort(arr):
    if len(arr) <= 1:
        return arr

    mid = len(arr) // 2
    # ここで分割を行う
    left = arr[:mid]
    right = arr[mid:]

    # 再帰的に分割を行う
    left = merge_sort(left)
    right = merge_sort(right)

    # returnが返ってきたら、結合を行い、結合したものを次に渡す
    return merge(left, right)


def merge(left, right):
    merged = []
    l_i, r_i = 0, 0

    # ソート済み配列をマージするため、それぞれ左から見ていくだけで良い
    while l_i < len(left) and r_i < len(right):
        # ここで=をつけることで安定性を保っている
        if left[l_i] <= right[r_i]:
            merged.append(left[l_i])
            l_i += 1
        else:
            merged.append(right[r_i])
            r_i += 1

    # 上のwhile文のどちらかがFalseになった場合終了するため、あまりをextendする
    if l_i < len(left):
        merged.extend(left[l_i:])
    if r_i < len(right):
        merged.extend(right[r_i:])
    return merged

N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
 
h = []

 
for i in range(N):
    for j in range(N):
        h.append(A[i]+A[j])
 
 
h=merge_sort(h)

print(sum(h[len(h)-M:]))

Submission status: Time Limit Exceeded

Solution code:
import itertools
n, m = map(int, input().split())
A = list(map(int, input().split()))

A.sort(reverse=True)

B = list(itertools.product(A,A))
B.sort(key=lambda x: sum(x), reverse=True)

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

Submission status: Time Limit Exceeded

Solution code:
import sys
n, m = map(int, input().split())
a = list(map(int, sys.stdin.readline().split()))
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: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

sortA = sorted(A,reverse=True)
# print("sortA:",sortA)

handshake = 0
flag = 0

score = []

for i in range(N):
    for j in range(N):
#         print("i,j:",i,j)
#         print("sortA[i],sortA[j]:",sortA[i],sortA[j])
        score.append(sortA[i] + sortA[j])

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
n,m=map(int,input().split())
pows=list(map(int,input().split()))

pairpows=[s+t for s,t in product(pows,pows)]
print(sum(sorted(pairpows)[-m:]))

Submission status: Time Limit Exceeded

Solution code:
import heapq
N,M=map(int,input().split())
A=list(map(int,input().split()))
A=list(sorted(A,reverse=True))
L=[0]*N
LIST=[]
heapq.heapify(LIST)

for i in range(N):
    heapq.heappush(LIST,((A[0]+A[i])*-1,i))

s=0
c=0
while(M>c):
    t=heapq.heappop(LIST)
    s-=t[0]
    L[t[1]]+=1
    if(N>L[t[1]]):  
        heapq.heappush(LIST,((A[t[1]]+A[L[t[1]]])*-1,t[1]))
    c+=1

print(s)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N, M = map(int, input().split())
A = list(map(int, input().split()))
perm = list(itertools.permutations(A, 2))
scores = [sum(perm[i]) for i in range(len(perm))]
scores.extend([A[i]*2 for i in range(len(A))])
scores = sorted(scores, reverse=True)

print(sum(scores[:M]))

Submission status: Time Limit Exceeded

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)
    for i, a in enumerate(A):
        j = bisect_left(A, l - a)  
        total += a * (N - j) + (S[N] - S[j])
        count += N - j
        if count_l > M:
            double = bisect_left(A, h - a)
            for k in range(j, min(N - 1, double + 1)):
                heapq.heappush(Q, a + A[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: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
l=list(map(int,input().split()))
ll=[]
for i in range(n):
	for j in range(n):
		ll.append(l[i]+l[j])
ll=sorted(ll)[::-1]
print(sum(ll[:m]))

Submission status: Time Limit Exceeded

Solution code:
l = input().split()
n = int(l[0])
m = int(l[1])

l = [int(i) for i in input().split()]
r = []

for i in range(n):
  for k in range(n):
    r.append(l[i] + l[k])

r.sort(reverse=True)
result = 0
for i in range(m):
  result += r[i]

print(result)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

pair = list(itertools.product(A, repeat=2))
pair = sorted(pair, key=lambda x: x[0]+x[1], reverse=True)

for i in range(M):
  sum += pair[i][0] + pair[i][1]

print(sum)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n, m = map(int, input().split())
a = list(map(int, input().split()))
c = list(itertools.product(a,a))
d = []
for i in range(len(c)):
  d.append(sum(c[i]))
d.sort(reverse=True)
print(sum(d[:m]))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int,input().split())
guest = sorted(list(map(int,input().split())),reverse=True)
pattern = [guest[_] * 2 for _ in range(n)]

for i in range(n-1):
    for j in range(i+1, n):
        pattern.append(guest[i] + guest[j])
        pattern.append(guest[i] + guest[j])

pattern.sort(reverse=True)
ans = 0
for i in range(m):
    ans += pattern[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int,input().split())
A = list(map(int,input().split()))
s_list= []
#A.sort()

for i in reversed(range(N)):
    for j in reversed(range(N)):
        s_list.append(A[i] + A[j])

s_list.sort()

print(sum(s_list[N**2-M:]))

Submission status: Time Limit Exceeded

Solution code:
from itertools import product
N, M = map(int, input().split())
A = [int(x) for x in input().split()]
l = list(x+y for x, y in product(A, repeat=2))
l.sort()
print(sum(l[-M:]))

Submission status: Time Limit Exceeded

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])

b.sort(reverse=True)

print(sum(b[:m]))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
a = list(map(int, input().split()))
import itertools
c = list(map(sum, itertools.product(a, repeat=2)))
c.sort(reverse=True)
print(sum(c[:m]))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
A.reverse()
cnt_list = [0 for i in range(N)]
ans = 0
shake_cnt = M
max_index = None
max_shake = -1
ans = 0
while 0 < shake_cnt:
    for i in range(N):
        if cnt_list[i]<=N-1:
            if max_shake<A[i]+A[cnt_list[i]]:
                max_shake = A[i]+A[cnt_list[i]]
                max_index = i
        if cnt_list[i]==0:
            break
    ans+=max_shake
    cnt_list[max_index]+=1
    shake_cnt-=1
    max_shake = -1
print(ans)

Submission status: Time Limit Exceeded

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

S=sorted([sum(t) for t in product(A,A)],reverse=True)
print(sum(S[:M]))

Submission status: Time Limit Exceeded

Solution code:
N, M = (int(x) for x in input().split())
A=input().split()

power_list = []
for i in range(N):
    for j in range(N):
        power_list.append(int(A[i])+int(A[j]))
power_list.sort(reverse=True)

score = 0
for i in range(M):
    score += power_list[i]
print(score)

Submission status: Time Limit Exceeded

Solution code:
N, M = map( int, input().split() )
A = [ int(x) for x in input().split() ]

#A.sort()

temp = []
for i in range(N) :
   for j in range(N) :
      if j==i :
         temp.append(A[N-1-i]+A[N-1-j])
      elif j>i :
         temp.append(A[N-1-i]+A[N-1-j])
         temp.append(A[N-1-i]+A[N-1-j])

temp.sort()
#print(temp)

sum = 0
for k in range(M) :
    sum += temp[len(temp)-1-k]

print(sum)

Submission status: Time Limit Exceeded

Solution code:
from heapq import *
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    n,m=MI()
    aa=LI()
    aa.sort(reverse=True)
    fin=set()
    hp=[]
    heappush(hp,[-aa[0]*2,0,0])
    fin.add((0,0))
    ans=0
    for _ in range(m):
        a,i,j=heappop(hp)
        ans-=a
        if i+1<n and (i+1,j) not in fin:
            heappush(hp,[-aa[i+1]-aa[j],i+1,j])
            fin.add((i+1,j))
        if j+1<n and (i,j+1) not in fin:
            heappush(hp,[-aa[i]-aa[j+1],i,j+1])
            fin.add((i,j+1))

    print(ans)

main()

Submission status: Time Limit Exceeded

Solution code:
N, M = map( int, input().split() )
A = [ int(x) for x in input().split() ]

A.sort()

temp = []
for i in range(N) :
   for j in range(N) :
      if j==i :
         temp.append(A[N-1-i]+A[N-1-j])
      elif j>i :
         temp.append(A[N-1-i]+A[N-1-j])
         temp.append(A[N-1-i]+A[N-1-j])

temp.sort()
#print(temp)

sum = 0
for k in range(M) :
    sum += temp[len(temp)-1-k]

print(sum)

Submission status: Time Limit Exceeded

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])
  
list.sort(b, reverse = True)

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

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
kouho=[]
for i in range(n):
    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: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
a =  [int(n) for n in input().split()]
b = [i + j for i in a for j in a ]
b.sort(reverse=True)
print(sum(b[:m]))

Submission status: Time Limit Exceeded

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)

    scores = []
    min_score = 0
    for i in range(n):
        for j in range(n):
            p = powers[i] + powers[j]
            if p < min_score:
                break
            scores.append(p)
        if len(scores) >= m:
            scores.sort(reverse=True)
            scores = scores[0:m]
            min_score = scores[-1]

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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = []
for i in range(0, n):
    for j in range(0, n):
        b.append(a[i] + a[j])
b.sort(reverse=True)
score = sum(b[0:m])
print(score)

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
A=list(map(int,input().split()))
A=list(sorted(A,reverse=True))
L=[0]*N
LIST=[]
for i in range(N):
    LIST.append((A[0]+A[i],i))
s=0
c=0
while(M>c):
    LIST=sorted(LIST)
    t=LIST.pop(-1)
    s+=t[0]
    L[t[1]]+=1
    if(N>L[t[1]]):  
        LIST.append((A[t[1]]+A[L[t[1]]],t[1]))
    c+=1

print(s)

Submission status: Time Limit Exceeded

Solution code:
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

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

ads = []
for i in range(len(a)):
    for j in range(len(a)):
        ads.append(a[i] + a[j])
ads.sort(reverse=True)
ans = 0
for i in range(m):
    ans += ads[i]
print(ans)

Submission status: Time Limit Exceeded

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)
        double = bisect_left(A, h - a)
        total += a * (N - j) + (S[N] - S[j])
        count += N - j
        for k in range(j, min(N - 1, double + 1)):
            heapq.heappush(Q, a + A[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: Time Limit Exceeded

Solution code:
N,M = map(int,input().split(' '))
An =list(map(int,input().split(' ')))
#An.sort(reverse=True)

all_list = []
for i,a in enumerate(An):
    all_list.append(a+a)
    for a2 in An[i+1:]:
        all_list.append(a+a2)
        all_list.append(a+a2)
all_list.sort(reverse=True)
print(sum(all_list[:M]))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=[]
a=sorted(a,reverse=True)
for i in range(len(a)):
    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: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=[0 for i in range(n**2)]
for i in range(n):
  for j in range(n):
    b[i*n+j]=a[i]+a[j]
b.sort(reverse=1)
ans=sum(b[:m])






print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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)
        double = bisect_left(A, h - a)
        total += a * (N - j) + (S[N] - S[j])
        count += N - j
        for k in range(j, min(N - 1, double + 1)):
            heapq.heappush(Q, a + A[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: Time Limit Exceeded

Solution code:
import itertools

def multi_int_input():
    return map(int, input().split())

N, M = multi_int_input()
a_list = list(multi_int_input())

result = sorted(itertools.product(a_list, a_list), key=lambda x: x[0]+x[1], reverse=True)
result = [x[0] + x[1] for x in result[:M]]
print(sum(result))

Submission status: Time Limit Exceeded

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

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

for i in range(n):
  for j in range(n):
    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: Time Limit Exceeded

Solution code:
n, m = [int(x) for x in input().split()]
# powers = [(i, int(x)) for i, x in enumerate(input().split())]
# sorted_powers = sorted(powers, key=lambda x: x[1])
powers = [int(x) for x in input().split()]
# sorted_powers = sorted(powers)

conbinations = [powers[i] + powers[j] for i in range(n) for j in range(n)]
sorted_conbi = sorted(conbinations, reverse=True)

print(sum(sorted_conbi[:m]))

Submission status: Time Limit Exceeded

Solution code:
import itertools
N,M = map(int,input().split())
A = sorted(map(int,input().split()))[::-1]
I = M
for i in range(N):
    if i*i > M:
        I = i
A = A[:I]
print(
    sum(
        sorted(
            map(sum,itertools.product(A, repeat=2))
            )[::-1][:M]
        )
    )

Submission status: Time Limit Exceeded

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(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: Time Limit Exceeded

Solution code:
import itertools
import numpy as np

N,M=map(int,input().split())
A=list(map(int,input().split()))
H = [a + b for (a, b) in list(itertools.product(A, A))]
H.sort(reverse=True)
arr_H = np.array(H[:M])
print(np.sum(arr_H))

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
A = list(map(int, input().split()))
values = []
count = 0
ans = 0

for i in range(N):
    for j in range(N):
        values.append(A[i] + A[j])
        
values = sorted(values, reverse=True)

while count != M:
    ans += values[count]
    count += 1
# print(values)
print(ans)

Submission status: Time Limit Exceeded

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 = min(m//n,n-1)
for i in range(n):
    for k in range(i,n):
        cur = a[i] + a[k]
        if cur < a[temp]:
            break
        if i != k:
            heapq.heappush(heap,-(a[i] + a[k]))
        heapq.heappush(heap,-(a[i] + a[k]))
ans = 0
for _ in range(m):
    if len(heap) >= 1:
        ans += heapq.heappop(heap)
print(-ans)

Submission status: Time Limit Exceeded

Solution code:
tmp = input().split()

N = int(tmp[0])
M = int(tmp[1])

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

A.sort(reverse = True)
# print(A)

dada = []

for i in range(N):

    for j in range(N):

        dada.append(A[i] + A[j])
        # print(len(dada))
        # if len(dada) == M:
        #     break

dada.sort(reverse = True)
# print(dada)

ans = 0
for i in range(M):
    ans += dada[i]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def p_e():
    import numpy as np
    n, m = map(int, input().split())
    A = np.array(list(map(int, input().split())))
    x = np.array([])
    for i in range(n):
        x = np.hstack((x, A + np.roll(A, i)))
    print(np.sum(np.sort(x)[::-1][:m], dtype=np.int))
    
p_e()

Submission status: Time Limit Exceeded

Solution code:
import itertools

n,m = map(int, input().split())
A = [int(i) for i in input().split()]

As = [v[0]+v[1] for v in itertools.product(A,repeat=2)]

S = sorted(As,reverse=True)[:m]

print(sum(S))

Submission status: Time Limit Exceeded

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

for i in range(0, N):
    for j in range(0, N):
        C.append(A[i] + A[j])
        
C.sort(reverse=True)
print(sum(C[:M]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

for x, y in itertools.product(a, a):
    anslist.append(x+y)

anslist.sort(reverse=True)

ans = sum(anslist[:m])

print(ans)

Submission status: Time Limit Exceeded

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

a.sort(reverse=True)
lr = []


for ai in a[:m]:
    for aj in a[:m]:
        lr.append(ai+aj)
lr.sort()

print(sum(lr[-m:]))

Submission status: Time Limit Exceeded

Solution code:
import heapq

N,M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

h = []
heapq.heapify(h)

for i in range(N):
    for j in range(i,N):
        if i == j:
            heapq.heappush(h,(-A[i]*2,0))
        else:
            heapq.heappush(h,(-A[i]-A[j],1))


x = 0
for i in range(M):
    a = heapq.heappop(h)
    #print(a)
    x -= a[0]
    if a[1]:
        heapq.heappush(h,(a[0],0))
        
print(x)

Submission status: Time Limit Exceeded

Solution code:
(n, m), *D, = [list(map(int, s.split())) for s in open(0)]
D = D[0]
d_squared = [2*d for d in D]
d_comb = [D[i] + D[j] for i in range(n-1) for j in range(i+1, n)]*2
d_squared.extend(d_comb)
print(sum(sorted(d_squared, reverse=True)[:m]))

Submission status: Time Limit Exceeded

Solution code:
def ints():
  return [int(x) for x in input().split()]
def ii():
  return int(input())

N, M = ints()
A = ints()

H = []
for i in range(N):
  for j in range(N):
    x = A[i]+A[j]
    H.append((x, i, j))
H.sort()
H = list(reversed(H))
s = 0
for i in range(M):
  s += H[i][0]
print(s)

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
#import numpy as np
stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N, M = rl()
A = rl()

B = [x+y for x, y in list(itertools.product(A, A))]
B.sort(reverse=True)

answer = sum(B[:M])
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

scorel = []
for i, j in itertools.permutations(range(N), 2):
    scorel.append(Alist[i]+Alist[j])
for i in range(N):
    scorel.append(Alist[i]*2)
    
print(sum(sorted(scorel, reverse=True)[0:M]))

Submission status: Time Limit Exceeded

Solution code:
import itertools

n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

a.sort(reverse=True)

a = list(itertools.product(a, repeat=2))
b = list(map(lambda x: x[0] + x[1], a))
b.sort(reverse=True)

print(sum(b[:m]))

Submission status: Time Limit Exceeded

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)

    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: Time Limit Exceeded

Solution code:
import heapq
n,m=map(int,input().split())
al=list(map(int,input().split()))
hq=[]
for i in range(n):
    heapq.heappush(hq,-al[i]*2)
    for j in range(i+1,n):
        heapq.heappush(hq,-al[i]-al[j])
        heapq.heappush(hq,-al[i]-al[j])
ans=0
for i in range(m):
    ans-=heapq.heappop(hq)
print(ans)