Problem:


Score: 400 points

Problem Statement

Quickly after finishing the tutorial of the online game ATChat, you have decided to visit a particular place with N-1 players who happen to be there. These N players, including you, are numbered 1 through N, and the friendliness of Player i is A_i.

The N players will arrive at the place one by one in some order. To make sure nobody gets lost, you have set the following rule: players who have already arrived there should form a circle, and a player who has just arrived there should cut into the circle somewhere.

When each player, except the first one to arrive, arrives at the place, the player gets comfort equal to the smaller of the friendliness of the clockwise adjacent player and that of the counter-clockwise adjacent player. The first player to arrive there gets the comfort of 0.

What is the maximum total comfort the N players can get by optimally choosing the order of arrivals and the positions in the circle to cut into?

Constraints

All values in input are integers.

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the maximum total comfort the N players can get.

Sample Input 1

4
2 2 1 3

Sample Output 1

7

By arriving at the place in the order Player 4, 2, 1, 3, and cutting into the circle as shown in the figure, they can get the total comfort of 7.

They cannot get the total comfort greater than 7, so the answer is 7.

Sample Input 2

7
1 1 1 1 1 1 1

Sample Output 2

6



Sample input:
4
2 2 1 3


Sample output:
7

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

import sys
from typing import Any, Callable, Deque, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union
# import time
# import math, cmath
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
from heapq import _heapify_max, _heappop_max, _siftdown_max
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import Fraction               # Fraction(a, b) => a / b ∈ Q. note: Fraction(0.1) do not returns Fraciton(1, 10). Fraction('0.1') returns Fraction(1, 10)



def main():
    Num = Union[int, float]
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79e+308
    # inf = 2 ** 63 - 1             # (for fast JIT compile in PyPy) 9.22e+18
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input():  return sys.stdin.readline().rstrip()
    def ii():     return int(input())
    def isp():    return input().split()
    def mi():     return map(int, input().split())
    def mi_0():   return map(lambda x: int(x)-1, input().split())
    def lmi():    return list(map(int, input().split()))
    def lmi_0():  return list(map(lambda x: int(x)-1, input().split()))
    def li():     return list(input())
    def debug(x): print(x, file=sys.stderr)
    def _heappush_max(h, item): h.append(item); _siftdown_max(h, 0, len(h)-1)
    
    
    n = ii()
    L = sorted(lmi())
    h = [L.pop()]
    ans = 0

    while L:
        num = L.pop()
        max_interval = _heappop_max(h)
        ans += max_interval
        assert max_interval >= num
        _heappush_max(h, num)
        _heappush_max(h, num)
    
    print(ans)




if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=1)
from heapq import heappop,heappush
h=[]
heappush(h,-a[0])
ans=0
for i in a[1:]:
  ans-=heappop(h)
  heappush(h,-i)
  heappush(h,-i)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
l.sort()

out = [l.pop()]
while l:
    out.append(l[-1])
    out.append(l.pop())

print(sum(out[:(n - 1)]))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    a.sort(reverse=True)
    if n%2:
        ans += a[0]
        for i in range(1, n//2):
            ans += a[i]*2
        ans += a[n//2]
    else:
        ans += a[0]
        for i in range(1, n//2):
            ans += a[i]*2
    print(ans)
        

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
from queue import deque
q = deque([a[0]])
ans = 0
for num in a[1:]:
    ans += q.popleft()
    q.append(num)
    q.append(num)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())), reverse=True)
ans = a[0]
for i in range(2, n):
	ans += a[i//2]
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = sorted(map(int, input().split()), reverse=True)

ans, que = 0, [a[0]]
for i in enumerate(a[1:]):
    ans += que[i]
    que += [i, i]
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
A = sorted(A, reverse=True)
ans = 2*sum(A[:N//2])-A[0]+(N%2==1)*A[N//2+1]
print(ans)

Submission status: Runtime Error

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

sortA = sorted(A)

min_val = sortA[0]
min_count = 1
for i in range(1, N):
    if sortA[i] == min_val:
        min_count += 1
    break

mins = sortA[:min_count]
larges = sortA[min_count:]

result = sum(larges[1:])

if len(mins) < len(larges):
    result += sum(larges[len(larges)-len(mins)-1:-1])
elif len(mins) == len(larges):
    result += sum(larges[len(larges)-len(mins)-1:-1])
    result += larges[0]
else:
    result += sum(larges[len(larges)-len(mins)-1:-1])
    result += larges[0]
    rem_mins_count = len(mins) - len(larges)
    result += rem_mins_count * min_val

print(result)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split())).sorted()
 
len_A = len(A)
ans = 0
for i in range(1, len_A-1):
  ans += A[len_A-1-int(i/2)]

print(ans)

Submission status: Runtime Error

Solution 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)

Submission status: Runtime Error

Solution code:
4
2 2 1 3

Submission status: Runtime Error

Solution code:
import collections

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

b = []
cnt = 0
for i in range(n-1):
  if i!=n:
    b.append(a[i])
    if len(b)==1:
      pass
    else:
      cnt += min(b[-2],b[0])

c = collections.Counter(b)
cnt+= max([k for k, v in c.items() if v >=2])
print(cnt)

Submission status: Runtime Error

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

a.sort(reverse=True)

arrival = 1
ans = 0

while arrival < N:
    score = a.popleft()
    if arrival == 1:
        ans += score
        arrival += 1
    else:
        ans += score*min(2, N - arrival)
        arrival += 2

print(ans)

Submission status: Runtime Error

Solution code:
score = 0
turn = 0
n = int(input())
people = sorted(list(map(int, input().split())), reverse=True)
people = sorted(np.unique(people).tolist(), reverse=True)

remain = n-1
while True:
    if remain > 2**turn:
        score += people[turn] * (2**turn)
        remain -= 2 ** turn
        turn += 1
    else:
        score += people[turn] * remain
        break
    
print(score)

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define FORC(i,a,b,c) for(ll i=(a);i<(b);i+=(c))
#define pb(x) push_back(x)
#define ff first
#define ss second
#define dd double
#define mod 1000000007

template<class K,class other=null_type,class cmp=less<K>> using ordered_set=tree<K,other,
cmp, rb_tree_tag, tree_order_statistics_node_update>;

inline ll md(ll x){
    return x<mod?x:x%mod;
}

ll power(ll x, ll y){
    if(y==0){
        return 1;
    }
    ll x_=power(x,y/2);
    if(y & 1){
        return md(md(x_*x_)*md(x));
    }
    else{
        return md(x_*x_);
    }
}

ll mod_inverse(ll x){
    return power(x,mod-2);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n;
    cin>>n;
    ll arr[n];
    REP(i,n){
        cin>>arr[i];
    }
    sort(arr,arr+n);
    ll ans=arr[n-1];
    ll j=n-3;
    ll i=n-2;
    while(j>=0){
        ans+=arr[i];
        j--;
        if(j<0) break;
        ans+=arr[i];
        j--;
        i--;
    }
    cout<<ans<<endl;
}

Submission status: Runtime Error

Solution code:
n=int(input())
arr=[int(i) for i in input().strip().split(" ")]
arr=sorted(arr)
arr=arr[::-1]
newArr=[arr[0]]
ans=0
for i in range(1,n//2):
    newArr.append(arr[i])
    newArr.append(arr[i])
for i in range(n-1):
    ans+=newArr[i]
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
arr=[int(i) for i in input().strip().split(" ")]
arr=sorted(arr)
arr=arr[::-1]
newArr=[arr[0]]
ans=0
for i in arr[1:]:
    newArr.append(arr[i])
    newArr.append(arr[i])
for i in range(n-1):
    ans+=newArr[i]
print(ans)

Submission status: Runtime Error

Solution code:
def main() :
  def minp(list, len) :
    point = 0
    val = (10**9)*2
    for i in range((len-1)) :
      num = min(list[i], list[i+1])
      if num >= point and val > list[i] + list[i+1]:
        point = num
        res = i + 1
        val = list[i] + list[i+1]
    return res, point
  
  

  n = int(input().strip())
  people = [int(i)for i in input().strip().split(' ')]
  people.sort(reverse=True)
  ring = [people[0]]*2 #python は .eachがないためfor i in で代用
  people.pop(0)
  result = 0
  for person in people :
    res, point = minp(ring, len(ring))
    ring.insert(res, person)
    result += point
  print(result)
    
main()

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = sorted(a,reverse = True)
ans = 0
if n % 2 == 0:
    for i in range(0,n//2):
        if i == 0:
            ans += b[0]
        else:
            ans += 2*b[i]
else:
    for i in range(0,n):
        if i == 0:
            ans += 0
        else:
            ans += b[n-i//2]
print(ans)

Submission status: Runtime Error

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

A = sorted(A,reverse=True)

if N <= 3:
  print(sum(A[:-1]))
elif N % 2 == 0:
  ans = A[0] + sum(A[1:N//2])*2
elif N % 2 == 1:
  ans = A[0] + sum(A[1:N//2])*2 + A[1+N//2]
  
print(ans)

Submission status: Runtime Error

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

A = sorted(A, reverse = True)

la = len(A)

for i in range(la-1):
   ans += A[i]

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = sorted(map(int,input().split()),reverse = True)
answer = 0
i = 1
s = 0
answer = A[0]
while True:
    s = (i+1)//2
    answer += A[s]
    i += 1
    if i == N-1:
        break
    
print(answer)

Submission status: Runtime Error

Solution code:
N = int(input())
A = sorted(map(int,input().split()),reverse = True)
answer = 0
i = 1
s = 0
answer = A[0]
while True:
    s = (i+1)//2
    answer += A[s]
    print(i,s)
    i += 1
    if i == N-1:
        break
    
print(answer)

Submission status: Runtime Error

Solution code:
N = int(input())
A = sorted(map(int,input().split()),reverse = True)
answer = 0
i = 1
s = 0
answer = A[0]
while True:
    s = i-(i-1)%2
    answer += A[s]
    #print(i,s)
    i += 1
    if i == N-1:
        break
    
print(answer)

Submission status: Runtime Error

Solution code:
N = int(input())
A = sorted(map(int,input().split()),reverse = True)
answer = 0
i = 1
s = 0
answer = A[0]
while True:
    s = i+i%2
    answer += A[s]
    #print(i,s)
    i += 1
    if i == N-1:
        break
    
print(answer)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int n;
    cin >> n;
    int a[n];
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a, a + n);
    int ans = 0;
    int count = 2;
    ans = a[n - 1];
    for(int i = n - 2; i >= 0 and count < n; i--) {
        if(count != n) {
            ans += a[i];
            count++;
        }
        if(count != n) {
            ans += a[i];
            count++;
        }
    }
    cout << ans << endl;

    return 0;
}

Submission status: Runtime Error

Solution code:
import bisect
import statistics
n = int(input())
a = list((int(x) for x in input().split()))

a.sort()
incl = []
score = 0

for i in range(len(a)):
    ain = a.pop()
    incl.append(ain)
    if i==0: continue
    if (len(incl)%2 == 1):
        score += incl[ int(len(incl))/2 ]
    else:
        # smaller of middle two
        score += incl[ int(len(incl)/2)-1 ]

print(score)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
 
using namespace std;
 
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {
    return '"' + s + '"';
}
string to_string(const char* s) {
    return to_string((string) s);
}
string to_string(bool b) {
    return (b ? "true" : "false");
}
string to_string(vector<bool> v) {
    bool first = true;
    string res = "{";
    for (int i = 0; i < static_cast<int>(v.size()); i++) {
        if (!first) {
            res += ", ";
        }
        first = false;
        res += to_string(v[i]);
    }
    res += "}";
    return res;
}
template <size_t N>
string to_string(bitset<N> v) {
    string res = "";
    for (size_t i = 0; i < N; i++) {
        res += static_cast<char>('0' + v[i]);
    }
    return res;
}
template <typename A>
string to_string(A v) {
    bool first = true;
    string res = "{";
    for (const auto &x : v) {
        if (!first) {
            res += ", ";
        }
        first = false;
        res += to_string(x);
    }
    res += "}";
    return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
    return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
    return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
    return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
//Auther: Arin Roday
#define vec vector
#define i64 long long
#define endl '\n'
#define sz(x) (x).size()
#define times(n) for(int i = 0; i < (n); ++i)
#define bits(x) __builtin_popcount(x)
 
int test_case_number = 1;

void print_case(){
    #ifdef LOCAL
    cout << "Case #" << test_case_number++ << ": ";
    #endif
}

void solve(){
    int n; cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    sort(a.begin(), a.end(), greater<int> ());
    debug(a);
    int ans = 0;
    int k = 0;
    for(int i = 1; i < n; ++i){
        ans += a[k];
        if(i % 2 == 0 || i == 1){
            k ++;
        }
        debug(i, k, ans);
    }
    cout << ans << endl;
}
 
int main(){
    solve();
}

Submission status: Runtime Error

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

L = len(P) // 2

if len(A) % 2 == 0:
    #偶数の時
    ans = sum(A[:L]) + sum(A[1:L])
else:
    #奇数の時
    ans = sum(A[:L]) + sum(A[1:L+1])

print(ans)

Submission status: Runtime Error

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

a.sort(reverse=True)
ans = a[0]
i = 1
while True:
    ans += a[i]
    i += 1
    if i == n - 1:
        break
    ans += a[i]
    i += 1
    if i == n - 1:
        break
print(ans)

Submission status: Runtime Error

Solution 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[(N+1)/2]
print(ans)

Submission status: Runtime Error

Solution 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)

Submission status: Runtime Error

Solution 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)

Submission status: Runtime Error

Solution 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)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
print(A[0] + sum(A[1:1+(N-2)//2])*2 + ((N-2)%2)*A[1+1+(N-1)//2])

Submission status: Runtime Error

Solution 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)

Submission status: Runtime Error

Solution code:
N = int(input())
a = [i for i in input().split()]
a = sorted(a,reverse=True)
a = a[:N-1]
a = sum(a)
print(a)

Submission status: Runtime Error

Solution code:
N = int(input())
a = [i for i in input().split()]
a = sorted(a,reverse=True)
a = a[:N-1]
a = sum(a)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i =0;i<(int)(n);i++)
#define endl '\n'
typedef pair<int,int> P;
typedef long long ll;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,n){
        cin >> a[i];
    }
    ll res = 0;
    sort(a.begin(),a.end());
    for(int i=1;i<n;i++){
        res+=a[n-1-i/2];
    }
    cout << res << endl;
}

Submission status: Runtime Error

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

ans = 0

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

Submission status: Runtime Error

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

ans = 0

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

Submission status: Runtime Error

Solution code:
import heapq

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))

n=int(input())
A=input_array()
sortA=sorted(A,reverse=True) #降順にソート

ans=0
q=[]
heapq.heappush(q,-sortA[0])

for i in range(1,n):
	tmp=heapq.heappop(q)
	tmp*=-1
	ans+=tmp
	heapq.heappush(q,-sortA[i])
	heapq.heappush(q,-sortA[i])
	print(q)
print(ans)

Submission status: Runtime Error

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

A.sort(reverse=True)
ans = 0

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

print(ans)

Submission status: Runtime Error

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

A.sort(reverse=True)
ans = 0

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

Submission status: Runtime Error

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

A.sort(reverse=True)
ans = 0

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

Submission status: Runtime Error

Solution code:
N = int(input())
A = []
for i in range(N):
	A.append(map(int, input().split()))

A.sort(reverse=True)
ans = 0

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

Submission status: Runtime Error

Solution 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)

Submission status: Runtime Error

Solution code:
N = int(input())
confort_list = map(int, input().split())

result = 0
for i in range(N-1):
    result += confort_list[i]
print(result)

Submission status: Runtime Error

Solution code:
N = int(input())
confort_list = map(int, input().split())

result = 0
for i in range(len(N-1)):
    result += confort_list[i]
print(result)

Submission status: Runtime Error

Solution code:
N = int(input())
A = sorted(list(map(int, input().split())),reverse=True)
ring = [A[0]]
score = [A[0]]
s = 0

for i in A[1:N]:
    s += max(score)
    j = score.index(max(score))
    ring.insert(j,A[i])
    score.insert(j,A[i])
    score[j-1] = A[i]

print(s)

Submission status: Runtime Error

Solution code:
num=int(input().rstrip())
m=input().rstrip().split()
main=[]
for i in m:
    main.append(int(i))
sample=sorted(main,reverse=True)
new=[]
pluss=[]
result=0
for i in range(num):
    if len(new)==0:
        new.append(sample[i])
    elif len(new)<=2:
        new.append(sample[i])
        result+=sample[i-1]
    else:
        plus=new[len(new)-1]
        point=len(new)
        for j in range(len(new)-1):
            if plus<new[j] and plus<new[j+1]:
                plus=min([new[j],new[j+1]])
                point=j+1
                if plus==pluss[len(pluss)-1]:
                    break
        result+=plus
        pluss.append(plus)
        if point==len(new):
            new.append(sample[i])
        else:
            new.insert(point,sample[i])
print(result)

Submission status: Runtime Error

Solution code:
n=int(input())
x=list(map,int,input().split())
print(sum(x)-min(x))

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int.input().split()))
a.sort(reverse=True)
ans=0
x=n
for i in range(1,n):
  ans+=a[i//2]
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = map(int,input().split())
print(sum(a)-min(a))

Submission status: Runtime Error

Solution code:
# coding: utf-8
N,K=map(int,input().split())
A=list(map(int,input().split()))
MOD=10**9+7

ans=1

if K==N:
    for i in range(N):
        ans*=A[i]
        ans%MOD
elif max(A)<0 and K%2==1:
    A.sort(reverse=True)
    for i in range(K):
        ans*=A[i]
        ans%MOD
else:
    Ap=[]
    Am=[]
    
    for i in range(N):
        if A[i]>0:
            Ap.append(A[i])
        else:
            Am.append(A[i])
        

    Ap.sort(reverse=True)
    Am.sort()
    
    if K%2==1:
        ans*=Ap.pop(0)
    
    if len(Ap)%2==1:
        Ap.append(1)
    
    P=[]
    
    for i in range(len(Ap)//2):
        P.append(Ap[i*2]*Ap[i*2+1])
    
    for i in range(len(Am)//2):
        P.append(Am[i*2]*Am[i*2+1])
    
    P.sort(reverse=True)
    
    for i in range(K//2):
        ans*=P[i]
        ans%MOD

print(ans%MOD)

Submission status: Runtime Error

Solution code:
a=int(input())
b=list(map(int,input().split()))
b.sort(reverse=True)
if a%2==0:
  print(sum(b[:a//2-1]*2-b[0]))
else:
  print(sum(b[:a//2+1]*2-b[0]-b[a//2])

Submission status: Runtime Error

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

    ans += A[i+1]*amari

    print(ans)

main()

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split())
ans = 0
A = sorted(A, reverse = True)
for i in range(N-2):
  ans += a[i]
ans += a[0]
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split())
ans = 0
A = sorted(A, reverse = True)
for i in range(N-2)
         ans += a[i]
ans += a[0]
print(ans)

Submission status: Runtime Error

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

A.sort(reversed=True)
ans = 0
for i in range(N-1):
  ans += A[(i+1)//2]

print(ans)

Submission status: Runtime Error

Solution code:
n=input()
a=[int(i) for i in raw_input().split()]
s=0
a.sort()
for i in range(1,n):
  s+=a[i]
 print s

Submission status: Runtime Error

Solution code:
n = int(input())
data = list(map(int, input().split())).sorted()

comfort = 0
circle = []

flag = false

for index, tmp in enumerate(data):
  circle.append(tmp)
  if flag:
    comfort += circle[index-1]
    
  flag = true
    

print(comfort)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
A.pop()
print(A.sum())

Submission status: Runtime Error

Solution code:
n=int(input())
a=input().split()
a=[0 for i in range(n+1)]
for i in range(1,n+1):
  a[i]=int(a[i-1])

a[0]=1000000000

m=floor((n-2)/2)

a=sorted(a,reverse=True)
comfort=a[1]
if(n==2):
  print(comfort)
else:
  if(n%2 ==0):
    for i in range(2,m+2):
      comfort+=(2*a[i])
  else:
    for i in range(2,m+2):
      comfort+=(2*a[i])

    comfort+=a[m+2]

Submission status: Runtime Error

Solution code:
from collections import deque

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

A.sort()
A.reverse()
Ad = deque()
Ad.append(A[0])
Ad.append(A[1])
sum = A[0]
for i in range(2, N):
    a = Ad.popleft()
    sum += min(a, Ad[-1])
    Ad.append(A[i])


print(sum)
from collections import deque

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

A.sort()
A.reverse()
Ad = deque()
Ad.append(A[0])
Ad.append(A[1])
sum = A[0]
for i in range(2, N):
    a = Ad.popleft()
    sum += min(a, Ad[-1])
    Ad.append(A[i])


print(sum)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
i = 2
ret = A[0]
while True:
  ret += A[(i - 2)//2 + 2]
  i += 1
  if i == N:
    break
print(ret)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
i = 2
ret = A[0]
while True:
  ret += A[(i - 2)//2 + 2]
  i += 1
  if i == len(N):
    break
  
print(ret)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
i = 2
ret = A[0]
while True:
  ret += A[(i - 2)//2 + 2]
  i += 1
  if i == len(N):
    break
  
return ret

Submission status: Runtime Error

Solution code:
import sys
#import time
#from collections import deque, Counter, defaultdict
#from fractions import gcd
import bisect
#import heapq
#import math
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
inf = 10**18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()

n = ri()
a = rl()
a=a+a
a.sort()

a.pop()
ans=0
for i in range(n-1):
    ans+=a[-i-1]
print(ans)

Submission status: Runtime Error

Solution code:
import sys
#import time
#from collections import deque, Counter, defaultdict
#from fractions import gcd
import bisect
#import heapq
#import math
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
inf = 10**18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))
import numpy as np

n = ri()
a = rl()
a=a+a
a.sort()

a.pop()
ans=0
for i in range(n-1):
    ans+=a[-i-1]
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
lst = []
for i in range(n):
  lst.append(int(input()))
lst.sort()
a = lst.sum() - lst[n-1]
print(a)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
A=reversed(sorted(A))
i=len(A)
i//=2
print(sum(A[-i-1:-1]))

Submission status: Runtime Error

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

if len(A) <= 2:
    print(sum(A))
else:
    ans = A[0] + 2*sum(A[1:math.floor(n/2)]) + (N%2)*A[math.floor[n/2]]
    print(ans)