Buggy python submission (verdict: Time Limit Exceeded):
import sys

N = int(sys.stdin.readline())
A = [int(i) for i in sys.stdin.readline().split(' ')]

A.sort(reverse=True)
max_value = A.pop(0)
diff = [max_value]
ans = 0
for i in A:
    ans = ans + diff.pop(0)
    diff = diff + [i, i]

print(ans)

Fixed submission:
import sys
from collections import deque

N = int(sys.stdin.readline())
A = [int(i) for i in sys.stdin.readline().split(' ')]

A.sort(reverse=True)
max_value = A.pop(0)
diff = deque([max_value])
ans = 0
for i in A:
    ans = ans + diff.popleft()
    diff.append(i)
    diff.append(i)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

input = sys.stdin.readline

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

A.sort(reverse=True)
max_value = A.pop(0)
diff = [max_value]
ans = 0
for i in A:
    ans = ans + diff.pop(0)
    diff = diff + [i, i]

print(ans)

Fixed submission:
import sys
from collections import deque

N = int(sys.stdin.readline())
A = [int(i) for i in sys.stdin.readline().split(' ')]

A.sort(reverse=True)
max_value = A.pop(0)
diff = deque([max_value])
ans = 0
for i in A:
    ans = ans + diff.popleft()
    diff.append(i)
    diff.append(i)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(i) for i in input().split(' ')]

A.sort(reverse=True)
max_value = A.pop(0)
diff = [max_value]
ans = 0
for i in A:
    n = diff.pop(0)
    ans = ans + n
    diff = diff + [i, i]

print(ans)

Fixed submission:
import sys
from collections import deque

N = int(sys.stdin.readline())
A = [int(i) for i in sys.stdin.readline().split(' ')]

A.sort(reverse=True)
max_value = A.pop(0)
diff = deque([max_value])
ans = 0
for i in A:
    ans = ans + diff.popleft()
    diff.append(i)
    diff.append(i)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(i) for i in input().split(' ')]

max_value = A.pop(A.index(max(A)))
diff = [max_value]
ans = 0
for i in range(N - 1):
    ans = ans + max(diff)
    n = diff.index(max(diff))
    max_value = A.pop(A.index(max(A)))
    diff[n] = max_value
    diff.insert(n + 1, max_value)

print(ans)

Fixed submission:
import sys
from collections import deque

N = int(sys.stdin.readline())
A = [int(i) for i in sys.stdin.readline().split(' ')]

A.sort(reverse=True)
max_value = A.pop(0)
diff = deque([max_value])
ans = 0
for i in A:
    ans = ans + diff.popleft()
    diff.append(i)
    diff.append(i)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(i) for i in input().split(' ')]

A.sort(reverse=True)
max_value = A.pop(0)
diff = [max_value]
ans = 0
for i in A:
    ans = ans + max(diff)
    n = diff.index(max(diff))
    diff[n] = i
    diff.insert(n + 1, i)

print(ans)

Fixed submission:
import sys
from collections import deque

N = int(sys.stdin.readline())
A = [int(i) for i in sys.stdin.readline().split(' ')]

A.sort(reverse=True)
max_value = A.pop(0)
diff = deque([max_value])
ans = 0
for i in A:
    ans = ans + diff.popleft()
    diff.append(i)
    diff.append(i)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(i) for i in input().split(' ')]

A.sort(reverse=True)
ring = []
max_value = A.pop(0)
ring.append(max_value)
ring.append(max_value)
diff = [max_value]
ans = 0
flag = True
for i in A:
    ans = ans + max(diff)
    n = diff.index(max(diff))
    ring.insert(n + 1, i)
    diff[n] = i
    diff.insert(n + 1, i)

print(ans)

Fixed submission:
import sys
from collections import deque

N = int(sys.stdin.readline())
A = [int(i) for i in sys.stdin.readline().split(' ')]

A.sort(reverse=True)
max_value = A.pop(0)
diff = deque([max_value])
ans = 0
for i in A:
    ans = ans + diff.popleft()
    diff.append(i)
    diff.append(i)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(i) for i in input().split(' ')]

A.sort(reverse=True)
ring = []
max_value = A.pop(0)
ring.append(max_value)
ring.append(max_value)
ans = 0
flag = True
while len(A) != 0:
    i = A.pop(0)
    n = []
    if flag:
        for j in range(0, len(ring) - 1):
            check = min(ring[j:j + 2])
            n.append(check)
            flag = False
            if check == max(ring):
                break
    else:
        for j in range(len(ring), 1, -1):
            check = min(ring[j - 2:j])
            n.append(check)
            flag = True
            if check == max(ring):
                break
    ans = ans + max(n)
    ring.insert(n.index(max(n)) + 1, i)

print(ans)

Fixed submission:
import sys
from collections import deque

N = int(sys.stdin.readline())
A = [int(i) for i in sys.stdin.readline().split(' ')]

A.sort(reverse=True)
max_value = A.pop(0)
diff = deque([max_value])
ans = 0
for i in A:
    ans = ans + diff.popleft()
    diff.append(i)
    diff.append(i)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(i) for i in input().split(' ')]

A.sort(reverse=True)
ring = []
max_value = A.pop(0)
ring.append(max_value)
ring.append(max_value)
ans = 0

while len(A) != 0:
    i = A.pop(0)
    n = []
    for j in range(0, len(ring) - 1):
        check = min(ring[j:j + 2])
        n.append(check)
        if check == max(ring):
            break
    ans = ans + max(n)
    ring.insert(n.index(max(n)) + 1, i)

print(ans)

Fixed submission:
import sys
from collections import deque

N = int(sys.stdin.readline())
A = [int(i) for i in sys.stdin.readline().split(' ')]

A.sort(reverse=True)
max_value = A.pop(0)
diff = deque([max_value])
ans = 0
for i in A:
    ans = ans + diff.popleft()
    diff.append(i)
    diff.append(i)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(i) for i in input().split(' ')]

A.sort(reverse=True)
ring = []
max_value = A.pop(0)
ring.append(max_value)
ring.append(max_value)
ans = 0
for i in A:
    n = []
    for j in range(0, len(ring) - 1):
        check = min(ring[j:j + 2])
        n.append(check)
        if check == max(ring):
            break
    ans = ans + max(n)
    ring.insert(n.index(max(n)) + 1, i)

print(ans)

Fixed submission:
import sys
from collections import deque

N = int(sys.stdin.readline())
A = [int(i) for i in sys.stdin.readline().split(' ')]

A.sort(reverse=True)
max_value = A.pop(0)
diff = deque([max_value])
ans = 0
for i in A:
    ans = ans + diff.popleft()
    diff.append(i)
    diff.append(i)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(i) for i in input().split(' ')]

A.sort(reverse=True)
ring = []
max_value = A.pop(0)
ring.append(max_value)
ring.append(max_value)
ans = 0
for i in A:
    buf = [x for x in ring]
    n = []
    for j in range(0, len(ring) - 1):
        n.append(min(ring[j:j+2]))
    ans = ans + max(n)
    ring.insert(n.index(max(n)) + 1, i)

print(ans)

Fixed submission:
import sys
from collections import deque

N = int(sys.stdin.readline())
A = [int(i) for i in sys.stdin.readline().split(' ')]

A.sort(reverse=True)
max_value = A.pop(0)
diff = deque([max_value])
ans = 0
for i in A:
    ans = ans + diff.popleft()
    diff.append(i)
    diff.append(i)

print(ans)