input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
from operator import itemgetter n,m = list(map(int,input().split())) AB = [] sAB = [] s = 0 c = 0 for i in range(n): AB.append(list(map(int,input().split()))) sAB = sorted(AB,key=itemgetter(0)) for i,(a,b) in enumerate(sAB): if c + b < m: c += b s += a*b elif c + b >= m:...
n,m=list(map(int,input().split())) ab=[] for i in range(n): a,b=list(map(int,input().split())) ab.append((a,b)) ab.sort(key=lambda x: x[0]) ans=0 t=10**10 for a,b in ab: t=min(b,m) if t<=0:break m-=t ans+=a*t print(ans)
p03103
n,m = list(map(int,input().split())) d = [None]*n for i in range(n): a,b = list(map(int,input().split())) d[i] = [a,b] d.sort(key=lambda x: x[0]) ryoukin = 0 cnt = 0 for lst in d: for i in range(lst[1]): ryoukin += lst[0] cnt += 1 if cnt == m: break ...
n,m = list(map(int,input().split())) d = [None]*n for i in range(n): a,b = list(map(int,input().split())) d[i] = [a,b] d.sort(key=lambda x: x[0]) ryoukin = 0 cnt = 0 for lst in d: for i in range(lst[1]): ryoukin += lst[0] cnt += 1 if cnt == m: break ...
p03103
n,m = list(map(int,input().split())) d = [None]*n for i in range(n): a,b = list(map(int,input().split())) d[i] = [a,b] d.sort(key=lambda x: x[0]) ryoukin = 0 cnt = 0 for lst in d: for i in range(lst[1]): ryoukin += lst[0] cnt += 1 if cnt == m: print(ryouk...
import sys n,m = list(map(int,input().split())) d = [None]*n for i in range(n): a,b = list(map(int,input().split())) d[i] = [a,b] d.sort(key=lambda x: x[0]) ryoukin = 0 cnt = 0 for lst in d: for i in range(lst[1]): ryoukin += lst[0] cnt += 1 if cnt == m: ...
p03103
n, m = list(map(int, input().split())) data = [] for i in range(n): a, b = list(map(int, input().split())) data.append([a, b]) data.sort(key=lambda x: x[0]) #値段でソート prices = [] for p in data: for i in range(p[1]): prices.append(p[0]) print((sum(prices[:m])))
n, m = list(map(int, input().split())) data = [] #値段、個数 for i in range(n): a, b = list(map(int, input().split())) data.append([a, b]) data.sort(key=lambda x: x[0]) #値段でソート ans = 0 for p in data: ans += p[0] * min(m, p[1]) m -= p[1] if m <= 0: break print(ans)
p03103
N , M = list(map(int,input().split())) drink = [] for _ in range(N): a , b = list(map(int,input().split())) drink.append([a,b]) drink.sort() value = 0 hold = 0 while hold != M: if drink[0][1] == 0: drink.pop(0) value += drink[0][0] hold += 1 drink[0][1] -= 1 print(value...
N, M = list(map(int, input().split())) shops = list(list(map(int, input().split())) for _ in range(N)) shops.sort() ans = 0 for value, count in shops: tmp_M = max(M - count , 0) ans += (M - tmp_M) * value M = tmp_M if M == 0: break print(ans)
p03103
from sys import stdin n,m = [int(x) for x in stdin.readline().rstrip().split()] li = [list(map(int,stdin.readline().rstrip().split())) for _ in range(n)] li.sort(key=lambda x:(x[0],x[1])) cost = 0 while m > 0: if li[0][1] > 0: cost += li[0][0] li[0][1] -= 1 m -= 1 else: ...
from sys import stdin n,m = list(map(int,stdin.readline().rstrip().split())) li = [list(map(int,stdin.readline().rstrip().split())) for _ in range(n)] li.sort() yen = 0 for i in li: j,k = i if k >= m: yen += j*m break yen += j*k m -= k print(yen)
p03103
s = input().split() N = int(s[0]) M = int(s[1]) AB = {} for i in range(N): data = input().split() if int(data[0]) in list(AB.keys()): AB[int(data[0])] += int(data[1]) else: AB[int(data[0])] = int(data[1]) AB = sorted(AB.items()) money = 0 num = 0 for A,B in AB: for b in range(B): n...
s = input().split() N = int(s[0]) M = int(s[1]) AB = {} for i in range(N): data = input().split() if int(data[0]) in list(AB.keys()): AB[int(data[0])] += int(data[1]) else: AB[int(data[0])] = int(data[1]) AB = sorted(AB.items()) money = 0 num = 0 for A,B in AB: if num+B < M: money ...
p03103
def main(): n, m = list(map(int, input().split())) ab = [tuple(map(int, input().split())) for _ in range(n)] ab.sort() ans = 0 for i, j in ab: if m < j: ans += i*m break m -= j ans += i*j print(ans) main()
import sys input = sys.stdin.readline def main(): n, m = list(map(int, input().split())) ab = [tuple(map(int, input().split())) for _ in range(n)] ab.sort() ans = 0 for i, j in ab: if m < j: ans += i*m break m -= j ans += i*j ...
p03103
N,M=list(map(int,input().split())) yen=[] hon=[] for i in range(N): a,b=list(map(int,input().split())) yen.append(a) hon.append(b) loop=True pay=0 while loop: mise=yen.index(min(yen)) value=yen[mise] stock=hon[mise] if M>stock: pay+=stock*value else: p...
N,M=list(map(int,input().split())) mises=[] for i in range(N): mises.append(list(map(int,input().split()))) mises.sort() pay=0 for i in mises: if i[1]<M: pay+=i[0]*i[1] M-=i[1] else: pay+=i[0]*M break print(pay)
p03103
def is_prime(x): # Perform prime number discrimination for odd numbers of 3 or more. return pow(2, x - 1, x) == 1 def solve(): n = int(input()) ans = [] print("{}: ".format(n), end='') while n % 2 == 0: n //= 2 ans.append(2) d = 3 while n >= d: ...
def is_prime(x): if x == 1: return False elif x == 2: return True elif x % 2 == 0: return False l = x ** 0.5 n = 3 while n <= l: if x % n == 0: return False n += 2 return True def solve(): n = int(input()) print("...
p02467
def is_prime(x): if x == 1: return False elif x == 2: return True elif x % 2 == 0: return False l = x ** 0.5 n = 3 while n <= l: if x % n == 0: return False n += 2 return True def solve(): n = int(input()) print("...
def is_prime(x): # Perform prime number discrimination for odd numbers of 3 or more. return pow(2, x - 1, x) == 1 def solve(): n = int(input()) print("{}: ".format(n), end='') if is_prime(n): print(n) else: ans = [] while n % 2 == 0: n //= 2 ...
p02467
N = int(input()) n = N m = N ** 0.5 L = [] i = 3 print(str(N) + ': ', end = '') while n%2 == 0: n = n/2 L.append(2) while i <= N: while n%i == 0: n = n/i L.append(i) i += 2 #if len(L) == 0: # L.append(N) print(' '.join(map(str, L)))
N = int(input()) n = N m = N ** 0.5 L = [] i = 3 print(str(N) + ': ', end = '') while n%2 == 0: n = n/2 L.append(2) while i <= m: while n%i == 0: n = n/i L.append(i) i += 2 if n not in L: L.append(int(n)) if 1 in L: L.remove(1) if len(L) == 0: ...
p02467
# -*- coding:utf-8 -*- n = int(input()) print(str(n)+':',end='') array = [] count = 0 while True: for i in range(2,n+1): if n%i == 0 and i != n: array.append(i) n = int(n/i) break if i == n: count += 1 break if count == ...
# -*- coding:utf-8 -*- n = int(input()) print(str(n)+':',end='') array = [] count = 0 while True: if n == 999993031: break for i in range(2,n+1): if n%i == 0 and i != n: array.append(i) n = int(n/i) break if i == n: count +=...
p02467
n = int(eval(input())) orig_n = n res=[] i = 2 half_n = n // 2 root_n = int(n ** (1/2)) is_prime = True while i <= (root_n): if orig_n % i == 0: is_prime = False break while i <= half_n and not(is_prime): if n ==1: break if n % i == 0: n = n // i res...
n = int(eval(input())) orig_n = n res=[] i = 2 half_n = n // 2 root_n = int(n ** (1/2)) is_prime = True while i <= (root_n): if orig_n % i == 0: is_prime = False break i += 1 if is_prime: res.append(orig_n) while i <= half_n and not(is_prime): if n ==1: break if...
p02467
#! -*- coding: utf-8-unix -*- import sys # if __name__=='__main__': # lines = [int(x.strip()) for x in sys.stdin.readlines()] # print lines # n = lines[0] # for i in xrange(n): # # a, b = int(lines[i+1]), int(lines[i+2]) # a, b = lines[2*i+1], lines[2*i+2] # if len(str(a+b)) > 80: # ...
import sys if __name__=='__main__': lines = [int(x.strip()) for x in sys.stdin.readlines() if x != '' and x != '\n'] n = lines[0] for i in range(n): a, b = lines[2*i+1], lines[2*i+2] if len(str(a+b)) > 80: print('overflow') else: print(a+b)
p00015
import math n = int(eval(input())); for i in range(n): num1 = int(eval(input())); num2 = int(eval(input())); total = num1 + num2; if len(str(total)) > 80: print("overflow"); else: print(total);
import math n = int(eval(input())); for i in range(0,n): num1 = int(eval(input())); num2 = int(eval(input())); total = num1 + num2; if len(str(total)) > 80: print("overflow"); else: print(total);
p00015
# AOJ 0015 National Budget # Python3 2018.6.9 bal4u n = int(eval(input())) for i in range(n): a = str(eval(input())) b = str(eval(input())) if len(a) > 80 or len(b) > 80: print('overflow') else: s = str(int(a)+int(b)) if len(s) <= 80: print(s) el...
# AOJ 0015 National Budget # Python3 2018.6.9 bal4u n = int(eval(input())) for i in range(n): s = int(eval(input())) + int(eval(input())) if len(str(s)) <= 80: print(s) else: print('overflow')
p00015
s="overflow" n=eval(input()) for i in range(n): a=input() b=input() if len(a)>80 or len(b)>80: print(s) else: tmp=int(a)+int(b) if len(str(tmp))>80: print(s) else: print(tmp)
m=10**80 n=eval(input()) for i in range(n): s="overflow" a=eval(input()) b=eval(input()) if a<m and b<m: tmp=a+b if tmp<m: s=tmp print(s)
p00015
import sys;sys.setrecursionlimit(10**9) class UnionFind: def __init__(self,n): self.n=[-1]*n self.r=[0]*n self.siz=n def find_root(self,x): if self.n[x]<0: return x else: self.n[x]=self.find_root(self.n[x]) return self.n[x] def unite(self,x,y): x=self.find_ro...
#-----UnionFind-----(0-indexed) import sys;sys.setrecursionlimit(10**9) class UnionFind: def __init__(self,n): self.n=[-1]*n self.r=[0]*n self.siz=n def find_root(self,x): if self.n[x]<0: return x else: self.n[x]=self.find_root(self.n[x]) return self.n[x] def uni...
p03770
import heapq from collections import defaultdict from functools import lru_cache from bisect import bisect_right while True: N, M, C, S, G = list(map(int, input().split())) if N == M == C == S == G == 0: # 駅の数、路線の数10000、鉄道会社の数20、出発駅、目的地駅 break E = [[[] for _ in range(N + 1)] for _ in range(...
import heapq from collections import defaultdict from functools import lru_cache from bisect import bisect_right while True: N, M, C, S, G = list(map(int, input().split())) if N == M == C == S == G == 0: # 駅の数、路線の数10000、鉄道会社の数20、出発駅、目的地駅 break #E = [[[] for _ in range(N + 1)] for _ in ran...
p00763
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(0,-1),(1,0),(0,1),(-1,0)] ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)] def LI(): return [int(x) for x in s...
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(0,-1),(1,0),(0,1),(-1,0)] ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)] def LI(): return [int(x) for x in s...
p01812
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(0,-1),(1,0),(0,1),(-1,0)] ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)] def LI(): return [int(x) for x in s...
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(0,-1),(1,0),(0,1),(-1,0)] ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)] def LI(): return [int(x) for x in s...
p01812
n=int(eval(input())) a=list(map(int,input().split())) k=0 b=[] for i in range(0,n+1): k+=a[i]*pow(2,n-i) b.append(k) if k>pow(2,n): print((-1)) else: current=0 ans=0 for i in range(0,n+1): current+=a[n-i] if i==n: ans+=1 else: s=(pow(2,n)-b[n-i-1])//pow(2,i) i...
n=int(eval(input())) a=list(map(int,input().split())) k=0 N=sum(a) failflag=0 node=1 min1=[] min2=[] for i in range(0,n+1): node=node*2-a[i] if i==0: node=1-a[0] node=min(node,N) min1.append(node+a[i]) if node<0: failflag=1 if failflag==1: print((-1)) else: current=0 for i in...
p02665
import sys input = sys.stdin.readline from collections import * N = int(eval(input())) A = list(map(int, input().split())) low = [0]*(N+1) high = [0]*(N+1) low[N] = A[N] high[N] = A[N] if A[N]>2**N: print((-1)) exit() for i in range(N-1, -1, -1): low[i] = (low[i+1]+1)//2+A[i] high[i]...
import sys input = sys.stdin.readline N = int(eval(input())) A = list(map(int, input().split())) low = [0]*(N+1) high = [0]*(N+1) low[N] = A[N] high[N] = A[N] for i in range(N-1, -1, -1): low[i] = (low[i+1]+1)//2+A[i] high[i] = high[i+1]+A[i] if not low[0]<=1<=high[0]: print((-1)) exi...
p02665
N, *A = list(map(int, open(0).read().split())) cnt = [0] * (N + 1) cnt[0] = 1 for i in range(N): cnt[i + 1] = (cnt[i] - A[i]) * 2 ans = 0 cur = 0 for i in reversed(list(range(N + 1))): cur += A[i] ans += min(cnt[i], cur) if all(cnt[i] >= A[i] for i in range(N + 1)): print(ans) ...
N = int(eval(input())) X = list(map(int, input().split())) cnt = [0] * (N + 1) cnt[0] = 1 for i in range(N): cnt[i + 1] = (cnt[i] - X[i]) * 2 ans = 0 cur = 0 for i in reversed(list(range(N + 1))): cur += X[i] ans += min(cnt[i], cur) if all(cnt[i] >= X[i] for i in range(N + 1)): print...
p02665
n=int(eval(input())) a=list(map(int,input().split())) tot=sum(a) v=[1] for q in a: tot-=q v.append(min(2*(v[-1]-q),tot)) if all([u>=0 for u in v]): print((sum(v))) else: print((-1))
n,*a=list(map(int,open(0).read().split())) tot=sum(a) v=[1] for q in a: tot-=q vt=min(2*(v[-1]-q),tot) if vt<0: print((-1)) exit() v.append(vt) print((sum(v)))
p02665
import sys input = sys.stdin.readline n = int(eval(input())) a = list(map(int, input().split())) pos = [pow(2, i) - a[i] for i in range(n+1)] for i in range(n): pos[i+1] = pos[i] * 2 - a[i+1] for i in range(n+1): if pos[i] < 0: print((-1)) exit() cnt = a[-1] memo = 0 for i in ...
import sys input = sys.stdin.readline n = int(eval(input())) a = list(map(int, input().split())) pos = [0 for i in range(n+1)] pos[0] = 1 - a[0] for i in range(n): pos[i+1] = pos[i] * 2 - a[i+1] for i in range(n+1): if pos[i] < 0: print((-1)) exit() cnt = a[-1] memo = 0 for i...
p02665
N=int(eval(input())) A=list(map(int,input().split()))[::-1] def f(A): nodes=[] for i in list(range(len(A))): t = len(A)-i-1 if i == 0: if 2**t < A[i]: return -1 nodes.append(A[i]) else: tmp = min(nodes[-1] + A[i],2**t) ...
N = int(eval(input())) A = list(map(int, input().split())) def solve(A): ans = 0 leaves = sum(A) prev_node_capacity = 1 for ai in A: if ai > prev_node_capacity: return -1 ans += min(prev_node_capacity, leaves) leaves -= ai prev_node_capacity -...
p02665
# coding: utf-8 def solve(*args: str) -> str: n = int(args[0]) A = list(map(int, args[1].split())) if 0 < A[0]: return '1' if n == 0 and A[0] == 1 else '-1' R = [0]*(n+1) R[0] = 1 for i in range(1, n+1): R[i] = 2*(R[i-1]-A[i-1]) prev = 0 ret = 0 ...
# coding: utf-8 def solve(*args: str) -> str: n = int(args[0]) A = list(map(int, args[1].split())) rem = sum(A) cur = 1 ret = 0 for a in A: cur = min(rem, cur) rem -= a ret += cur cur = 2*(cur-a) if cur < 0: ret = -1 ...
p02665
#!/usr/bin/env python3 import sys def solve(N: int, A: "List[int]"): if N==0: if A[0] == 1: print((1)) else: print((-1)) return prev = 0 leafs = 0 ans = 0 for i, a in enumerate(A): leafs = leafs*2 + a rem = pow...
#!/usr/bin/env python3 import sys def solve(N: int, A: "List[int]"): if N==0: if A[0] == 1: print((1)) else: print((-1)) return s = [0]*(N+1) s[N] = A[N] for i in range(N-1, -1, -1): s[i] = s[i+1] + A[i] leafs = 0 ...
p02665
import math #import numpy as np import queue from collections import deque,defaultdict import heapq from sys import stdin,setrecursionlimit #from scipy.sparse.csgraph import dijkstra #from scipy.sparse import csr_matrix ipt = stdin.readline setrecursionlimit(10**7) def main(): n = int(ipt()) a = [...
import math #import numpy as np import queue from collections import deque,defaultdict import heapq from sys import stdin,setrecursionlimit #from scipy.sparse.csgraph import dijkstra #from scipy.sparse import csr_matrix ipt = stdin.readline setrecursionlimit(10**7) def main(): n = int(ipt()) a = [...
p02665
#!/usr/bin/env python3 import sys import math from bisect import bisect_right as br from bisect import bisect_left as bl from collections import defaultdict from itertools import accumulate from collections import Counter from collections import deque from operator import itemgetter from itertools import p...
#!/usr/bin/env python3 import sys import math from bisect import bisect_right as br from bisect import bisect_left as bl from collections import defaultdict from itertools import accumulate from collections import Counter from collections import deque from operator import itemgetter from itertools import p...
p02665
def main(): N = int( eval(input())) A = list( map( int, input().split())) root = 1 if A[0] == 1: if N == 0: print((1)) else: print((-1)) return B = A[::-1] low = high = B[0] # Low = [] Hight = [high] Low = [low] for b...
#import sys #input = sys.stdin.readline def main(): N = int( eval(input())) A = list( map( int, input().split())) root = 1 if A[0] == 1: if N == 0: print((1)) else: print((-1)) return B = A[::-1] low = high = B[0] # Low = [] ...
p02665
n = int(eval(input())) A = list(map(int, input().split())) if n==0: print((1 if A[0] == 1 else -1)) exit() if A[0]!=0: print((-1)) exit() node1=[0]*(n+1) node1[0]=1 node2=[0]*(n+1) node2[0]=1 node2[-1]=A[-1] for i in range(n): x=(node1[i]-A[i])*2 node1[i+1]=x for i in range(n,0...
n = int(eval(input())) A = list(map(int, input().split())) if n==0: print((1 if A[0] == 1 else -1)) exit() if A[0]>0: print((-1)) exit() if A[-1]>2**n: print((-1)) exit() node1=[0]*(n+1) node1[0]=1 node2=[0]*(n+1) node2[-1]=A[-1] for i in range(n): x=(node1[i]-A[i])*2 if...
p02665
from math import ceil def a(): N = int(eval(input())) A = list(map(int,input().split())) INF = 10**13 if N == 0: if A[0] == 1: return 1 else: return -1 BMAX = [INF] * (N+1) BMIN = [1] * (N+1) a = 1 for i in range(N+1): B...
from math import ceil def a(): N = int(eval(input())) A = list(map(int,input().split())) INF = 10**13 if N == 0: if A[0] == 1: return 1 else: return -1 BMAX = [INF] * (N+1) BMIN = [1] * (N+1) BMAX[0] = 1 - A[0] BMAX[N] = 0 B...
p02665
from collections import defaultdict as dd import bisect from sys import exit n = int(eval(input())) A = list(map(int, input().split())) coutner = 0 dp = dd(int) dp[1] = 1 limit = [0] * (n + 1) total = 0 for i in range(n, -1, -1): total += A[i] limit[i] = total #print(limit) for depth in range(...
from collections import defaultdict as dd import bisect from sys import exit n = int(eval(input())) A = list(map(int, input().split())) coutner = 0 dp = dd(int) dp[1] = 1 limit = [0] * (n + 1) total = 0 for i in range(n, -1, -1): total += A[i] limit[i] = total #print(limit) """ for depth in r...
p02665
N = int(eval(input())) *A, = list(map(int, input().split())) Asum = [a for a in A] for i in range(N-1, -1, -1): Asum[i] += Asum[i+1] ans = 0 v = 1 # 子になれる頂点数 for i in range(N+1): if v < A[i]: ans = -1 break ans += v p = v-A[i] if i < N: if p > Asum[i+1]: ...
N = int(eval(input())) *A, = list(map(int, input().split())) Asum = [a for a in A] for i in range(N-1, -1, -1): Asum[i] += Asum[i+1] ans = 0 v = 1 # 子になれる頂点数 for i in range(N+1): if v < A[i]: ans = -1 break ans += v p = v-A[i] if i < N: v = min(Asum[i+1],...
p02665
def main(): eval(input()) A = list(map(int, input().split())) cusum = [0] * len(A) cusum[-1] = A[-1] if A[0] > 1: print((-1)) return for i in range(len(A)-2, -1, -1): cusum[i] = cusum[i+1] + A[i] pre_node = 1 ans = 1 for i in range(1, len(A)): node = (pre_node - A[i-...
def main(): eval(input()) in_a = list(map(int, input().split())) leaves = [0] * len(in_a) leaves[-1] = in_a[-1] if in_a[0] > 1: print((-1)) return for i in range(len(in_a)-2, -1, -1): leaves[i] = leaves[i+1] + in_a[i] node = 1 total = 1 for i in range(1, len(in_a)): ...
p02665
n = int(eval(input())) a = list(map(int, input().split())) if n == 0 and a[0] == 0: print((-1)) exit() if n != 0 and a[0] >= 1: print((-1)) exit() if n == 0 and a[0] >= 2: print((-1)) exit() ar = list(reversed(a)) #print(ar) com = [ar[0]] for i in range(n): com.append(com[i] +...
n = int(eval(input())) a = list(map(int, input().split())) ar = list(reversed(a)) #print(ar) com = [ar[0]] for i in range(n): com.append(com[i] + ar[i+1]) #print(com) com.reverse() ans = 0 val = 1 for i in range(n+1): ans += min(val,com[i]) val -= a[i] if (i < n and val <= 0) or (i == ...
p02665
def Csum(a): b,c=[0],0 for i in range(len(a)): c+=a[i] b.append(c) return b n=int(eval(input())) a=list(map(int,input().split())) s=Csum(a) su=sum(a) b=1 c=0 for i in range(n+1): c+=b b-=a[i] if b<=0: if i==n and b==0: break print((...
#全体的な方針 #浅いほうから貪欲に頂点を倍化していく #現在見ている段の頂点数がそれより深い段の葉の数を超えると余ってしまう #よって、minで押さえつける必要がある(累積和で高速化が必要) #累積和の定義 def Csum(a): b,c=[],0 for i in range(len(a)): c+=a[i] b.append(c) return b #n:二分木の深さ #a:それぞれの段数の葉の数 #s:aの累積和をとったもの(その深さよりも浅い葉の数) #su:aの総和(全体の葉の数) #b:現在の深さの頂点数 #c:現在ま...
p02665
n = int(eval(input())) a = list(map(int,input().split())) cumsum_a = a.copy() for i in range(n-1, -1, -1): cumsum_a[i] += cumsum_a[i+1] ans = 0 childable_node = 1 for i in range(n + 1): if a[i] > childable_node: ans = -1 break ans += childable_node if i < n: ...
n = int(eval(input())) a = list(map(int,input().split())) cumsum_a = a.copy() for i in range(n-1, -1, -1): cumsum_a[i] += cumsum_a[i+1] ans = 0 node = 1 for i in range(n + 1): if a[i] > node: ans = -1 break ans += node if i < n: node = min(2 * (n...
p02665
n = int(eval(input())) leaves = list(map(int, input().split())) ans = 0 nodes_min = [0] * (n + 1) nodes_max = [0] * (n + 1) nodes_min[n] = leaves[n] nodes_max[n] = leaves[n] for depth in range(n, 0, -1): root_min = nodes_min[depth] // 2 + nodes_min[depth] % 2 nodes_min[depth - 1] = leaves[depth - 1...
n = int(eval(input())) leaves = list(map(int, input().split())) sum_leaves = [0]*(n+1) sum_leaves[0] = leaves[0] for depth in range(n): sum_leaves[depth+1] = sum_leaves[depth] + leaves[depth+1] ans = 0 root = [None] * (n + 1) if n == 0: root[0] = 0 else: root[0] = 1 if root[0] + leaves[0] ...
p02665
(N,) = [int(x) for x in input().split()] A = [int(x) for x in input().split()] numLeafs = A minNonLeafs = [None for i in range(N + 1)] maxNonLeafs = [None for i in range(N + 1)] maxNonLeafs[0] = 1 for i in range(1, N + 1): maxNonLeafs[i] = 2 * maxNonLeafs[i - 1] - numLeafs[i] minNonLeafs[N] = 0 maxNo...
(N,) = [int(x) for x in input().split()] A = [int(x) for x in input().split()] numLeafs = A if not (0 <= numLeafs[0] <= 1): print((-1)) exit() # Bound max non-leaves by how many expansions the previous level can do maxNonLeafs = [None for i in range(N + 1)] maxNonLeafs[0] = 1 - numLeafs[0] for i in...
p02665
import math def main(): depth = int(eval(input())) leaves = [int(x) for x in input().split()] nodes_range = [None] * (depth + 1) for i in range(depth, -1, -1): if i == depth: nodes_range[i] = (leaves[i], leaves[i]) continue min_nodes = math.ceil(nod...
import math def main(): depth = int(eval(input())) leaves = [int(x) for x in input().split()] nodes_range = [None] * (depth + 1) for i in range(depth, -1, -1): if i == depth: nodes_range[i] = (leaves[i], leaves[i]) continue # 枝をなるべくまとめていく。 ...
p02665
from functools import reduce from math import ceil n = int(eval(input())) a = list(map(int, input().split())) bottom_n_list = [(0, 0) for _ in range(n + 1)] bottom_n_list[n] = (a[n], a[n]) for i in range(n-1, -1, -1): leaf_num = a[i] min_a_node_num = ceil(bottom_n_list[i + 1][0] / 2) max_a_node...
from math import ceil n = int(eval(input())) a = list(map(int, input().split())) bottom_n_list = [(0, 0) for _ in range(n + 1)] bottom_n_list[n] = (a[n], a[n]) for i in range(n-1, -1, -1): leaf_num = a[i] min_a_node_num = ceil(bottom_n_list[i + 1][0] / 2) max_a_node_num = min(bottom_n_list[i + 1...
p02665
N=int(eval(input())) A=[int(i) for i in input().split()] a=[1-A[0]] if a[0]<0: print((-1)) exit() for k in range(N): ak=a[-1] a.append(2*ak-A[k+1]) if a[-1]<0: print((-1)) exit() if a[-1]>10**17: break M=len(a) for k in range(N,0,-1): if k-1<M: ...
N=int(eval(input())) A=[int(i) for i in input().split()] #len(A)=N+1 ak=1-A[0] if ak<0: print((-1)) exit() a=[ak] k=0 while(ak>=0 and k<N): ak=2*ak-A[k+1] a.append(ak) if ak<0: print((-1)) exit() if ak>=10**14: #以降に関わらず構成可能 break k+=1 #print...
p02665
N = int(eval(input())) A = list(map(int, input().split())) rng = [[A[N], A[N]]] for i in range(N - 1, -1, -1): merged = (rng[-1][0] + 1) // 2 + A[i] nonmerged = rng[-1][1] + A[i] rng.append([merged, nonmerged]) if rng[-1][0] > 1: print((-1)) exit() rng.reverse() prev_non_leaf = 1 ans...
N = int(eval(input())) A = list(map(int, input().split())) rng = [[A[N], A[N]]] for i in range(N - 1, -1, -1): merged = (rng[-1][0] + 1) // 2 + A[i] nonmerged = rng[-1][1] + A[i] rng.append([merged, nonmerged]) if rng[-1][0] > 1: print((-1)) exit() rng.reverse() prev_non_leaf = 1 ans...
p02665
n = int(eval(input())) a = list(map(int, input().split())) ans = 0 nonleaf_nodes = [] current = 1 for depth in range(0, n+1): c = current-a[depth] if c < 0: # 目いっぱい頂点を増やしても葉が足りない print((-1)) exit() nonleaf_nodes.append(c) current = c*2 nonleaf_nodes[-1]=0 ans = a[-1] ...
n = int(eval(input())) a = list(map(int, input().split())) ans = 0 nonleaf_nodes = [] current = 1 # とりあえず増やせるだけ頂点を増やしていく for depth in range(0, n+1): c = current-a[depth] # 目いっぱい頂点を増やしても葉を賄えない場合、木を作れない if c < 0: print((-1)) exit() nonleaf_nodes.append(c) current = c*2...
p02665
import os from collections import defaultdict N = int(eval(input())) A = list(map(int, input().split(" "))) pow2 = defaultdict(int) ret = 0 lim = [] for i in range(N+1): pow2[i] = 2**i for i in range(N+1): if i == 0: r = 2**i - A[i] else: r = 2*lim[i-1] - A[i] if r ...
import os from collections import defaultdict N = int(eval(input())) A = list(map(int, input().split(" "))) pow2 = defaultdict(int) ret = 0 lim = [0] * (N + 1) # for i in range(N + 1): # pow2[i] = 2 ** i for i in range(N + 1): if i == 0: r = 1 - A[i] else: r = 2 * lim[i ...
p02665
#import numpy as np import sys, math from itertools import permutations, combinations from collections import defaultdict, Counter, deque from math import factorial#, gcd from bisect import bisect_left #bisect_left(list, value) sys.setrecursionlimit(10**7) enu = enumerate MOD = 10**9+7 def input(): return sys....
# import numpy as np import sys, math, heapq from itertools import permutations, combinations from collections import defaultdict, Counter, deque from math import factorial, gcd from bisect import bisect_left, bisect_right sys.setrecursionlimit(10 ** 7) MOD = 10 ** 9 + 7 input = lambda: sys.stdin.readline()[:...
p02665
N = int(eval(input())) A = list(map(int, input().split())) capacity = [1 for k in range(N+1)] cut = 0 for k in range(N+1): #print(capacity) if A[k] >= capacity[k] and k!= N: print((-1)) break if A[k] > capacity[k] and k == N: print((-1)) break if k < N: ...
N = int(eval(input())) A = list(map(int, input().split())) capacity = [1 for k in range(N+1)] cut = 0 for k in range(N+1): #print(capacity) if A[k] >= capacity[k] and k!= N: print((-1)) break if A[k] > capacity[k] and k == N: print((-1)) break if k < N: ...
p02665
N = int(eval(input())) A = list(map(int, input().split())) if N == 0 and A[0] == 1: print((1)) else: nodes = [0]*(N+1) s_d = 0 # 飽和してないノードが残ってる深さ cap_d = 1 - A[0] failed = False for i, a in enumerate(A): # a個の葉をなるべく浅いノードから取る while a > 0: # これ以上分岐することろがなければ...
N = int(eval(input())) A = list(map(int, input().split())) cap = [[0, 0] for _ in range(N+1)] cap[-1] = [A[-1], A[-1]] for i in range(N-1,-1,-1): cap[i][0] = (cap[i+1][0] + 2 - 1)//2 cap[i][1] = cap[i+1][1] + A[i] ans = 1 nodes = 1 failed = nodes < cap[0][0] for i in range(N): nodes = min((nodes ...
p02665
import sys import itertools N = int(eval(input())) A = list(input().split()) A = [int(A[i]) for i in range(N+1)] MAX = [2**i for i in range(N+1)] A_inter = list(reversed(list(itertools.accumulate(reversed(A))))) for i in range(N+1): if A[i] > MAX[i]: print((-1)) sys.exit() elif i ...
import sys import itertools N = int(eval(input())) A = list(input().split()) A = [int(A[i]) for i in range(N+1)] MAX = [1 for i in range(N+1)] A_inter = list(reversed(list(itertools.accumulate(reversed(A))))) for i in range(N+1): if A[i] > MAX[i]: print((-1)) sys.exit() elif i != ...
p02665
N = int(eval(input())) A = list(map(int, input().split())) d = [1] * (N+1) d[0] -= A[0] for i in range(N): d[i+1] = d[i] * 2 - A[i+1] if d[-1] < 0: print((-1)) exit() else: d[-1] = A[-1] for i in range(N, 0, -1): x = d[i] x += A[i-1] if d[i-1] * 2 < x: y = x - A[i-...
N = int(eval(input())) A = list(map(int, input().split())) d = [1] * (N+1) d[0] -= A[0] for i in range(N): d[i+1] = d[i] * 2 - A[i+1] if d[-1] < 0: print((-1)) exit() else: d[-1] = A[-1] for i in range(N, 0, -1): x = d[i] x += A[i-1] d[i-1] = min(x, d[i-1]+A[i-1]) print((s...
p02665
n=int(eval(input())) a=list(map(int,input().split())) s=sum(a)-1 ans,x=0,1 for i in range(n): if a[i]>x-1: print((-1)) exit() ans+=x x-=a[i] if i<n-1 and x<=0: print((-1)) exit() if s>=0: if s>=x: s-=x x=x*2 else: x=(x-s)+s*2 s=0 if x!=a[-1...
#葉の総和-1=分岐の数 n=int(eval(input())) a=list(map(int,input().split())) s=sum(a)-1 ans,x=0,1 for i in range(n): if a[i]>x-1: print((-1)) exit() ans+=x x-=a[i] if i<n-1 and x<=0: print((-1)) exit() if s>=0: if s>=x: s-=x x=x*2 else: x=(x-s)+s*2 ...
p02665
N=int(eval(input())) A=list(map(int,input().split())) node=[1 for _ in range(N+1)] Sn=sum(A) S=[Sn] for s in range(N): S.append(S[s]-A[s+1]) ans=1 for i in range(1,N+1): if node[i-1]*2<=S[i-1]: ans+=node[i-1]*2 node[i]=node[i-1]*2 elif node[i-1]<=S[i-1]<node[i-1]*2: no...
N=int(eval(input())) A=list(map(int,input().split())) node=[1 for _ in range(N+1)] #深さiで、葉にならない頂点の個数node[i] S=sum(A) #生産しなければならない葉の枚数の合計 ans=1 #深さ0である根は最初にカウントする #深さiの時点であと何枚の葉が必要か計算しつつ、必要な分だけノードを生産する #ノードの個数がその時点でのSまで届いたら、あとは各頂点が1つずつノードを生産し、 #各層でA[i]個のノードが生産を止めて葉となる for i in range(1,N+1): if node[i-1...
p02665
n = int(eval(input())) a = list(map(int, input().split())) b = [0]*(n+1) b[0] = 1 for i in range(n): b[i+1] = 2*(b[i] - a[i]) #根から計算した深さごとの要素数の最大値 c = [0]*(n+1) c[n] = a[n] for j in range(n): c[n-1-j] = c[n-j] + a[n-1-j] #葉から計算した最小値 #print(b) #print(c) d = [0]*(n+1) flag = 0 for k in ran...
n = int(eval(input())) a = list(map(int, input().split())) b = [0]*(n+1) b[0] = 1 for i in range(n): b[i+1] = 2*(b[i] - a[i]) c = [0]*(n+1) c[n] = a[n] for j in range(n): c[n-1-j] = c[n-j] + a[n-1-j] d = [0]*(n+1) flag = 0 for k in range(n+1): if b[k] < c[k]: d[k] = b[k] ...
p02665
*a,=list(map(int,[*open(0)][1].split())) c=sum(a) s=b=1 for a in a:c-=a;b=min(c,(b-a)*2);s+=b print((max(-1,s)))
c=sum(a:=[*list(map(int,[*open(0)][1].split()))]) s=b=1 for a in a:b=min(c:=c-a,b-a<<1);s+=b print((max(-1,s)))
p02665
*a,=list(map(int,[*open(0)][1].split())) c=sum(a) s=b=1 for a in a:c-=a;b=min(c,b-a<<1);s+=b print((max(-1,s)))
c=sum(a:=[*list(map(int,[*open(0)][1].split()))]) b=1 print((max(-1,sum((b:=min(c:=c-a,b-a<<1))for a in a)+1)))
p02665
from itertools import accumulate n, *A = list(map(int, open(0).read().split())) if A[0] != 0: if A[0] == 1 and n == 0: print((1)) else: print((-1)) else: B = list(accumulate(A[::-1]))[::-1] + [0] C = [0] * (n+1) D = [0] * (n+1) C[0] = 1 D[0] = 1 for i in ra...
from itertools import accumulate n, *A = list(map(int, open(0).read().split())) if A[0] != 0: if A[0] == 1 and n == 0: print((1)) else: print((-1)) else: B = list(accumulate(A[::-1]))[::-1] C = [1] * (n+1) D = [1] * (n+1) for i in range(1, n+1): C[i] = min(B...
p02665
n = int(eval(input())) a = list(map(int, input().split())) b = a[::-1] if n == 0 and a[0] == 1: print((1)) exit() if n == 0: print((-1)) exit() if a[0] >= 1: print((-1)) exit() ans = [0] * (n + 1) for i in range(n): ans[i + 1] = ans[i] + b[i] ans = ans[::-1...
n = int(eval(input())) a = list(map(int, input().split())) b = a[::-1] if n == 0 and a[0] == 1: print((1)) exit() if a[0] >= 1: print((-1)) exit() ans = [0] * (n + 1) for i in range(n): ans[i + 1] = ans[i] + b[i] ans = ans[::-1] ans = ans[1::] a = a[1::] res = 1 prev_...
p02665
def resolve(): N = int(eval(input())) A = [int(x) for x in input().split(" ")] if N == 0 and A[0] != 1: print((-1)) return True min_maxs = {} min_maxs[len(A)-1] = [A[-1], A[-1]] for index in reversed(list(range(len(A)-1))): min_maxs[index] = [min_maxs[index+1][0] // 2 + A[index], mi...
def resolve(): N = int(eval(input())) A = [int(x) for x in input().split(" ")] if N == 0 and A[0] != 1: print((-1)) return True min_maxs = [0] * len(A) min_maxs[len(A)-1] = [A[-1], A[-1]] for index in reversed(list(range(len(A)-1))): min_maxs[index] = [min_maxs[index+1][0] // 2 + A[in...
p02665
from itertools import accumulate n=int(eval(input())) a=list(map(int,input().split())) c=list(accumulate(a)) b=[0]*(n+1) #大きくしすぎてしまう場合がある #いや上限を考えろや def f(): global n,a,b for i in range(n+1): if i==0: b[0]=min(c[n]-c[0],1) else: b[i]=min(2*(b[i-1]-a[i-1]),c[n...
n,*a=list(map(int,open(0).read().split())) t,v=sum(a),[1] for q in a: v+=[min(2*(v[-1]-q),t:=t-q)] if v[-1]<0:print((-1));exit() print((sum(v)))
p02665
n,*a=list(map(int,open(0).read().split())) t,v,w=sum(a),1,1 for q in a: w=min(2*(w-q),t:=t-q);(v:=v+w) if w<0:print((-1));exit() print(v)
n,*a=list(map(int,open(0).read().split())) t=sum(a);v=w=1 for q in a: w=min(2*(w-q),t:=t-q);(v:=v+w) if w<0:print((-1));exit() print(v)
p02665
n,*a=list(map(int,open(0).read().split())) t=sum(a);v=w=1 for q in a: w=min(2*(w-q),t:=t-q);(v:=v+w) if w<0:print((-1));exit() print(v)
n,*a=list(map(int,open(0).read().split())) t=sum(a);v=w=1 for q in a: if(w:=min(2*(w-q),t:=t-q))<0:print((-1));exit() v+=w print(v)
p02665
n,*a=map(int,open(0).read().split());t=sum(a);v=w=1 for q in a: if(w:=min(2*(w-q),t:=t-q))<0:exit(print(-1)) v+=w print(v)
n,*a=map(int,open(0).read().split());t=sum(a);v=w=1 print(sum([1]+[exit(print(-1)) if(w:=min(2*(w-q),t:=t-q))<0 else w for q in a]))
p02665
p=print;n,*a=map(int,open(0).read().split());t=sum(a);p(sum([w:=1]+[exit(p(-1))if(w:=min(2*(w-q),t:=t-q))<0 else w for q in a]))
p,s=print,sum;n,*a=map(int,open(0).read().split());t=s(a);p(s([w:=1]+[exit(p(-1))if(w:=min(2*(w-q),t:=t-q))<0 else w for q in a]))
p02665
W, H, x, y= list(map(int, input().split())) S = W*H/2 flg = 1 if (x==W/2 and y==H/2) else 0 print(('{:.6f}'.format(S)+" "+str(flg)))
W, H, x, y= list(map(int, input().split())) print(('{:.6f}'.format(W*H/2)+" "+str(1 if (x==W/2 and y==H/2) else 0)))
p03001
# AOJ ITP2_3_C: Count # Python3 2018.6.24 bal4u n = int(eval(input())) a = list(map(int, input().split())) q = int(eval(input())) for i in range(q): b, e, k = list(map(int, input().split())) s = a[b:e] print((s.count(k)))
# AOJ ITP2_3_C: Count # Python3 2018.6.24 bal4u n = int(eval(input())) a = list(map(int, input().split())) q = int(eval(input())) for i in range(q): b, e, k = list(map(int, input().split())) print((a[b:e].count(k)))
p02441
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys def max_heapify(index): # global heap_length left, right, largest = 2 * index, 2 * index + 1, None if left <= heap_length and heap_array[left] > heap_array[index]: largest = left else: largest = index if rig...
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import sys import heapq as hp if __name__ == '__main__': _input = sys.stdin.readlines() heap_array = [] for each in _input: if each.startswith('in'): hp.heappush(heap_array, -1 * int(each[7:])) elif each.startswith('e...
p02289
import sys #@profile def max_heapify(A, i): j = i largest = i H = len(A) -1 while True: l = j * 2 r = l + 1 if l <= H and A[l] > A[j]: largest = l if r <= H and A[r] > A[largest]: largest = r if largest != j: A[j...
import sys #@profile def max_heapify(A, i): j = i largest = i H = len(A) -1 while True: l = j * 2 r = l + 1 if l <= H and A[l] > A[j]: largest = l if r <= H and A[r] > A[largest]: largest = r if largest != j: A[j...
p02289
class PQueue: def __init__(self): self.keys = [] def maxHeapify(self, i): l = i * 2 + 1 r = i * 2 + 2 if l < len(self.keys) and self.keys[l] > self.keys[i]: largest = l else: largest = i if r < len(self.keys) and self.ke...
class PQueue: def __init__(self): self.keys = [] def maxHeapify(self, i): l = i * 2 + 1 r = i * 2 + 2 if l < len(self.keys) and self.keys[l] > self.keys[i]: largest = l else: largest = i if r < len(self.keys) and self.ke...
p02289
class PQueue: def __init__(self): self.keys = [] def maxHeapify(self, i): l = i * 2 + 1 r = i * 2 + 2 if l < len(self.keys) and self.keys[l] > self.keys[i]: largest = l else: largest = i if r < len(self.keys) and self.ke...
def maxHeapify(keys, i): l = i * 2 + 1 r = i * 2 + 2 if l < len(keys) and keys[l] > keys[i]: largest = l else: largest = i if r < len(keys) and keys[r] > keys[largest]: largest = r if largest != i: keys[i], keys[largest] = keys[largest], keys[i] ...
p02289
def maxHeapify(i): l = i * 2 + 1 r = i * 2 + 2 if l < len(A) and A[l] > A[i]: largest = l else: largest = i if r < len(A) and A[r] > A[largest]: largest = r if largest != i: A[i], A[largest] = A[largest], A[i] maxHeapify(largest) def heapInse...
def setHeapDown_max(heap, pos): targetkey = heap[pos] while pos > 0: parentpos = (pos - 1) >> 1 parent = heap[parentpos] if parent < targetkey: heap[pos] = parent pos = parentpos continue break heap[pos] = targetkey def setHeap...
p02289
def setHeapDown_max(heap, pos): targetkey = heap[pos] while pos > 0: parentpos = (pos - 1) >> 1 parent = heap[parentpos] if parent < targetkey: heap[pos] = parent pos = parentpos continue break heap[pos] = targetkey def setHeap...
def setHeapDown_max(heap, pos): targetkey = heap[pos] while pos > 0: parentpos = (pos - 1) >> 1 parent = heap[parentpos] if parent < targetkey: heap[pos] = parent pos = parentpos continue break heap[pos] = targetkey def setHeap...
p02289
import sys def max_heapify(A, i): H = len(A) - 1 l, r, m = i*2, i*2+1, i if l <= H and A[l] > A[i]: m = l if r <= H and A[r] > A[m]: m = r if m != i: A[i], A[m] = A[m], A[i] max_heapify(A, m) def build_max_heap(A): H = len(A) - 1 for i in range(H // 2, 0, -1): max_heapify(A, ...
import sys def max_heapify(A, H, i): target = A[i] m = i l = i * 2 while l <= H: largest = target if A[l] > target: m = l largest = A[m] r = l + 1 if r <= H and A[r] > largest: m = r largest = A[m] if m != i: A[i] = largest i = m ...
p02289
import sys class PriorityQueue: def __init__(self): self.nodes = [] self.num = 0 def max_heapify(self, i): if i >= self.num: return left, right = (i+1)*2-1, (i+1)*2 largest = i if left < self.num and self.nodes[i] < self.nodes[left]: largest = left ...
import sys from heapq import heappush, heappop # わからん pq = [] lines = sys.stdin.readlines() for line in lines: op = line.strip().split(' ') if op[0] == 'insert': heappush(pq, -int(op[1])) elif op[0] == 'extract': print((-heappop(pq))) else: break # import sys, ti...
p02289
import sys class QueueEmptyError(Exception): pass class PriorityQueue: def __init__(self): self._nodes = [] def add(self, v): self._nodes.append(v) i = len(self._nodes) - 1 while i > 0: parent = (i-1) // 2 vp, vi = self._nodes[pare...
import sys def run(): q = [] for line in sys.stdin: if line.startswith("i"): v = int(line[7:]) q.append(v) i = len(q) - 1 while i > 0: p = (i-1) // 2 vp = q[p] if vp < v: ...
p02289
import sys def h(i): l=2*i;r=l+1 if r<=H: if A[i]<A[l]: if A[l]<A[r]:A[i],A[r]=A[r],A[i];h(r) else:A[i],A[l]=A[l],A[i];h(l) elif A[i]<A[r]:A[i],A[r]=A[r],A[i];h(r) elif l<=H and A[i]<A[l]:A[i],A[l]=A[l],A[i];h(l) H=0 A=[0]*2000001 o='' for e in sys.stdin: if'i'==e[0]: H+=1;A[H]=int(e[7:]);...
import sys from heapq import * H=[] O=[] for e in sys.stdin: if'i'==e[0]:heappush(H,-int(e[7:])) elif't'==e[2]:O+=[-heappop(H)] print(("\n".join(map(str,O))))
p02289
import sys from heapq import * H=[] O='' for e in sys.stdin: if'i'==e[0]:heappush(H,-int(e[7:])) elif'x'==e[1]:O+=f'{-heappop(H)}\n' print((O[:-1]))
import sys from heapq import * H,O=[],[] for e in sys.stdin: if'i'==e[0]:heappush(H,-int(e[7:])) elif'x'==e[1]:O+=[-heappop(H)] print(('\n'.join(map(str,O))))
p02289
import sys from heapq import * H=[] O=[] for e in sys.stdin: if'end'==e:break if'i'==e[0]:heappush(H,-int(e[7:])) elif't'==e[2]:O+=[-heappop(H)] print(('\n'.join(map(str,O))))
import sys from heapq import * H=[] O=[] for e in sys.stdin: if'i'==e[0]:heappush(H,-int(e[7:])) elif't'==e[2]:O+=[-heappop(H)] else:print(('\n'.join(map(str,O))))
p02289
import sys INFTY = 1<<30 class MaxHeap(): def __init__(self, a, h): self.a = a self.h = h def parent(self, i): return i//2 def left(self, i): return 2 * i def right(self, i): return 2 * i + 1 def maxHeapify(self, i): ...
import sys INFTY = 1<<30 a = [None] * (2000000 + 1) h = 0 ret = [None] * (2000000 + 1) i = 0 def left(i): return 2 * i def right(i): return 2 * i + 1 def maxHeapify(i): global a, h l = left(i) r = right(i) if l <= h and a[l] > a[i]: largest = l else: la...
p02289
import sys INFTY = 1<<30 a = [None] * (2000000 + 1) h = 0 ret = [None] * (2000000 + 1) i = 0 def left(i): return 2 * i def right(i): return 2 * i + 1 def maxHeapify(i): global a, h l = left(i) r = right(i) if l <= h and a[l] > a[i]: largest = l else: la...
import sys INFTY = 1<<30 a = [None] * 2000001 h = 0 ret = [] def maxHeapify(i): global a, h l = 2 * i r = 2 * i + 1 if l <= h and a[l] > a[i]: largest = l else: largest = i if r <= h and a[r] > a[largest]: largest = r if largest != i: a[i], ...
p02289
import sys from collections import deque INFTY = 1<<30 a = [None] * 2000001 h = 0 ret = deque([]) def maxHeapify(i): global a, h l = 2 * i r = 2 * i + 1 if l <= h and a[l] > a[i]: largest = l else: largest = i if r <= h and a[r] > a[largest]: largest = r ...
from sys import stdin, exit def max_heapify(a, i): heap_size = a[0] L = 2*i R = 2*i + 1 if L <= heap_size and a[L] > a[i]: largest = L else: largest = i if R <= heap_size and a[R] > a[largest]: largest = R if largest != i: a[i], a[largest] = ...
p02289
from sys import stdin, exit def max_heapify(a, i): heap_size = a[0] L = 2*i R = 2*i + 1 if L <= heap_size and a[L] > a[i]: largest = L else: largest = i if R <= heap_size and a[R] > a[largest]: largest = R if largest != i: a[i], a[largest] = ...
from sys import stdin, stdout, exit def max_heapify(a, i): heap_size = a[0] L = 2*i R = 2*i + 1 if L <= heap_size and a[L] > a[i]: largest = L else: largest = i if R <= heap_size and a[R] > a[largest]: largest = R if largest != i: a[i], a[l...
p02289
from sys import stdin, stdout, exit def max_heapify(a, i): heap_size = a[0] L = 2*i R = 2*i + 1 if L <= heap_size and a[L] > a[i]: largest = L else: largest = i if R <= heap_size and a[R] > a[largest]: largest = R if largest != i: a[i], a[l...
import heapq import sys h = [] for com in sys.stdin.readlines(): if com[0] == "i": heapq.heappush(h, -int(com[7:])) elif com[1] == "x": print((-heapq.heappop(h)))
p02289
import sys def max_heapify(a, i): h = len(a) l = 2 * i + 1 r = 2 * i + 2 if l < h and a[l] > a[i]: largest = l else: largest = i if r < h and a[r] > a[largest]: largest = r if largest != i: tmp = a[i] a[i] = a[largest] a[lar...
def extract_max(A): x = A[0] A[0] = A[-1] A.pop() H = len(A) i = 0 while i < H: # ??????????????? v = A[i] rv = 0 lv = 0 r = (i + 1) * 2 l = r - 1 if l < H: lv = A[l] if r < H: rv = A[r] ...
p02289
def extract_max(A): x = A[0] A[0] = A[-1] A.pop() H = len(A) i = 0 while i < H: # ??????????????? v = A[i] rv = 0 lv = 0 r = (i + 1) * 2 l = r - 1 if l < H: lv = A[l] if r < H: rv = A[r] ...
import sys def extract_max(A): x = A[0] A[0] = A[-1] A.pop() H = len(A) i = 0 while i < H: v = A[i] rv = 0 lv = 0 r = (i + 1) * 2 l = r - 1 if l < H: lv = A[l] if r < H: rv = A[r] ...
p02289
def max_heapify(A, i): l = i * 2 r = i * 2 + 1 largest = i if l < len(A): if A[l] > A[largest]: largest = l if r < len(A): if A[r] > A[largest]: largest = r if largest != i: A[i], A[largest] = A[largest], A[i] max_heapify(A, largest) def build_max_heap(A): for i in range(int(len(A) /...
def insert(A, k): i = len(A) A.append(k) while i != 0: j = int((i - 1) / 2) if A[j] < A[i]: A[i], A[j] = A[j], A[i] i = j def extract_max(A): k = A[0] A[0] = A[-1] A.pop() n = len(A) p = 0 while True: c = 2 * p + 1 if c >= n: break if c < n - 1 and A[c] < A[c + 1]: c +...
p02289
import heapq def main(): q = [] heapq.heapify(q) while True: s = input().split() if s[0] == 'end': break elif s[0] == 'insert': heapq.heappush(q, (-int(s[1]), int(s[1]))) else: p = heapq.heappop(q) print((p[1])) ...
import heapq def main(): q = [] heapq.heapify(q) while True: s = input().split() if s[0] == 'end': break elif s[0] == 'insert': heapq.heappush(q, -int(s[1])) else: p = heapq.heappop(q) print((-p)) if __name__ ==...
p02289
def maxHeapify(i, node_list): n = len(node_list) if 2*i+1 < n and node_list[2*i+1] > node_list[i]: i_largest = 2*i+1 # node_list[i], node_list[2*i+1] = node_list[2*i+1], node_list[i] else: i_largest = i if 2*i+2 < n and node_list[2*i+2] > node_list[i_largest]: i_l...
def maxHeapify(i, node_list): n = len(node_list) if 2*i+1 < n and node_list[2*i+1] > node_list[i]: i_largest = 2*i+1 else: i_largest = i if 2*i+2 < n and node_list[2*i+2] > node_list[i_largest]: i_largest = 2*i+2 if i_largest != i: node_list[i], node_list[i...
p02289
import sys readline = sys.stdin.readline def maxHeapify(A, i): l = i * 2 + 1 r = i * 2 + 2 if l < n and A[l] > A[i]: largest = l else: largest = i if r < n and A[r] > A[largest]: largest = r if largest != i: A[i], A[largest] = A[largest], A[i] ...
import sys readline = sys.stdin.readline def maxHeapify(A, i): l = i * 2 + 1 r = i * 2 + 2 if l < n and A[l] > A[i]: largest = l else: largest = i if r < n and A[r] > A[largest]: largest = r if largest != i: A[i], A[largest] = A[largest], A[i] ...
p02289
import sys readline = sys.stdin.readline def maxHeapify(A, i): l = i * 2 + 1 r = i * 2 + 2 if l < n: largest = l if r < n and A[r] > A[l]: largest = r if A[largest] > A[i]: A[i], A[largest] = A[largest], A[i] maxHeapify(A, largest) def ...
import sys readline = sys.stdin.readline def maxHeapify(A, i): l = i * 2 + 1 r = i * 2 + 2 if l < n: largest = l if r < n and A[r] > A[l]: largest = r if A[largest] > A[i]: A[i], A[largest] = A[largest], A[i] maxHeapify(A, largest) def ...
p02289
import sys def maxHeapify(A, i): l = i * 2 + 1 r = i * 2 + 2 if l < n: largest = l if r < n and A[r] > A[l]: largest = r if A[largest] > A[i]: A[i], A[largest] = A[largest], A[i] maxHeapify(A, largest) def insert(S, k): global n ...
import sys from heapq import heapify, heappush, heappop hq = [] S = [] for s in sys.stdin: if s[2] == "s": heappush(hq, -int(s[7:])) elif s[2] == "t": S.append(-heappop(hq)) else: break print(("\n".join(map(str, S))))
p02289
from sys import stdin readline = stdin.readline import heapq def main(): heap = [] while True: line = readline() if line[2] == 't': print((heapq.heappop(heap)[1])) elif line[2] == 's': n = int(line.split()[1]) heapq.heappush(heap, (-n,...
from sys import stdin from heapq import heappop, heappush readline = stdin.readline heap = [] while True: line = readline() if line[1] == 'x':print((-heappop(heap))) elif line[0] == 'i':heappush(heap, -int(line.split()[1])) else:break
p02289
def dw_heap(A,i,length): l = i*2 r = i*2 + 1 largest = i if l < length and A[l] > A[largest]: largest = l if r < length and A[r] > A[largest]: largest = r if largest != i: A[i], A[largest] = A[largest], A[i] dw_heap(A, largest, length) def up_heap(A,i): if i==1...
def dw_heap(A,i,length): l = i*2 r = i*2 + 1 largest = i if l < length and A[l] > A[largest]: largest = l if r < length and A[r] > A[largest]: largest = r if largest != i: A[i], A[largest] = A[largest], A[i] dw_heap(A, largest, length) def up_heap(A,i): if i==1...
p02289
#!/usr/bin/python3 import sys import queue # main q = queue.PriorityQueue() m = 2 * (10 ** 9) while True: s = sys.stdin.readline().split() if s[0].startswith('i'): q.put(m - int(s[1])) elif s[0].startswith('ex'): print((m - q.get())) if s[0] == 'end': exit()
#!/usr/bin/python3 import sys import queue # main q = queue.PriorityQueue() m = 2 * (10 ** 9) S = sys.stdin.readlines() for line in S: s = line.split() if s[0].startswith('i'): q.put(m - int(s[1])) elif s[0].startswith('ex'): print((m - q.get())) if s[0] == 'end': ...
p02289
#!/usr/bin/python3 import sys import queue # main q = queue.PriorityQueue() m = 2 * (10 ** 9) S = sys.stdin.readlines() for line in S: s = line.split() if s[0].startswith('i'): q.put(m - int(s[1])) elif s[0].startswith('ex'): print((m - q.get())) if s[0] == 'end': ...
#!/usr/bin/python3 import sys import heapq # main q = [] S = sys.stdin.readlines() for line in S: if line[0].startswith('i'): heapq.heappush(q, -int(line.split()[1])) elif line[1] == 'x': print((-heapq.heappop(q))) else: exit()
p02289
import sys import heapq heap = [] while True: input_line = sys.stdin.readline() input_line = input_line.strip().split(' ') if input_line[0][0] == 'i' : heapq.heappush(heap,-int(input_line[1])) heapq.heapify(heap) elif input_line[0][1] == 'x' : print((-int(heapq.heappop(heap)))) else : break
import sys import heapq heap = [] while True: input_line = sys.stdin.readline() input_line = input_line.strip().split(' ') if input_line[0][0] == 'i' : heapq.heappush(heap,-int(input_line[1])) elif input_line[0][1] == 'x' : print((-int(heapq.heappop(heap)))) else : break
p02289
# -*- coding: utf-8 -*- from heapq import heappush, heappop A = [] inp = [None] while inp[0] != "end": inp = [n for n in input().split()] if inp[0] == "insert": heappush(A, -int(inp[1])) elif inp[0] == "extract": print((-heappop(A)))
# -*- coding: utf-8 -*- from heapq import heappush, heappop A = [] inp = [None] while inp[0] != "end": inp = input().split() if inp[0] == "insert": heappush(A, -int(inp[1])) elif inp[0] == "extract": print((-heappop(A)))
p02289