qyliang/CodeNanoFix huggingface dataset:
Buggy code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=list(input())

# d={'r':0,'s':0,'p':0}
d={}
ans=0
c=0
for i in t:

    if c >= k:
        x =d[c-k]
        if i =='r':
            if x == 'p':
                if t[c+k] == 's':
                    d[c]='s'
                else:
                    d[c]='r'
            else:
                ans += p ; d[c]='p'
        elif i == 's': 
            if x == 'r':
               if t[c+k] == 'p':
                    d[c]='p'
               else:
                    d[c]='s'
            else:
                ans += r;d[c]='r'
        elif i == 'p': 
            if x == 's':
               if t[c+k] == 'r':
                    d[c]='r'
               else:
                    d[c]='p'
            else:
                ans += s;d[c]='s'

    else:
        if i =='r':
            ans += p ; d[c]='p'
        elif i == 's': 
            ans += r;d[c]='r'
        elif i == 'p': 
            ans += s;d[c]='s'

    c+=1


print(ans)

Fixed code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=list(input())

# d={'r':0,'s':0,'p':0}
d={}
ans=0
c=0
for i in t:

    if c >= k:
        x =d[c-k]
        y = c+k
        if i =='r':
            if x == 'p':
                if  y < n and t[y] == 's':
                    d[c]='s'
                else:
                    d[c]='r'
            else:
                ans += p ; d[c]='p'
        elif i == 's': 
            if x == 'r':
               if y < n and t[y] == 'p' :
                    d[c]='p'
               else:
                    d[c]='s'
            else:
                ans += r;d[c]='r'
        elif i == 'p': 
            if x == 's':
               if  y < n and t[y] == 'r':
                    d[c]='r'
               else:
                    d[c]='p'
            else:
                ans += s;d[c]='s'

    else:
        if i =='r':
            ans += p ; d[c]='p'
        elif i == 's': 
            ans += r;d[c]='r'
        elif i == 'p': 
            ans += s;d[c]='s'

    c+=1


print(ans)
Buggy code:
N,K= [int(i) for i in input().split()]
R,S,P= [int(i) for i in input().split()]
n=input()
C=N//K + 1
s=0
for i in range(K):
        m=""
        for j in range(i,N,K):
                if n[j] =='r':
                        if j < K:
                                s+=P
                                m ='r'
                        elif n[j-K]=='r' and m=='r': m =""
                        else:
                                s+=P
                                m ='r'
                elif n[j] =='s':
                        if j < K:
                                s+=R
                                m ='s'
                        elif n[j-K]=='s' and m=='s': m =""
                        else:
                                s+=R
                                m ='s'
                elif n[j] =='p':
                        if j < K:
                                s+=S
                                m ='p'
                        elif n[j-K]=='p' and m=='p': m =""
                        else:
                                s+=S
                                m ='p               
print(score)

Fixed code:
N,K= [int(i) for i in input().split()]
R,S,P= [int(i) for i in input().split()]
n=input()
C=N//K + 1
s=0
for i in range(K):
        m=""
        for j in range(i,N,K):
                if n[j] =='r':
                        if j < K:
                                s+=P
                                m ='r'
                        elif n[j-K]=='r' and m=='r': m =""
                        else:
                                s+=P
                                m ='r'
                elif n[j] =='s':
                        if j < K:
                                s+=R
                                m ='s'
                        elif n[j-K]=='s' and m=='s': m =""
                        else:
                                s+=R
                                m ='s'
                elif n[j] =='p':
                        if j < K:
                                s+=S
                                m ='p'
                        elif n[j-K]=='p' and m=='p': m =""
                        else:
                                s+=S
                                m ='p'               
print(s)
Buggy code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
score = {"r":P, "s":R, "p":S} 
m = {"r":"p", "s":"r", "p":"s"}
command= []
mm = []
ans = 0


for i, t in enumerate(T):
    if t == "r":
      command = "p"
      point = P
    elif t == "s":
      command ="r" 
      point = R
    elif t == "p":
      command = "s"
      point = P
    
    if (i - K > 0) and (command[i - K] == command):
        command = ''
        point = 0
    
    ans += point
    command.append(command)

Fixed code:
N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()
score = {"r":P, "s":R, "p":S} 
m = {"r":"p", "s":"r", "p":"s"}
command= []
mm = []
ans = 0


for i, t in enumerate(T):
    if t == "r":
      c = "p"
      point = P
    elif t == "s":
      c ="r" 
      point = R
    elif t == "p":
      c = "s"
      point = S
    
    if (i - K >= 0) and (command[i - K] == c):
        c = ""
        point = 0
    
    ans += point
    command.append(c)
print(ans)
Buggy code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
T = int(input())

scores = {'r':p,'s':r,'p':s}


ans = 0
memo = []

for i in range(n):
  if i < k:
    ans += scores[T[i]]
    memo.append(T[i])
  else:
    if memo[i-k] == T[i]:
      memo.append('N')
    else:
      ans += scores[T[i]]
      memo.append(T[i])
print(ans)
      

Fixed code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
T = input()

scores = {'r':p,'s':r,'p':s}


ans = 0
memo = []

for i in range(n):
  if i < k:
    ans += scores[T[i]]
    memo.append(T[i])
  else:
    if memo[i-k] == T[i]:
      memo.append('N')
    else:
      ans += scores[T[i]]
      memo.append(T[i])
print(ans)
Buggy code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
score = {'r':p, 's':r, 'p':s}
from collections import deque
past = deque([])

ans = 0
for i in range(n):
  past.append(t[i])
  if i < k:
    ans += score[t[i]]
  else:
    if past.popleft() != t[i]:
      ans += score[t[i]]
print(ans)
    
    

Fixed code:
n,k = map(int,input().split())
r,s,p = map(int,input().split())
t = input()
score = {'r':p, 's':r ,'p':s}
from collections import deque
past = deque([])

ans = 0
for i in range(n):
  if i < k:
    ans += score[t[i]]
    past.append(t[i])
  else:
    if past.popleft() != t[i]:
      ans += score[t[i]]
      past.append(t[i])
    else:
      past.append('nn')
      
  
print(ans)
Buggy code:
def main():
    import sys
    input = sys.stdin.readline

    N, K = map(int, input().split())
    RSP = list(map(int, input().split()))
    T = list(input().rsplit()[0])

    win_hand = ['s', 'p', 'r']


    ans = 0
    for i in range(N):
        for j in range(3):
            if T[i] == win_hand[j]:
                if i >= K:
                    if T[i] == T[i-K]:
                        continue
                ans += RSP[j]
    print(ans)


if __name__ == "__main__":
    main()


Fixed code:
def main():
    import sys
    input = sys.stdin.readline

    N, K = map(int, input().split())
    RSP = list(map(int, input().split()))
    T = list(input().rsplit()[0])

    win_hand = ['s', 'p', 'r']

    ans = 0
    hand = [0]*N
    for i in range(N):
        for j in range(3):
            if T[i] == win_hand[j]:
                if i >= K:
                    if T[i] == T[i-K] and T[i] == hand[i-K]:
                        continue
                hand[i] = win_hand[j]
                ans += RSP[j]
    print(ans)


if __name__ == "__main__":
    main()
Buggy code:
import collections

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())

for i in range(N-K):
    if T[i] == T[i+K]:
        T[i+K] = "a"

c = colelctions.Counter(T)

print(c["r"]*P + c["s"]*R + c["p"]*S)

Fixed code:
import collections

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = list(input())

for i in range(N-K):
    if T[i] == T[i+K]:
        T[i+K] = "a"

c = collections.Counter(T)

print(c["r"]*P + c["s"]*R + c["p"]*S)
Buggy code:
import math
# import bisect
# import heapq
# from copy import deepcopy
# from collections import deque
# from collections import Counter
# from itertools import accumulate
# from itertools import permutations
# import numpy as np

n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()
# p = list(map(int, input().split()))
score = 0
rev = [False] * k
for i in range(k):
    if t[i] == 'r':
        score += p

    elif t[i] == 'p':
        score += s

    elif t[i] == 's':
        score += r

for i in range(k,n):

    if t[i] == t[i-k] and not rev[(i+1)%k] :
        rev[i % k] = True

    elif t[i] == t[i-k] and rev[(i+1)%k] :
        if t[i]=='r': score += p
        elif t[i] == 'p': score += s
        elif t[i] == 's': score += r

        rev[i % k] = False

    elif t[i] != t[i-k]:
        if t[i]=='r': score += p
        elif t[i] == 'p': score += s
        elif t[i] == 's': score += r

        rev[i % k] = True

print(score)

Fixed code:
import math
# import bisect
# import heapq
# from copy import deepcopy
# from collections import deque
# from collections import Counter
# from itertools import accumulate
# from itertools import permutations
# import numpy as np

n,k = map(int, input().split())
r,s,p = map(int, input().split())
t = input()
# p = list(map(int, input().split()))
score = 0
rev = [True] * k
for i in range(min(k,n)):
    if t[i] == 'r':
        score += p

    elif t[i] == 'p':
        score += s

    elif t[i] == 's':
        score += r

if n>k:

    for i in range(k,n):

        if t[i] == t[i-k] and rev[i%k] :
            rev[i % k] = False

        elif t[i] == t[i-k] and not rev[i%k] :
            if t[i]=='r': score += p
            elif t[i] == 'p': score += s
            elif t[i] == 's': score += r

            rev[i % k] = True

        elif t[i] != t[i-k]:
            if t[i]=='r': score += p
            elif t[i] == 'p': score += s
            elif t[i] == 's': score += r

            rev[i % k] = True

print(score)
Buggy code:
import sys
bbn=1000000007
	
#+++++
		
def main():
	n, k = map(int, input().split())
	r, s, p =  map(int, input().split())
	t = input()
	dd={'s':r, 'p':s, 'r':p}
	point = lambda c : dd[c]
	
	max_p=sum([point(c) for c in t])
	same_k={'s':0, 'p':0, 'r':0}
	tn=t
	for i in range(k+1,n):
		if tn[i]==tn[i-k] and tn[i]!='_':
			same_k[tn[i]] += 1
			same_k[i] = '_'
	down=0
	for cc in ['r','s','p']:
		down+=same_k[cc]*point(cc)
			
	print(max_p-down)
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Fixed code:
import sys
bbn=1000000007
	
#+++++
		
def main():
	n, k = map(int, input().split())
	r, s, p =  map(int, input().split())
	t = input()
	dd={'s':r, 'p':s, 'r':p}
	point = lambda c : dd[c]
	
	max_p=sum([point(c) for c in t])
	same_k={'s':0, 'p':0, 'r':0}
	tn=list(t)
	for i in range(k,n):
		if tn[i]==tn[i-k] and tn[i]!='_':
			same_k[tn[i]] += 1
			tn[i] = '_'
	down=0
	#print(tn)
	for cc in ['r','s','p']:
		down+=same_k[cc]*point(cc)
			
	print(max_p-down)
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)
Buggy code:
num = input().split(' ')
point = input().split(' ')
point_int = [int(point[i]) for i in range(3)]
data = input()
count = 1
result = 0
judge = [0 for i in range(int(num[0]))]
for i in range(int(num[0])):
    if(data[i] == 'r'):
        if(i - int(num[1]) >= 0):
            if(data[i - int(num[1])] == 'r'):
                if(judge[i - int(num[1])] == 0):
                    result += point_int[2]
                    judge[i] = 1
                else:
                    pass

            else:
                result += point_int[2]
                judge[i] = 1
        else:
            result += point_int[2]
            judge[i] = 1
    elif(data[i] == 's'):
        if(i - int(num[1]) >= 0):
            if(data[i - int(num[1])] == 's'):
                if(judge[i - int(num[1])] == 0):
                    result += point_int[0]
                    judge[i] = 1
                else:
                    pass
            else:
                result += point_int[0]
                judge[i] = 1
        else:
            result += point_int[0]
            judge[i] = 1

    else:
        if(i - int(num[1]) >= 0):
            if(data[i - int(num[1])] == 'p'):
                if(judge[i - int(num[1])] == 0):
                    result += point_int[1]
                    judge[i] = 1
                else:
                    pass
            else:
                result += point_int[1]
                judge[i] = 1
        else:
            result += point_int[1]
            judge[i] = 1
    print(result)

print(result)


Fixed code:
num = input().split(' ')
point = input().split(' ')
point_int = [int(point[i]) for i in range(3)]
data = input()
count = 1
result = 0
judge = [0 for i in range(int(num[0]))]
for i in range(int(num[0])):
    if(data[i] == 'r'):
        if(i - int(num[1]) >= 0):
            if(data[i - int(num[1])] == 'r'):
                if(judge[i - int(num[1])] == 0):
                    result += point_int[2]
                    judge[i] = 1
                else:
                    pass

            else:
                result += point_int[2]
                judge[i] = 1
        else:
            result += point_int[2]
            judge[i] = 1
    elif(data[i] == 's'):
        if(i - int(num[1]) >= 0):
            if(data[i - int(num[1])] == 's'):
                if(judge[i - int(num[1])] == 0):
                    result += point_int[0]
                    judge[i] = 1
                else:
                    pass
            else:
                result += point_int[0]
                judge[i] = 1
        else:
            result += point_int[0]
            judge[i] = 1

    else:
        if(i - int(num[1]) >= 0):
            if(data[i - int(num[1])] == 'p'):
                if(judge[i - int(num[1])] == 0):
                    result += point_int[1]
                    judge[i] = 1
                else:
                    pass
            else:
                result += point_int[1]
                judge[i] = 1
        else:
            result += point_int[1]
            judge[i] = 1

print(result)
Buggy code:
N,K =map(int,input().split())
R,S,P = map(int,input().split())
T = input()

def score(T):
    sa = 0
    x = 0
    for i in range(len(T)):
        if x == 1 and T[i] == T[i-1] :
            if T[i] == "r":
                sa += P
                x = 0
            elif T[i] == "s":
                sa += R
                x = 0
            elif T[i] == "p":
                sa += S
                x = 0
            
        elif i > 1 and T[i] == T[i-1] :
            x = 1
        elif T[i] == "r":
            sa += P
            x = 0
        elif T[i] == "s":
            sa += R
            x = 0
        elif T[i] == "p":
            sa += S
            x = 0
    return sa
ans = 0
for k in range(K):
    ans += score(T[k::K])

print(ans)

Fixed code:
N,K =map(int,input().split())
R,S,P = map(int,input().split())
T = input()

def score(T):
    sa = 0
    x = 0
    for i in range(len(T)):
        if x == 1 and T[i] == T[i-1] :
            if T[i] == "r":
                sa += P
                x = 0
            elif T[i] == "s":
                sa += R
                x = 0
            elif T[i] == "p":
                sa += S
                x = 0
            
        elif i > 0 and T[i] == T[i-1] :
            x = 1
        elif T[i] == "r":
            sa += P
            x = 0
        elif T[i] == "s":
            sa += R
            x = 0
        elif T[i] == "p":
            sa += S
            x = 0
    return sa
ans = 0
for k in range(K):
    ans += score(T[k::K])

print(ans)
Buggy code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
point=0
l=[]
point_list=[r,s,p]
kind=['r','s','p','r']
for i in range(n):
  for j in range(len(kind)-1):
    if t[i]==kind[j]:
      if i-k>=0 and t[i]==kind[l[i-k]+1]:
        if i+k<n and t[i+k]==kind[j+1]:
          if j==2:j=-1
          l.append(j+1)
        else:
          l.append(j)
      else:
        point+=point_list[j-1]
        if j==0:j=3
        l.append(j-1)
      break
print(l)
print(point)

Fixed code:
n,k=map(int,input().split())
r,s,p=map(int,input().split())
t=input()
point=0
l=[]
point_list=[r,s,p]
kind=['r','s','p','r']
for i in range(n):
  for j in range(len(kind)-1):
    if t[i]==kind[j]:
      if i-k>=0 and t[i]==kind[l[i-k]+1]:
        if i+k<n and t[i+k]==kind[j+1]:
          if j==2:j=-1
          l.append(j+1)
        else:
          l.append(j)
      else:
        point+=point_list[j-1]
        if j==0:j=3
        l.append(j-1)
      break
print(point)
Buggy code:
from bisect import bisect_left, bisect_right

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

ok, ng = 0, 10**18+1
while abs(ok-ng) > 1:
    mid = (ok+ng)//2
    ok_flag = True
    cnt = 0
    # print('-----')
    # print(ok,ng,mid)
    for i,a in enumerate(al):
        rem = mid-a
        cnt_a = n-bisect_left(al,rem)
        # cnt_a = min(n-i-1, cnt_a)
        cnt += cnt_a
        # print(i,a,cnt_a)
    if cnt >= m:
        ok = mid
    else:
    	ng = mid

min_sum = ok
cum_sums = [0]
csum = 0
for a in al:
    csum += a
    cum_sums.append(csum)
    
# print(min_sum)

ans = 0
cnt = 0
for i,a in enumerate(al):
    rem = min_sum - a
    ind = bisect_left(al, rem)
    # ind = ind if 0 <= ind < n else None
    # ind = max(i+1,ind)
    csum = cum_sums[n] - cum_sums[ind]
    print(i,a,csum)
    ans += csum
    ans += a*(n-ind)
    cnt += (n-ind)

ans -= (cnt-m)*min_sum

print(ans)
# print(min_sum)

Fixed code:
from bisect import bisect_left, bisect_right

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

ok, ng = 0, 10**18+1
while abs(ok-ng) > 1:
    mid = (ok+ng)//2
    ok_flag = True
    cnt = 0
    # print('-----')
    # print(ok,ng,mid)
    for i,a in enumerate(al):
        rem = mid-a
        cnt_a = n-bisect_left(al,rem)
        # cnt_a = min(n-i-1, cnt_a)
        cnt += cnt_a
        # print(i,a,cnt_a)
    if cnt >= m:
        ok = mid
    else:
    	ng = mid

min_sum = ok
cum_sums = [0]
csum = 0
for a in al:
    csum += a
    cum_sums.append(csum)
    
# print(min_sum)

ans = 0
cnt = 0
for i,a in enumerate(al):
    rem = min_sum - a
    ind = bisect_left(al, rem)
    # ind = ind if 0 <= ind < n else None
    # ind = max(i+1,ind)
    csum = cum_sums[n] - cum_sums[ind]
    # print(i,a,csum)
    ans += csum
    ans += a*(n-ind)
    cnt += (n-ind)

ans -= (cnt-m)*min_sum

print(ans)
# print(min_sum)
Buggy code:
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse=True)
s = [0]
for ai in a:
    s.append(ai + s[-1])

def count(x, accum=False):
    ret = 0
    for ai in a:
        lo, hi = -1, n
        while hi - lo > 1:
            mid = (lo + hi) // 2
            if ai + a[mid] >= x:
                lo = mid
            else:
                hi = mid
        ret += ai * hi + s[hi] if accum else hi
    return ret

lo, hi = 0, 1000000000
while hi - lo > 1:
    mid = (lo + hi) // 2
    if count(mid) >= m:
        lo = mid
    else:
        hi = mid
print(count(lo, accum=True) - (count(lo) - m) * hi)

Fixed code:
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse=True)
s = [0]
for ai in a:
    s.append(ai + s[-1])

def count(x, accum=False):
    ret = 0
    for ai in a:
        lo, hi = -1, n
        while hi - lo > 1:
            mid = (lo + hi) // 2
            if ai + a[mid] >= x:
                lo = mid
            else:
                hi = mid
        ret += ai * hi + s[hi] if accum else hi
    return ret

lo, hi = 0, 1000000000
while hi - lo > 1:
    mid = (lo + hi) // 2
    if count(mid) >= m:
        lo = mid
    else:
        hi = mid

print(count(lo, accum=True) - (count(lo) - m) * lo)
Buggy code:
import bisect
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

a_cum = [0 for _ in range(n+1)]
for i in range(n):
    a_cum[i+1] = a_cum[i] + a[i]

l, r = 0, 10 ** 6
while r - l > 1:
    x = (l + r) // 2
    cnt = 0

    for i in range(n):
        idx = bisect.bisect_left(a, x-a[i])
        cnt += n - idx

    if cnt >= m:
        l = x

    else:
        r = x

ans = 0
cnt = 0
for i in range(n):
    idx = bisect.bisect_left(a, l-a[i])
    cnt += n - idx
    ans += a_cum[n] - a_cum[idx]
    ans += a[i] * (n - idx)

if cnt > m:
    ans -= a[i] * (cnt - m)

print(ans)

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

a_cum = [0 for _ in range(n+1)]
for i in range(n):
    a_cum[i+1] = a_cum[i] + a[i]

l, r = 0, 10 ** 6
while r - l > 1:
    x = (l + r) // 2
    cnt = 0

    for i in range(n):
        idx = bisect.bisect_left(a, x-a[i])
        cnt += n - idx

    if cnt >= m:
        l = x

    else:
        r = x

ans = 0
cnt = 0
for i in range(n):
    idx = bisect.bisect_left(a, l-a[i])
    cnt += n - idx
    ans += a_cum[n] - a_cum[idx]
    ans += a[i] * (n - idx)

if cnt > m:
    ans -= l * (cnt - m)

print(ans)
Buggy code:
import numpy as np
N,M,*A = map(int, open('0').read().split())
A = np.sort(A)
B = np.cumsum(np.concatenate([[0],A[::-1]]))
l = -1
r = 10**6
while l+1<r:
  s = (l+r)//2
  cnt = np.sum(N-np.searchsorted(A,s-A))
  if cnt>=M:
    l = s
  else:
    r = s
ind = np.searchsorted(A,r-A)
M -= np.sum(N-ind)
ans = np.sum(B[N-ind]+A*(N-ind))+M*l
print(ans)


Fixed code:
import numpy as np
N,M,*A = map(int, open(0).read().split())
A = np.sort(A)
B = np.cumsum(np.concatenate([[0],A[::-1]]))
l = -1
r = 10**6
while l+1<r:
  s = (l+r)//2
  cnt = np.sum(N-np.searchsorted(A,s-A))
  if cnt>=M:
    l = s
  else:
    r = s
ind = np.searchsorted(A,r-A)
M -= np.sum(N-ind)
ans = np.sum(B[N-ind]+A*(N-ind))+M*l
print(ans)
Buggy code:
from bisect import bisect_left
from itertools import accumulate
N,M,*A = map(int, open('0').read().split())
A.sort()
B = list(accumulate(A[::-1]))
l = -1
r = 10**9
while l+1<r:
  s = (l+r)//2
  cnt = 0
  for c in A:
    i = bisect_left(A,s-c)
    cnt += N-i
  if cnt>=M:
    l = s
  else:
    r = s
ans = 0
for c in A:
  i = bisect_left(A,r-c)
  M -= N-i
  if i<N:
    ans += B[N-1-i]+c*(N-i)
ans += M*l
print(ans)

Fixed code:
from bisect import bisect_left
from itertools import accumulate
N,M,*A = map(int, open(0).read().split())
A.sort()
B = list(accumulate(A[::-1]))
l = -1
r = 10**9
while l+1<r:
  s = (l+r)//2
  cnt = 0
  for c in A:
    i = bisect_left(A,s-c)
    cnt += N-i
  if cnt>=M:
    l = s
  else:
    r = s
ans = 0
for c in A:
  i = bisect_left(A,r-c)
  M -= N-i
  if i<N:
    ans += B[N-1-i]+c*(N-i)
ans += M*l
print(ans)