input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
N = int(eval(input())) ans = '' if N == 0: print((0)) exit() while N != 0 and N != 1: if N > 0: if N%2: ans += '1' N = -(N//2) else: ans += '0' N = -N//2 else: if -N%2: ans += '1' N = -(N-1)//2 else: ans += '0' N = N//(-2) if N: ans += '1' print((ans[::-1]))
N = int(eval(input())) if N == 0: print((0)) exit() ans = '' while N != 0: if N % 2: ans += '1' N -= 1 else: ans += '0' N //= -2 print((ans[::-1]))
p03286
import sys def solve(): readline = sys.stdin.buffer.readline mod = 10 ** 9 + 7 n = int(readline()) if n == 0: print((0)) else: ans = [] while n: if n % -2 == 0: ans.append("0") n //= -2 else: ans.append("1") n //= -2 n += 1 ans.reverse() print(("".join(ans))) if __name__ == '__main__': solve()
import sys def solve(): input = sys.stdin.readline mod = 10 ** 9 + 7 n = int(input().rstrip('\n')) bl = [] while True: if n == 0: break else: bl.append(str(n & 1)) n //= 2 n *= -1 bl.reverse() print(("".join(bl) if len(bl) != 0 else 0)) if __name__ == '__main__': solve()
p03286
N = int(eval(input())) if N == 0: print((0)) exit() ans = '' while N != 0: if N%2: N -= 1 ans += '1' else: ans += '0' N //= -2 print((ans[::-1]))
N = int(eval(input())) if N == 0: print((0)) exit() ans = '' while N: if N%2: N -= 1 ans += '1' else: ans += '0' N //= -2 print((ans[::-1]))
p03286
# -*- coding: utf-8 -*- from bisect import bisect_left def inpl(): return list(map(int, input().split())) N = int(eval(input())) A = [] B = [] for i in range(2**16): A.append(int(("0{}"*16).format(*list(bin(i)[2:].zfill(16))), 2)) for i in range(2**16): B.append(-int(("{}0"*16).format(*list(bin(i)[2:].zfill(16))), 2)) for j, b in enumerate(B): i = bisect_left(A, N-b) if i == len(A): continue a = A[i] if a+b == N: ans = [""]*32 ans[::2] = list(bin(j)[2:].zfill(16)) ans[1::2] = list(bin(i)[2:].zfill(16)) print((int("".join(ans)))) break
from bisect import bisect_left def inpl(): return list(map(int, input().split())) N = int(eval(input())) U = [0]*32 L = [0]*32 U[0] = 1 for i in range(1, 32): if i%2: U[i] = U[i-1]*1 L[i] = L[i-1] + 2**i else: U[i] = U[i-1] + 2**i L[i] = L[i-1] * 1 searched = [0]*32 while N: if N < 0: d = bisect_left(L, -N) assert searched[d] != 1 searched[d] = 1 N += 2**d else: d = bisect_left(U, N) assert searched[d] != 1 searched[d] = 1 N -= 2**d print(("0"*(sum(searched) == 0) + "".join(map(str, searched)).rstrip("0")[::-1]))
p03286
a=int(eval(input()));s='' while a!=0:s+=str(a%2);a=-(a//2) print((int(s[::-1]) if s != '' else 0))
a=int(eval(input()));s='' while a!=0:s=str(a%2)+s;a=-(a//2) print((s if s != '' else 0))
p03286
from collections import deque S = deque() def main(): N = int(eval(input())) while N: if (abs(N % -2) == 1): S.appendleft('1') N -= 1 else: S.appendleft('0') N //= -2 if not S: S.append('0') print((''.join(list(S)))) return main()
def main(): N = int(eval(input())) S = "" if N == 0: print((0)) else: while N: if N % 2 != 0: S = '1' + S N -= 1 else: S = '0' + S N //= -2 print(S) return main()
p03286
def main(): N = int(eval(input())) S = '' if N == 0: print((0)) else: while N: if N % 2 != 0: S = '1' + S N -= 1 else: S = '0' + S N //= -2 print(S) return main()
def main(): N = int(eval(input())) S = "" if N == 0: print("0") else: while N: if N % 2: N -= 1 S = "1" + S else: S = "0" + S N //= -2 print(S) return main()
p03286
n=int(eval(input())) def isdig(n): if n>=0: digit=0 while n>2**digit: n-=2**digit digit+=2 else: digit=1 while n<-2**digit: n+=2**digit digit+=2 return digit x=["0"]*(isdig(n)+1) while n!=0: digit=isdig(n) if digit%2==0: n-=2**digit else: n+=2**digit x[digit]="1" x.reverse() print(("".join(x)))
n=eval(input()) s=[] n=int(n) i=0 while n!=0: if n%2**(i+1)==0: s.append("0") else: s.append("1") n-=(-2)**i i+=1 s.reverse() if s==[]: s=["0"] print(("".join(s)))
p03286
# abc105c N = int(eval(input())) if N==0: print((0)) exit() a = "" b = -2 while N!=1: if N%b==0: a += "0" N = N//b else: a += "1" if N>0: N = N//b + 1 else: N = N//b + 1 if N==1: a += "1" print((a[::-1]))
import sys N = int(eval(input())) if N==0: print((0)) sys.exit() ans = "" b = -2 while N!=1: if N%b==0: ans += "0" N = N/b else: ans += "1" N = (N-1)/b ans += "1" print((ans[::-1]))
p03286
N = int(eval(input())) ans = "" while N != 0: if(N%2 == 0): ans += "0" else: ans += "1" N -= 1 N //= -2 if(ans == ""): ans = "0" print((ans[::-1]))
N = int(eval(input())) tmp = '' while N: if(N%2 == 0): tmp += '0' else: tmp += '1' N -= 1 N //= -2 if(tmp == ''): tmp = '0' print((tmp[::-1]))
p03286
def main(): n = int(eval(input())) ans = "" while n!=0: rem = n%(-2) n= n//(-2) if rem<0: rem = rem+2 n = n+1 ans = ans + chr(rem + ord('0')) if ans =="": print((0)) exit() print((ans[::-1])) if __name__ =="__main__": main()
# source wikipedia negabinary def main(): n = int(eval(input())) ans = "" while n!=0: rem = n%(-2) n= n//(-2) if rem<0: rem = rem+2 n = n+1 ans = ans + chr(rem + ord('0')) if ans =="": print((0)) exit() print((ans[::-1])) if __name__ =="__main__": main()
p03286
#import sys #input = sys.stdin.readline #template input import bisect def IT(): return int(eval(input())) def IL(): return [int(_) for _ in input().split()] def SL(): return [int(_) for _ in input().split()] def ILS(n): return [int(eval(input())) for _ in range(n)] def SLS(n): return [eval(input()) for _ in range(n)] def ILSS(n): return [[int(_) for _ in input().split()] for j in range(n)] #template technique def bit_full_search(ss): n = len(ss) for i in range(1 << n): s = "" for j in range(n + 1): if ((1 & i >> j) == 1): s += ss[j] print(s) def bit_full_search2(A): #https://blog.rossywhite.com/2018/08/06/bit-search/ value = [] for i in range(1 << len(A)): output = [] for j in range(len(A)): if ((i >> j) & 1) == 1: #output.append(A[j]) output.append(A[j]) value.append([format(i, 'b').zfill(16), sum(output)]) return value """ここからメインコード""" def main(): n = IT() #それぞれ半分全列挙 list1 = [(-2) ** i for i in range(16)] list2 = [(-2) ** (i + 16) for i in range(16)] list1 = bit_full_search2(list1) list1.sort(key=lambda x:x[1]) list1_bin = [list1[i][0] for i in range(len(list1))] list1_val = [list1[i][1] for i in range(len(list1))] list2 = bit_full_search2(list2) list2.sort(key=lambda x:x[1]) list2_bin = [list2[i][0] for i in range(len(list2))] list2_val = [list2[i][1] for i in range(len(list2))] ans = 0 for i in range(len(list1_val)): j = bisect.bisect_left(list2_val, n - list1_val[i]) if j < len(list2_val): if list1_val[i] + list2_val[j] == n: ans = list2_bin[j] +list1_bin[i] break print((int(ans))) main()
#import sys #input = sys.stdin.readline #template input import bisect def IT(): return int(eval(input())) def IL(): return [int(_) for _ in input().split()] def SL(): return [int(_) for _ in input().split()] def ILS(n): return [int(eval(input())) for _ in range(n)] def SLS(n): return [eval(input()) for _ in range(n)] def ILSS(n): return [[int(_) for _ in input().split()] for j in range(n)] """ここからメインコード""" def main(): n = IT() ans = "" for i in range(32): if n % (2 ** (i+1)) != 0: n -= n % (2 ** (i+1)) * (-1) ** (i % 2) ans += "1" else: ans += "0" print((int(ans[::-1]))) main()
p03286
N=int(eval(input())) if(N==0): print((0)) exit() res='' while(N!=0): r=N%2 if(r<0): r+=2 res+=str(r) N=(N-r)//(-2) print((res[::-1]))
N=int(eval(input())) s='' if(N==0): print((0)) exit() while(N!=0): m=N%2 s+=str(m) N=(N-m)//-2 print((s[::-1]))
p03286
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy sys.setrecursionlimit(10**7) inf=10**20 mod=10**9+7 dd=[(-1,0),(0,1),(1,0),(0,-1)] ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS(): return sys.stdin.readline().split() def S(): return eval(input()) def main(): n=I() if n==0: return 0 ans='' while n!=0: if n%2==0: ans='0'+ans else: ans='1'+ans n//=2 n=-n return ans # main() print((main()))
# https://atcoder.jp/contests/arc026/tasks/arc026_2 import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy sys.setrecursionlimit(10**7) inf=10**20 mod=10**9+7 dd=[(-1,0),(0,1),(1,0),(0,-1)] ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS(): return sys.stdin.readline().split() def S(): return eval(input()) def main(): n=I() ans='' if n==0: return 0 while True: if abs(n)==0: return ans if n%2==0: ans='0'+ans else: ans='1'+ans n//=2 n*=(-1) # main() print((main()))
p03286
# C - Base -2 Number n = int(eval(input())) s = "" while n!=0: s = str(n%2)+s n = -(n//2) print((0 if s=="" else s))
n=int(eval(input())) s='' while n!=0: s=str(n%2)+s n=-(n//2) if s!='': print(s) else: print((0))
p03286
n = int(eval(input())) ans = [] if n == 0: print('0') else: while n != 0: if n % -2 == -1: n = n // -2 + 1 ans.append('1') else: n = n // -2 ans.append('0') ans.reverse() print((''.join(ans)))
n = int(eval(input())) binary = [] if n == 0: print((0)) exit() while n != 0: if n % -2 != 0: binary.append('1') n //= -2 n += 1 else: binary.append('0') n //= -2 binary.reverse() print((''.join(binary)))
p03286
N = int(eval(input())) if N == 0: print((0)) exit() L = [] M = [] def bit_range(bit_length,maximum=0,minimal=0): for i in range(bit_length-1,-1,-1): if i % 2 == 0: maximum += pow(2,i) else: minimal += pow(-2,i) L.append(minimal) M.append(maximum) for i in range(1,41): bit_range(i) identify = [] for i in range(40): if i == 0: ap_list = [L[0],M[0]] elif i % 2 == 1: ap_list = [L[i],L[i-1]] elif i % 2 == 0: ap_list = [M[i-1],M[i]] identify.append(ap_list) bit = [0]*40 for i in range(39,-1,-1): left = identify[i][0] right = identify[i][1] if i % 2 == 0 and left < N <= right: bit[39-i] = 1 N -= pow(-2,i) elif i % 2 == 1 and left <= N < right: bit[39-i] = 1 N -= pow(-2,i) ans = "".join(str(e) for e in bit).lstrip("0") print(ans)
N = int(eval(input())) if N == 0: print((0)) exit() L = [] M = [] for length in range(1,41): if length % 2 == 0: l = -2 * pow(4,length//2) // 3 m = (pow(4,length//2) - 1) // 3 elif length % 2 == 1: l = -2 * (pow(4,(length-1)//2) - 1) // 3 m = (pow(4,(length+1)//2) - 1) // 3 L.append(l) M.append(m) identify = [] for i in range(40): if i == 0: ap_list = [L[0],M[0]] elif i % 2 == 1: ap_list = [L[i],L[i-1]] elif i % 2 == 0: ap_list = [M[i-1],M[i]] identify.append(ap_list) bit = [0]*40 for i in range(39,-1,-1): left = identify[i][0] right = identify[i][1] if i % 2 == 0 and left < N <= right: bit[39-i] = 1 N -= pow(-2,i) elif i % 2 == 1 and left <= N < right: bit[39-i] = 1 N -= pow(-2,i) ans = "".join(str(e) for e in bit).lstrip("0") print(ans)
p03286
N = int(eval(input())) if N == 0: print((0)) exit() L = [] M = [] for length in range(1,41): if length % 2 == 0: l = -2 * pow(4,length//2) // 3 m = (pow(4,length//2) - 1) // 3 elif length % 2 == 1: l = -2 * (pow(4,(length-1)//2) - 1) // 3 m = (pow(4,(length+1)//2) - 1) // 3 L.append(l) M.append(m) identify = [] for i in range(40): if i == 0: ap_list = [L[0],M[0]] elif i % 2 == 1: ap_list = [L[i],L[i-1]] elif i % 2 == 0: ap_list = [M[i-1],M[i]] identify.append(ap_list) bit = [0]*40 for i in range(39,-1,-1): left = identify[i][0] right = identify[i][1] if i % 2 == 0 and left < N <= right: bit[39-i] = 1 N -= pow(-2,i) elif i % 2 == 1 and left <= N < right: bit[39-i] = 1 N -= pow(-2,i) ans = "".join(str(e) for e in bit).lstrip("0") print(ans)
N = int(eval(input())) ans = "" if N == 0: ans = "0" while N != 0: if N % 2 == 1: ans = "1" + ans else: ans = "0" + ans N //= 2 N *= -1 print(ans)
p03286
#!/usr/bin/env python n = int(eval(input())) if n == 0: print((0)) exit() ans = '' while abs(n) > 0: r = n%2 ans += str(r) n //= 2 n = -n if abs(n)%2 == 1: print(ans) else: print((ans[::-1]))
#!/usr/bin/env python n = int(eval(input())) if n == 0: print((0)) exit() ans = '' while abs(n) > 0: r = n%2 ans = str(r) + ans if r == 1: n -= 1 n //= (-2) print(ans)
p03286
N = int(eval(input())) A = list(map(int,input().split())) A.sort(reverse=True) # print(A) ans = A[0] for i in range(2,N): ans += A[i//2] print(ans)
def main(): import sys input = sys.stdin.readline N = int(eval(input())) A = list(map(int,input().split())) A.sort(reverse=True) ans = A[0] for i in range(2,N): ans += A[i//2] print(ans) if __name__ == '__main__': main()
p02615
n = int(eval(input())) a = list(map(int, input().split())) a.sort() if n%2: print((2*sum(a[n//2:])-a[-1]-a[n//2])) else: print((2*sum(a[n//2:])-a[-1]))
import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n = int(eval(input())) a = sorted(list(map(int, input().split()))) print((2*sum(a[n//2:])-a[-1]-(a[n//2] if n%2 else 0)))
p02615
import sys input = sys.stdin.readline from collections import * N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) l = [A[0]] for Ai in A[1:]: l.append(Ai) l.append(Ai) print((sum(l[:N-1])))
import sys input = sys.stdin.readline N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) l = [A[0]] for i in range(1, N): l.append(A[i]) l.append(A[i]) print((sum(l[:N-1])))
p02615
import itertools import sys input = sys.stdin.readline N = int(eval(input())) A = list(map(int, input().split())) ans = 0 for L in itertools.permutations(A): temp = 0 for i in range(1, N): temp += min(L[0], L[i-1]) ans = max(ans, temp) print(ans)
import itertools import sys input = sys.stdin.readline N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) ans = A[0] for i in range(2, N): ans += A[i//2] print(ans)
p02615
import sys def input(): return sys.stdin.readline().strip() def mapint(): return list(map(int, input().split())) sys.setrecursionlimit(10**9) N = int(eval(input())) As = list(mapint()) As.sort(reverse=True) from heapq import heappush, heappop first = (As.pop(0)) hq = [] if N<2: print((0)) else: ans = 0 second = As.pop(0) ans += first heappush(hq, (-second, -first)) heappush(hq, (-second, -first)) for a in As: s, f = heappop(hq) ans -= s heappush(hq, (-a, s)) heappush(hq, (-a, f)) print(ans)
import sys def input(): return sys.stdin.readline().strip() def mapint(): return list(map(int, input().split())) sys.setrecursionlimit(10**9) N = int(eval(input())) As = list(mapint()) conf = 0 from heapq import heapify, heappop, heappush As.sort(reverse=True) m = As.pop(0) s = As.pop(0) conf += m Q = [] heappush(Q, -s) heappush(Q, -s) for a in As: s = heappop(Q) conf -= s heappush(Q, -a) heappush(Q, -a) print(conf)
p02615
# -*- coding: utf-8 -*- N = int(eval(input())) A = list(map(int,input().split())) A.sort(reverse=True) #print(A) ans = 0 list = [] for i in range(N): if ( i==0 ) : list.append(A[0]) else: max_index = list.index(max(list)) ans += list[max_index] list[max_index] = A[i] list.insert(max_index, A[i]) #print(list,ans) print(ans)
# -*- coding: utf-8 -*- N = int(eval(input())) A = list(map(int,input().split())) A.sort(reverse=True) #print(A) """ ans = 0 list = [] for i in range(N): if ( i==0 ) : list.append(A[0]) else: max_index = list.index(max(list)) ans += list[max_index] list[max_index] = A[i] list.insert(max_index, A[i]) #print(list,ans) """ ans = 0 for i in range(N): if ( i==0 ) : ans += 0 else: ans += A[i//2] print(ans)
p02615
N, *A = list(map(int, open(0).read().split())) A.sort(reverse=True) ans = 0 for i in range(N - 1): if i == 0: ans += A[i] else: ans += A[-(-i // 2)] print(ans)
N = int(eval(input())) X = list(map(int, input().split())) X.sort(reverse=True) ans = 0 cnt = 0 j = 0 for i in range(N): if i == 0: continue elif i == 1: ans += X[j] j += 1 else: ans += X[j] cnt += 1 if cnt == 2: j += 1 cnt = 0 print(ans)
p02615
#!/usr/bin/env python3 # encoding:utf-8 import copy import random import bisect #bisect_left これで二部探索の大小検索が行える import fractions #最小公倍数などはこっち import math import sys import collections from decimal import Decimal # 10進数で考慮できる mod = 10**9+7 sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000 d = collections.deque() def LI(): return list(map(int, sys.stdin.readline().split())) N = int(eval(input())) A = LI() A.sort(reverse=1) ans = 0 ans += A[0] tmp = 1 for i in range(1, N - 1): ans += A[tmp] if i % 2 == 0: tmp += 1 print(ans)
N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) ans = 0 ans += A[0] tmp = 1 for i in range(1, N - 1): ans += A[tmp] if i % 2 == 0: tmp += 1 print(ans)
p02615
import heapq class ErasableHeapq: def __init__(self): self.p = list() self.q = list() self.len = 0 def insert(self, x): heapq.heappush(self.p, x) self.len += 1 return def erase(self, x): heapq.heappush(self.q, x) self.len -= 1 return def minimum(self): while self.q and self.p[0] == self.q[0]: heapq.heappop(self.p) heapq.heappop(self.q) return self.p[0] def __len__(self): return self.len def empty(self): return self.len == 0 def main(): N = int(eval(input())) A = list(map(int, input().split())) # 大きい順 A.sort() sA = A[::-1] ans = sA[0] eq = ErasableHeapq() eq.insert((-sA[1], -sA[0])) eq.insert((-sA[1], -sA[0])) for a in sA[2:]: best_pair = eq.minimum() eq.erase(best_pair) # print(a, best_pair) ans += -best_pair[0] eq.insert((-a, best_pair[0])) eq.insert((-a, best_pair[1])) print(ans) if __name__ == '__main__': main()
def main(): N = int(eval(input())) A = list(map(int, input().split())) # 大きい順 A.sort() A = A[::-1] ans = 0 for i in range(1, N): ans += A[i // 2] print(ans) if __name__ == '__main__': main()
p02615
n = int(eval(input())) A = [int(x) for x in input().split()] A.sort(reverse=True) line = [A[0]] comfort = 0 for a in A[1:]: diff_comfort = [min(line[(i + 1) % len(line)], line[i]) for i in range(len(line))] pos = min(list(range(len(line))), key=lambda i: diff_comfort[i]) comfort += diff_comfort[pos] line.insert(pos + 1 if pos < len(line) - 1 else 0, a) print(comfort)
n = int(eval(input())) A = [int(x) for x in input().split()] A.sort(reverse=True) print((A[0] + sum(A[1:(n - 2) // 2 + 1]) * 2 + (0 if n % 2 == 0 else A[(n - 1) // 2])))
p02615
N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=False) #N = 12 #A = [12,9,9,6,6,5,5,4,3,2,1,1] ret = 0 array = [] array.append(A.pop()) while len(A) > 0: #print('array', array) comfort = [0 for i in range(len(array))] for i in range(len(comfort)): comfort[i] = min(array[i-1], array[i]) #print('comfort', comfort) val = max(comfort) ret += val #print('ret', ret) array.insert(comfort.index(val), A.pop()) print(ret)
N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=False) ret = 0 c_pointer = 0 comfort = [] tmp = A.pop() comfort.append(tmp) while len(A) > 0: tmp = A.pop() c_pointer += 1 comfort.append(tmp) comfort.append(tmp) print((sum(comfort[:c_pointer])))
p02615
n = int(eval(input())) a = list(map(int, input().split())) a = sorted(a, reverse=True) comfort = a[0:2] sum = max(a[0], a[1]) for j in a[2:]: key = 0 for i in range(len(comfort) - 1): comfort_value = min(comfort[1], comfort[-1]) comfort_value_kari = min(comfort[i], comfort[i + 1]) if comfort_value < comfort_value_kari: comfort_value = comfort_value_kari key = i comfort.insert(key, j) sum += comfort_value print(sum)
import heapq n = int(eval(input())) arr = list(map(int, input().split())) arr = sorted(arr, reverse=True) ans = 0 q = [] heapq.heappush(q, -arr[0]) for i in range(1, n): tmp = heapq.heappop(q) tmp *= -1 ans += tmp heapq.heappush(q, -arr[i]) heapq.heappush(q, -arr[i]) print(ans)
p02615
import math n = int(eval(input())) f = list(map(int,input().split())) f.sort() ans = 0 for i in range(1,n): ans += f[n-math.floor(i/2)-1] print(ans)
n = int(eval(input())) f = list(map(int,input().split())) f.sort(reverse = True) ans = 0 if n%2 == 0: for i in range(n//2): ans += f[i] * 2 ans -= f[0] else: for i in range(n//2): ans += f[i] * 2 ans += f[n//2]-f[0] print(ans)
p02615
N=int(eval(input())) A=list(map(int,input().split())) A.sort(reverse=True) cnt=0 ans=[] ans.append(A[0]) ans.append(A[1]) cnt+=ans[0] for i in range(2,N): k=ans.index(A[int((i-1)/2)]) if k==len(ans)-1: kb=k-1 kn=0 else: kb=k-1 kn=k+1 if ans[kb]>=ans[kn]: cnt+=min(ans[k],ans[kb]) ans.insert(kb,A[i]) elif ans[kb]<ans[kn]: cnt+=min(ans[k],ans[kn]) ans.insert(kn,A[i]) print(cnt)
N=int(eval(input())) A=list(map(int,input().split())) A.sort() A.insert(0,0) ans=0 for i in range(1,N): ans+=A[N-int(i/2)] print(ans)
p02615
n,*l=list(map(int,open(0).read().split())) from heapq import * l.sort() hq=[-l.pop()] heapify(hq) a=0 for _ in range(n-1): m=heappop(hq) a-=m t=-l.pop() heappush(hq,t) heappush(hq,t) print(a)
n,*l=list(map(int,open(0).read().split())) from heapq import * l.sort() q=[-l.pop()] a=0 for _ in range(n-1): m=heappop(q) a-=m t=-l.pop() heappush(q,t) heappush(q,t) print(a)
p02615
n,*l=list(map(int,open(0).read().split())) from heapq import * l.sort() q=[-l.pop()] a=0 for _ in range(n-1): m=heappop(q) a-=m t=-l.pop() heappush(q,t) heappush(q,t) print(a)
n,*l=list(map(int,open(0).read().split()));print((sum(sorted(l*2)[-2:~n:-1])))
p02615
n,*l=list(map(int,open(0).read().split()));print((sum(sorted(l*2)[-2:~n:-1])))
n,*l=list(map(int,open(0).read().split())) l.sort(reverse=1) print((sum(l[i//2] for i in range(1,n))))
p02615
N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) ans = A[0] S = [A[0], A[1]] for i in range(2, len(A)): m = min(A[0], A[-1]) cnt = -1 for j in range(len(A)-1): if min(A[j], A[j+1]) > m: m = min(A[j], A[j+1]) cnt = j S = S[:cnt] + [A[i]] + S[cnt:] ans += m print(ans)
N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) ans = A[0] N -= 2 for i in range(1, N): if N >= 2: ans += A[i]*2 N -= 2 elif N == 1: ans += A[i] N -= 1 else: break print(ans)
p02615
n = int(eval(input())) a = sorted(list(map(int, input().split())), reverse=True) sum_com = a[0] comfort = [a[1], a[1]] for ai in a[2:]: sum_com += comfort[0] if comfort[0] == ai: comfort.append(ai) else: comfort.pop(0) comfort += [ai, ai] print(sum_com)
n = int(eval(input())) a = sorted(list(map(int, input().split())), reverse=True) sum_com = 0 distinct_a = sorted(list(set(a)), reverse=True) comfort = {i: 0 for i in distinct_a} comfort[a[0]] = 1 max_com = 0 for ai in a[1:]: sum_com += distinct_a[max_com] if distinct_a[max_com] == ai: comfort[distinct_a[max_com]] += 1 else: comfort[distinct_a[max_com]] -= 1 comfort[ai] += 2 if comfort[distinct_a[max_com]] == 0: max_com += 1 print(sum_com)
p02615
def nth_element(array, will): will -= 1 n = len(array) left = 0 right = n-1 while right > left: i = left-1 j = right v = array[right] while True: i += 1 j -= 1 while array[i]<v:i+=1 while array[j]>v:j-=1 if i>=j:break array[i],array[j] = array[j],array[i] array[i],array[right] = array[right],array[i] if i == will:break if i > will: right = i-1 else: left = i+1 return array # from random import randrange # for i in range(100): n = int(eval(input())) A = list(map(int,input().split())) # n = randrange(2,10) # A = [0]*n # for j in range(n): # A[j] = randrange(1,1000) k = 1 + (n-1)//2 nth_element(A,n-k+1) # B = sorted(A) ans = 0 M = 0 # m = B[-1] for i in range(-1,n-1): ans += A[n - (i+1)//2 - 1] M = max(M, A[n- (i+1)//2 - 1]) # ans -= B[n-(i+1)//2 - 1] ans -= M # ans += m # if ans != 0: # print(n,A) print(ans)
# 入力 n = int(eval(input())) A = list(map(int,input().split())) A.sort() ans = 0 can = 1 # lastの数が後何回足されうるか will = n-1 # 次に足されるであろう数のindex for i in range(n-1): ans += A[will] can -= 1 if can == 0: # 足されきったら一つ小さい数へ can = 2 # 最大値以外は2回足されうる will -= 1 print(ans)
p02615
n = int(eval(input())) a = list(map(int,input().split())) sa = sorted(a,reverse=True) ans = 0 ls = [sa[0]] for v in sa[1:]: i = ls.index(max(ls)) ans+= ls[i] new_min = min(ls[i], v) ls = ls[:i] + [new_min, new_min] + ls[i+1:] #print(ls) print(ans)
n = int(eval(input())) a = list(map(int,input().split())) sa = sorted(a,reverse=True) ans = sa[0] i = 2 for v in sa[1:]: if i >= n: break #print("i:{} ans:{}".format(i,ans)) ans += v i += 1 if i >= n: break #print("i:{} ans:{}".format(i,ans)) ans += v i += 1 print(ans)
p02615
import itertools N = int(eval(input())) A = list(map(int, input().split())) conf_max = 0 for p in itertools.permutations(A): conf = 0 for i in range(1, N): conf += min(p[i-1], p[0]) if conf_max < conf: conf_max = conf print(conf_max)
N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) conf_max = 0 for i in range(1, N): conf_max += A[i//2] print(conf_max)
p02615
import heapq n=int(eval(input())) arr=list(map(int,input().split())) arr=sorted(arr,reverse=True) ans=0 q=[] heapq.heappush(q,-arr[0]) for i in range(1,n): tmp=heapq.heappop(q) tmp*=-1 ans+=tmp heapq.heappush(q,-arr[i]) heapq.heappush(q,-arr[i]) print(ans)
n=int(eval(input())) arr=list(map(int,input().split())) arr=sorted(arr,reverse=True) ans=0 for i in range(1,n): ans+=arr[i//2] print(ans)
p02615
# coding: utf-8 from collections import deque def solve(*args: str) -> str: n = int(args[0]) A = sorted(map(int, args[1].split()), reverse=True) Q = deque() ret = 0 for i in range(-(-n//2)): if 0 < i: ret += min(Q[-1], Q[0]) if i % 2: Q.appendleft(A[i]) else: Q.append(A[i]) for i in range(n-len(Q)): ret += min(Q[i], Q[(i+1) % len(Q)]) return str(ret) if __name__ == "__main__": print((solve(*(open(0).read().splitlines()))))
# coding: utf-8 def solve(*args: str) -> str: n = int(args[0]) A = sorted(map(int, args[1].split()), reverse=True) return str(sum([A[i//2] for i in range(1, n)])) if __name__ == "__main__": print((solve(*(open(0).read().splitlines()))))
p02615
n = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) cnt = n-1 i = 0 ans = 0 while cnt: if i == 0: cnt -= 1 ans += A[i] i += 1 else: if cnt < 2: ans += A[i]*cnt cnt = 0 else: ans += A[i]*2 cnt -= 2 i += 1 print(ans)
n = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) ans = 0 cnt = 0 for i, a in enumerate(A): if i == 0: ans += a cnt += 1 else: if n-1 - cnt >= 2: ans += a*2 cnt += 2 else: ans += a cnt += 1 if cnt == n-1: break print(ans)
p02615
#D - Chat in a Circle N = int(eval(input())) A = list(map(int,input().split())) #ソート #B = [] arr = A.copy() left = 0 right = N -1 #B = sorted(A,reverse = True) def quicksort(arr,left,right): index = partition(arr,left,right) if left < index- 1 : quicksort(arr,left,index - 1) if index < right: quicksort(arr,index,right) def partition(arr,left,right): pivot = arr[int((left + right) / 2)] while left <= right: while arr[left] > pivot: left += 1 while arr[right] < pivot: right -= 1 if left <= right: arr[left],arr[right] = arr[right],arr[left] left += 1 right -= 1 return left quicksort(arr,left,right) Chat = [] Friend_min = 0 Friend_min_max = 0 Friend_min_max_wa = 0 max_no = 0 i =0 Chat.append(arr[0]) for i in range(N): for j in range(i): if (Chat[j] > Chat[j+1]): Friend_min = Chat[j+1] else: Friend_min = Chat[j] if Friend_min >= Friend_min_max: Friend_min_max = Friend_min max_no = j + 1 Friend_min = 0 Chat.insert(max_no,arr[i]) Friend_min_max_wa = Friend_min_max_wa + Friend_min_max Friend_min_max = 0 max_no = 0 # print(Chat) # 出力 print(Friend_min_max_wa)
#D - Chat in a Circle N = int(eval(input())) A = list(map(int,input().split())) #ソート arr = [] arr = sorted(A,reverse = True) Friend_min_max_wa = 0 i =0 j = 0 for i in range(N -1): if i % 2 != 0 : j = j + 1 Friend_min_max_wa = Friend_min_max_wa + arr[j] # 出力 print(Friend_min_max_wa)
p02615
from heapq import heapify, heappop, heappush N = int(eval(input())) A = list(map(int,input().split())) A.sort(reverse = True) #print(A) ans = A[0] Q = [(-A[1],-A[0]),(-A[1],-A[0])] #一つ目が大きいほうへ入れる heapify(Q) for i in range(2,N): nxt = A[i] s,t = heappop(Q) s *= -1; t*= -1 #正に戻す #print(s,ans) ans += s heappush(Q,(-nxt,-s)) heappush(Q,(-nxt,-t)) #print(Q) print(ans)
N = int(eval(input())) A = list(map(int,input().split())) A.sort(reverse=True) B = [A[0]] for i in range(N-1): B.append(A[i+1]) B.append(A[i+1]) #print(B) ans = 0 for i in range(N-1): ans += B[i] print(ans)
p02615
from collections import deque n = int(eval(input())) a = sorted(map(int, input().split()), reverse=True) ans, cnt = 0, deque([a[0]]) for i in a[1:]: ans += cnt.popleft() cnt.extend([i,i]) print(ans)
n = int(eval(input())) a = sorted(map(int, input().split()), reverse=True) ans, que, cnt = 0, [a[0]], 0 for i in a[1:]: ans += que[cnt] que += [i, i] cnt += 1 print(ans)
p02615
import bisect import statistics n = int(eval(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 += statistics.median(incl) else: # smaller of middle two score += incl[ int(len(incl)/2)-1 ] print(score)
import bisect import statistics n = int(eval(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)
p02615
n = int(eval(input())) lst = list(map(int, input().split())) lst = sorted(lst, reverse=True) class Queue: def __init__(self): self.queue_lst = [] self.first_flag = True self.count = 0 def add(self, friend_val): if self.first_flag: self.queue_lst.append(friend_val) self.first_flag = False else: self.queue_lst.append(friend_val) self.queue_lst.append(friend_val) self.count += self.queue_lst.pop(0) queue = Queue() for i in lst: queue.add(i) print((queue.count))
n = int(eval(input())) lst = list(map(int, input().split())) lst = sorted(lst, reverse=True) class Queue: def __init__(self): self.queue_lst = [] self.first_flag = True self.count = 0 self.index = 0 def add(self, friend_val): if self.first_flag: self.queue_lst.append(friend_val) self.first_flag = False else: self.queue_lst.append(friend_val) self.queue_lst.append(friend_val) self.count += self.queue_lst[self.index] self.index += 1 queue = Queue() for i in lst: queue.add(i) print((queue.count))
p02615
N = int(eval(input())) a = list(map(int, input().split())) a.sort(reverse = True) p=0 ppl = [a[0]]*2 best_index = 1 best_point = a[0] for i in a[1:]: p += best_point ppl = ppl[:best_index]+[i]+ppl[best_index:] #print(ppl) point_list = [min(ppl[i],ppl[i+1]) for i in range(0,len(ppl)-1)] #print(point_list) best_point = max(point_list) for j,q in enumerate(point_list): if q == best_point: best_index = j+1 continue print(p)
N = int(eval(input())) a = list(map(int, input().split())) a.sort(reverse = True) p = a[0] for i in range(N-2): #-1は上記a[0]分、もう-1は最初の要素挿入は加点されないため。 p += a[(i//2)+1] print(p)
p02615
import sys, re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from bisect import bisect, bisect_left from heapq import heappush, heappop from functools import reduce def input(): return sys.stdin.readline().strip() def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) def ZIP(n): return list(zip(*(MAP() for _ in range(n)))) sys.setrecursionlimit(10 ** 9) INF = float('inf') mod = 10 ** 9 + 7 N = INT() A = LIST() A.sort(reverse=True) ans = A[0] cnt = 1 for x in A[1:]: if cnt == N-1: break if cnt == N-2: ans += x cnt += 1 else: ans += 2*x cnt += 2 print(ans)
import sys, re from collections import deque, defaultdict, Counter from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement from operator import itemgetter, mul from copy import deepcopy from string import ascii_lowercase, ascii_uppercase, digits from bisect import bisect, bisect_left from heapq import heappush, heappop from functools import reduce def input(): return sys.stdin.readline().strip() def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) def ZIP(n): return list(zip(*(MAP() for _ in range(n)))) sys.setrecursionlimit(10 ** 9) INF = float('inf') mod = 10 ** 9 + 7 N = INT() A = LIST() A.sort(reverse=True) ans = 0 for i in range(N-1): ans += A[0--i//2] print(ans)
p02615
#!/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()
p02615
N = int(eval(input())) A = [] a, *an = list(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[0] num = A.pop() Node = Node[1:] Node.append(num) Node.append(num) #print(Node) print(score)
N = int(eval(input())) A = [] a, *an = list(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)
p02615
n=int(eval(input())) a=list(map(int,input().split())) a.sort(reverse=True) d=[a[0]] ans=0 for i in range(1,n): tmp=max(d) ans=ans+tmp d.remove(tmp) d.append(a[i]) d.append(a[i]) print(ans)
n=int(eval(input())) a=list(map(int,input().split())) a.sort(reverse=True) ans=0 for i in range(1,n): ans=ans+a[i//2] print(ans)
p02615
from collections import deque N = int(eval(input())) K = list(sorted(map(int, input().split()), reverse=True)) max1 = K[0] max2 = K[1] if N == 2: print(max1) else: max3 = K[2] count = max1 + max2 cands = [(max1, max2), (max2, max3)] for k in K[3:]: max_cand = (0, 0) max_i = -1 for i, cand in enumerate(cands): if min(cand) > min(max_cand): max_cand = cand max_i = i hi, lo = max_cand count += lo cands.pop(max_i) cands.append((hi, k)) cands.append((lo, k)) print(count)
from collections import deque N = int(eval(input())) K = list(sorted(map(int, input().split()), reverse=True)) max1 = K[0] max2 = K[1] if N == 2: print(max1) else: max3 = K[2] count = max1 + max2 cands = deque([max2, max3, max3]) for k in K[3:]: cand = cands.popleft() count += cand cands.append(k) cands.append(k) print(count)
p02615
N = int(eval(input())) A = sorted([int(x) for x in input().split()], reverse=True) ans = 0 for i in range(N): if i == 0: B = [A[0]] continue index, mx = 0, 0 for j in range(len(B)): tmp = min(B[j-1], B[j]) if mx < tmp: mx = tmp index = j B.insert(index, A[i]) ans += mx print(ans)
N = int(eval(input())) A = sorted([int(x) for x in input().split()], reverse=True) ans = 0 for i in range(N): if i == 0: continue else: ans += A[i//2] print(ans)
p02615
N=int(eval(input())) A=list(map(int,input().split())) B=sorted(A,reverse=True) ans=B[0] C=[B[0],B[1]] ans=B[0] for i in range(2,N): jmax=1 scoremax=min(C[0],C[1]) for j in range(1,len(C)-1): score = min(C[j],C[j+1]) if scoremax < score: scoremax = score jmax=j ans += scoremax C=C[0:jmax]+[B[i]]+C[jmax:] # print(C) print(ans)
N=int(eval(input())) A=list(map(int,input().split())) B=sorted(A,reverse=True) ans=B[0] sheets=[B[0]] ans=0 for i in range(1,N): ans += sheets[i-1] sheets.append(B[i]) sheets.append(B[i]) print(ans)
p02615
import collections n=int(eval(input())) A=list(map(int,input().split())) A.sort(reverse=True) q=[] for i in range(n): if i==0: q.append(A[i]) else: q+=[A[i]]*2 print((sum(q[:n-1])))
n=int(eval(input())) A=list(map(int,input().split())) A.sort(reverse=True) q=[] for i in range(n): if i==0: q.append(A[i]) else: q+=[A[i]]*2 print((sum(q[:n-1])))
p02615
N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse = True) Sum = 0 for i in range(len(A)): if i == 0: pass elif i == 1: Sum += A.pop(0) else: if i % 2 == 0: Sum += A[0] else: Sum += A.pop(0) print(Sum)
N = int(eval(input())) A = list(map(int, input().split())) A.sort() Sum = 0 for i in range(len(A)): if i == 0: pass elif i == 1: Sum += A.pop() else: if i % 2 == 0: temp = A.pop() Sum += temp else: Sum += temp print(Sum)
p02615
import sys from collections import deque input = sys.stdin.readline N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) deq = deque([A[0]]) ans = 0 for x in A[1:]: ans += deq.popleft() deq.append(x) deq.append(x) print(ans)
import sys input = sys.stdin.readline N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) ans = 0 for i in range(1, N): ans += A[i // 2] print(ans)
p02615
n = int(eval(input())) a = list(map(int,input().split())) a.sort(reverse=True) ans = a[0] n -= 2 now = 1 while n > 0: if n >= 2: ans += a[now]*2 now += 1 n -= 2 elif n == 1: ans += a[now] n -= 1 now += 1 print(ans)
N = int(eval(input())) A = sorted(list(map(int,input().split())),reverse=True) ans = 0 for i in range(1,N): ans += A[i//2] print(ans)
p02615
n=int(eval(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)
n=int(eval(input())) a=sorted(list(map(int,input().split())),reverse=1) b=0 for i in range(n-1): b+=a[(i+1)//2] print(b)
p02615
n = int(eval(input())) a = list(map(int, input().split())) a.sort() cnt = a.pop() tmp = a.pop() l = 0 for i in range(n - 2): if l == 2: l = 0 tmp = a.pop() cnt += tmp l += 1 print(cnt)
n = int(eval(input())) a = sorted(list(map(int, input().split())))[::-1] print((a[0] + sum(a[1:n // 2]) * 2 + sum(a[n // 2:n // 2 + n % 2])))
p02615
from collections import deque N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) a = deque(A) 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)
N = int(eval(input())) a = list(map(int, input().split())) a.sort() arrival = 1 ans = 0 while arrival < N: score = a.pop() if arrival == 1: ans += score arrival += 1 else: ans += score*min(2, N - arrival) arrival += 2 print(ans)
p02615
N = int(eval(input())) A = list(map(int,input().split())) A.sort(reverse = True) circle = [A[0]] ans = 0 for i in range(1,N): max = 0 l = len(circle) for j in range(l): if max < min(circle[j],circle[(j+1)%l]): ins = j max = min(circle[j],circle[(j+1)%l]) circle.insert(ins,A[i]) ans += max print(ans)
N = int(eval(input())) A = list(map(int,input().split())) A.sort(reverse = True) ans2 = A[0] cnt = 0 j = 1 for i in range(1,N-1): if cnt == 2: j += 1 cnt = 0 ans2 += A[j] cnt += 1 print(ans2)
p02615
# -*- coding: utf-8 -*- N = int(eval(input())) A = list(map(int, input().split())) a_sorted = sorted(A, key=lambda x: -x) circle = [a_sorted[0]] ans = 0 for a in a_sorted: tmp = 0 tmp_idx = -1 for i in range(len(circle) - 1): cand = min(circle[i], circle[i + 1]) if cand > tmp: tmp = cand tmp_idx = i ans += tmp circle.insert(tmp_idx + 1, a) print(ans)
# -*- coding: utf-8 -*- N = int(eval(input())) A = list(map(int, input().split())) a_sorted = sorted(A, key=lambda x: -x) circle = [a_sorted[0]] ans = 0 for idx, a in enumerate(a_sorted[1:]): ans += circle[idx] circle.extend([a, a]) print(ans)
p02615
n = int(eval(input())) friendly = [int(i) for i in input().split()] friendly.sort(reverse=True) place = [] place.append(friendly[0]) place=place*2 ans=0 for i in friendly[1:]: comfortable=[min(place[i], place[i+1]) for i in range(len(place)-1)] ans += max(comfortable) place.insert(comfortable.index(max(comfortable))+1, i) print(ans)
n = int(eval(input())) friendly = list(map(int, input().split())) friendly.sort(reverse=True) if len(friendly)<=2: print((sum(friendly[0:n-1]))) else: ans = friendly[0]+sum(friendly[1:(n+1)//2-1])*2+friendly[(n+1)//2-1]*((n+1)%2+1) print(ans)
p02615
N = int(eval(input())) alist = list(map(int, input().split())) alist.sort(reverse=True) sumscore=0 posit = list() posit.append(alist[0]) for n in range(1,N): sumscore = sumscore + posit[0] posit = posit[1:] posit.append(alist[n]) posit.append(alist[n]) print(sumscore)
N = int(eval(input())) alist = list(map(int, input().split())) alist.sort(reverse=True) sumscore=alist[0] for n in range(1,int(N/2)): sumscore = sumscore + alist[n]*2 if N%2==1: sumscore += alist[n+1] print(sumscore)
p02615
eval(input()) l = sorted(map(int, input().split(' '))) answer = 0 spots = [l.pop()] while l: n = l.pop() answer += spots[0] spots = spots[1:] + [n] * 2 print(answer)
eval(input()) l = sorted(map(int, input().split(' '))) answer = 0 spots = [l.pop()] x = len(l) for i in range(x): n = l.pop() answer += spots[i] spots.append(n) spots.append(n) print(answer)
p02615
def MergeSort(num_list): n = len(num_list) if len(num_list) == 1: return num_list #1. 整列されていないリストを2つのサブリストに分割する mid = int(round(n/2)) left = num_list[:mid] right = num_list[mid:] #2. 分割したリスト(サブリスト)を整列する。サブリストもマージソートを使って整列させる。(再帰関数) l = MergeSort(left) r = MergeSort(right) #3. 2つのサブリストをマージして1つの整列済みリストにする return Merge(l,r) # 整列済みリストA、Bを使って、整列済みリストCを作成する def Merge(A,B): C = [] # print("A",A) # print("B",B) # 1. AとBの先頭の数字を比較し、小さい方をCに追加し追加した値はリストから削除する # 2. 手順1をどちらか一方のリストが空になるまで繰り返す。 while (len(A) > 0) and (len(B) > 0): if A[0] < B[0]: C.append(A[0]) del A[0] else: C.append(B[0]) del B[0] # 3. 残った要素をCの末尾に追加する if len(A) > 0: C.extend(A) else: C.extend(B) # print("C",C) return C n = int(eval(input())) array =list(map(int, input().split())) list = MergeSort(array) ans = 0 if n % 2 == 0: for i in range(n//2): ans += 2* list[-i-1] ans -= list[-1] else: for i in range((n-1)//2): ans += 2*list[-i-1] ans += list[-(n-1)//2-1] ans -= list[-1] print(ans)
n = int(eval(input())) list =list(map(int, input().split())) list.sort(reverse=True) ans = 0 if n % 2 == 0: for i in range(n//2): ans += 2* list[i] ans -= list[0] else: for i in range((n-1)//2): ans += 2*list[i] ans += list[(n-1)//2] ans -= list[0] print(ans)
p02615
N = int(eval(input())) A = list(map(int,input().split())) A.sort(reverse=True) ans = 0 B = [] for a in A: if len(B)==0: ans += 0 B.append(a) elif len(B)==1: ans += B[0] B.append(a) else: max_ = min(B[0],B[-1]) idx = len(B)-1 for i in range(len(B)-1): if min(B[i],B[i+1]) > max_: max_ = min(B[i],B[i+1]) idx = i ans += max_ B = B[:idx] + [a] + B[idx:] print(ans)
N = int(eval(input())) A = list(map(int,input().split())) A.sort(reverse=True) ans = 0 idx = 0 cnt = 1 for a in range(N-1): ans += A[idx] cnt += 1 if cnt == 2: idx += 1 cnt = 0 print(ans)
p02615
n = int(eval(input())) lis = list(map(int, input().split())) lis = sorted(lis, reverse=True) ans = lis[0] t = 0 for i in range(1,n-1): if i % 2 != 0: t += 1 ans += lis[t] print(ans)
n = int(eval(input())) lis = list(map(int, input().split())) lis = sorted(lis, reverse=True) ans = 0 for i in range(1,n): ans += lis[i//2] print(ans)
p02615
from collections import deque def main(): eval(input()) # n friendliness = [int(x) for x in input().split()] friendliness.sort() friendliness = deque(friendliness) edges = deque() popped = friendliness.pop() edges.append(popped) ans = 0 while friendliness: ans += edges.pop() popped = friendliness.pop() edges.append(popped) edges.append(popped) edges = deque(sorted(edges)) print(ans) if __name__ == '__main__': main()
from collections import deque def main(): n = int(eval(input())) # n friendliness = [int(x) for x in input().split()] friendliness.sort() friendliness = deque(friendliness) edges = deque() popped = friendliness.pop() edges.append(popped) ans = 0 counter = 0 while friendliness: ans += edges.popleft() counter += 1 popped = friendliness.pop() edges.append(popped) edges.append(popped) print(ans) if __name__ == '__main__': main()
p02615
from collections import deque def main(): eval(input()) # n friendliness = [int(x) for x in input().split()] friendliness.sort() friendliness = deque(friendliness) edges = deque() popped = friendliness.pop() edges.append(popped) ans = 0 while friendliness: ans += edges.popleft() popped = friendliness.pop() edges.append(popped) edges.append(popped) print(ans) if __name__ == '__main__': main()
from collections import deque def main(): eval(input()) # n friendliness = deque(sorted(int(x) for x in input().split())) edges = deque() popped = friendliness.pop() edges.append(popped) ans = 0 while friendliness: ans += edges.popleft() popped = friendliness.pop() edges.append(popped) edges.append(popped) print(ans) if __name__ == '__main__': main()
p02615
from collections import deque def main(): eval(input()) # n friendliness = deque(sorted(int(x) for x in input().split())) edges = deque() popped = friendliness.pop() edges.append(popped) ans = 0 while friendliness: ans += edges.popleft() popped = friendliness.pop() edges.append(popped) edges.append(popped) print(ans) if __name__ == '__main__': main()
from collections import deque def main(): eval(input()) # n friendliness = [int(x) for x in input().split()] friendliness.sort(reverse=True) edges = deque() edges.append(friendliness[0]) ans = 0 for fri in friendliness[1:]: ans += edges.popleft() edges.append(fri) edges.append(fri) print(ans) if __name__ == '__main__': main()
p02615
from collections import deque def main(): eval(input()) (edges := deque()).append( (friendliness := deque(sorted(int(x) for x in input().split()))).pop()) ans = 0 while friendliness: ans += edges.popleft() edges.append(popped := friendliness.pop()) edges.append(popped) print(ans) if __name__ == '__main__': main()
from collections import deque def main(): eval(input()) # n friendliness = [int(x) for x in input().split()] friendliness.sort() friendliness = deque(friendliness) edges = deque() popped = friendliness.pop() edges.append(popped) ans = 0 while friendliness: ans += edges.popleft() popped = friendliness.pop() edges.append(popped) edges.append(popped) print(ans) if __name__ == '__main__': main()
p02615
from collections import deque def main(): eval(input()) # n array = [int(x) for x in input().split()] array.sort() coming = deque(array) edges = deque() noob = coming.pop() edges.append(noob) ans = 0 while coming: ans += edges.popleft() noob = coming.pop() edges.append(noob) edges.append(noob) print(ans) if __name__ == '__main__': main()
def main(): n = int(eval(input())) friendliness = [int(x) for x in input().split()] friendliness.sort(reverse=True) ans = 0 for i in range(1, n): ans += friendliness[i // 2] print(ans) if __name__ == '__main__': main()
p02615
import sys import math import itertools import bisect from copy import copy from collections import deque,Counter from decimal import Decimal def s(): return eval(input()) def i(): return int(eval(input())) def S(): return input().split() def I(): return list(map(int,input().split())) def L(): return list(input().split()) def l(): return list(map(int,input().split())) def lcm(a,b): return a*b//math.gcd(a,b) sys.setrecursionlimit(10**9) INF = 10**9 mod = 10**9+7 N = i() A = l() A.sort(reverse=True) ans = 0 lis = [A[0]] A = A[1:] for i in range(N-1): lis.append(A[i]) lis.append(A[i]) for i in range(N-1): ans += lis[i] print(ans)
N=int(eval(input())) l=sorted(list(map(int,input().split()))) l.reverse() s=l[0]+N%2*l[N//2] for i in range(N//2-1): s+=l[i+1]*2 print(s)
p02615
N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) ans = 0 tmp = 0 for i in range(N): if i == 0: continue elif i == 1: ans += A.pop(0) elif i % 2 == 0: tmp = A.pop(0) ans += tmp else: ans += tmp print(ans)
N = int(eval(input())) A = list(map(int, input().split())) A.sort() ans = 0 tmp = 0 for i in range(N): if i == 0: continue elif i == 1: ans += A.pop() elif i % 2 == 0: tmp = A.pop() ans += tmp else: ans += tmp print(ans)
p02615
n = int(eval(input())) nums = [] nums = list(map(int, input().split())) nums = sorted(nums, reverse=True) cnt = 0 fir_num = nums[0] del nums[0] for i in range(n-2): fir_num += nums[0] cnt += 1 if cnt == 2: del nums[0] cnt = 0 print(fir_num)
n = int(eval(input())) nums = [] nums = list(map(int, input().split())) nums = sorted(nums, reverse=True) cnt = 0 fir_num = nums[0] del nums[0] for i in range(n-2): fir_num += nums[int(i/2)] print(fir_num)
p02615
# -*- coding: utf-8 -*- N = int(eval(input())) A = list(map(int, input().split())) A_sort = sorted(A, reverse=True) sum_num = A_sort.pop(0) i = 2 while i + 2 <= N: sum_num += A_sort.pop(0) * 2 i += 2 if i + 1 <= N: sum_num += A_sort.pop(0) print(sum_num)
# -*- coding: utf-8 -*- N = int(eval(input())) A = list(map(int, input().split())) A_sort = sorted(A) sum_num = A_sort.pop(-1) i = 2 while i + 2 <= N: sum_num += A_sort.pop(-1) * 2 i += 2 if i + 1 <= N: sum_num += A_sort.pop(-1) print(sum_num)
p02615
from collections import deque n = int(eval(input())) A = [*list(map(int, input().split()))] # print(n) # print(A) A.sort(reverse=True) # print(A) q = deque([A[0]]) B = [] for a in A[1:]: x = q.popleft() B.append(x) q.append(a) q.append(a) # print(q) # print(B) print((sum(B)))
from collections import deque eval(input()) A = [*list(map(int, input().split()))] A.sort(reverse=True) q = deque([A[0]]) P = [] for a in A[1:]: p = q.popleft() P.append(p) q.append(a); q.append(a) print((sum(P)))
p02615
from heapq import * n = int(eval(input())) A = [*[-int(x) for x in input().split()]] heapify(A) ans = -heappop(A) for c in range(0, n-2, 2): ans += -heappop(A) * (1<<(c+1 < n-2)) print(ans)
n = int(eval(input())) A = sorted([*list(map(int, input().split()))], reverse=True) ans = -A[0] for i in range(n): ans += A[i//2] print(ans)
p02615
N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) ans = A[0] cur = 1 count = 1 while True: if count <= N - 3: ans += A[cur] * 2 count += 2 cur += 1 else: ans += A[cur] count += 1 if count == N - 1: break print(ans)
N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) ans = A[0] cur = 1 count = 1 while True: if count <= N - 3: ans += A[cur] * 2 count += 2 cur += 1 elif count == N - 2: ans += A[cur] count += 1 elif count == N - 1: break print(ans)
p02615
import sys readline = sys.stdin.readline readall = sys.stdin.read ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep='\n') def solve(): n = ni() a = sorted(nl(), reverse=True) c = a[0] n -= 2 for x in a[1:]: if n == 0: break if n == 1: c += x n -= 1 else: c += x * 2 n -= 2 print(c) return solve()
import sys readline = sys.stdin.readline readall = sys.stdin.read ns = lambda: readline().rstrip() ni = lambda: int(readline().rstrip()) nm = lambda: map(int, readline().split()) nl = lambda: list(map(int, readline().split())) prn = lambda x: print(*x, sep='\n') def solve(): n = ni() a = sorted(nl(), reverse=True) print(sum(a[:n//2]) + sum(a[1:(n+1)//2])) return solve()
p02615
import sys from io import StringIO import unittest import os import heapq # 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む) sys.setrecursionlimit(999999999) # 実装を行う関数 def resolve(test_def_name=""): n = int(eval(input())) a_s = list(map(int, input().split())) a_s.sort(reverse=True) que = [] heapq.heapify(que) ans = 0 for cnt, a in enumerate(a_s): a = -a if cnt == 0: heapq.heappush(que, a) continue # キューから値の取り出し target = heapq.heappop(que) ans += -target heapq.heappush(que, a) heapq.heappush(que, a) print(ans) # テストクラス class TestClass(unittest.TestCase): def assertIO(self, assert_input, output): stdout, sat_in = sys.stdout, sys.stdin sys.stdout, sys.stdin = StringIO(), StringIO(assert_input) resolve(sys._getframe().f_back.f_code.co_name) sys.stdout.seek(0) out = sys.stdout.read()[:-1] sys.stdout, sys.stdin = stdout, sat_in self.assertEqual(out, output) def test_input_1(self): test_input = """4 2 2 1 3""" output = """7""" self.assertIO(test_input, output) def test_input_2(self): test_input = """7 1 1 1 1 1 1 1""" output = """6""" self.assertIO(test_input, output) # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること def test_1original_1(self): test_input = """4 2 2 3 3""" output = """9""" self.assertIO(test_input, output) # 実装orテストの呼び出し if __name__ == "__main__": if os.environ.get("USERNAME") is None: # AtCoder提出時の場合 resolve() else: # 自PCの場合 unittest.main()
import sys from io import StringIO import unittest import os import heapq # 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む) sys.setrecursionlimit(999999999) # 検索用タグ、優先度付きキュー、値の大きい物から取得 # 実装を行う関数 def resolve(test_def_name=""): n = int(eval(input())) a_s = list(map(int, input().split())) a_s.sort(reverse=True) # 優先度付きキューの作成 que = [] heapq.heapify(que) ans = 0 # 大きい値から順番に処理していく for cnt, a in enumerate(a_s): # 大きい値から順番に処理していくため、値を反転(優先度付きキュー利用時の注意) a = -a if cnt == 0: # ループ一回目の時だけ、こっちを通過。 heapq.heappush(que, a) continue # キューから値の取り出し target = heapq.heappop(que) ans += -target # 現在の値を追加できるパターンが2つできるので、それをキューに追加 heapq.heappush(que, a) heapq.heappush(que, a) print(ans) # テストクラス class TestClass(unittest.TestCase): def assertIO(self, assert_input, output): stdout, sat_in = sys.stdout, sys.stdin sys.stdout, sys.stdin = StringIO(), StringIO(assert_input) resolve(sys._getframe().f_back.f_code.co_name) sys.stdout.seek(0) out = sys.stdout.read()[:-1] sys.stdout, sys.stdin = stdout, sat_in self.assertEqual(out, output) def test_input_1(self): test_input = """4 2 2 1 3""" output = """7""" self.assertIO(test_input, output) def test_input_2(self): test_input = """7 1 1 1 1 1 1 1""" output = """6""" self.assertIO(test_input, output) # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること def test_1original_1(self): test_input = """4 2 2 3 3""" output = """9""" self.assertIO(test_input, output) # 実装orテストの呼び出し if __name__ == "__main__": if os.environ.get("USERNAME") is None: # AtCoder提出時の場合 resolve() else: # 自PCの場合 unittest.main()
p02615
import sys sys.setrecursionlimit(10 ** 9) # input = sys.stdin.readline #### def int1(x): return int(x) - 1 def II(): return int(eval(input())) def MI(): return list(map(int, input().split())) def MI1(): return list(map(int1, input().split())) def LI(): return list(map(int, input().split())) def LI1(): return list(map(int1, input().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def MS(): return input().split() def LS(): return list(eval(input())) def LLS(rows_number): return [LS() for _ in range(rows_number)] def printlist(lst, k=' '): print((k.join(list(map(str, lst))))) INF = float('inf') # from math import ceil, floor, log2 from collections import deque # from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations # from heapq import heapify, heappop, heappush # import numpy as np # cumsum # from bisect import bisect_left, bisect_right def solve(): N = II() A = LI() A = deque(sorted(A)[::-1]) q = deque([A.popleft()]) ans = 0 while A: ans += q.popleft() a = A.popleft() q.extend([a, a]) print(ans) if __name__ == '__main__': solve()
import sys sys.setrecursionlimit(10 ** 9) # input = sys.stdin.readline #### def int1(x): return int(x) - 1 def II(): return int(eval(input())) def MI(): return list(map(int, input().split())) def MI1(): return list(map(int1, input().split())) def LI(): return list(map(int, input().split())) def LI1(): return list(map(int1, input().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def MS(): return input().split() def LS(): return list(eval(input())) def LLS(rows_number): return [LS() for _ in range(rows_number)] def printlist(lst, k=' '): print((k.join(list(map(str, lst))))) INF = float('inf') # from math import ceil, floor, log2 from collections import deque # from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations # from heapq import heapify, heappop, heappush # import numpy as np # cumsum # from bisect import bisect_left, bisect_right def solve(): N = II() A = LI() A = deque(sorted(A, reverse=True)) q = deque([A.popleft()]) # print(q) ans = 0 while A: ans += q.popleft() a = A.popleft() q.extend([a, a]) print(ans) if __name__ == '__main__': solve()
p02615
#!/usr/bin/env python3 import sys import collections def II(): return int(sys.stdin.readline()) def MI(): return list(map(int, sys.stdin.readline().split())) def LI(): return list(map(int, sys.stdin.readline().split())) def main(): N = II() As = LI() As.sort() As.reverse() ans = [] ans.append(As.pop(0)) N -= 1 for i in range(len(As)): tmp = As.pop(0) ans.extend([tmp, tmp]) print((sum(ans[:N]))) # oj t -c "pypy3 main.py" # acc s main.py -- --guess-python-interpreter pypy main()
#!/usr/bin/env python3 import sys import collections def II(): return int(sys.stdin.readline()) def MI(): return list(map(int, sys.stdin.readline().split())) def LI(): return list(map(int, sys.stdin.readline().split())) def main(): N = II() As = LI() As.sort() As.reverse() As = collections.deque(As) ans = [] tmp = As.popleft() ans.append(tmp) for i in range(len(As)): tmp = As.popleft() ans.extend([tmp, tmp]) print((sum(ans[:N-1]))) # print(ans) # oj t -c "pypy3 main.py" # acc s main.py -- --guess-python-interpreter pypy main()
p02615
n = int(eval(input())) a = list(map(int, input().split())) a.sort() a.reverse() ans = 0 seq = [a[0]] seq.append(a[1]) ans += a[0] l = 2 for i in range(2, n): pos = 0 m_ans, M_ans = 0, 0 for p in range(l): np = p + 1 if np == l: np = 0 m, M = min(seq[p], seq[np]), max(seq[p], seq[np]) if m >= m_ans and M >= m_ans: pos = np m_ans = m M_ans = M ans += m_ans seq.insert(pos, a[i]) l += 1 print(ans)
n = int(eval(input())) a = list(map(int, input().split())) a.sort() a.reverse() #ans = 0 #seq = [a[0]] # #seq.append(a[1]) #ans += a[0] # #l = 2 #for i in range(2, n): # pos = 0 # m_ans, M_ans = 0, 0 # for p in range(l): # np = p + 1 # if np == l: # np = 0 # m, M = min(seq[p], seq[np]), max(seq[p], seq[np]) # if m >= m_ans and M >= m_ans: # pos = np # m_ans = m # M_ans = M # ans += m_ans # seq.insert(pos, a[i]) # l += 1 #print(ans) ans = 0 cur = 0 cnt = 1 for i in range(n - 1): if cnt == 2: cur += 1 cnt = 0 ans += a[cur] cnt += 1 print(ans)
p02615
n=int(eval(input())) a=list(map(int,input().split(' '))) def min_point(data): lgt = len(data) m=0 ins=0 for i in range(lgt): if i ==0: buf = min(data[i],data[lgt-1]) else: buf = min(data[i],data[i-1]) if buf > m: m=buf ins=i return [m,ins] a.sort(reverse=True) cnt=0 lst=[] for i in range(n): if i == 0: lst.append(a[i]) elif len(lst) == 1: cnt += lst[0] lst.append(a[i]) else: mp = min_point(lst) cnt += mp[0] lst.insert(mp[1], a[i]) print(cnt)
n=int(eval(input())) a=list(map(int,input().split(' '))) a.sort(reverse=True) cnt=0 for i in range(1,n): if i==1: cnt+=a[0] else: j = (i // 2) cnt += a[j] print(cnt)
p02615
# -*- coding: utf-8 -*- N, = list(map(int, input().split())) A = list(map(int, input().split())) A = sorted(A)[::-1] ans = 0 for i, a in enumerate(A): if i==0: first = a elif i==1: ans += first pre = [A[1], A[1]] else: ans += pre[0] pre = pre[1:] pre.append(a) pre.append(a) print(ans)
# -*- coding: utf-8 -*- N, = list(map(int, input().split())) A = list(map(int, input().split())) A = sorted(A)[::-1] ans = 0 for i, a in enumerate(A): if i==0: ans += a itr = 2 if itr >= N: break else: ans += a itr += 1 if itr >= N: break ans += a itr += 1 if itr >= N: break print(ans)
p02615
N=int(eval(input())) A=list(map(int,input().split())) list.sort(A) l=[] n=A.pop() n2=A.pop() sum=n comfort={n2:[[n,n2],[n2,n]]} while A: max=sorted(comfort.keys())[-1] if comfort.get(max)==None:print((0)) n=comfort[max].pop() if comfort[max]==[]:del comfort[max] sum+=max n2=A.pop() if comfort.get(min(n[0],n2))==None:comfort[min(n[0],n2)]=[] if comfort.get(min(n[1],n2))==None:comfort[min(n[1],n2)]=[] comfort[min(n[0],n2)].append([n[0],n2]) comfort[min(n[1],n2)].append([n[1],n2]) print(sum)
eval(input()) A=list(map(int,input().split())) A.sort(reverse=True) B=[] ans=0 for i in A: if len(B)>=1: ans+=B[len(B)//2] B.append(i) print(ans)
p02615
n = int(eval(input())) a = list(map(int,input().split())) a.sort(reverse=True) cnt = a.pop(0) for i in range(n-2): if i % 2 == 0: cnt += a[0] else: cnt += a.pop(0) print(cnt)
n = int(eval(input())) a = list(map(int,input().split())) a.sort(reverse=True) cnt = a.pop(0) if (n-2) % 2 == 0: tmp = 2 * sum(a[0:(n-2)//2]) else: tmp = 2 * sum(a[0:(n-2)//2]) + a[(n-2)//2] cnt += tmp print(cnt)
p02615
conf = 0 num_char = int(eval(input())) char = list(map(int, input().split())) char.sort(reverse=True) count = 0 conf_int = 0 for i in range(num_char): if count == 0: count = 1 elif count == 1: count = 2 conf = char[conf_int] count_int = 0 conf_int = 1 else: if count_int == 0 : conf += char[conf_int] count_int = 1 else: conf += char[conf_int] conf_int += 1 count_int = 0 print(conf)
N = int(eval(input())) As = sorted(map(int, input().split()), reverse=True) score = As[0] counter = 0 index = 1 for i in range(2, N): score += As[index] if counter == 0: counter += 1 else: counter -= 1 index += 1 print(score)
p02615
N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) cnt = 1 idx = 0 ans = 0 while cnt <= N-1: ans += A[idx] if cnt % 2 == 1: #ans += A[idx] idx += 1 #else: #ans += A[idx] cnt += 1 print(ans)
N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) ans = A[0] #最大値だけ1回 for i in range(N-2): ans += A[i//2 + 1] print(ans)
p02615
import heapq N = int(eval(input())) A = list(map(int,input().split())) heap = [] for i in range(N): heapq.heappush(heap,-A[i]) if N==2: ans = -heapq.heappop(heap) elif N==3: ans = -heapq.heappop(heap)-heapq.heappop(heap) else: ans = 0 ans = -heapq.heappop(heap) if N%2==0: for _ in range((N-2)//2): a = -heapq.heappop(heap) ans += 2*a else: for _ in range((N-3)//2): a = -heapq.heappop(heap) ans += 2*a a = -heapq.heappop(heap) ans += a print(ans)
N = int(eval(input())) A = sorted(list(map(int,input().split())),reverse=True) if N%2==0: cnt = A[0] for i in range(1,N//2): cnt += 2*A[i] print(cnt) else: cnt = A[0] for i in range(1,N//2): cnt += 2*A[i] cnt += A[N//2] print(cnt)
p02615
N = int(eval(input())) A = sorted(list(map(int,input().split())),reverse=True) if N%2==0: cnt = A[0] for i in range(1,N//2): cnt += 2*A[i] print(cnt) else: cnt = A[0] for i in range(1,N//2): cnt += 2*A[i] cnt += A[N//2] print(cnt)
N = int(eval(input())) A = sorted(list(map(int,input().split())),reverse=True) cnt = A[0] k = (N+1)//2-1 for i in range(1,1+k): cnt += 2*A[i] if N%2==1: cnt -= A[k] print(cnt)
p02615
import os import sys from io import BytesIO, IOBase # from collections import defaultdict as dd # from collections import deque as dq # import itertools as it # from math import sqrt, log, log2 # from fractions import Fraction import heapq def main(): n = int(eval(input())) nums = list(map(int, input().split())) nums.sort(reverse=True) if n<=3: print((sum(nums[:n-1]))) exit() hp = list() ans = 0 # Push the first three intervals heapq.heappush(hp, (-nums[1], nums[0])) heapq.heappush(hp, (-nums[2], nums[0])) heapq.heappush(hp, (-nums[2], nums[1])) ans = sum(nums[:2]) i = 3 while i < n: comfort, big = heapq.heappop(hp) ans += -comfort small = -comfort curr = nums[i] # what enters the circle #push the smaller interval heapq.heappush(hp, (-curr, small)) heapq.heappush(hp, (-curr, big)) i += 1 print(ans) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main()
n = int(eval(input())) friend = list(map(int, input().split())) friend.sort(reverse = True) ans = friend[0] i = 1 ind = 1 while i < n - 1: for j in range(2): ans += friend[ind] i += 1 if i == n-1: break ind += 1 print(ans)
p02615
N=int(eval(input())) A=list(map(int,input().split())) A.sort(reverse=True) ans=0 li=[A[0]] for i in range(1,N): ans+=li[0] li.pop(0) li.append(A[i]) li.append(A[i]) print(ans)
N=int(eval(input())) A=list(map(int,input().split())) A.sort(reverse=True) ans=0 li=[A[0]] for i in range(1,N): ans+=li[i-1] li.append(A[i]) li.append(A[i]) print(ans)
p02615
N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) ans = sum(A[:N//2])*2-A[0] if N%2==1: ans += A[N//2] print(ans)
def solve(): N = int(eval(input())) A = list(map(int, input().split())) A.sort(reverse=True) ans = A[0] cnt = 1 for i in range(1,N-1): ans += A[i] cnt += 1 if cnt==N-1: break ans += A[i] cnt += 1 if cnt==N-1: break return ans print((solve()))
p02615