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 as iters
N,M = [int(i) for i in input().rstrip().split(' ')]
A = [int(i) for i in input().rstrip().split(' ')]
S = [sum(B) for B in iters.product(A,repeat=2)]
S = sorted(S,reverse=True)
#print(S)
wel = 0
for s in S:
    wel += s
    M -= 1
    #print(wel,s)
    if M <= 0:
        break
print(wel)

Submission status: Time Limit Exceeded

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

D=[]
for i in range(1,N+1):
    for j in range(1,N+1):
        if i*j>M:
            break
        else:
            D.append((i-1,j-1))

E=[]
for i in D:
    E.append(A[i[0]]+A[i[1]])

E.sort(reverse=True)
#print(E)
print(sum(E[:M]))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort(reverse=True)
B=[]

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

B.sort(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()))
B=[]

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

B.sort(reverse=True)
ans=0

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
  import bisect
  import sys
  from itertools import accumulate
  input = sys.stdin.readline
  def hsnum(x):
    num = 0
    for i in a:
      y = x-i
      num += n-bisect.bisect_left(a,y)
    return num
  n,m = map(int, input().split())
  a = list(map(int, input().split()))
  a.sort()
  suma = [0]+list(accumulate(a))
  lx = 0
  rx = 2*10**5+2
  error = 0
  while lx < rx:
    mx = (lx+rx)//2
    if hsnum(mx) < m:
      rx = mx
    elif hsnum(mx) > m:
      if hsnum(mx+1) < m:
        error = hsnum(mx)-m
        lx = mx
        break
      lx = mx + 1
    else:
      lx = mx
      break
  ans = 0
  for i in a:
    rema = lx - i
    j = bisect.bisect_left(a,rema)
    ans += (n-j)*i-suma[j]
  ans += suma[n]*n
  print(ans-error*lx)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  import bisect
  import sys
  from itertools import accumulate
  input = sys.stdin.readline
  def hsnum(x):
    num = 0
    for i in a:
      y = x-i
      num += n-bisect.bisect_left(a,y)
    return num
  n,m = map(int, input().split())
  a = list(map(int, input().split()))
  a.sort()
  suma = [0]+list(accumulate(a))
  lx = 0
  rx = 2*10**5+2
  error = 0
  while lx < rx:
    mx = (lx+rx)//2
    if hsnum(mx) < m:
      rx = mx
    elif hsnum(mx) > m:
      if hsnum(mx+1) < m:
        error = hsnum(mx)-m
        lx = mx
        break
      lx = mx + 1
    else:
      lx = mx
      break
  ans = 0
  for i in a:
    rema = lx - i
    j = bisect.bisect_left(a,rema)
    ans += (n-j)*i+suma[n]-suma[j]
  print(ans-error*lx)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  import bisect
  import sys
  from itertools import accumulate
  input = sys.stdin.readline
  def hsnum(ls,x):
    num = 0
    for i in ls:
      y = x-i
      num += n-bisect.bisect_left(ls,y)
    return num
  n,m = map(int, input().split())
  a = list(map(int, input().split()))
  a.sort()
  suma = [0]+list(accumulate(a))
  lx = 0
  rx = 2*10**5+2
  error = 0
  while lx < rx:
    mx = (lx+rx)//2
    if hsnum(a,mx) < m:
      rx = mx
    elif hsnum(a,mx) > m:
      if hsnum(a,mx+1) < m:
        error = hsnum(a,mx)-m
        lx = mx
        break
      lx = mx + 1
    else:
      lx = mx
      break
  ans = 0
  for i in a:
    rema = lx - i
    j = bisect.bisect_left(a,rema)
    ans += (n-j)*i+suma[n]-suma[j]
  print(ans-error*lx)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  import bisect
  import sys
  from itertools import accumulate
  input = sys.stdin.readline
  def hsnum(ls,x):
    num = 0
    for i in range(n):
      y = x-ls[i]
      num += n-bisect.bisect_left(ls,y)
    return num
  n,m = map(int, input().split())
  a = list(map(int, input().split()))
  a.sort()
  suma = [0]+list(accumulate(a))
  lx = 0
  rx = 2*10**5+2
  error = 0
  while lx < rx:
    mx = (lx+rx)//2
    if hsnum(a,mx) < m:
      rx = mx
    elif hsnum(a,mx) > m:
      if hsnum(a,mx+1) < m:
        error = hsnum(a,mx)-m
        lx = mx
        break
      lx = mx + 1
    else:
      lx = mx
      break
  ans = 0
  for i in a:
    rema = lx - i
    j = bisect.bisect_left(a,rema)
    ans += (n-j)*i+suma[n]-suma[j]
  print(ans-error*lx)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import bisect
    import sys
    from itertools import accumulate
    input = sys.stdin.readline
    def hsnum(ls,x):
        num = 0
        for i in range(n):
            y = x-ls[i]
            num += n-bisect.bisect_left(ls,y)
        return num
    n,m = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    suma = [0]+list(accumulate(a))
    lx = 0
    rx = 2*10**5+2
    error = 0
    while lx < rx:
        mx = (lx+rx)//2
        if hsnum(a,mx) < m:
            rx = mx
        elif hsnum(a,mx) > m:
            if hsnum(a,mx+1) < m:
                error = hsnum(a,mx)-m
                lx = mx
                break
            lx = mx + 1
        else:
            lx = mx
            break
    ans = 0
    for i in range(n):
        rema = lx - a[i]
        j = bisect.bisect_left(a,rema)
        ans += (n-j)*a[i]+suma[n]-suma[j]
    print(ans-error*lx)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    import bisect
    import sys
    from itertools import accumulate
    input = sys.stdin.readline
    def hsnum(ls,x,n):
        num = 0
        for i in range(n):
            y = x-ls[i]
            num += n-bisect.bisect_left(ls,y)
        return num
    n,m = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    a = tuple(a)
    suma = [0]+list(accumulate(a))
    lx = 0
    rx = 2*10**5+2
    error = 0
    while lx < rx:
        mx = (lx+rx)//2
        if hsnum(a,mx,n) < m:
            rx = mx
        elif hsnum(a,mx,n) > m:
            if hsnum(a,mx+1,n) < m:
                error = hsnum(a,mx,n)-m
                lx = mx
                break
            lx = mx + 1
        else:
            lx = mx
            break
    ans = 0
    for i in range(n):
        rema = lx - a[i]
        j = bisect.bisect_left(a,rema)
        ans += (n-j)*a[i]+suma[n]-suma[j]
    print(ans-error*lx)
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect

def main():
    n,m = map(int, input().split())
    score = list(map(int, input().split()))

    score_list = []

    for i in range(n):
        for j in range(n):
            bisect.insort(score_list, score[i]+score[j])
    
    final_list = score_list[n*n-m:]

    print(sum(final_list))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import bisect

def score_sum(left,right):
    score = left + right

    return score


def main():
    n,m = map(int, input().split())
    score = list(map(int, input().split()))

    score_list = []

    for i in range(n):
        for j in range(n):
            bisect.insort(score_list, score_sum(score[i],score[j]))
    
    total_score = 0
    for i in range(m):
        total_score += score_list[n*n-1-i]

    print(total_score)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools
n, m = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
b = []

for v in itertools.product(a, repeat=2):
  b.append(sum(v))
b = sorted(b)
print(sum(b[-m:]))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
scores = sorted(map(int, input().split()), reverse=True)
results = []

def suggest_for(i):
	if i>=n:
		return
	x = scores[i]
	results.append(x+x)
	for j, y in enumerate(scores[i+1 : min(n, m)]):
		results.append(x+y)
		results.append(x+y)

for i in range(m):
	suggest_for(i)

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
f_i = stdin

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

A.sort()

from bisect import bisect_left

def shake_count(power):
    return N ** 2 - sum(bisect_left(A, power - left_hand) for left_hand in A)

lower = A[0] * 2 - 1
upper = A[-1] * 2 + 1
while lower + 1 < upper:
    mid = (lower + upper) // 2
    if shake_count(mid) >= M:
        lower = mid
    else:
        upper = mid

ans = 0
for left_hand in A:
    idx = bisect_left(A, upper - left_hand)
    ans += sum(A[idx:]) + left_hand * (N - idx)

ans += lower * (M - shake_count(upper))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

a.sort()


def isok(sum_):
    n_pattern = 0
    for i in range(n):
        n_pattern += n - bisect_left(a, sum_ - a[i])
        if n_pattern >= m:
            return True

    return False

ok = min(a)*2
ng = max(a)*2 +1

while ng - ok > 1:
    mid = (ok+ng)//2
    if isok(mid):
        ok = mid
    else:
        ng = mid

sum_ng = ng
sum_ok = ok

m_counter_ng = 0

ans = 0
for i in range(n):
    id_ng = bisect_left(a, sum_ng - a[i])
    ans += sum(a[id_ng:])
    ans += a[i]*(n - id_ng)
    m_counter_ng += n - id_ng

ans += sum_ok * (m - m_counter_ng)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
C = list(map(int, input().split()))
A = list(map(int, input().split()))
B, ans=[], 0
for x in A:
    for y in A:
        B.append(x+y)
B.sort(reverse=True)
for i in range(C[1]):
    ans=ans+B[i]
print(ans)

Submission status: Time Limit Exceeded

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

Pairs = []
from itertools import product as pr
for pair in pr(A, repeat=2):
  Pairs.append(pair[0] + pair[1])
Pairs_s = sorted(Pairs, reverse=True)

print(sum(Pairs_s[:m]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
[n,m]=input().split()
a_vec=input().split()
n=int(n)
m_val=int(m)
for i in range(0,len(a_vec)):
    a_vec[i]=int(a_vec[i])
a_vec.sort(reverse=True)
a_vec=np.asarray(a_vec)

def find_th(a,m):
    check =0
    x_high = a[0]*2
    x_low  = a[n-1]*2    
    while check == 0:
        coef=m/(n*n)
        x_tmp = round((x_high+x_low)/2)
    
        tmp=0
        for i in range(0,len(a)):
            tmp=tmp+sum(a+a[i]>x_tmp)
        x_prev=x_tmp
        if tmp==m:
            x_out = x_tmp
            check =1
        elif tmp>m:
            x_high = x_high
            x_low = x_tmp
            x_tmp = round((x_high+x_low)/2)
        elif tmp<m:
            x_high=x_tmp
            x_low=x_low
            x_tmp=round((x_high+x_low)/2)
        if x_prev==x_tmp:
            x_out = x_tmp
            check = 1
            
    return(x_tmp,tmp)

def get_score(a,x):
    score=0
    for i in range(0,len(a)):
        tmp_a=a+a[i]
        score=score+sum(tmp_a[tmp_a>x])
    return(score)

[x_tmp,tmp]=find_th(a_vec,m_val)

if tmp ==m_val:
    score=get_score(a_vec,x_tmp)
elif tmp < m_val:
    score1=get_score(a_vec,x_tmp)
    check2=0
    j = 0
    while check2==0:
        j = j+1
        [x_tmp2,tmp2]=find_th(a_vec,m_val+j)
        if m_val <= tmp2:
            score2=get_score(a_vec,x_tmp2)
            score=int(score1 + (m_val-tmp)*(score2-score1)/(tmp2-tmp))
            check2=1
        elif (m_val+j)==n*n:
            score2=get_score(a_vec,0)
            score=int(score1 + (m_val-tmp)*(score2-score1)/(n*n-tmp))
            check2=1
            
elif tmp > m_val:
    score1=get_score(a_vec,x_tmp)
    check2=0
    j = 0
    while check2==0:
        j = j+1
        [x_tmp2,tmp2]=find_th(a_vec,m_val-j)
        if m_val-j==tmp2:
            score2=get_score(a_vec,x_tmp2)
            score=int(score1 -(tmp-m_val)*(score1-score2)/(tmp-tmp2))
            check2=1
    
print(score)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left

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

a.sort(reverse=True)
b = list(reversed(a))

rs = [0] * n
rs[0] = a[0]
for i, x in enumerate(a[1:]):
    rs[i+1] = rs[i] + x

L = 0
H = sum(a) ** 2 + 1

while H - L > 1:
    M = (L + H) // 2
    count = 0
    for x in a:
       count += n - bisect_left(b, M - x)
    if count == m:
        L = M
        break
    elif count > m:
        L = M
    else:
        H = M

ans = 0
count = 0
for x in a:
    c = n - 1 -  bisect_left(b, L - x)
    count += c + 1
    if c >= 0:
        ans += rs[c] + x * (c+1)

print(ans - (count-m) * L)

Submission status: Time Limit Exceeded

Solution code:
N,M= [int(i) for i in input().split()]
n = sorted([int(x) for x in input().split()])

s =[0]*(N+1)
s[0]=n[0]
for i in range(1,N+1):
        s[i] = s[i-1]+n[i-1]
MM=n[-1]*2
mm=n[0]*2

while True:
        c=0        
        r=(mm+MM)//2
        for i in range(N):
                for j in range(N):
                   if n[i]+n[j] >= r:
                        c+=N-j
                        break
        if (MM-mm) < 2:
                break
        elif c>=M:
                mm=r
        elif c<M:
                MM=r

v=0
for i in range(N):
        for j in range(N):
                t=n[i]+n[j]
                if  t >= r:
                        v+= (s[N] - s[j]) + (N-j)*n[i]
                        break
v = v - (c-M)*(r)

print(v)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split(" "))
ar = list(map(int,input().split(" ")))
br = []
for i in range(n):
    for j in range(n):
        br.append(ar[i] + ar[j])
br = sorted(br,reverse=True)
count = 0
for i in range(k):
    count += br[i]
print(count)

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int, input().split())
A=list(map(int, input().split()))
ans=0
A.sort(reverse=True)
point = []
for i in range(N):
  for j in range(N):
    point.append(A[i] + A[j])
point.sort(reverse=True)
for i in range(M):
  ans+=point[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = [int(x) for x in input().split(" ")]
A = [int(x) for x in input().split(" ")]
 
A.sort(reverse=True)
 
ryoute = [x * 2 for x in A]
 
katate = []
 
for i_1, a_1 in enumerate(A):
    for i_2, a_2 in enumerate(A):
        if i_1 >= i_2 and a_1 != a_2:
            katate.append(a_1 + a_2)
 
katate_tmp = []
 
for t in katate:
    katate_tmp.append(t)
    katate_tmp.append(t)
 
katate = katate_tmp    
 
te = ryoute
te.extend(katate)
 
total = 0
te.sort(reverse=True)
for m in range(M):
    total += te[m]
 
print(total)

Submission status: Time Limit Exceeded

Solution code:
# import copy

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

A.sort(reverse=True)

ryoute = [x * 2 for x in A]

katate = []

for i_1, a_1 in enumerate(A):
    for i_2, a_2 in enumerate(A):
        if i_1 >= i_2 and a_1 != a_2:
            katate.append(a_1 + a_2)

katate_tmp = []

for t in katate:
    katate_tmp.append(t)
    katate_tmp.append(t)

katate = katate_tmp    

te = ryoute
te.extend(katate)

total = 0
te.sort(reverse=True)
for m in range(M):
    total += te[m]

print(total)

Submission status: Time Limit Exceeded

Solution code:
#import sys
#sys.setrecursionlimit(10**7)
#from functools import reduce
#from fractions import gcd


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

# process
blist=[]
for i in alist:
    for j in alist:
        blist.append(i+j)

blist.sort(reverse=True)

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

# output
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from numpy.fft import fft
from numpy.fft import ifft
class FFT:
  def exe(s, A, B):
    N = len(A) + len(B) - 1
    A, B = s.arrN(N, A), s.arrN(N, B)
    A = fft(A) * fft(B)
    A = ifft(A).real + 0.5
    return list(map(int, A))
  def arrN(s, N, L):
    return np.zeros(N) + (L + [0] * (N - len(L)))

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

MAX = max(A)
L = [0] * (MAX + 1)
for i in A:
  L[i] += 1

f = FFT()
L = f.exe(L, L)

ans = 0
for i in range(len(L) - 1, -1, -1):
  if L[i] < M:
    ans += i * L[i]
    M -= L[i]
  else:
    ans += i * M
    break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
import numpy as np
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()

def check(i):
  n = bisect_left(ans,A[i]*2)
  if n > 0:
    return True
  else:
    return False

ans = [0]
for i in reversed(range(N)):
  if check(i):
    ans = np.r_[ans,A[i]*2 ,[A[i]] + np.array(A[:i]),[A[i]] + np.array(A[:i])]
    #ans.append(A[i]*2)
    #[ans.extend([A[i]+a,A[i]+a]) for a in A[:i]]
    ans.sort()
    ans = ans[-M:]
  else:break

print(int(sum(ans)))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
#import numpy as np
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()

def check(i):
  n = bisect_left(ans,A[i]*2)
  if n > 0:
    return True
  else:
    return False

ans = [0]
for i in reversed(range(N)):
  if check(i):
    #ans = np.r_[ans,A[i]*2 ,[A[i]] + np.array(A[i+1:n+1]),[A[i]] + np.array(A[i+1:n+1])]
    ans.append(A[i]*2)
    [ans.extend([A[i]+a,A[i]+a]) for a in A[:i]]
    ans.sort()
    ans = ans[-M:]
  else:break

print(int(sum(ans)))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
#import numpy as np
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()

def check(i):
  n = bisect_left(ans,A[i]*2)
  if n > 0:
    return True
  else:
    return False

ans = [0]
for i in reversed(range(N)):
  if check(i):
    #ans = np.r_[ans,A[i]*2 ,[A[i]] + np.array(A[i+1:n+1]),[A[i]] + np.array(A[i+1:n+1])]
    ans.append(A[i]*2)
    [ans.extend([A[i]+a,A[i]+a]) for a in A[:i]]
    ans.sort()
    ans = ans[-M:]

print(int(sum(ans)))

Submission status: Time Limit Exceeded

Solution code:
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()
 
ans = [0]
for i in reversed(range(N)):
    #ans = np.r_[ans,A[i]*2 ,[A[i]] + np.array(A[i+1:n+1]),[A[i]] + np.array(A[i+1:n+1])]
    ans.append(A[i]*2)
    [ans.extend([A[i]+a,A[i]+a]) for a in A[:i]]
    ans.sort()
    ans = ans[-M:]

print(int(sum(ans[-M:])))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
#import numpy as np
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()

ans = [0]
for i in reversed(range(N)):
  n = bisect_left(ans,A[i]*2)
  if n > 0:
    #ans = np.r_[ans,A[i]*2 ,[A[i]] + np.array(A[i+1:n+1]),[A[i]] + np.array(A[i+1:n+1])]
    ans.append(A[i]*2)
    [ans.extend([A[i]+a,A[i]+a]) for a in A[:i]]
    ans.sort()
    ans = ans[-M:]

print(int(sum(ans)))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
#import numpy as np
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()

ans = [0]
for i in reversed(range(N)):
    #ans = np.r_[ans,A[i]*2 ,[A[i]] + np.array(A[i+1:n+1]),[A[i]] + np.array(A[i+1:n+1])]
    ans.append(A[i]*2)
    [ans.extend([A[i]+a,A[i]+a]) for a in A[:i]]

ans.sort()
ans = ans[-M:]

print(int(sum(ans)))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
#import numpy as np
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()

ans = [0]
for i in reversed(range(N)):
    #ans = np.r_[ans,A[i]*2 ,[A[i]] + np.array(A[i+1:n+1]),[A[i]] + np.array(A[i+1:n+1])]
    ans.append(A[i]*2)
    [ans.extend([A[i]+a,A[i]+a]) for a in A[:i]]
    ans.sort()
    ans = ans[-M:]

print(int(sum(ans[-M:])))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
#import numpy as np
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()

def check(a):
  for i in reversed(range(N)):
    n = bisect_left(ans, a + A[i])
    if n == 0:
      return i+1
  return i

ans = [0]
for i in reversed(range(N)):
  n = check(A[i])
  if n < N:
    #ans = np.r_[ans,A[i]*2 ,[A[i]] + np.array(A[i+1:n+1]),[A[i]] + np.array(A[i+1:n+1])]
    ans.append(A[i]*2)
    [ans.extend([A[i]+a,A[i]+a]) for a in A[n:i]]
    ans.sort()
    ans = ans[-M:]

print(int(sum(ans[-M:])))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
#import numpy as np
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()

def check(a):
  for i in reversed(range(N)):
    n = bisect_left(ans, a + A[i])
    if n == 0:
      return i+1
  return i

ans = [0]
for i in reversed(range(N)):
  n = check(A[i])
  if n < N:
    #ans = np.r_[ans,A[i]*2 ,[A[i]] + np.array(A[i+1:n+1]),[A[i]] + np.array(A[i+1:n+1])]
    ans.append(A[i]*2)
    for a in A[n:i]:
      ans.append(A[i]+a)
      ans.append(A[i]+a)
    ans.sort()
    ans = ans[-M:]

print(int(sum(ans[-M:])))

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
import numpy as np
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A = np.sort(A)[::-1]

def check(a):
  for i in range(N):
    n = bisect_left(ans, a + A[i])
    if n == 0:
      return i-1
  return i

ans = [0]
for i in range(N):
  n = check(A[i])
  if n >= 0:
    ans = np.r_[ans,A[i]*2 ,[A[i]] + np.array(A[i+1:n+1]),[A[i]] + np.array(A[i+1:n+1])]
    ans.sort()
    ans = ans[-M:]

print(int(sum(ans[-M:])))

Submission status: Time Limit Exceeded

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

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

A2 = sorted(A, reverse=True)
A = sorted(A)


# 必要となる左右合計の下限値 X を求める

def f1(m):
    cnt = 0
    for a1 in A2:
        flg = False
        for a2 in A2:
            if cnt >= M:
                break
            if a1 + a2 < m:
                break
            else:
                flg = True
                cnt += 1
        if not flg:
            break
    return cnt >= M


l = 0
r = A2[0] * 2 + 10

while r - l > 1:
    m = (l + r) // 2
    if f1(m):
        l = m
    else:
        r = m

X = l
# print('X', X)

acc = list(accumulate(A2))  # 累積和

total_cnt = 0
ans = 0
for a in A2:
    # a を左手としたときの右手の最小値
    min_right = X - a

    # 右手の値から出す手数を算出
    idx = bisect_left(A, min_right)
    cnt = N - idx

    # それらの全合計を加算
    ans += a * cnt + acc[cnt - 1]
    total_cnt += cnt
    if total_cnt >= M:
        break
# total_cnt が大きすぎる場合は, その分だけ Xを減算. X は必要な両手合計の最小値なので, 減算すべきは必ず X*出しすぎた分 となる
if total_cnt > M:
    ans -= X * (total_cnt - M)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left
import numpy as np
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A = np.sort(A)[::-1]

def check(a):
  for i in range(N):
    n = bisect_left(ans, a + A[i])
    if n == 0:
      return i-1
  return i

ans = [0]
for a in A:
  n = check(a)
  if n >= 0:
    ans = np.r_[ans,[a] + np.array(A[:n+1])]
    ans.sort()
    ans = ans[-M:]

print(int(sum(ans[-M:])))

Submission status: Time Limit Exceeded

Solution code:
import heapq

l = []

N, M = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
for i in range(N):
    for j in range(i, N):
        heapq.heappush(l, -(A[i] + A[j]))
        if i!=j:
            heapq.heappush(l, -(A[i] + A[j]))
ans = 0
for i in range(M):

    v= heapq.heappop(l)
    ans +=v
print(-ans)

Submission status: Time Limit Exceeded

Solution code:
#import numpy as np
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()
A = A[-M:]
 
ans = []
[ans.extend([A[i]+x for x in A]) for i in range(len(A))]

ans.sort()
print(int(sum(ans[-M:])))

Submission status: Time Limit Exceeded

Solution code:
#import numpy as np
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()
A = A[-M:]
 
ans = []
for i in range(len(A)):
  #ans = np.r_[ans,a + np.array(A)]
  ans.extend([A[i]+x for x in A])

ans.sort() 
print(int(sum(ans[-M:])))

Submission status: Time Limit Exceeded

Solution code:
#import numpy as np
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()
A = A[-M:]
 
ans = []
for i in range(len(A)):
  #ans = np.r_[ans,a + np.array(A)]
  ans.extend([A[i]+x for x in A])
  ans.sort()
  ans = ans[-M:]
  
print(int(sum(ans)))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()
A = A[-M:]

ans = []
for i in range(len(A)):
  ans = np.r_[ans, A[i] + np.array(A)]
  #ans.sort()
  #ans = ans[-M:]

ans.sort()
print(int(sum(ans[-M:])))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()
A = A[-M:]

ans = []
for i in range(len(A)):
  ans = np.r_[ans, A[i] + np.array(A)]
  ans.sort()
  ans = ans[-M:]
  
print(int(sum(ans)))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,M = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()
A = A[-M:]

ans = []
for a in A:
  ans = np.r_[ans,a + np.array(A)]
  ans.sort()
  ans = ans[-M:]
  
print(int(sum(ans)))

Submission status: Time Limit Exceeded

Solution code:
n1, n2 = [int(i) for i in input().split()]
n3 = [int(i) for i in input().split()]

n6 = []
for i4 in range(n1):
    for i5 in range(n1):
        n6.append(n3[i4] + n3[i5])

n6.sort(reverse = True)
r8 = 0
for i7 in range(n2):
    r8 += n6[i7]
print(r8)

Submission status: Time Limit Exceeded

Solution code:
import heapq

N,M = map(int,input().split())
A = sorted(list(map(int,input().split())),reverse = True)
#print(A)

hq = [(-A[0]*2,0,0)]
used = [[False for _ in range(N)] for _ in range(N)]
used[0][0] = True
ans = 0
for i in range(M):
  a,p,q = heapq.heappop(hq)
  ans -= a
  if p+1 < N and not used[p+1][q]:
    heapq.heappush(hq, (-A[p+1]-A[q],p+1,q))
    used[p+1][q] = True
  if q+1 < N and not used[p][q+1]:
    heapq.heappush(hq, (-A[p]-A[q+1],p,q+1))
    used[p][q+1] = True
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def nibutan(n, M):
  ok = 0
  ng = M + 1
  while abs(ok - ng) > 1:
    mid = (ok + ng) // 2
    #print("value:", mid)
    if nasu(mid) >= n:
      ok = mid
    else:
      ng = mid
  return ok

def nasu(x):
  ans = 0
  for i in A:
    #print("L:", i, end = " ")
    t = N - hogetan(x - i)
    #print("n:", t)
    ans += t
  return ans

def hogetan(n):
  ok = N
  ng = -1
  while abs(ok - ng) > 1:
    mid = (ok + ng) // 2
    if A[mid] >= n:
      ok = mid
    else:
      ng = mid
  return ok

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


A.sort()
MAX = A[-1] + A[-1]
inf = MAX + 10

t = nibutan(M, MAX)
mi = inf
cnt = 0
ans = 0
for i in A:
  k = hogetan(t - i)
  n = N - k
  cnt += n
  if n != 0:
    mi = min(mi, i + A[k])
  ans += n * 2 * i

ans -= mi * (cnt - M)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
 
import math
import itertools
import sys
import copy
 
# 入力
#A, B, C, D = map(int, input().split())
#L = list(map(int, input().split()))
#S = list(str(input()))
#N = int(input())
N, M = map(int, input().split())
A = list(map(int, input().split()))
#print (A)
#AMAX = 80
#AMAX = 200001
AMAX = (max(A)*2)+1
dp = [0 for _ in range(AMAX)]

for i in range(N) :
  for j in range(i+1) :
    if i == j :
      dp[A[i] + A[j]] += 1
    else :
      dp[A[i] + A[j]] += 2
#print (dp)

sum = 0
for k in range(AMAX) :
  if dp[AMAX-k-1] < M :
    sum += dp[AMAX-k-1] * (AMAX-k-1)
    M -= dp[AMAX-k-1]
  else :
    sum += M * (AMAX-k-1)
    print (sum)
    exit()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
 
import math
import itertools
import sys
import copy
 
# 入力
#A, B, C, D = map(int, input().split())
#L = list(map(int, input().split()))
#S = list(str(input()))
#N = int(input())
N, M = map(int, input().split())
A = list(map(int, input().split()))
#print (A)
#AMAX = 80
AMAX = 200001
dp = [0 for _ in range(AMAX)]

for i in range(N) :
  for j in range(i+1) :
    if i == j :
      dp[A[i] + A[j]] += 1
    else :
      dp[A[i] + A[j]] += 2
#print (dp)

sum = 0
for k in range(AMAX) :
  if dp[AMAX-k-1] < M :
    sum += dp[AMAX-k-1] * (AMAX-k-1)
    M -= dp[AMAX-k-1]
  else :
    sum += M * (AMAX-k-1)
    print (sum)
    exit()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
 
import math
import itertools
import sys
import copy
 
# 入力
#A, B, C, D = map(int, input().split())
#L = list(map(int, input().split()))
#S = list(str(input()))
#N = int(input())
N, M = map(int, input().split())
A = list(map(int, input().split()))
#print (A)
#AMAX = 80
AMAX = 200001
dp = [0 for _ in range(AMAX)]

for i in range(N) :
  for j in range(N) :
    dp[A[i] + A[j]] += 1
#print (dp)

sum = 0
for k in range(AMAX) :
  if dp[AMAX-k-1] < M :
    sum += dp[AMAX-k-1] * (AMAX-k-1)
    M -= dp[AMAX-k-1]
  else :
    sum += M * (AMAX-k-1)
    print (sum)
    exit()