#!/usr/bin/env python3

import sys

from collections import deque



class MM:

    def __init__(self, c_max, c_cnt):

        self.c_max = c_max

        self.c_cnt = c_cnt



def solve(N: int, A: "List[int]"):

    sl = list(reversed(sorted(A)))

    score = 0

    c_max = sl[0]

    h = {sl[0]: 1}

    queue = deque()

    queue.append(c_max)

    for i in range(1, len(sl)):

        score += c_max



        h[c_max] -= 1

        if sl[i] in h:

            h[sl[i]] += 2

        else:

            h[sl[i]] = 2

            queue.append(sl[i])



        if h[c_max] == 0:

            queue.popleft()

            c_max = queue[0]

        # print(f'{i} {sl[i]} {c_max} {h}')

    print(score)

    return





def main():

    def iterate_tokens():

        for line in sys.stdin:

            for word in line.split():

                yield word

    tokens = iterate_tokens()

    N = int(next(tokens))  # type: int

    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"

    solve(N, A)



if __name__ == '__main__':

    main()

#!/usr/bin/env python3

import sys

from collections import deque



def solve(N: int, A: "List[int]"):

    sorted_list = list(reversed(sorted(A)))

    score = 0

    queue = deque()

    queue.append(sorted_list[0])

    cnt_map = {sorted_list[0]: 1}

    for i in range(1, len(sorted_list)):

        current_max = queue[0]

        score += current_max

        cnt_map[current_max] -= 1

        num = sorted_list[i]

        if num in cnt_map:

            cnt_map[num] += 2

        else:

            cnt_map[num] = 2

            queue.append(num)



        if cnt_map[current_max] == 0:

            queue.popleft()

    print(score)

    return





def main():

    def iterate_tokens():

        for line in sys.stdin:

            for word in line.split():

                yield word

    tokens = iterate_tokens()

    N = int(next(tokens))  # type: int

    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"

    solve(N, A)



if __name__ == '__main__':

    main()

#!/usr/bin/env python3
import sys
from collections import deque


class MM:
    def __init__(self, c_max, c_cnt):
        self.c_max = c_max
        self.c_cnt = c_cnt


def solve(N: int, A: "List[int]"):
    sl = list(reversed(sorted(A)))
    score = 0
    c_max = sl[0]
    h = {sl[0]: 1}
    queue = deque()
    queue.append(c_max)
    for i in range(1, len(sl)):
        score += c_max
        h[c_max] -= 1
        if sl[i] in h:
            h[sl[i]] += 2
        else:
            h[sl[i]] = 2
            queue.append(sl[i])
        if h[c_max] == 0:
            queue.popleft()
            c_max = queue[0]
        # print(f'{i} {sl[i]} {c_max} {h}')
    print(score)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)


if __name__ == "__main__":
    main()

#!/usr/bin/env python3
import sys
from collections import deque


def solve(N: int, A: "List[int]"):
    sorted_list = list(reversed(sorted(A)))
    score = 0
    queue = deque()
    queue.append(sorted_list[0])
    cnt_map = {sorted_list[0]: 1}
    for i in range(1, len(sorted_list)):
        current_max = queue[0]
        score += current_max
        cnt_map[current_max] -= 1
        num = sorted_list[i]
        if num in cnt_map:
            cnt_map[num] += 2
        else:
            cnt_map[num] = 2
            queue.append(num)
        if cnt_map[current_max] == 0:
            queue.popleft()
    print(score)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)


if __name__ == "__main__":
    main()

-class MM: -    def __init__(self, c_max, c_cnt): -        self.c_max = c_max -        self.c_cnt = c_cnt - - -    sl = list(reversed(sorted(A))) +    sorted_list = list(reversed(sorted(A))) -    c_max = sl[0] -    h = {sl[0]: 1} -    queue.append(c_max) -    for i in range(1, len(sl)): -        score += c_max -        h[c_max] -= 1 -        if sl[i] in h: -            h[sl[i]] += 2 +    queue.append(sorted_list[0]) +    cnt_map = {sorted_list[0]: 1} +    for i in range(1, len(sorted_list)): +        current_max = queue[0] +        score += current_max +        cnt_map[current_max] -= 1 +        num = sorted_list[i] +        if num in cnt_map: +            cnt_map[num] += 2 -            h[sl[i]] = 2 -            queue.append(sl[i]) -        if h[c_max] == 0: +            cnt_map[num] = 2 +            queue.append(num) +        if cnt_map[current_max] == 0: -            c_max = queue[0] -        # print(f'{i} {sl[i]} {c_max} {h}')