input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
#!/usr/bin/env python3 import sys, heapq, bisect sys.setrecursionlimit(300000) def _count(A, n, v): ret = 0 for a in A: target = v - a idx = bisect.bisect_left(A, target) ret += n - idx return ret def _sum(A, n, v, l, sums): ss = 0 mins = [] for a in A:...
#!/usr/bin/env python3 import sys, heapq, bisect sys.setrecursionlimit(300000) def _count(A, n, v): ret = 0 for a in A: target = v - a idx = bisect.bisect_left(A, target) ret += n - idx return ret def _sum(A, n, v, sums): ss = 0 for a in A: target =...
p02821
n,m=list(map(int,input().split())) a=list(map(int,input().split())) for i in range(n):a[i]*=-1 a.sort() from bisect import bisect_left,bisect_right def check(mid): mm=0 for i in range(n): if -(a[i]+a[0])<mid:break mm+=bisect_right(a,-(mid+a[i])) return mm ok=0 ng=10**10+7 while ng!=ok+1: m...
from bisect import bisect_right as br n,m=list(map(int,input().split())) a=list(map(int,input().split())) for i in range(n):a[i]*=-1 a.sort() b=[0] for i in a:b.append(b[-1]+i) ng=2*10**5+7 ok=-1 while ok+1!=ng: mid=(ng+ok)//2 co=0 for i in a:co+=br(a,-(mid+i)) if co<m:ng=mid else:ok=mid ans=0 ...
p02821
N,M=[int(c) for c in input().split()] A=[int(c) for c in input().split()] B=sorted([x+y for x in A for y in A],reverse=True) ans=0 for i in range(M): ans+=B[i] print(ans)
N,M=[int(c) for c in input().split()] A=[int(c) for c in input().split()] B=sorted([x+y for x in A for y in A],reverse=True) print((sum(B[0:M])))
p02821
import sys input=sys.stdin.readline import bisect n,k=list(map(int,input().split())) a=list(map(int,input().split())) a.sort() r=a[n-1]*2+1 l=0 while r-l>=2: mid=(l+r)//2 #mid以上はいくつあるか #答えはl以上r未満 cnt=0 for i in range(n): cnt+=n-bisect.bisect_left(a,mid-a[i]) if cnt>=k: l=mid else: r=mid ...
import bisect n,k=list(map(int,input().split())) a=list(map(int,input().split())) a.sort() r=a[n-1]*2+1 l=0 while r-l>=2: mid=(l+r)//2 #mid以上はいくつあるか #答えはl以上r未満 cnt=0 for i in range(n): cnt+=n-bisect.bisect_left(a,mid-a[i]) if cnt>=k: l=mid else: r=mid #lだとk個以上 rだとk個未満 ans,p,=0,0 b=[0] f...
p02821
from bisect import bisect_left from itertools import accumulate N, M = list(map(int, input().split())) A = sorted(list(map(int, input().split()))) Acum = [0] + list(accumulate(A)) left = 0 right = 10 ** 10 + 1 while left + 1 < right: mid = (left + right) // 2 tmp = 0 for i in range(N): bi =...
from bisect import bisect_left from itertools import accumulate N, M = list(map(int, input().split())) A = sorted(list(map(int, input().split()))) Acum = [0] + list(accumulate(A)) left = 0 right = max(A) * 2 + 1 while left + 1 < right: mid = (left + right) // 2 tmp = 0 for i in range(N): bi...
p02821
from bisect import bisect_left from itertools import accumulate N, M = list(map(int, input().split())) A = sorted(list(map(int, input().split()))) Acum = [0] + list(accumulate(A)) left = 0 right = max(A) * 2 + 1 while left + 1 < right: mid = (left + right) // 2 tmp = 0 for i in range(N): bi...
from itertools import accumulate N, M = list(map(int, input().split())) A = list(map(int, input().split())) A.sort(reverse = True) #print(A) ok = -1 ng = 10 ** 11 while abs(ok - ng) > 1: mid = (ok + ng) // 2 left, right = 0, N - 1 cnt = 0 while left <= N - 1 and right >= 0: if ...
p02821
import bisect def judge(k, N, A): t = 0 for i in A: j = bisect.bisect_left(A, k - i) t += N - j return t def main(): N, M = list(map(int, input().split())) A = list(map(int, input().split())) A.sort() t = A[-1] * 2 b = 0 X = None while t - b > 1: ...
import bisect def judge(k, N, A): t = 0 for i in A: j = bisect.bisect_left(A, k - i) t += N - j return t def main(): N, M = list(map(int, input().split())) A = list(map(int, input().split())) A.sort() t = A[-1] * 2 b = 0 X = None while t - b > 1: ...
p02821
def judge(k, N, A): t = 0 s = N for i in A: for j in reversed(list(range(s))): if A[j] + i >= k: s = j + 1 t += j + 1 break return t def main(): N, M = list(map(int, input().split())) A = list(map(int, input().split(...
def judge(k, N, A): p = N - 1 t = 0 for i in A: while p >= 0 and A[p] + i < k: p -= 1 t += (p + 1) return t def main(): N, M = list(map(int, input().split())) A = list(map(int, input().split())) A.sort(reverse=True) t = A[0] * 2 b = 0 X =...
p02821
from itertools import accumulate from heapq import heappush, heappop, heapify def bisect(ng, ok, judge, eps=1): while abs(ng-ok) > eps: m = (ng+ok)//2 if judge(m): ok = m else: ng = m return ok N,M = list(map(int,input().split())) A = sorted(map(int...
from itertools import accumulate def bisect(ng, ok, judge, eps=1): while abs(ng-ok) > eps: m = (ng+ok)//2 if judge(m): ok = m else: ng = m return ok N,M = list(map(int,input().split())) A = sorted(map(int,input().split())) acc = [0]+list(accumulate(...
p02821
from heapq import * import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): r...
import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in ...
p02821
n,m = list(map(int, input().split())) A = sorted(list(map(int, input().split())),reverse=True) def cnt(x): c = 0 for j in range(len(A)): i = 0 while i <n and A[j]+A[i]>=x: c += 1 i += 1 return c def happiness(x): c = 0 for j in range(len(A)): ...
N, M = list(map(int, input().split())) A = sorted(list(map(int,input().split())), reverse=True) # jとの組み合わせがx以上になるiの個数 def combinations(x): s = 0 i = 0 for j in reversed(list(range(N))): while i<N and A[i]+A[j]>=x: i += 1 s += i return s #↑のような組み合わせの合計幸福度 def koufukudo(x): s = 0...
p02821
import heapq n, m = list(map(int, input().split())) A = list(map(int, input().split())) A.sort(reverse=True) idx_dict = {} for i in range(n): idx_dict[i] = i+1 ans = [A[i]+A[i] for i in range(n)] candidates = [[-(A[i]+A[i+1]), i] for i in range(n-1)] heapq.heapify(candidates) for i in range(m//2...
n, m = list(map(int, input().split())) A = list(map(int, input().split())) A.sort(reverse=True) cumsumA = [] total = 0 for a in A: total += a cumsumA.append(total) cumsumA.append(0) l = 0 r = 10 ** 6 while l + 1 < r: num = (l+r)//2 pair_idx = n-1 count = 0 for a in A: ...
p02821
#!/usr/bin/env python3 import sys # sys.setrecursionlimit(10000000) INF = 1<<32 from bisect import bisect_left, bisect_right def solve(N: int, M: int, A: "List[int]"): def binary_search(left: int, right: int): s = 0 c = 0 mi = INF while right-left > 1 or c...
#!/usr/bin/env python3 import sys # sys.setrecursionlimit(10000000) INF = 1<<32 from bisect import bisect_left, bisect_right def solve(N: int, M: int, A: "List[int]"): def binary_search(left: int, right: int): s = 0 c = 0 mi = INF while right-left > 1 or c...
p02821
from bisect import bisect_left N, M = list(map(int, input().split())) A = list(map(int, input().split())) A.sort() def isOverEq(n): cnt = 0 for a in A: cnt += N - bisect_left(A, n - a) return cnt >= M overEq = -1 less = 10**12 while less - overEq > 1: mid = (less + overEq) //...
from bisect import bisect_left N, M = list(map(int, input().split())) A = list(map(int, input().split())) A.sort() def isOverEq(n): cnt = 0 for a in A: cnt += N - bisect_left(A, n - a) return cnt >= M overEq = -1 less = A[-1] * 2 + 100 while less - overEq > 1: mid = (less + o...
p02821
from bisect import bisect_left from itertools import accumulate N,M,*A = list(map(int, open(0).read().split())) A.sort() B = list(accumulate(A[::-1])) l = -1 r = 10**9 while l+1<r: s = (l+r)//2 cnt = 0 for c in A: i = bisect_left(A,s-c) cnt += N-i if cnt>=M: l = s else: r = s ...
from bisect import bisect_left from itertools import accumulate N,M,*A = list(map(int, open(0).read().split())) A.sort() B = list(accumulate(A[::-1])) l = -1 r = 10**6 while l+1<r: s = (l+r)//2 cnt = 0 for c in A: i = bisect_left(A,s-c) cnt += N-i if cnt>=M: l = s else: r = s ...
p02821
import bisect n,m=list(map(int,input().split())) A=list(map(int,input().split())) A.sort() def hand(x): cnt=0 for i in range(n): p=x-A[i] cnt+=n-bisect.bisect_left(A,p) return cnt l=0 h=10**15+3 mid=(l+h)//2 while l+1<h: mid=(l+h)//2 if hand(mid)<m: h=mid else: l=...
import sys read = sys.stdin.buffer.read input = sys.stdin.buffer.readline inputs = sys.stdin.buffer.readlines import bisect n,m=list(map(int,input().split())) A=list(map(int,input().split())) A.sort() def hand(x): cnt=0 for i in range(n): p=x-A[i] cnt+=n-bisect.bisect_left(A,p) retur...
p02821
import sys from itertools import accumulate read = sys.stdin.buffer.read input = sys.stdin.buffer.readline inputs = sys.stdin.buffer.readlines import bisect n,m=list(map(int,input().split())) A=list(map(int,input().split())) A.sort() def hand(x): cnt=0 for i in range(n): p=x-A[i] cnt+=...
import sys from itertools import accumulate read = sys.stdin.buffer.read input = sys.stdin.buffer.readline inputs = sys.stdin.buffer.readlines import bisect n,m=list(map(int,input().split())) A=list(map(int,input().split())) A.sort() def hand(x): cnt=0 for i in range(n): p=x-A[-i-1] k=...
p02821
import sys from itertools import accumulate read = sys.stdin.buffer.read input = sys.stdin.buffer.readline inputs = sys.stdin.buffer.readlines import bisect n,m=list(map(int,input().split())) A=list(map(int,input().split())) A.sort() def hand(x): cnt=0 for i in range(n): p=x-A[-i-1] k=...
import sys from itertools import accumulate read = sys.stdin.buffer.read input = sys.stdin.buffer.readline inputs = sys.stdin.buffer.readlines import bisect n,m=list(map(int,input().split())) A=list(map(int,input().split())) A.sort() def hand(x): cnt=0 for i in range(n): p=x-A[-i-1] k=...
p02821
#!/usr/bin/env python3 import sys from itertools import accumulate from bisect import bisect_left N, M = [int(_) for _ in input().split()] A = [int(_) for _ in input().split()] A2 = sorted(A, reverse=True) A = sorted(A) # 必要となる左右合計の下限値 X を求める def f1(m): cnt = 0 for a1 in A2: flg = ...
#!/usr/bin/env python3 import sys from itertools import accumulate from bisect import bisect_left N, M = [int(_) for _ in input().split()] A = [int(_) for _ in input().split()] A2 = sorted(A, reverse=True) A = sorted(A) # 必要となる左右合計の下限値 X を求める def f1(m): cnt = 0 for a1 in A2: tmp = ...
p02821
import sys input=sys.stdin.readline N,M=list(map(int,input().split())) A=list(map(int,input().split())) A.sort() def condition(num): count=0 s=N-1 t=0 while N-1>=t and s>=0: if num>A[s]+A[t]: t+=1 else: count+=N-t s-=1 return c...
N,M=list(map(int,input().split())) A=list(map(int,input().split())) A.sort() def condition(num): count=0 s=N-1 t=0 while N-1>=t and s>=0: if num>A[s]+A[t]: t+=1 else: count+=N-t s-=1 return count>=M subans=0 start=1 end=2*A[N-1]...
p02821
def main(): from bisect import bisect_left as bl from itertools import accumulate as ac n, m = list(map(int, input().split())) a = list(map(int, input().split())) a.sort() aa = [0]+list(ac(a)) def b_search(ok, ng, value): while abs(ok-ng) > 1: mid = (ok+ng)//...
def main(): from bisect import bisect_left as bl from itertools import accumulate as ac n, m = list(map(int, input().split())) a = list(map(int, input().split())) a.sort() aa = [0]+list(ac(a)) ok = 0 ng = 10**15 while abs(ok-ng) > 1: mid = (ok+ng)//2 q ...
p02821
import sys import cmath input = sys.stdin.buffer.readline pi = cmath.pi exp = cmath.exp """ https://tjkendev.github.io/procon-library/python/fft/fft.html """ def make_exp_t(fn, base): exp_t = {0: 1} temp = fn while temp: exp_t[temp] = exp(base / temp) temp >>= 1 return e...
import sys import cmath input = sys.stdin.buffer.readline pi = cmath.pi exp = cmath.exp """ https://tjkendev.github.io/procon-library/python/fft/fft.html """ def make_exp_t(fn, base): exp_t = {0: 1} temp = fn while temp: exp_t[temp] = exp(base / temp) temp >>= 1 return e...
p02821
def binary_search(*, ok, ng, is_ok): while abs(ok - ng) > 1: mid = (ng + ok) // 2 if is_ok(mid): ok = mid else: ng = mid return ok def main(): from itertools import accumulate N, M = list(map(int, input().split())) *A, = sorted(map(int...
# 過去の提出を見ながら解いた def main(): N, M = list(map(int, input().split())) *a, = list(map(int, input().split())) a.sort() def count(mid) -> int: cnt = 0 # (i,j)>=mid の個数 j = N # j: iと組んでペア和>=midを満たすjの下限, 初期値は範囲外=条件を満たすjはない for i in range(N): while j > 0 and a[i...
p02821
#import sys #sys.setrecursionlimit(10**7) #from functools import reduce #from fractions import gcd # input n, m = list(map(int, list(input().split()))) alist = list(map(int, input().split())) # process blist=[] for i in alist: for j in alist: blist.append(i+j) blist.sort(reverse=True) ...
# input N, M = list(map(int, list(input().split()))) A = list(map(int, input().split())) # process A.sort(reverse=True) # 1回の握手の幸福度がx以上となるものの数、幸福度の合計を求める def calc(x): count, sum = 0, 0 j, t = 0, 0 for i in reversed(list(range(N))): while j < N and A[i]+A[j] >= x: t += A[...
p02821
from bisect import bisect_left, bisect_right from itertools import accumulate def resolve(): def shake_cnt(x): cnt = 0 pos = 0 for i in range(N): pos = bisect_left(A, x - A[i]) cnt += N - pos return cnt < M # X以上の和がM個未満かどうか N, M = list(map(in...
from bisect import bisect_left from itertools import accumulate def resolve(): def getCount(x): count = 0 for Ai in A: idx = bisect_left(A, x - Ai) count += N - idx return count >= M N, M = list(map(int, input().split())) A = sorted(map(int, in...
p02821
from bisect import bisect_left from itertools import accumulate def resolve(): def getCount(x): cnt = 0 for a in A: # X未満の個数 idx = bisect_left(A, x - a) # X以上の個数 cnt += N - idx # X以上を使う!と決めた時、M個以上になるか return cnt >= M ...
from bisect import bisect_left from itertools import accumulate def resolve(): def getCount(x): cnt = 0 for a in A: # X未満の個数 idx = bisect_left(A, x - a) # X以上の個数 cnt += N - idx # X以上を使う!と決めた時、M個以上になるか return cnt >= M ...
p02821
# -*- coding: utf-8 -*- import sys from bisect import bisect_left from itertools import accumulate def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e):...
# -*- coding: utf-8 -*- import sys from bisect import bisect_left from itertools import accumulate def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e):...
p02821
import sys from bisect import bisect, bisect_left import heapq def solve(): input = sys.stdin.readline N, M = list(map(int, input().split())) A = [int(a) for a in input().split()] A.sort() S = [0] * (N + 1) for i, a in enumerate(A): S[i + 1] = a + S[i] l, h = 1, A[N-1] * 2 + 1...
import sys from bisect import bisect, bisect_left import heapq def solve(): input = sys.stdin.readline N, M = list(map(int, input().split())) A = [int(a) for a in input().split()] A.sort() S = [0] * (N + 1) for i, a in enumerate(A): S[i + 1] = a + S[i] l, h = 1, A[N-1] * 2 + 1...
p02821
import sys input = sys.stdin.readline import bisect from itertools import accumulate N, M = [int(x) for x in input().split()] A = [int(x) for x in input().split()] A.sort() left = -1 right = 10 ** 6 def shake(x): cnt = 0 for i in A: cnt += N - bisect.bisect_left(A, x - i) if cnt...
import sys input = sys.stdin.readline import bisect from itertools import accumulate N, M = [int(x) for x in input().split()] A = [int(x) for x in input().split()] A.sort() left = -1 right = 1 + 2 * 10 ** 5 def shake(x): cnt = 0 for i in A: cnt += N - bisect.bisect_left(A, x - i) ...
p02821
from bisect import bisect_left as bl from itertools import accumulate as ac n,m=list(map(int,input().split())) a=sorted(list(map(int,input().split()))) s=[0]+list(ac(a[::-1])) l,r=2*a[-1]*(n**2)+1,0 while l-r>1: t=(l+r)//2 p=0 for i in range(n): x=bl(a,t-a[i]) p+=n-x if p<m: ...
from bisect import bisect_left as bl from itertools import accumulate as ac n,m=list(map(int,input().split())) a=sorted(list(map(int,input().split()))) s=[0]+list(ac(a[::-1])) l,r=2*a[-1]*(n**2)+1,0 while l-r>1: t=(l+r)//2 p=0 for i in range(n-1,-1,-1): if a[i]+a[-1]>=t: x=bl(...
p02821
from heapq import heappush,heappop,heapify from collections import deque,defaultdict,Counter import itertools from itertools import permutations,combinations import sys import bisect import string import math import time import random def I(): return int(input()) def MI(): return map(int,input().sp...
from bisect import bisect as B LI=lambda:list(map(int,input().split())) A=0 n,m=LI() a=sorted(LI()) ra=a[::-1] def Q(x,M): w=0 for i in range(n): y=x-ra[i] c=B(a,y-1) w+=n-c if w>=M:return True return False N=a[-1]*3 O=-1 while abs(O-N)>1: f=(O+N)//2 ...
p02821
from bisect import bisect as B LI=lambda:list(map(int,input().split())) A=0 n,m=LI() a=sorted(LI()) ra=a[::-1] def Q(x,M): w=0 for i in range(n): y=x-ra[i] c=B(a,y-1) w+=n-c if w>=M:return True return False N=a[-1]*3 O=-1 while abs(O-N)>1: f=(O+N)//2 ...
from bisect import bisect as B LI=lambda:list(map(int,input().split())) A=0 n,m=LI() a=sorted(LI()) R=a[::-1] def Q(x,M): w=0 for i in range(n): y=x-R[i] c=B(a,y-1) w+=n-c if w>=M:return True return False N=a[-1]*3 O=-1 while N-O>1: f=(O+N)//2 if Q(f...
p02821
import bisect LI=lambda:list(map(int,input().split())) A=0 n,m=LI() a=sorted(LI()) R=a[::-1] def Q(x,M): w=0 for i in range(n): y=x-R[i] c=bisect.bisect(a,y-1) w+=n-c if w>=M:return True return False N=a[-1]*3 O=-1 while N-O>1: f=(O+N)//2 if Q(f,m): ...
from bisect import bisect as B LI=lambda:list(map(int,input().split())) A=0 n,m=LI() a=sorted(LI()) R=a[::-1] def Q(x,M): w=0 for i in range(n): y=x-R[i] c=B(a,y-1) w+=n-c if w>=M:return 1 return 0 N=a[-1]*3 O=-1 while N-O>1: f=(O+N)//2 if Q(f,m):O=f else:N=f P=0 for i i...
p02821
from collections import Counter,defaultdict,deque from heapq import heappop,heappush,heapify import sys,bisect,math,itertools,fractions,pprint sys.setrecursionlimit(10**8) mod = 10**9+7 INF = float('inf') def inp(): return int(sys.stdin.readline()) def inpl(): return list(map(int, sys.stdin.readline().split())) ...
from collections import Counter,defaultdict,deque from heapq import heappop,heappush from bisect import bisect_left,bisect_right import sys,math,itertools,fractions,pprint sys.setrecursionlimit(10**8) mod = 10**9+7 INF = float('inf') def inp(): return int(sys.stdin.readline()) def inpl(): return list(map(int, ...
p02821
from bisect import bisect_left, bisect_right N, M = list(map(int, input().split())) *A, = list(map(int, input().split())) A.sort() ac = [0]*(N+1) for i in range(1, N+1): ac[i] = ac[i-1]+A[i-1] # ll = [] # for i in A: # for j in A: # ll.append(i+j) # ll.sort() def f(x): v = 0 ...
from bisect import bisect_right N, M = list(map(int, input().split())) *A, = list(map(int, input().split())) INF = 10**12 A.sort() ac = [0]*(N+1) for i in range(1, N+1): ac[i] = ac[i-1]+A[i-1] def f(x): v = 0 for i in A: v += N-bisect_right(A, x-i) return v def g(l): ans ...
p02821
n,m=list(map(int,input().split())) a=sorted(list(map(int,input().split()))) import bisect # 解説の二分探索 # あるXを定め、X以上になる握手の組み合わせが何通りあるか計算。ちょうどM個以上になるXを探す l,r=0,a[-1]*2 while r-l>1: x=(l+r)//2 cnt=0 for ai in a: cnt+=n-bisect.bisect_left(a,x-ai) # x以上がm個以上ある->xを増加 [l,r]=[x,r] if cnt>=m else [l,x] x...
import sys input = sys.stdin.readline # 文字列をinput()した場合、末尾に改行が入るので注意 def main(): n,m=list(map(int,input().split())) a=sorted(list(map(int,input().split()))) import bisect # 解説の二分探索 # あるXを定め、X以上になる握手の組み合わせが何通りあるか計算。M個以上になる最大のXを探す # [l,r)半開区間 l,r=0,a[-1]*2+1 while r-l>1: x=(l+r)//2 ...
p02821
from bisect import bisect_left def check(x, m): # 幸福度が x 以上となる握手が m 通り以上か cnt = 0 for i in range(N): y = x - A[i] p = bisect_left(A, y) cnt += N - p return cnt >= m N, M = list(map(int, input().split())) A = list(map(int, input().split())) A.sort() S = [0] *...
from bisect import bisect_left def solve(m): r = N - 1 l = 0 cnt = 0 while 0 <= r and l < N: if A[r] + A[l] < m: l += 1 else: cnt += N - l r -= 1 return cnt N, M = list(map(int, input().split())) A = list(map(int, input().split...
p02821
import sys import bisect import itertools def solve(): readline = sys.stdin.buffer.readline mod = 10 ** 9 + 7 n, m = list(map(int, readline().split())) a = list(map(int, readline().split())) a.sort() cor_v = -1 inc_v = 10 ** 20 while - cor_v + inc_v > 1: bin_v =...
import sys import bisect import itertools def solve(): readline = sys.stdin.buffer.readline mod = 10 ** 9 + 7 n, m = list(map(int, readline().split())) a = list(map(int, readline().split())) a.sort() cor_v = -1 inc_v = 10 ** 11 while - cor_v + inc_v > 1: bin_v =...
p02821
#python3 INF = int(1e9) def main(): n, m = list(map(int, input().split())) a = [int(i) for i in input().split()] a.sort() s = [0] for i in range(n): s.append(s[i]+a[i]) #Xより大きい要素の数を数える def counter(x): count = 0 for i in range(n): l = -1 ...
INF = int(1e9) def main(): n, m = list(map(int, input().split())) a = [int(i) for i in input().split()] a.sort() s = [0] for i in range(n): s.append(s[i]+a[i]) #Xより大きい要素の数を数える def counter(x): count = 0 for i in range(n): l = -1 ...
p02821
import bisect n, m = list(map(int, input().split())) a = list(map(int, input().split())) a = sorted(a) ruiseki = [0]*(n+1) for i in range(n): ruiseki[i+1] = ruiseki[i] + a[i] def solve(min_power): ans = 0 for pos in range(n): tmp = min_power - a[pos] next_pos = bisect.bisect...
import bisect n, m = list(map(int, input().split())) a = list(map(int, input().split())) a = sorted(a) ruiseki = [0]*(n+1) for i in range(n): ruiseki[i+1] = ruiseki[i] + a[i] def solve(min_power): ans = 0 for pos in range(n): tmp = min_power - a[pos] next_pos = max(bisect....
p02821
import bisect def solve(mid): cnt = 0 r = n for i in range(n): l = bisect.bisect_left(a, mid - a[i]) cnt += (r - l) return cnt >= m n, m = list(map(int, input().split())) a = list(map(int, input().split())) a = sorted(a) ok = 0 ng = 10 ** 18 while abs(ok - ng) ...
import bisect def solve(mid): cnt = 0 l = n r = n for i in range(n): while True: if l == 0: break if a[l - 1] < mid - a[i]: break l -= 1 cnt += (r - l) return cnt >= m n, m = list(map(int, i...
p02821
from sys import stdin f_i = stdin N, M = list(map(int, f_i.readline().split())) A = list(map(int, f_i.readline().split())) A.sort() from bisect import bisect_left def shake_count(power): return N ** 2 - sum(bisect_left(A, power - left_hand) for left_hand in A) lower = A[0] * 2 - 1 upper = A[-1] *...
from sys import stdin f_i = stdin N, M = list(map(int, f_i.readline().split())) A = list(map(int, f_i.readline().split())) A.sort() from bisect import bisect_left def shake_count(power): return N ** 2 - sum(bisect_left(A, power - left_hand) for left_hand in A) lower = A[0] * 2 - 1 upper = A[-1] *...
p02821
from bisect import bisect_left n, m = list(map(int, input().split())) a = list(map(int, input().split())) a.sort(reverse=True) b = list(reversed(a)) rs = [0] * n rs[0] = a[0] for i, x in enumerate(a[1:]): rs[i+1] = rs[i] + x L = 0 H = sum(a) ** 2 + 1 while H - L > 1: M = (L + H) // 2 ...
from bisect import bisect_left n, m = list(map(int, input().split())) a = list(map(int, input().split())) a.sort(reverse=True) b = list(reversed(a)) rs = [0] * n rs[0] = a[0] for i, x in enumerate(a[1:]): rs[i+1] = rs[i] + x L = a[-1] * 2 H = a[0] * 2 while H - L > 1: M = (L + H) // 2 ...
p02821
import sys input = sys.stdin.readline # 昇順ソートされた配列の挿入位置indexを返す O(logN) from bisect import bisect_left def isOver(a,n,x): cnt = 0 for i in range(n): cnt += n-bisect_left(a,x-a[i]) return cnt def calX(a,n,m,l,r): while r-l>1: # x: 握手で得たい最低パワー値 x = (l+r)//2 if isOver(a,n,x) >= m: l = x ...
import sys input = sys.stdin.readline # 昇順ソートされた配列の挿入位置indexを返す O(logN) from bisect import bisect_left def isOver(A,n,x): cnt = 0 for a in A: cnt += n-bisect_left(A,x-a) return cnt def calOut(A,s,n,x): out = 0 for a in A: idx = bisect_left(A,x-a) out += s[n]-s[idx]+a*(n-idx) return out - (...
p02821
def convolve(a, b): def fft(f): d = n // 2 v = w while d >= 1: u = 1 for i in range(d): for j in range(i, n, 2*d): f[j], f[j+d] = (f[j] + f[j+d]) % p, u * (f[j] - f[j+d]) % p u = u * v % p v = v...
k = 34 K = 1<<k nu = lambda L: int("".join([bin(K+a)[-k:] for a in L[::-1]]), 2) st = lambda n: bin(n)[2:] + "0" li = lambda s: [int(a, 2) if len(a) else 0 for a in [s[-(i+1)*k-1:-i*k-1] for i in range(len(B)*2-1)]] N, M = list(map(int, input().split())) A = [int(a) for a in input().split()] B = [0] * 100001 ...
p02821
A = [1] * (1<<18) B = [1] * (1<<18) def convolve(a, b): def fft(f, w, p): d = n // 2 v = w while d >= 1: u = 1 for i in range(d): for j in range(i, n, 2*d): x = f[j] y = f[j+d] f[j...
k = 34 K = 1<<k nu = lambda L: int("".join([bin(K+a)[-k:] for a in L[::-1]]), 2) st = lambda n: bin(n)[2:] + "0" li = lambda s: [int(a, 2) if len(a) else 0 for a in [s[-(i+1)*k-1:-i*k-1] for i in range(len(B)*2-1)]] N, M = list(map(int, input().split())) A = [int(a) for a in input().split()] B = [0] * 100001 ...
p02821
import sys import bisect input = sys.stdin.readline class Bisect(object): def bisect_max(self, reva, func,M): ok = 0 # exist ng = 10**9 # not exist while abs(ok-ng) > 1: cnt = (ok + ng) // 2 if func(cnt,reva,M): ok = cnt else: ...
import sys import bisect input = sys.stdin.readline class Bisect(object): def bisect_max(self, reva, func,M): ok = 0 # exist ng = 4*(10**5) # not exist while abs(ok-ng) > 1: cnt = (ok + ng) // 2 if func(cnt,reva,M): ok = cnt el...
p02821
import sys import bisect input = sys.stdin.readline class Bisect(object): def bisect_max(self, reva, func,M): ok = 0 # exist ng = 4*(10**5) # not exist while abs(ok-ng) > 1: cnt = (ok + ng) // 2 if func(cnt,reva,M): ok = cnt el...
import sys import bisect input = sys.stdin.readline class Bisect(object): def bisect_max(self, reva, func,M): ok = 0 # exist ng = 4*(10**5) # not exist while abs(ok-ng) > 1: cnt = (ok + ng) // 2 if func(cnt,reva,M): ok = cnt el...
p02821
import sys import bisect input = sys.stdin.readline class Bisect(object): def bisect_max(self, reva, func,M): ok = 0 # exist ng = 4*(10**5) # not exist while abs(ok-ng) > 1: cnt = (ok + ng) // 2 if func(cnt,reva,M): ok = cnt el...
import sys import bisect input = sys.stdin.readline class Bisect(object): def bisect_max(self, reva, func,M): ok = 0 # exist ng = 2*(10**5)+1 # not exist while abs(ok-ng) > 1: cnt = (ok + ng) // 2 if func(cnt,reva,M): ok = cnt ...
p02821
# coding: utf-8 # Your code here! import sys sys.setrecursionlimit(10**6) readline = sys.stdin.readline n,m = [int(i) for i in readline().split()] a = [int(i) for i in readline().split()] a.sort(reverse=True) from heapq import * ans = 0 q = [] cc = 10**12 c = 10**6 heappush(q,(-2*a[0]*cc)) ...
# coding: utf-8 # Your code here! import sys sys.setrecursionlimit(10**6) readline = sys.stdin.readline n,m = [int(i) for i in readline().split()] a = [int(i) for i in readline().split()] #a.sort(reverse=True) a.sort() def check(mid): num = 0 p = 0 for i in range(n-1,-1,-1): ...
p02821
from collections import Counter n, m = list(map(int, input().split())) a = list(map(int, input().split())) MAX = 10 ** 5 c = Counter(a) scores = [0] * (2 * MAX + 1) for k1, v1 in list(c.items()): for k2, v2 in list(c.items()): scores[k1 + k2] += v1 * v2 ans = 0 for i, e in enumerate(scores...
from itertools import accumulate n, m = list(map(int, input().split())) a = list(map(int, input().split())) MAX = 10 ** 5 scores = [0] * (2 * MAX + 1) for e in a: scores[e] += 1 acc = list(accumulate(scores)) def f(x): cnt = 0 for e in a: cnt += n - acc[max(0, x - e - 1)] ...
p02821
import bisect import sys from itertools import accumulate input = sys.stdin.readline def hsnum(ls,x,n): num = 0 for i in range(n): y = x-ls[i] num += n-bisect.bisect_left(ls,y) return num n,m = list(map(int, input().split())) a = list(map(int, input().split())) a.sort() suma = [...
import bisect import sys from itertools import accumulate input = sys.stdin.readline def hsnum(ls,x,n): num = 0 for i in range(n): y = x-ls[i] num += n-bisect.bisect_left(ls,y) return num n,m = list(map(int, input().split())) a = list(map(int, input().split())) a.sort() suma = [...
p02821
def main(): import bisect import sys from itertools import accumulate input = sys.stdin.readline def hsnum(ls,x,n): num = 0 for i in range(n): y = x-ls[i] num += n-bisect.bisect_left(ls,y) return num n,m = list(map(int, input().split()))...
def main(): import bisect import sys from itertools import accumulate input = sys.stdin.readline def hsnum(ls,x): num = 0 for i in range(n): y = x-ls[i] num += n-bisect.bisect_left(ls,y) return num n,m = list(map(int, input().split())) ...
p02821
from bisect import bisect_left from bisect import bisect_right n,m = list(map(int, input().split())) a = list(map(int, input().split())) a = sorted(a) a_count = [0]*110000 a_sum = [0]*110000 for i in range(n): a_count[a[i]] += 1 a_sum[a[i]] += a[i] for i in range(100005,0,-1): a_count[i] ...
from bisect import bisect_left from bisect import bisect_right n,m = list(map(int, input().split())) a = list(map(int, input().split())) a = sorted(a) a_count = [0]*100010 a_sum = [0]*100010 for i in range(n): a_count[a[i]] += 1 a_sum[a[i]] += a[i] for i in range(100005,-1,-1): a_count[i]...
p02821
import bisect N, M = list(map(int, input().split())) A = list(map(int, input().split())) A.sort() rev_A = A[::-1] def check(x, a=A, m=M): result = False n = len(a) count = 0 for ai in a: count += (n - bisect.bisect_left(a, x-ai)) return count>=m left = 0 right = 2*10**5 + 1 mid =...
import bisect N, M = list(map(int, input().split())) Al = list(map(int, input().split())) Al.sort() # 左手で握手する人のリスト Ar = Al.copy()[::-1] # 右手で握手する人のリスト def count_large_2(L1, L2, x): # L1.sort() # L2.sort(reverse=True) N1, N2 = len(L1), len(L2) i = j = 0 c = 0 while i<N1 and j<N2: if L1[...
p02821
import bisect def justM(x,N): sum_=0 for i in range(N): b=bisect.bisect_left(A,x-A[i]) sum_+=N-b return sum_ N,M=list(map(int,input().split())) A=sorted(map(int,input().split())) left,right=-1,10**20 while(right-left>1): mid=(right+left)//2 s=justM(mid,N) if s<M: ...
import bisect def justM(x,N): sum_=0 for i in range(N): b=bisect.bisect_left(A,x-A[i]) sum_+=N-b return sum_ N,M=list(map(int,input().split())) A=sorted(map(int,input().split())) left,right=-1,(10**5)*2+1 while(right-left>1): mid=(right+left)//2 s=justM(mid,N) if s...
p02821
n,m=list(map(int,input().split())) A=sorted(list(map(int,input().split()))) # M回の握手の幸福度最大化 # X以上になる握手を全て採用したときに、M通りを越える # Xの最大値を求める import bisect ok=-1 ng=(10**10)+2 # x以上の握手をm通り以上作れるか def isOk(x,m): cnt=0 # 左手をループする for i in range(len(A)): left=A[i] minright=x-left # minright以上...
n,m=list(map(int,input().split())) A=sorted(list(map(int,input().split()))) # M回の握手の幸福度最大化 # X以上になる握手を全て採用したときに、M通りを越える # Xの最大値を求める import bisect ok=-1 ng=(10**10)+1 # x以上の握手をm通り以上作れるか def isOk(x,m): cnt=0 # 左手をループする for i in range(len(A)): left=A[i] minright=x-left # minright以上...
p02821
N, M = list(map(int, input().split())) A = list(map(int, input().split())) A.sort() dp = [[0 for _ in range(N)] for _ in range(N)] for i in range(N): for j in range(N): dp[i][j] = A[i] + A[j] flat = [] for p in dp: flat.extend(p) flat.sort() print((sum(flat[-M:])))
n, m = list(map(int, input().split())) A = list(map(int, input().split())) A.sort(reverse=True) def naive_main(): hands = [] for i in A: for j in A: hands.append(i + j) hands.sort(reverse=True) print((sum(hands[:m]))) naive_main()
p02821
#!/usr/bin/env python3 import sys import heapq INF = float("inf") def solve(N: int, M: int, A: "List[int]"): A.sort() # print(A) heap = [(-2*a, i, i) for i, a in enumerate(A)] heapq.heapify(heap) ans = 0 while M > 0: score, i, j = heapq.heappop(heap) # print(-...
#!/usr/bin/env python3 import sys from bisect import bisect_left from itertools import accumulate INF = float("inf") def solve(N: int, M: int, A: "List[int]"): A.sort() # 幸福度X以上を得られる握手をカウントし、M以上となる境界を探す # Xが小さい方が握手の個数は多く、Xが大きい方が握手の個数は少ない left = 0 right = 3*(10**5) while right ...
p02821
import bisect import copy n,m=list(map(int,input().split())) A=list(map(int,input().split())) A.sort() ng=-1 ok=A[-1]*2+1 B=[0]*n C=[0]*n while ok-ng>1: mid=(ok+ng)//2 d=0 for i in range(n): c=n-bisect.bisect_right(A,mid-A[i],lo=0,hi=len(A)) B[i]=c d=d+c if d<m: ok=mid C=co...
import bisect import copy n,m=list(map(int,input().split())) A=list(map(int,input().split())) A.sort() ng=-1 ok=A[-1]*2+1 B=[0]*n C=[0]*n while ok-ng>1: mid=(ok+ng)//2 d=0 for i in range(n): c=n-bisect.bisect_right(A,mid-A[i],lo=0,hi=len(A)) B[i]=c d=d+c if d<m: ok=mid C=co...
p02821
import bisect import copy n,m=list(map(int,input().split())) A=list(map(int,input().split())) A.sort() ng=-1 ok=A[-1]*2+1 B=[0]*n C=[0]*n while ok-ng>1: mid=(ok+ng)//2 d=0 for i in range(n): c=n-bisect.bisect_right(A,mid-A[i],lo=0,hi=len(A)) B[i]=c d=d+c if d<m: ok=mid C=co...
import bisect n,m=list(map(int,input().split())) A=list(map(int,input().split())) A.sort() ng=-1 ok=A[-1]*2+1 B=[[0]*n for k in range(2)] ii=0 while ok-ng>1: mid=(ok+ng)//2 d=0 for i in range(n): c=n-bisect.bisect_right(A,mid-A[i],lo=0,hi=len(A)) B[ii][i]=c d=d+c if d<m: ok=mid ...
p02821
from itertools import product n, m = list(map(int, input().split())) a = list(reversed(sorted(list(map(int, input().split()))))) c = len(a) ans = 0 i, j = 0, 0 li = list(product(a, a)) li.sort(key=lambda x: sum(x), reverse=True) for i in range(m): ans += sum(li[i]) print(ans)
from itertools import product n, m = list(map(int, input().split())) a = list(reversed(sorted(list(map(int, input().split()))))) c = len(a) i, j = 0, 0 li = [sum(x) for x in product(a, a)] li.sort(reverse=True) print((sum(li[:m])))
p02821
n,m=list(map(int,input().split())) a=list(map(int,input().split())) a.sort() if n==1: print((a[0]*2)) exit() from bisect import bisect_left,bisect_right #幸福度がx以上がm以上か? def f(x): global n,a ret=0 for i in range(n): ret+=(n-bisect_left(a,x-a[i])) return ret l,r=-1,10**6 whil...
n,m=list(map(int,input().split())) a=list(map(int,input().split())) a.sort() if n==1: print((a[0]*2)) exit() from bisect import bisect_left,bisect_right #幸福度がx以上がm以上か? def f(x): global n,a ret=0 for i in range(n): ret+=(n-bisect_left(a,x-a[i])) return ret #端がやはりコーナーケース(ここまじ...
p02821
# 上からm番目がx # x以上のヒトがm人以上、x+1以上のヒトがm-1人以下 # x以上のヒトがm人以上となる最大のx from bisect import bisect_left from itertools import accumulate n, m = list(map(int, input().split())) A = list(map(int, input().split())) A.sort() ok = 0 ng = 10 ** 15 B = [0] + A[::-1] C = list(accumulate(B)) while ng - ok > 1: mid =...
# 上からm番目がx # x以上のヒトがm人以上、x+1以上のヒトがm-1人以下 # x以上のヒトがm人以上となる最大のx from bisect import bisect_left from itertools import accumulate import sys input = sys.stdin.buffer.readline n, m = list(map(int, input().split())) A = list(map(int, input().split())) A.sort() ok = 0 ng = 3 * (10 ** 5) B = [0] + A[::-1] ...
p02821
import bisect n, m = list(map(int, input().split())) a = list(map(int, input().split())) a.sort() def guchoku(n, m, a): tmp = [] for i in range(n): for j in range(n): tmp.append(a[i]+a[j]) tmp.sort(reverse=True) return sum(tmp[:m]) def C(mid): tmp = [] fo...
import bisect N, M = list(map(int, input().split())) A = list(map(int, input().split())) A.sort() B = [0] * N B[-1] = A[-1] for i in range(N - 2, -1, -1): B[i] = B[i+1]+A[i] def C(mid): tmp = 0 for a in A: pos = bisect.bisect_right(A, mid - a) tmp += N-pos return tmp >...
p02821
import bisect N = int(eval(input())) a = list(map(int,input().split(' '))) a.sort(reverse = True) fathers = [a.pop(0)] for _ in range(N): sons = list() for father in fathers: for index,value in enumerate(a): if father > value: sons.append(a.pop(index)) ...
N = int(eval(input())) A = sorted((int(x) for x in input().split())) parent = [A[-1]] rest = A[:-1] bl = True for _ in range(N): next_parent = [] next_rest = [] for p in parent[::-1]: while rest: x = rest.pop() if x < p: next_parent.append(x) ...
p02920
N = int(eval(input())) a = list(map(int,input().split(' '))) a.sort(reverse = True) fathers = [a.pop(0)] for _ in range(N): sons = [] b = [] for father in fathers: while a: x = a.pop(0) if father > x: sons.append(x) break ...
N = int(eval(input())) a = list(map(int,input().split(' '))) a.sort() fathers = [a.pop()] for _ in range(N): sons = list() b = list() for father in fathers: while a: x = a.pop() if father > x: sons.append(x) break ...
p02920
class MultisetBIT: """ 最大値が小さい数について、その重複を許した集合を管理する。 最大値 maxvalue を受け取り、[0, maxvalue] を管理する。 counter は辞書型かリスト型で、counter[i] が i の個数を持つことを仮定する。 counter が与えられなかった場合は、全て個数 0 で初期化される。 基本的に閉区間で管理する。 内部では受け取った数を +1 したものをindexとして持っているので注意。 """ __slots__ = ["n", "k", "data"] ...
class MultisetBIT: """ 最大値が小さい数について、その重複を許した集合を管理する。 最大値 maxvalue を受け取り、[0, maxvalue] を管理する。 counter は辞書型かリスト型で、counter[i] が i の個数を持つことを仮定する。 counter が与えられなかった場合は、全て個数 0 で初期化される。 基本的に閉区間で管理する。 内部では受け取った数を +1 したものをindexとして持っているので注意。 """ __slots__ = ["n", "k", "data"] ...
p02920
from collections import Counter n = int(eval(input())) a = [int(item) for item in input().split()] cnt = Counter(a) key_num = len(list(cnt.keys())) rank = [0] * key_num keys = list(cnt.keys()) keys.sort(reverse=True) for i, key in enumerate(keys): rank[i] = cnt[key] cur = [0] * key_num cur[0] = 1 fo...
from collections import Counter n = int(eval(input())) a = [int(item) for item in input().split()] cnt = Counter(a) key_num = len(list(cnt.keys())) rank = [0] * key_num keys = list(cnt.keys()) keys.sort(reverse=True) for i, key in enumerate(keys): rank[i] = cnt[key] cur = [0] * key_num cur[0] = 1 fo...
p02920
import sys import bisect input = sys.stdin.readline def main(): N = int(eval(input())) S = [int(s) for s in input().split()] S.sort() cur = [] cur.append(S.pop(-1)) for i in range(N): nxt = [j for j in cur] bl = len(S) for x in cur: bl = bis...
import sys def I(): return(int(sys.stdin.readline())) def LI(): return([int(x) for x in sys.stdin.readline().split()]) def main(): N = I() S = sorted(LI(),reverse=True) flag = [True]*len(S) cur = [] cur.append(S[0]) flag[0] = False for i in range(N): nxt = [x f...
p02920
import sys def I(): return(int(sys.stdin.readline())) def LI(): return([int(x) for x in sys.stdin.readline().split()]) def main(): N = I() S = sorted(LI(),reverse=True) flag = [True]*len(S) cur = [] cur.append(S[0]) flag[0] = False for i in range(N): nxt = [x f...
import sys def I(): return(int(sys.stdin.readline())) def LI(): return([int(x) for x in sys.stdin.readline().split()]) def main(): N = I() S = sorted(LI(),reverse=True) flag = [True]*len(S) cur = [] nxt = [] cur.append(S[0]) nxt.append(S[0]) flag[0] = False ...
p02920
from collections import Counter N = int(eval(input())) S = list(map(int,input().split())) count = Counter(S) S = sorted(S) T = sorted(list(set(S))) M = len(T) pre = {T[i]:T[i-1] if i > 0 else 0 for i in range(M)} l = [S[-1]] count[S[-1]] -= 1 while len(l) < 2**N: for n in l[:]: m = pre[n] ...
from collections import Counter N = int(eval(input())) S = list(map(int,input().split())) #N = 18 #S = list(range(1,2**N+1)) def solve(): global S S += [0] count = Counter(S) S = sorted(S) T = sorted(list(set(S))) M = len(T) pre = {T[i]:T[i-1] if i > 0 else 0 for i in range(M)}...
p02920
from sys import exit, stderr, stdin input = stdin.readline # setrecursionlimit(10**7) from collections import deque import heapq def debug(var, name="hoge"): print(name +":" + str(type(var)) + " = " + repr(var), file=stderr) return def main(): N = int(input()) S = [int(a) for a in input()....
from sys import exit, stderr, stdin input = stdin.readline from collections import defaultdict from sys import stderr def debug(var, name="hoge"): print(name +":" + str(type(var)) + " = " + repr(var), file=stderr) return def main(): N = int(input()) S = [int(a) for a in input().split()] S ...
p02920
import time start = time.time() n = int(eval(input())) s = list(map(int, input().split())) s.sort() s.reverse() flag = True for i in range(n): a = s[0:2 ** i] b = s[2 ** i:2 ** (i + 1)] for j in range(len(a)): if b[j] >= a[j]: flag = False break if flag:...
n = int(eval(input())) s = list(map(int, input().split())) s.sort() a = [s[-1]] b = s[:-1] for i in range(n): a2 = [] b2 = [] for x in a: while b: y = b.pop() if x>y: a2.append(y) break else: b...
p02920
#!/usr/bin/env python3 import sys import math from bisect import bisect_right as br from bisect import bisect_left as bl sys.setrecursionlimit(2147483647) from heapq import heappush, heappop,heappushpop from collections import defaultdict from itertools import accumulate from collections import Counter from...
#!/usr/bin/env python3 import sys import math from bisect import bisect_right as br from bisect import bisect_left as bl sys.setrecursionlimit(2147483647) from heapq import heappush, heappop,heappushpop from collections import defaultdict from itertools import accumulate from collections import Counter from...
p02920
N=int(eval(input())) S=list(map(int,input().split())) S.sort() p = [S.pop(-1)] r = S for i in range(N): for p_ in p[::-1]: for r_ in r[::-1]: if r_ < p_: p.append(r_) r.remove(r_) break p.sort() print(("Yes" if len(r)==0 else "...
N=int(eval(input())) S=list(map(int,input().split())) S.sort() p = [S.pop(-1)] r = S for i in range(N): nr = [] for p_ in p[::-1]: while r: r_ = r.pop() if r_ < p_: p.append(r_) break nr.append(r_) p.sort() r+=n...
p02920
N=int(eval(input())) S=list(map(int,input().split())) S.sort() p = [S.pop(-1)] r = S for i in range(N): nr = [] for p_ in p[::-1]: while r: r_ = r.pop() if r_ < p_: p.append(r_) break nr.append(r_) p.sort() r+=n...
N=int(eval(input())) S=list(map(int,input().split())) S.sort() p=[S[-1]] r=S[:-1] for i in range(N): nr = [] for p_ in p[::-1]: while r: nr.append(r.pop()) if nr[-1]<p_: p.append(nr.pop(-1)) break p.sort() r+=nr[::-1] pri...
p02920
N=int(eval(input())) S=list(map(int,input().split())) S.sort() p=[S[-1]] r=S[:-1] for i in range(N): nr = [] for p_ in p[::-1]: while r: nr.append(r.pop()) if nr[-1]<p_: p.append(nr.pop(-1)) break p.sort() r+=nr[::-1] pri...
N=int(eval(input())) S=list(map(int,input().split())) S.sort() p=S[-1:] r=S[:-1] for i in range(N): nr=[] for p_ in p[::-1]: while r: r_=r.pop() if r_<p_: p.append(r_) break nr.append(r_) p.sort() r+=nr[::-1] pri...
p02920
#!/usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop from bisect import bisect_left, bisect_right import sys, random, itertools, math sys.setrecursionlimit(10**5) input = sys.stdin.readline sqrt = math.sqrt def LI(): return list(map(int, input().split())) def LF(...
#!/usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop from bisect import bisect_left, bisect_right import sys, random, itertools, math sys.setrecursionlimit(10**5) input = sys.stdin.readline sqrt = math.sqrt def LI(): return list(map(int, input().split())) def LF(...
p02920
import sys input = sys.stdin.readline N = int(eval(input())) S = list(map(int, input().split())) S.sort(reverse=True) Closed = [False] * (1<<N) Closed[0] = True for _ in range(N): cnt = 0 cnt_ = 0 s_old = -1 for i, s in enumerate(S): if s_old!=s: cnt += cnt_ ...
N = int(eval(input())) S = list(map(int, input().split())) S.sort(reverse=True) Closed = [False] * (1<<N) Closed[0] = True for j in range(N): num = 1<<j cnt_all = 0 cnt = 0 cnt_ = 0 s_old = -1 for i, s in enumerate(S): if s_old!=s: cnt += cnt_ cnt_ =...
p02920
from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify import math import bisect import random from itertools import permutations, accumulate, combinations, product import sys import string from bisect import bisect_left, bisect_right from math import factorial, ceil, ...
from collections import defaultdict, deque, Counter from heapq import heappush, heappop, heapify from math import gcd import bisect import random from itertools import permutations, accumulate, combinations, product import sys import string from bisect import bisect_left, bisect_right from math import factoria...
p02920
from bisect import bisect_left N = int(eval(input())) S = [int(i) for i in input().split()] def solve() : S.sort() T = [S.pop()] for i in range(N) : T.sort(reverse=True) for j in range(2**i) : if S[0] >= T[j] : return 'No' ...
N = int(eval(input())) S = [int(i) for i in input().split()] def solve() : S.sort() par = [S.pop()] rst = S for _ in range(N) : n_par = [] n_rst = [] for p in par[::-1] : flg = True while rst : if rst[-1] < p : ...
p02920
import sys import collections def main(): N,*S = list(map(int, open(0).read().split())) S = sorted(S) g = [S.pop(-1)] for gen in range(N): g.sort(reverse=True) for p in g[:2**gen]: for si, s in enumerate(S[::-1]): if p > s: ...
## 他の方と処理内容がほぼ同じなのに何でTLEになるか分からないので、他の方のコードでテスト N = int(eval(input())) a = list(map(int,input().split(' '))) a.sort() fathers = [a.pop()] for _ in range(N): sons = list() b = list() for father in fathers: while a: x = a.pop() if father > x: ...
p02920
import sys def main(): N,*S = list(map(int, open(0).read().split())) S = sorted(S) g = [S.pop(-1)] for _ in range(N): g.sort(reverse=True) c = [] for p in g: for si, s in enumerate(S[::-1]): if p > s: c.append(s) ...
import sys def main(): N,*S = list(map(int, open(0).read().split())) S = sorted(S) g = [S.pop(-1)] for _ in range(N): c = [] tmp = [] for p in g: while S: s = S.pop(-1) if p > s: c.append(s) ...
p02920
import sys def main(): N,*S = list(map(int, open(0).read().split())) S = sorted(S) g = [S.pop(-1)] for _ in range(N): c = [] tmp = [] for p in g: while S: s = S.pop(-1) if p > s: c.append(s) ...
import sys def main(): N,*S = list(map(int, open(0).read().split())) S = sorted(S) g = [S.pop(-1)] for _ in range(N): tmp = [] lg = len(g) for p in g[:lg]: while S: s = S.pop(-1) if p > s: g.append(...
p02920
from collections import Counter n = int(eval(input())) a = sorted(list(Counter(list(map(int, input().split()))).items()), reverse=True) if len(a) == 2 ** n: print('Yes') exit(0) g = [0]*(n+1) g[n] = 1 for _, x in a: if sum(g) < x: print('No') break t = [0]*(n+1) for i in range(n, -1, -1):...
from collections import Counter n = int(eval(input())) a = sorted(list(Counter(list(map(int, input().split()))).items()), reverse=True) g = [0]*(n+1) g[n] = 1 for _, x in a: if sum(g) < x: print('No') break t = [0]*(n+1) for i in range(n, -1, -1): if g[i] <= x: t[i-1] = t[i] + g[i] ...
p02920
def i1(): return int(eval(input())) def i2(): return [int(i) for i in input().split()] import sys sys.setrecursionlimit(10**6) n=i1() s=i2() m=10**9+1 import heapq import bisect s.sort() x=[s[-1]] s[-1]=m a="Yes" for i in range(n): y=[] for j in range(1<<i): s.sort() z=bisect.bisect_left(s,...
def i1(): return int(eval(input())) def i2(): return [int(i) for i in input().split()] n=i1() s=i2() m=10**9+1 s.sort(reverse=True) x=[s[0]] s[0]=m a="Yes" for i in range(n): y=[] k=0 for j in range(len(s)): if k>=len(x): break if s[j]<x[k]: y.append(s[j]) s[j]=m k+=1...
p02920
import sys input = sys.stdin.readline n = int(eval(input())) S = sorted(map(int, input().split()), reverse=True) L = [S[0]] used = [False]*(1<<n) used[0] = True for i in range(n): T = [] for l in L: for j, s in enumerate(S): if used[j]: continue if l > s: T.append(s) ...
import sys input = sys.stdin.readline n = int(eval(input())) S = sorted(map(int, input().split()), reverse=True) L = [S[0]] used = [False]*(1<<n) used[0] = True for i in range(n): L.sort(reverse=True) idx = 0 for j, s in enumerate(S): if used[j]: continue if L[idx] > s: L.appen...
p02920
import bisect n = int(eval(input())) s = list(map(int, input().split(" "))) # n = 18 # s = list(reversed(range(1, 2 ** n + 1))) s = sorted(s) from collections import deque def solve(): global s parents = [s[-1]] s.pop() for i in range(n): newparents = list(parents) ...
import bisect n = int(eval(input())) s = list(map(int, input().split(" "))) # n = 18 # s = list(reversed(range(1, 2 ** n + 1))) s = sorted(s) from collections import deque def solve(): global s parents = [s[-1]] s.pop() for i in range(n): newparents = list(parents) ...
p02920
from heapq import heappush, heappop from copy import deepcopy n = int(eval(input())) s = list(map(int, input().split())) left = [] for e in s: heappush(left, -e) mx = heappop(left) made = [] heappush(made, mx) for _ in range(n): made_nxt = deepcopy(made) tmp = [] while made: ...
from heapq import heappush, heappop from copy import deepcopy n = int(eval(input())) s = list(map(int, input().split())) left = [] for e in s: heappush(left, -e) mx = heappop(left) made = [] heappush(made, mx) for _ in range(n): made_prev = deepcopy(made) tmp = [] while made_prev:...
p02920
import heapq N = int(eval(input())) N2 = 2**N S = list(map(int, input().split())) S = [-s for s in S] heapq.heapify(S) slimes = [heapq.heappop(S)] num = 1 for i in range(N): slimes.sort(reverse=True) n = num temp = [] while n and S: s = heapq.heappop(S) if s > slimes[n-1]: slime...
N = int(eval(input())) N2 = 2**N S = list(map(int, input().split())) S.sort(reverse=True) slimes = [S[0]] S[0] = float('inf') num = 1 min_idx = 1 for i in range(N): slimes.sort() n = num idx = min_idx is_continuous = True while n and idx<=N2-1: if S[idx] < slimes[n-1]: slimes.append...
p02920
N = int(eval(input())) N2 = 2**N S = list(map(int, input().split())) S.sort(reverse=True) slimes = [S[0]] S[0] = float('inf') num = 1 min_idx = 1 for i in range(N): slimes.sort() n = num idx = min_idx is_continuous = True while n and idx<=N2-1: if S[idx] < slimes[n-1]: slimes.append...
N = int(eval(input())) N2 = 2**N S = list(map(int, input().split())) S.sort(reverse=True) slimes = [S[0]] INF = S[0] + 1 S[0] = INF num = 1 min_idx = 1 for i in range(N): slimes.sort() n = num idx = min_idx is_continuous = True while n and idx<=N2-1: if S[idx] < slimes[n-1]: slimes...
p02920
import bisect N = int(eval(input())) S = sorted([int(x) for x in input().split()]) Slimes = [S.pop(-1)] flag = True for i in range(1, N + 1): newSlimes = [] for j in Slimes: idx = bisect.bisect_left(S, j) if idx == 0: flag = False break newSlimes...
N = int(eval(input())) N2 = 1 << N S = sorted([int(x) for x in input().split()]) s = [] s.append(S[-1]) S[-1] = -1 flag = True for i in range(N): p = sorted([x for x in s]) Si = N2 - 1 while len(p) > 0 and Si >= 0: if S[Si] == -1: Si -= 1 continue ...
p02920
import sys def bsearch(target, min_i, max_i, func): # func(index) < target <= func(index+1) となるindexを返す if func(max_i) < target: return max_i if target <= func(min_i): return None index = (max_i + min_i)//2 while True: if func(index) < target: if targe...
import sys def b(bs, cs): d = set() bl, cl = len(bs), len(cs) bi, ci = 0, 0 ncs = [] while True: if bs[bi] > cs[ci]: bs.append(cs[ci]) bi += 1 else: ncs.append(cs[ci]) ci += 1 if bi == bl or ci == cl: break if len(bs) != 2 * ...
p02920
N = int(eval(input())) S = list(map(int, input().split())) INF = 10 ** 10 S.sort() parents = [S[-1]] S[-1] = INF from_here = 1 for _ in range(N): checking = from_here first = True parents.sort(reverse=True) for i in parents[:]: while True: if S[-checking] < i and f...
def main(): N = int(eval(input())) S = list(map(int, input().split())) INF = float('inf') S.sort() parents = [S[-1]] S[-1] = INF for _ in range(N): checking = 2 parents.sort(reverse=True) for i in parents[:]: while True: i...
p02920
import sys def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def I(): return int(sys.stdin.buffer.readline()) def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def IR(n): return [I() for i in ran...
import sys def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def I(): return int(sys.stdin.buffer.readline()) def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split() def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8') def IR(n): return [I() for i in ran...
p03957
def main(): from sys import setrecursionlimit, stdin, stderr from os import environ from collections import defaultdict, deque, Counter from math import ceil, floor from itertools import accumulate, combinations, combinations_with_replacement setrecursionlimit(10**6) dbg = (lambda *so...
def main(): from sys import setrecursionlimit, stdin, stderr from os import environ from collections import defaultdict, deque, Counter from math import ceil, floor from itertools import accumulate, combinations, combinations_with_replacement setrecursionlimit(10**6) dbg = (lambda *so...
p02605
from bisect import bisect_left M = 200001 n = int(eval(input())) l = [None] * n ans = 10 ** 18 for i in range(n): x, y, u = input().split() u = ('R', 'D', 'L', 'U').index(u) l[i] = (int(x), int(y), u) for _ in range(4): l.sort() xy = [[] for _ in range(M * 2 - 1)] yx = [[] for _ in r...
from bisect import bisect_left import sys input = sys.stdin.readline M = 200001 n = int(eval(input())) l = [None] * n ans = 10 ** 18 for i in range(n): x, y, u = input().split() u = ('R', 'D', 'L', 'U').index(u) l[i] = (int(x), int(y), u) for _ in range(3): l.sort() xy = [[] for _ in ra...
p02605
import sys from collections import defaultdict import bisect read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines in_n = lambda: int(readline()) in_nn = lambda: list(map(int, readline().split())) in_s = lambda: readline().rstrip().decode('utf-8') in_nl = ...
from collections import defaultdict from bisect import bisect_left INF = 10**18 def calc(data, p, q): ret = INF for k in list(data.keys()): for i in range(len(data[k][p])): now = data[k][p][i] j = bisect_left(data[k][q], now) if j < len(data[k][q]):...
p02605
import sys sys.setrecursionlimit(10**7) readline = sys.stdin.buffer.readline def readstr():return readline().rstrip().decode() def readstrs():return list(readline().decode().split()) def readint():return int(readline()) def readints():return list(map(int,readline().split())) def printrows(x):print(('\n'.join(m...
import sys sys.setrecursionlimit(10**7) readline = sys.stdin.buffer.readline def readstr():return readline().rstrip().decode() def readstrs():return list(readline().decode().split()) def readint():return int(readline()) def readints():return list(map(int,readline().split())) def printrows(x):print(('\n'.join(m...
p02605