qyliang/CodeNanoFix huggingface dataset:
Buggy code:

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

A = sorted(A)
A = A[::-1]
sumkoko = A.pop(0)
print(A)

sumkoko += sum(A[0:(N-1)//2]) * 2
if ((N-1)%2) == 1:
    sumkoko += A[(N-1)//2]

print(sumkoko)


Fixed code:

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

A = sorted(A)
A = A[::-1]
sumkoko = A.pop(0)
sumkoko += sum(A[0:(N-2)//2]) * 2
if ((N-2)%2) == 1:
    sumkoko += A[(N-2)//2]

print(sumkoko)
Buggy code:
n = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse=True)
ans = a[0]
idx = 1
flag = False
for i in range(n-1):
    ans += a[idx]
    if flag:
        idx += 1
        flag = False
    else:
        flag = True
print(ans)

Fixed code:
n = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse=True)
ans = a[0]
idx = 1
flag = False
for i in range(n-2):
    ans += a[idx]
    if flag:
        idx += 1
        flag = False
    else:
        flag = True
print(ans)
Buggy code:
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
if N % 2 == 0:
    d = int(N/2)
    print(A[1] + 2 * sum(A[1:d]))
else:
    b = int((N-1)/2)
    c = int(N/2)+1
    print(A[1] + 2 * sum(A[1:b]) + A[c])

Fixed code:
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
if N % 2 == 0:
    d = int(N/2)
    print(A[0] + 2 * sum(A[1:d]))
else:
    b = int((N-1)/2)
    c = int((N-1)/2)
    print(A[0] + 2 * sum(A[1:b]) + A[c])
Buggy code:
N = int(input())
A = sorted(list(map(int, input().split())), reverse=True)

if N == 2:
    ans = A[0]
elif N == 3:
    ans = sum(A[:2])
elif N == 4:
    ans = sum(A[:2])+A[1]
elif N == 5:
    ans = sum(A[:2])+A[1]+A[2]
elif N == 6:
    ans = sum(A[:2])+A[1]+A[2]*2
else:
    ans = sum(A[:2])+A[1]+A[2]*2
    print(ans)
    ind = N-6
    if ind%2 == 0:
        ans += sum(A[3:3+ind//2])*2
    else:
        ans += sum(A[3:3+ind//2])*2+A[3+ind//2+1]


print(ans)


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

if N == 2:
    ans = A[0]
elif N == 3:
    ans = sum(A[:2])
elif N == 4:
    ans = sum(A[:2])+A[1]
elif N == 5:
    ans = sum(A[:2])+A[1]+A[2]
elif N == 6:
    ans = sum(A[:2])+A[1]+A[2]*2
else:
    ans = sum(A[:2])+A[1]+A[2]*2
    # print(ans)
    ind = N-6
    if ind%2 == 0:
        ans += sum(A[3:3+ind//2])*2
    else:
        ans += sum(A[3:3+ind//2])*2+A[3+ind//2]


print(ans)
Buggy code:
import heapq


def get_tuple(a, b):
    if a > b:
        a, b = b, a
    return -2*b - a, a, b


def main(a, N):
    a.sort(reverse=True)
    h = []
    comfort = a[0]
    heapq.heappush(h, get_tuple(a[0], a[1]))
    for i in range(N - 2):
        val = heapq.heappop(h)
        comfort += val[1]
        heapq.heappush(h, get_tuple(a[i], val[1]))
        heapq.heappush(h, get_tuple(a[i], val[2]))
    print(comfort)


if __name__ == '__main__':
    N = int(input())
    a = list(map(int, input().split()))
    main(a, N)










Fixed code:
import heapq


def get_tuple(a, b):
    if a > b:
        a, b = b, a
    return -a, b


def main(a, N):
    a.sort(reverse=True)
    h = []
    if N == 1:
        comfort = 0
    else:
        comfort = a[0]
        heapq.heappush(h, get_tuple(a[0], a[1]))
        heapq.heappush(h, get_tuple(a[0], a[1]))
        for i in range(2, N):
            val = heapq.heappop(h)
            comfort -= val[0]
            heapq.heappush(h, get_tuple(a[i], -val[0]))
            heapq.heappush(h, get_tuple(a[i], val[1]))
    print(comfort)


if __name__ == '__main__':
    N = int(input())
    a = list(map(int, input().split()))
    main(a, N)
Buggy code:
import math
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse = True)

ans = 0
for i in range(N-1):
  a = math.floor(i/2)
  ans += A[a]
    
print(ans)

Fixed code:
import math
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse = True)

ans = 0
for i in range(1,N):
  a = math.floor(i/2)
  ans += A[a]
    
print(ans)
Buggy code:
N = int(input())

A = []
a, *an = map(int, input().split())
A.append(a)
A += an
A.sort()

Node = []
score = 0
Node.append(A.pop())

print(A)

for n in range(N - 1):
    score += Node.pop(0)
    num = A.pop()
    Node.append(num)
    Node.append(num)

#print(Node)
print(score)

Fixed code:
N = int(input())

A = []
a, *an = map(int, input().split())
A.append(a)
A += an
A.sort()

Node = []
score = 0
Node.append(A.pop())

#print(A)
start = 0

for n in range(N - 1):
    score += Node[start]
    num = A.pop()
    start += 1
    #Node = Node[1:]
    Node.append(num)
    Node.append(num)

#print(Node)
print(score)
Buggy code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jul  5 22:49:34 2020

@author: Aruto Hosaka
"""


N = int(input())
A = list(map(int,input().split()))
A = sorted(A,reverse = True)
count = 0
count += A[0]
if N % 2 == 1:
    for n in range(1,(N-1)//2):
        count += 2*A[n]
        count += A[N//2]
else:
    for n in range(1,N//2):
        count += 2*A[n]    

Fixed code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jul  5 22:49:34 2020

@author: Aruto Hosaka
"""


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

count = A[0]
if N % 2 == 1:
    for n in range(1,(N-1)//2):
        count += 2*A[n]
    count += A[N//2]
else:
    for n in range(1,N//2):
        count += 2*A[n]
print(count)
Buggy code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse = True)
ans = A[0]

for i in range(1,N//2):
    ans = ans + A[i]*2
print(ans)

Fixed code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse = True)
ans = A[0]

if N%2 == 0:
  for i in range(1,int(N/2)):
    ans = ans + A[i]*2
if N%2 == 1:
  for i in range(1,int((N-1)/2)):
    ans = ans + A[i]*2
  ans += A[int((N-1)/2)]
print(ans)
Buggy code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse = True)
ans = A[0]

if N%2 == 0:
  for i in range(1,N/2):
    ans = ans + A[i]*2
if N%2 == 1:
  for i in range(1,(N-1)/2)
    ans = ans + A[i]*2
  ans += A[(N+1)/2]
print(ans)

Fixed code:
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
ans = 0
for i in range(N-1):
    if i == 0:
        ans += A[i]
    else:
        ans += A[1+(i-1)//2]
print(ans)
Buggy code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse = True)
ans = A[0]

for i in range(1,N):
    ans = ans + A[i]:2
print(ans)

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

A.sort(reverse = True)

ans = A[0]

idx = 1
cnt = 0
for i in range(2,N):
    ans += A[idx]
    cnt += 1
    if cnt == 2:
        idx += 1
        cnt = 0

print(ans)
Buggy code:
N = int(input())
a = list(map(int, input().split()))

a.sort(reverse=True)

count = 0
if N % 2 == 0:
  for i in range(int(N/2)):
    count += 2 * a[i]
    count -= a[0]
else:
  for i in range(int(N+1)/2):
    count += 2 * a[i]
    count -= a[0]
    count -= a[(N-1)/2]
    
print(count)

Fixed code:
N = int(input())
a = list(map(int, input().split()))

a.sort(reverse=True)

count = 0
if N % 2 == 0:
  for i in range(int(N/2)):
    count += 2 * a[i]
  count -= a[0]
else:
  for i in range(int((N+1)/2)):
    count += 2 * a[i]
  count -= a[0]
  count -= a[int((N-1)/2)]
    
print(count)
Buggy code:
#D
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
ans = 0
for i in range(1, n - n // 2 + 1):
    ans += a[i] * 2
ans-=a[n - n // 2 + 1]
print(ans)

Fixed code:
#D
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
ans = [a[0]]
for i in range(1, n - n // 2 + 1):
    ans.append(a[i])
    ans.append(a[i])
print(sum(ans[:n - 1]))
Buggy code:
n = int(input())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
ans = 0
for i in range(0, n - 1):
    ans += A[(i + 1) // 2]
print(ans)

Fixed code:
n = int(input())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
ans = 0
for i in range(0, n - 1):
    ans += a_list[(i + 1) // 2]
print(ans)
Buggy code:
import heapq

n=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)

hp=[]

ans=-A[0]
heapq.heappush(hp,-A[0])
heapq.heappush(hp,-A[1])

for a in A[2:]:
    ans+=heapq.heappop(hp)
    heapq.heappush(hp,-a)
    heapq.heappush(hp,-a)
print(-ans)

Fixed code:
import heapq

n=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)

hp=[]

ans=0
heapq.heappush(hp,-A[0])

for a in A[1:]:
    ans+=heapq.heappop(hp)
    heapq.heappush(hp,-a)
    heapq.heappush(hp,-a)
print(-ans)
Buggy code:
from sys import stdin
N = int(stdin.readline().rstrip())
ran = [int(x) for x in stdin.readline().rstrip().split()]
ran.sort(reverse=True)

gen = len(ran) - 2
goukei = ran[0]
if gen % 2 == 0:
  for i in range(int(gen/2)):
    goukei += 2 * ran[i + 1]
else:
  for i in rangen(int((gen-1)/2)):
    goukei += 2 * ran[i + 1]
  goukei += ran[i + 2]

print(goukei)

Fixed code:
from sys import stdin
N = int(stdin.readline().rstrip())
ran = [int(x) for x in stdin.readline().rstrip().split()]
ran.sort(reverse=True)

gen = len(ran) - 2
goukei = ran[0]
if gen % 2 == 0:
  for i in range(int(gen/2)):
    goukei += 2 * ran[i + 1]
else:
  for i in range(int((gen-1)/2)):
    goukei += 2 * ran[i + 1]
  goukei += ran[i + 2]

print(goukei)
Buggy code:
N = int(input())
A = [int(i) for i in input().split()]
A = sorted(A,reverse=True)
ans = a[0]
if n==2:
    print(ans)
else:
    a = a[1]
    for i in range(N-2):
        idx = i//2
        ans += a[idx]
    print(ans)

Fixed code:
N = int(input())
A = [int(i) for i in input().split()]
A = sorted(A,reverse=True)
ans = A[0]
if N==2:
    print(ans)
else:
    A = A[1:]
    for i in range(N-2):
        idx = i//2
        ans += A[idx]
    print(ans)
Buggy code:
n = int(input())
a = list(map(int, input().split()))
a.sort()

ans = 0
for i in range(1,n+1):
  ans += a[n-i//2-1]
  
print(ans)
         

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

ans = 0
for i in range(1, n):
    ans += a[n - i//2 - 1]

print(ans)
Buggy code:
N = int(input())
A = list(map(int, input().split())).sort()

len_A = len(A)
ans = 0
for i in range(1, len_A-1):
  ans += A[len_A-1-int(i/2)]

print(ans)

Fixed code:
N = int(input())
A = list(map(int, input().split()))
A.sort()

len_A = len(A)
ans = 0
for i in range(1, len_A):
  ans += A[len_A-1-int(i/2)]

print(ans)
Buggy code:
N = int(input())
nums = list(map(int, input().split()))
nums.sort(reverse=True)
score = nums[0] + sum(nums[1:N//2])
if N%2 != 0:
  score += nums[N//2]
print(score)

Fixed code:
N = int(input())
nums = list(map(int, input().split()))
nums.sort(reverse=True)
score = nums[0] + 2 * sum(nums[1:N//2])
if N%2 != 0:
  score += nums[N//2]
print(score)
Buggy code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N = inputInt()
	A = inputList()

	A.sort()
	A = A[::-1]
	ans = 0
	maxs = 0
	cnt = 0
	fst = True

	ansans = []
	pgt = queue.Queue()
	for i,val in enumerate(A):
		if i == 0:
			pgt.put(val)
		else:
			tmp = pgt.get()
			ansans.append(tmp)

			pgt.put(val)

			if tmp == val:
				pgt.put(val)
			else:
				pgt.put(val)

	print(ansans)
	print(sum(ansans))


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()


Fixed code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N = inputInt()
	A = inputList()

	A.sort()
	A = A[::-1]
	ans = 0
	maxs = 0
	cnt = 0
	fst = True

	ansans = []
	pgt = queue.Queue()
	for i,val in enumerate(A):
		if i == 0:
			pgt.put(val)
		else:
			tmp = pgt.get()
			ansans.append(tmp)

			pgt.put(val)

			if tmp == val:
				pgt.put(val)
			else:
				pgt.put(val)

	#print(ansans)
	print(sum(ansans))


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()
Buggy code:
import sys
input = sys.stdin.readline

def li():
    return [int(x) for x in input().split()]

N = int(input())
A = li()

A.sort(reverse=True)

B = [A[i] for i in range(N) if i < N//2]

# 9 8 7 6 5
# 9 1 8 2 7 3 6 4 5
# 9 6 8 7
# 9+8+7+6+5+6+7+8

# 8 7 6 5
# 8 1 7 2 6 3 5 4
# 8+7+6+5+5+6+7

sm = B[0] + sum(B[i] * 2 for i in range(len(B)) if 0 < i)
print(sm)

Fixed code:
import sys
input = sys.stdin.readline

def li():
    return [int(x) for x in input().split()]

N = int(input())
A = li()

A.sort(reverse=True)

B = [A[i] for i in range(N) if i < N//2]

# 9 8 7 6 5
# 9 1 8 2 7 3 6 4 5
# 9 6 8 7
# 9+8+8+7+7+6+6+5

# 8 7 6 5
# 8 1 7 2 6 3 5 4
# 8+7+6+5+5+6+7

if N % 2 == 1:
    B = [A[i] for i in range(N) if i < N // 2 + 1]
    sm = B[0] + B[-1] + sum(B[i]*2 for i in range(N//2) if 0 < i)
    print(sm)
else:
    sm = B[0] + sum(B[i]*2 for i in range(N//2) if 0 < i)
    print(sm)
Buggy code:
import heapq as hq

n = int(input())
a = [int(_) for _ in input().split()]

a.sort()
p = a.pop()
q = []
hq.heappush(q, (-p, -p))
ans = 0
for _ in range(n - 1):
    p = a.pop()
    b1, b2 = hq.heappop(q)
    print(b1, b2)
    ans += -b1
    hq.heappush(q, sorted((b1, -p), reverse=True))
    hq.heappush(q, sorted((b2, -p), reverse=True))
print(ans)


Fixed code:
import heapq as hq

n = int(input())
a = [int(_) for _ in input().split()]

a.sort()
p = a.pop()
q = []
hq.heappush(q, (-p, -p))
ans = 0
for _ in range(n - 1):
    p = a.pop()
    b1, b2 = hq.heappop(q)
    ans += -b1
    hq.heappush(q, sorted((b1, -p), reverse=True))
    hq.heappush(q, sorted((b2, -p), reverse=True))
print(ans)
Buggy code:
n = int(input())

A = sorted(list(map(int, input().split())), reverse=True)

score_list = [0]
for i in range(1, len(A)):
    score_list.append(A[i])
    score_list.append(A[i])
    
print(sum(score_list[:n-1]))

Fixed code:
n = int(input())

A = sorted(list(map(int, input().split())), reverse=True)

score_list = [0, A[0]]
for i in range(1, len(A)):
    score_list.append(A[i])
    score_list.append(A[i])
    
print(sum(score_list[:n]))
Buggy code:
import heapq

N = int(input())
A = list(map(int, input().split()))
A = list(map(lambda x: x * (-1), A))

heapq.heapify(A)

ans = 0
N -= 2
ans += heapq.heappop(A) * (-1)

if N == 0:
    print(ans)
    exit()
elif N == 1:
    ans += A[0]
    print(ans)
    exit()
elif N >= 2:
    q = N // 2
    r = N % 2
    for i in range(1, q + 1):
        max_n = heapq.heappop(A) * (-1)
        ans += 2 * max_n
    if r == 1:
        ans += A[0]
    print(ans)


Fixed code:
import heapq

N = int(input())
A = list(map(int, input().split()))
A = list(map(lambda x: x * (-1), A))

heapq.heapify(A)

ans = 0
N -= 2
ans += heapq.heappop(A) * (-1)

if N == 0:
    print(ans)
    exit()
elif N == 1:
    ans += A[0]
    print(ans)
    exit()
elif N >= 2:
    q = N // 2
    r = N % 2
    for i in range(1, q + 1):
        ans += 2 * heapq.heappop(A) * (-1)
    if r == 1:
        ans += heapq.heappop(A) * (-1)
    print(ans)
Buggy code:
N = int(input())
A = list(map(int,input().split()))
A = sorted(A)
ans = A[N//2:]
if N//2 == 0:
    print(sum(ans)*2 - max(ans))
else:
    print(sum(ans)*2 - max(ans) - min(ans))

Fixed code:
N = int(input())
A = list(map(int,input().split()))
A = sorted(A)
ans = A[N//2:]
if N%2 == 0:
    print(sum(ans)*2 - max(ans))
else:
    print(sum(ans)*2 - max(ans) - min(ans))
Buggy code:
N = int(input())
A = list(map(int, input().split()))

A.sort(reverse=True)
result = 0
for i in range(N):
    result += A[(i + 1) // 2]
print(result)


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

A.sort(reverse=True)
result = 0
for i in range(N - 1):
    result += A[(i + 1) // 2]
print(result)
Buggy code:
n = int(input())
a = list(map(int, input().split()))
b = sorted(b, reverse=True)
ans = 0
for i in range(n):
    ans += b[(i+1)//2]
print(ans)


Fixed code:
n = int(input())
a = list(map(int, input().split()))
b = sorted(a, reverse=True)
ans = 0
for i in range(n-1):
    ans += b[(i+1)//2]
print(ans)
Buggy code:
import heapq
n = int(input())
A = list(map(int, input().split()))
A = sorted(A, reverse=True)
ans = 0
q = []
heapq.heappush(q, A[0]*-1)
for i in range(1, n):
    print(q)
    tmp = heapq.heappop(q)
    ans += tmp
    heapq.heappush(q, A[i] * -1)
    heapq.heappush(q, A[i] * -1)

print(ans*-1)

Fixed code:
import heapq
n = int(input())
A = list(map(int, input().split()))
A = sorted(A, reverse=True)
ans = 0
q = []
heapq.heappush(q, A[0]*-1)
for i in range(1, n):
    tmp = heapq.heappop(q)
    ans += tmp
    heapq.heappush(q, A[i] * -1)
    heapq.heappush(q, A[i] * -1)

print(ans*-1)
Buggy code:
import math

N = int(input())
A = list(map(int, input().split()))
Asrt = sorted(A, reverse=True)


num = math.ceil(N/2)

a = Asrt([:num])

if N % 2 == 0:#guusuu
    print(sum(a) * 2 - a[0])
else:#kisuu
    print(sum(a) * 2 - a[0] - a[-1])


Fixed code:
import math

N = int(input())
A = list(map(int, input().split()))
Asrt = sorted(A, reverse=True)


num = math.ceil(N/2)

a = Asrt[:num]

if N % 2 == 0:#guusuu
    print(sum(a) * 2 - a[0])
else:#kisuu
    print(sum(a) * 2 - a[0] - a[-1])
Buggy code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
a.sort(reverse=True)

ans += a[0]

for i in range(1, n // 2):
    ans += 2 * a[i]
if n % 2 == 0:
    ans += 2 * a[n//2]
else:
    ans += a[n//2]

print(ans)

Fixed code:
n=int(input())
a=sorted(list(map(int,input().split())),reverse=True)
ans=0
if n%2==0:
  for i in range(n//2):
    ans+=a[i]*2
    if i==0:
      ans-=a[i]
else:
  for i in range(n//2+1):
    ans+=a[i]*2
    if i==0:
      ans-=a[i]
    if i==n//2:
      ans-=a[i]
print(ans)
Buggy code:
import math
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")

N = I()
A = LI()
ans = 0

A.sort(reverse=True)
# print(A)
half = math.ceil(len(A)/2)
# print(half)
for i in range(half):
    ans += A[i]

for i in range(1,half):
    ans += A[i]

print(ans)

Fixed code:
import math
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")

N = I()
A = LI()

A.sort(reverse=True)
# print(A)
# half = math.ceil(len(A)/2)
q, mod = divmod(len(A),2)

if mod == 0:
    ans = A[0]
    for i in range(1,q):
        ans += 2 * A[i]
else:
    ans = A[0]
    for i in range(1,q):
        ans += 2 * A[i]
    ans += A[q]

print(ans)
Buggy code:
n = int(input())
li_a = list(map(int, input().split()))
sum_ = 0
li_a.sort()
for i in range(n-1):
  sum_ += li_a[n-1-(i)//2]
print(sum_)

Fixed code:
n = int(input())
li_a = list(map(int, input().split()))
sum_ = 0
li_a.sort()
for i in range(1, n):
  sum_ += li_a[n-1-i//2]
print(sum_)
Buggy code:
import sys
sys.setrecursionlimit(10**9)

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

    print(a)
    a.sort(reverse=True)
    ans = 0

    for i in range(0, n-1):
        now_index = (i+1) // 2
        ans += a[now_index]

    print(ans)
    return


if __name__ == "__main__":
    main()
    sys.exit(0)


Fixed code:
import sys
sys.setrecursionlimit(10**9)

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

    a.sort(reverse=True)
    ans = 0

    for i in range(0, n-1):
        now_index = (i+1) // 2
        ans += a[now_index]

    print(ans)
    return


if __name__ == "__main__":
    main()
    sys.exit(0)
Buggy code:
import heapq

def solve(n, a):
    a = sorted(a, reverse=True)
    ans = sum(a[:2])
    que = [(a[-2], a[-1])]
    heapq.heapify(que)
    for i in range(2, n):
        x, y = heapq.heappop(que)
        ans += min(x, y)
        heapq.heappush(que, (a[i], x))
        heapq.heappush(que, (a[i], y))
    return ans

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

Fixed code:
import heapq

def solve(n, a):
    a = sorted(a, reverse=True)
    ans = a[0]
    que = [(-a[1], -a[0]), (-a[1], -a[0])]
    heapq.heapify(que)
    for i in range(2, n):
        x, y = heapq.heappop(que)
        ans += min(-x, -y)
        heapq.heappush(que, (-a[i], x))
        heapq.heappush(que, (-a[i], y))
    return ans

n = int(input())
a = list(map(int, input().split()))
print(solve(n, a))
Buggy code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    n = II()
    a = LI()
    a.sort()
    ans = 0
    a = a[::-1]
    for i in range(n):
        ans += ans[(i + 1) // 2]
    print(ans)
    return


#main
if __name__ == '__main__':
    solve()


Fixed code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    n = II()
    a = LI()
    a.sort()
    ans = 0
    a = a[::-1]
    for i in range(1, n):
        ans += a[i // 2]
    print(ans)
    return


#main
if __name__ == '__main__':
    solve()