input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
import heapq N, M = list(map(int, input().split())) Work = sorted([list(map(int, input().split())) for i in range(N)], key=lambda x: x[0]) ans = 0 can_work = [] i = 0 for m in range(M + 1): while (i < N) and (Work[i][0]) <= m: heapq.heappush(can_work, -Work[i][1]) i += 1 if can_work: ans += -heapq.heappop(can_work) print(ans)
import heapq N, M = list(map(int, input().split())) works = sorted([list(map(int, input().split())) for i in range(N)]) ans = 0 hq = [] i = 0 for limit in range(1, M + 1): while (i < N) and (works[i][0] <= limit): cost, value = works[i] heapq.heappush(hq, -value) i += 1 if hq: ans += -heapq.heappop(hq) print(ans)
p02948
from heapq import* (N,M),*t=[list(map(int,s.split()))for s in open(0)] q=[0]*7**6 v=[[]for _ in q] z=0 for a,b in t:v[a-1]+=[b] for i in v[:M]: for j in i:heappush(q,-j) z-=heappop(q) print(z)
from heapq import* (n,m),*t=[list(map(int,s.split()))for s in open(0)] q=[0]*7**6 v=[[]for _ in q] z=0 for a,b in t:v[a-1]+=[b] for i in v[:m]: for j in i:heappush(q,-j) z-=heappop(q) print(z)
p02948
import sys import collections import heapq input = sys.stdin.readline def main(): N, M = list(map(int, input().split())) AB = [] for _ in range(N): a, b = list(map(int, input().split())) AB.append((a, b)) AB.sort(key=lambda x: x[0]) H = [] I = 0 ans = 0 for m in range(M): for i in range(I, N): if AB[i][0] <= m+1: heapq.heappush(H, -AB[i][1]) I += 1 if H: ans += -heapq.heappop(H) print(ans) if __name__ == '__main__': main()
import sys import collections import heapq input = sys.stdin.readline def main(): N, M = list(map(int, input().split())) AB = [] for _ in range(N): a, b = list(map(int, input().split())) AB.append((a, b)) AB.sort(key=lambda x: x[0]) H = [] I = 0 ans = 0 for m in range(M): for i in range(I, N): if AB[i][0] <= m+1: heapq.heappush(H, -AB[i][1]) I += 1 else: break if H: ans += -heapq.heappop(H) print(ans) if __name__ == '__main__': main()
p02948
# ABC137D - Summer Vacation import sys from heapq import heappop, heappush readline = sys.stdin.buffer.readline heappop_max = lambda heap: -heappop(heap) heappush_max = lambda heap, x: heappush(heap, -x) def main(): N, M = list(map(int, readline().split())) all_jobs = tuple(tuple(map(int, readline().split())) for _ in range(N)) available_jobs = [[] for _ in range(M + 1)] for waiting_days, reward in all_jobs: if waiting_days <= M: available_jobs[waiting_days].append(reward) heap = [] max_rewards = 0 for jobs in available_jobs[1:]: for job in jobs: heappush_max(heap, job) if heap: max_rewards += heappop_max(heap) print(max_rewards) if __name__ == "__main__": main()
# ABC137D - Summer Vacation import sys from heapq import heappop, heappush readline = sys.stdin.buffer.readline heappop_max = lambda heap: -heappop(heap) heappush_max = lambda heap, x: heappush(heap, -x) def main(): N, M = list(map(int, readline().split())) available_jobs = [[] for _ in range(M + 1)] for _ in range(N): waiting_days, reward = list(map(int, readline().split())) if waiting_days <= M: available_jobs[waiting_days].append(reward) heap = [] max_rewards = 0 for jobs in available_jobs[1:]: for job in jobs: heappush_max(heap, job) if heap: max_rewards += heappop_max(heap) print(max_rewards) if __name__ == "__main__": main()
p02948
# ABC137D - Summer Vacation import sys from heapq import heappop, heappush readline = sys.stdin.buffer.readline heappop_max = lambda heap: -heappop(heap) heappush_max = lambda heap, x: heappush(heap, -x) def main(): N, M = list(map(int, readline().split())) available_jobs = [[] for _ in range(M + 1)] for _ in range(N): waiting_days, reward = list(map(int, readline().split())) if waiting_days <= M: available_jobs[waiting_days].append(reward) heap = [] max_rewards = 0 for jobs in available_jobs[1:]: for job in jobs: heappush_max(heap, job) if heap: max_rewards += heappop_max(heap) print(max_rewards) if __name__ == "__main__": main()
# ABC137D - Summer Vacation import sys from heapq import heappop, heappush read = sys.stdin.buffer.read heappop_max = lambda heap: -heappop(heap) heappush_max = lambda heap, x: heappush(heap, -x) def main(): N, M, *all_jobs = list(map(int, read().split())) available_jobs = [[] for _ in range(M + 1)] for waiting_days, reward in zip(*[iter(all_jobs)] * 2): if waiting_days <= M: available_jobs[waiting_days].append(reward) heap = [] max_rewards = 0 for jobs in available_jobs[1:]: for job in jobs: heappush_max(heap, job) if heap: max_rewards += heappop_max(heap) print(max_rewards) if __name__ == "__main__": main()
p02948
import sys input = sys.stdin.readline import heapq N,M = (int(x) for x in input().split()) jobs = [] taken = [0] * (M+1) jappend = jobs.append for i in range(N): A,B = (int(x) for x in input().split()) if A <= M: jappend((B*(-1),A)) heapq.heapify(jobs) for i in range(len(jobs)): temp = heapq.heappop(jobs) if taken[(M-temp[1]+1)//2:M-temp[1]+1].count(0) > 0: searching = M - temp[1] else: if temp[1] == M: searching = 0 elif taken[0:(M-temp[1]+1)//2].count(0) > 0: searching = ((M-temp[1]+1)//2) - 1 else: continue for i in range(searching+1): if taken[searching-i] == 0: taken[searching-i] = temp[0] break print((sum(taken)*(-1)))
import sys input = sys.stdin.readline import heapq N,M = (int(x) for x in input().split()) jobs = [[] for i in range(M)] count = 0 h = [] for i in range(N): A,B = (int(x) for x in input().split()) if A <= M: jobs[A-1].append(B*(-1)) heapq.heapify(h) for i in range(M): if jobs[i]: for job in jobs[i]: heapq.heappush(h,job) if h: count -= heapq.heappop(h) print(count)
p02948
import heapq N,M=list(map(int,input().split())) AB=[list(map(int,input().split())) for _ in range(N)] AB.sort() works=[] heapq.heapify(works) ans=0 for i in range(1,M+1): day=M-i latest=i while AB and AB[0][0]<=latest: a,b=AB.pop(0) heapq.heappush(works,-b) if not works: continue ans-=heapq.heappop(works) print(ans)
import heapq N,M=list(map(int,input().split())) AB=[list(map(int,input().split())) for _ in range(N)] AB.sort(reverse=True) works=[] heapq.heapify(works) ans=0 for i in range(1,M+1): day=M-i latest=i while AB and AB[-1][0]<=latest: a,b=AB.pop() heapq.heappush(works,-b) if not works: continue ans-=heapq.heappop(works) print(ans)
p02948
import heapq N,M=list(map(int,input().split())) AB=[list(map(int,input().split())) for _ in range(N)] AB.sort(reverse=True) works=[] heapq.heapify(works) ans=0 for i in range(1,M+1): day=M-i latest=i while AB and AB[-1][0]<=latest: a,b=AB.pop(-1) heapq.heappush(works,-b) if not works: continue ans-=heapq.heappop(works) print(ans)
import heapq N,M=list(map(int,input().split())) AB=[list(map(int,input().split())) for _ in range(N)] AB.sort(reverse=True) works=[] heapq.heapify(works) ans=0 AB=AB+[[0,0]] for i in range(1,M+1): day=M-i latest=i while len(AB)>=2 and AB[-2][0]<=latest: a,b=AB.pop(-2) heapq.heappush(works,-b) if not works: continue ans-=heapq.heappop(works) print(ans)
p02948
from heapq import heappush, heappop n, m = list(map(int, input().split())) ab = [list(map(int, input().split())) for _ in range(n)] ab.sort() ans = 0 hq = [] for i in range(1, m+1): for a, b in ab: if a == i: heappush(hq, (-b, a)) if len(hq) != 0: ans -= heappop(hq)[0] print(ans)
from heapq import heappush, heappop n, m = list(map(int, input().split())) ab = [list(map(int, input().split())) for _ in range(n)] ab.sort() ans = 0 hq = [] idx = 0 for i in range(1, m+1): for j in range(idx, n): if ab[j][0] == i: heappush(hq, (-ab[j][1], ab[j][0])) idx += 1 else: break if len(hq) != 0: ans -= heappop(hq)[0] print(ans)
p02948
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() ab.reverse() #print(ab) here = 0 ans = 0 pool = [] import heapq for i in range(m+1): while True: if here == n: break elif ab[here][0] == -i: heapq.heappush(pool,-ab[here][1]) here += 1 else: break pool.sort() try: #print(pool) ans -= heapq.heappop(pool) #print(pool) except: pass print(ans)
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() ab.reverse() #print(ab) here = 0 ans = 0 pool = [] import heapq for i in range(m+1): while True: if here == n: break elif ab[here][0] == -i: heapq.heappush(pool,-ab[here][1]) here += 1 else: break try: #print(pool) ans -= heapq.heappop(pool) #print(pool) except: pass print(ans)
p02948
import heapq def main(): 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(reverse = True) q = [] ans = 0 j = N-1 for i in range(M-1, -1, -1): while j >=0 and AB[j][0] <= M-i: a, b = AB[j] heapq.heappush(q, -b) j -= 1 if q: v = -heapq.heappop(q) ans += v return ans if __name__ == '__main__': print((main()))
import heapq def main(): N, M = list(map(int, input().split())) task = [] for _ in range(N): A, B = list(map(int, input().split())) task.append([A, B]) task.sort() hq = [] ans = 0 for i in range(1, M+1): while task and task[0][0]<= i: _, v = heapq.heappop(task) heapq.heappush(hq, -v) if hq: v = heapq.heappop(hq) ans += -v return ans if __name__ == '__main__': print((main()))
p02948
# 3.4.3 import re from collections import deque from functools import reduce from itertools import permutations from math import pi from operator import itemgetter from operator import mul from operator import xor from os import linesep from queue import PriorityQueue from sys import stdin def rline() -> str: return stdin.readline().strip() def rlines(hint: int = 1): ret = ['' for i in range(hint)] for i in range(hint): ret[i] = rline() return ret def htokens(hint: int = 1): lns = rlines(hint) ret = list([ln.split() for ln in lns]) return ret def vtokens(hint: int = 1): m, _ = htokens() wint = len(m) ret = [[None] * hint for i in range(wint)] for y in range(hint): if y != 0: m, _ = htokens() # convert horizontal to vertical x = 0 for v in m: ret[x][y] = v x += 1 # if return value has one and only one element(list), if wint == 1: # then add an empty list. ret.append([]) return ret def rint(radix: int = 10) -> int: return int(rline(), radix) def hints(hint: int = 1, radix: int = 10): ret = htokens(hint) for i in range(len(ret)): ret[i] = list([int(v, radix) for v in ret[i]]) return ret def vints(hint: int = 1, radix: int = 10): ret = vtokens(hint) for i in range(len(ret)): ret[i] = list([int(v, radix) for v in ret[i]]) return ret def mat(hint: int, wint: int): return [[None]*wint for i in range(hint)] def filllist(lst, value: int) -> None: # destructive. for i in range(len(lst)): lst[i] = value def isprime(n: int) -> bool: if n <= 1: return False if n in (2, 3, 5): return True if n % 2 == 0: return False if n % 3 == 0: return False lst = int(n**0.5) f = 5 while f <= lst: if n % f == 0: return False if n % (f + 2) == 0: return False f += 6 return True def strmat(matrix, lnsep: str = linesep, fieldsep: str = ' ') -> str: return lnsep.join([fieldsep.join(map(str, row)) for row in matrix]) def strbool(boolval: int) -> str: return ['No', 'Yes'][boolval] def solve() -> None: # read here (n, m) = hints()[0] ab_s = hints(n) # solve here # delay asc, prize desc ab_s.sort(key=itemgetter(1), reverse=True) ab_s.sort(key=itemgetter(0), reverse=False) ans = 0 tasks = PriorityQueue() for t in range(m + 1): # from (m days after) to today while ab_s and ab_s[0][0] == t: a, b = ab_s.pop(0) tasks.put(-b) if not tasks.empty(): # do task ans -= tasks.get() # print here print(ans) if __name__ == '__main__': solve()
# 3.4.3 import re from collections import deque from functools import reduce from heapq import heappop from heapq import heappush from itertools import permutations from math import pi from operator import itemgetter from operator import mul from operator import xor from os import linesep from sys import stdin def rline() -> str: return stdin.readline().strip() def rlines(hint: int = 1): ret = ['' for i in range(hint)] for i in range(hint): ret[i] = rline() return ret def htokens(hint: int = 1): lns = rlines(hint) ret = list([ln.split() for ln in lns]) return ret def vtokens(hint: int = 1): m, _ = htokens() wint = len(m) ret = [[None] * hint for i in range(wint)] for y in range(hint): if y != 0: m, _ = htokens() # convert horizontal to vertical x = 0 for v in m: ret[x][y] = v x += 1 # if return value has one and only one element(list), if wint == 1: # then add an empty list. ret.append([]) return ret def rint(radix: int = 10) -> int: return int(rline(), radix) def hints(hint: int = 1, radix: int = 10): ret = htokens(hint) for i in range(len(ret)): ret[i] = list([int(v, radix) for v in ret[i]]) return ret def vints(hint: int = 1, radix: int = 10): ret = vtokens(hint) for i in range(len(ret)): ret[i] = list([int(v, radix) for v in ret[i]]) return ret def mat(hint: int, wint: int): return [[None]*wint for i in range(hint)] def filllist(lst, value: int) -> None: # destructive. for i in range(len(lst)): lst[i] = value def isprime(n: int) -> bool: if n <= 1: return False if n in (2, 3, 5): return True if n % 2 == 0: return False if n % 3 == 0: return False lst = int(n**0.5) f = 5 while f <= lst: if n % f == 0: return False if n % (f + 2) == 0: return False f += 6 return True def strmat(matrix, lnsep: str = linesep, fieldsep: str = ' ') -> str: return lnsep.join([fieldsep.join(map(str, row)) for row in matrix]) def strbool(boolval: int) -> str: return ['No', 'Yes'][boolval] def solve() -> None: # read here (n, m) = hints()[0] ab_s = hints(n) # solve here # delay asc, prize desc ab_s.sort(key=itemgetter(1), reverse=True) ab_s.sort(key=itemgetter(0), reverse=False) ans = 0 tasks = [] for t in range(m + 1): # from (m days after) to today while ab_s and ab_s[0][0] == t: a, b = ab_s.pop(0) heappush(tasks, -b) if tasks: # do task ans -= heappop(tasks) # print here print(ans) if __name__ == '__main__': solve()
p02948
# 3.4.3 import re from collections import deque from functools import reduce from heapq import heappop from heapq import heappush from itertools import permutations from math import pi from operator import itemgetter from operator import mul from operator import xor from os import linesep from sys import stdin def rline() -> str: return stdin.readline().strip() def rlines(hint: int = 1): ret = ['' for i in range(hint)] for i in range(hint): ret[i] = rline() return ret def htokens(hint: int = 1): lns = rlines(hint) ret = list([ln.split() for ln in lns]) return ret def vtokens(hint: int = 1): m, _ = htokens() wint = len(m) ret = [[None] * hint for i in range(wint)] for y in range(hint): if y != 0: m, _ = htokens() # convert horizontal to vertical x = 0 for v in m: ret[x][y] = v x += 1 # if return value has one and only one element(list), if wint == 1: # then add an empty list. ret.append([]) return ret def rint(radix: int = 10) -> int: return int(rline(), radix) def hints(hint: int = 1, radix: int = 10): ret = htokens(hint) for i in range(len(ret)): ret[i] = list([int(v, radix) for v in ret[i]]) return ret def vints(hint: int = 1, radix: int = 10): ret = vtokens(hint) for i in range(len(ret)): ret[i] = list([int(v, radix) for v in ret[i]]) return ret def mat(hint: int, wint: int): return [[None]*wint for i in range(hint)] def filllist(lst, value: int) -> None: # destructive. for i in range(len(lst)): lst[i] = value def isprime(n: int) -> bool: if n <= 1: return False if n in (2, 3, 5): return True if n % 2 == 0: return False if n % 3 == 0: return False lst = int(n**0.5) f = 5 while f <= lst: if n % f == 0: return False if n % (f + 2) == 0: return False f += 6 return True def strmat(matrix, lnsep: str = linesep, fieldsep: str = ' ') -> str: return lnsep.join([fieldsep.join(map(str, row)) for row in matrix]) def strbool(boolval: int) -> str: return ['No', 'Yes'][boolval] def solve() -> None: # read here (n, m) = hints()[0] ab_s = hints(n) # solve here # delay asc, prize desc ab_s.sort(key=itemgetter(1), reverse=True) ab_s.sort(key=itemgetter(0), reverse=False) ans = 0 tasks = [] ti = 0 for t in range(m + 1): # from (m days after) to today while ti < n and ab_s[ti][0] == t: a, b = ab_s[ti] ti += 1 heappush(tasks, -b) if tasks: # do task ans -= heappop(tasks) # print here print(ans) if __name__ == '__main__': solve()
# 3.4.3 import re from collections import deque from functools import reduce from heapq import * from itertools import permutations from math import pi from operator import itemgetter from operator import mul from operator import xor from os import linesep def isprime(n: int) -> bool: if n <= 1: return False if n in (2, 3, 5): return True if n % 2 == 0: return False if n % 3 == 0: return False lst = int(n**0.5) f = 5 while f <= lst: if n % f == 0: return False if n % (f + 2) == 0: return False f += 6 return True def strmat(matrix, lnsep: str = linesep, fieldsep: str = ' ') -> str: return lnsep.join([fieldsep.join(map(str, row)) for row in matrix]) def strbool(boolval: int) -> str: return ['No', 'Yes'][boolval] def solve() -> None: # read here n, m, *t = list(map(int, open(0).read().split())) # solve here d = [[] for i in range(m+1)] for i in range(n): a, b = t[2*i], t[2*i+1] if a > m: continue d[a].append(b) ans = 0 tasks = [] for t in range(m + 1): # from (m days after) to today for tasksize in d[t]: heappush(tasks, -tasksize) if tasks: # do task ans -= heappop(tasks) # print here print(ans) if __name__ == '__main__': solve()
p02948
import sys from collections import deque sys.setrecursionlimit(20000000) input = sys.stdin.readline def main(): n, limit = list(map(int, input().split())) jobs = [list(map(int, input().split())) for _ in range(n)] for j in jobs: j.append(True) money = 0 for d in range(1, limit+1): available_jobs = [] for j in jobs: if j[0] <= d: available_jobs.append(j) if available_jobs: available_jobs.sort(key=lambda x: x[1], reverse=True) for aj in available_jobs: if aj[2]: money += aj[1] aj[2] = False break print(money) if __name__ == '__main__': main()
import sys from collections import deque, defaultdict from heapq import heappop, heappush sys.setrecursionlimit(20000000) input = sys.stdin.readline def main(): n, limit = list(map(int, input().split())) jobs = defaultdict(deque) for _ in range(n): d, v = list(map(int, input().split())) jobs[d].append(v) money = 0 available_jobs = [] for d in range(1, limit+1): if d in jobs: for j in jobs[d]: heappush(available_jobs, -j) if available_jobs: money += -heappop(available_jobs) print(money) if __name__ == '__main__': main()
p02948
n,m = list(map(int,input().split())) ab = {i:[] for i in range(m+1)} for i in range(n): a,b = list(map(int,input().split())) if a in ab: ab[a].append(b) else: ab[a] = [b] #print(ab) ans = 0 for i in range(m)[::-1]: rem = m - i if ab[rem]: job = ab[rem] ans += max(job) del ab[rem][job.index(max(job))] if rem < m: ab[rem+1] += ab[rem] #print(ans) print(ans)
n,m = list(map(int,input().split())) ab = {i:[] for i in range(m+1)} for i in range(n): a,b = list(map(int,input().split())) if a in ab: ab[a].append(b) else: ab[a] = [b] #print(ab) ans = 0 for i in range(m)[::-1]: rem = m - i if ab[rem]: job = ab[rem] ans += max(job) del ab[rem][job.index(max(job))] if rem < m: ab[rem+1] += ab[rem] ab[rem] = [] #print(ans) print(ans)
p02948
def main(): from collections import deque n, m, *ab = list(map(int, open(0).read().split())) job = [[] for _ in range(1 + max(ab))] for i, j in zip(*[iter(ab)] * 2): job[i].append(j) ans = 0 t = [] for i in range(1, m + 1): t += job[i] if len(t) == 0: continue else: m = max(t) t.remove(m) ans += m print(ans) if __name__ == '__main__': main()
def main(): import heapq n, m, *ab = list(map(int, open(0).read().split())) l = [[] for _ in range(m + 1)] for i, j in zip(*[iter(ab)] * 2): if i <= m: l[i].append(-j) ans = 0 h = [] for i in range(1, m + 1): g = l[i] for j in g: heapq.heappush(h, j) if len(h) > 0: x = heapq.heappop(h) ans += x print((-ans)) if __name__ == '__main__': main()
p02948
def main(): import heapq n, m, *ab = list(map(int, open(0).read().split())) l = [[] for _ in range(m + 1)] for i, j in zip(*[iter(ab)] * 2): if i <= m: heapq.heappush(l[i], -j) ans = 0 h = [] for i in range(1, m + 1): for j in l[i]: heapq.heappush(h, j) if len(h) > 0: x = heapq.heappop(h) ans += x print((-ans)) if __name__ == '__main__': main()
def main(): from heapq import heappush as sushi from heapq import heappop as tempra n, m, *ab = list(map(int, open(0).read().split())) l = [[] for _ in range(m + 1)] for i, j in zip(*[iter(ab)] * 2): if i <= m: l[i].append(-j) w = [] h = [] for i in range(1, m + 1): for j in l[i]: sushi(h, j) if len(h) > 0: x = tempra(h) w.append(x) print((-sum(w))) if __name__ == '__main__': main()
p02948
import heapq N, M = list(map(int, input().split())) S = [[0] for _ in range(M)] for _ in range(N): a, b = list(map(int, input().split())) if M < a: continue heapq.heappush(S[M-a], -b) ans = heapq.heappop(S[M-1]) for i in range(M-2, -1, -1): for num in S[i+1]: heapq.heappush(S[i], num) ans += heapq.heappop(S[i]) print((-ans))
import heapq N, M = list(map(int, input().split())) S = list() A = [list() for _ in range(M+1)] for _ in range(N): a, b = list(map(int, input().split())) if M < a: continue heapq.heappush(A[a], -b) ans = 0 for i in range(1, M+1): for num in A[i]: heapq.heappush(S, num) if S == []: continue ans -= heapq.heappop(S) print(ans)
p02948
#!/usr/bin/env python3 from collections import deque class PriorityQueue: def __init__(self, size): self.tree = [None] * size self.bottom = 0 def add(self, data): n = self.bottom self.bottom += 1 self.tree[n] = data while n != 0: parent = (n - 1) // 2 if self.tree[parent][1] < self.tree[n][1]: self.tree[parent], self.tree[n] = ( self.tree[n], self.tree[parent], ) n = parent else: return def top(self): return self.tree[0] def size(self): return self.bottom def pop_top(self): rv = self.tree[0] self.tree[0] = self.tree[self.bottom - 1] self.bottom -= 1 n = 0 while True: child1 = 2 * n + 1 if child1 >= self.bottom: return rv child2 = 2 * n + 2 if child2 >= self.bottom: child2 = child1 if ( self.tree[n][1] > self.tree[child1][1] and self.tree[n][1] > self.tree[child2][1] ): return rv elif self.tree[child1][1] > self.tree[child2][1]: self.tree[n], self.tree[child1] = ( self.tree[child1], self.tree[n], ) n = child1 else: self.tree[n], self.tree[child2] = ( self.tree[child2], self.tree[n], ) n = child2 return rv def solve(N, M, A, B): ans = 0 # 日付順にソートする all_works = deque(sorted(zip(A, B), key=lambda x: x[0])) pq = PriorityQueue(N) for d in range(1, M+1): # print('d=', d) # 今日が期限の仕事があれば追加 while all_works and all_works[0][0] == d: work = all_works.popleft() pq.add(work) # print('all_works', all_works) # print('pq=', pq.tree) # 優先キューから最優先の仕事を選択する if pq.size(): work = pq.pop_top() ans += work[1] return ans if __name__ == '__main__': N, M = list(map(int, input().split())) A = [0] * N B = [0] * N for idx in range(N): A[idx], B[idx] = list(map(int, input().split())) ans = solve(N, M, A, B) print(ans)
#!/usr/bin/env python3 from collections import deque import heapq def solve(N, M, A, B): ans = 0 # 日付順にソートする all_works = deque(sorted(zip(A, B), key=lambda x: x[0])) pq = [] for d in range(1, M+1): # print('d=', d) # 今日が期限の仕事があれば追加 while all_works and all_works[0][0] == d: work = all_works.popleft() # 最小値優先なのでマイナスをつける heapq.heappush(pq, -work[1]) if pq: work = heapq.heappop(pq) ans -= work return ans if __name__ == '__main__': N, M = list(map(int, input().split())) A = [0] * N B = [0] * N for idx in range(N): A[idx], B[idx] = list(map(int, input().split())) ans = solve(N, M, A, B) print(ans)
p02948
from heapq import heapify, heappush, heappop N, M = list(map(int, input().split())) AB = [list(map(int, input().split())) for i in range(N)] AB.sort() queue = [] heapify(queue) ans = 0 for i in range(1, M+1): while len(AB) > 0: if AB[0][0] <= i: heappush(queue, -AB.pop(0)[1]) else: break if queue: ans += -heappop(queue) print(ans)
from heapq import heappush, heappop N, M = list(map(int, input().split())) salary = [[] for i in range(M+1)] for i in range(N): A, B = list(map(int, input().split())) if A <= M: salary[A].append(B) queue = [] ans = 0 for i in range(1, M+1): for s in salary[i]: heappush(queue, -s) if queue: ans += -heappop(queue) print(ans)
p02948
# -*- coding: utf-8 -*- from heapq import heappush, heappop n, m = list(map(int, input().split())) # A日後、報酬B a = [] b = [] for i in range(n): ai, bi = list(map(int, input().split())) a.append(ai) b.append(bi) sum = 0 used = [False] * n for i in range(0, m): # A <= iで未使用のものをheapqに入れる hq = [] for j in range(n): # print(a[j], i+1,a[j]<=i+1, not used[j]) if a[j] <= i + 1 and not used[j]: heappush(hq, (-b[j], j, a[j])) val = None if len(hq) > 0: val = heappop(hq) if val is not None: sum += -val[0] used[val[1]] = True print(sum)
# -*- coding: utf-8 -*- from heapq import heappush, heappop n, m = list(map(int, input().split())) # A日後、報酬B workList = {} for i in range(n): a, b = list(map(int, input().split())) if a not in workList: workList[a] = [-b] else: workList[a].append(-b) # print(workList) hq = [] sum = 0 for i in range(1, m + 1): if i in workList: for w in workList[i]: heappush(hq, w) val = None if len(hq) > 0: val = heappop(hq) if val is not None: sum += -val print(sum)
p02948
# -*- coding: utf-8 -*- from heapq import heappush, heappop n, m = list(map(int, input().split())) # A日後、報酬B workList = {} for i in range(n): a, b = list(map(int, input().split())) if a not in workList: workList[a] = [-b] else: workList[a].append(-b) # print(workList) hq = [] sum = 0 for i in range(1, m + 1): if i in workList: for w in workList[i]: heappush(hq, w) val = None if len(hq) > 0: val = heappop(hq) if val is not None: sum += -val print(sum)
# -*- coding: utf-8 -*- from heapq import heappush, heappop n, m = list(map(int, input().split())) # A日後、報酬B # 日付ごとに報酬をリスト化 workList = {} for i in range(n): a, b = list(map(int, input().split())) if a not in workList: workList[a] = [b] else: workList[a].append(b) hq = [] sum = 0 for i in range(1, m + 1): # 日付が短い順にheapに追加 # 最大値を取り出すため正負は逆に if i in workList: for w in workList[i]: heappush(hq, -w) # 最大値を取り出し、正負を戻して加算 if len(hq) > 0: val = heappop(hq) sum += -val print(sum)
p02948
from operator import itemgetter import sys input = sys.stdin.readline N, M = list(map(int,input().split())) # 複数の数字を変数に格納 List = [list(map(int,input().split())) for i in range(N)] # [[1,2],[3,4],[5,6]] List.sort(key=itemgetter(0)) List_S = [] result = 0 p = 0 for i in range(1, M+1): add = 0 while p < N and List[p][0] == i: List_S.append(List[p][1]) p += 1 add = 1 if len(List_S) != 0: if add == 1: List_S.sort() result += List_S.pop() print(result)
from operator import itemgetter import sys input = sys.stdin.readline N, M = list(map(int,input().split())) # 複数の数字を変数に格納 List = [list(map(int,input().split())) for i in range(N)] # [[1,2],[3,4],[5,6]] List.sort(key=itemgetter(0)) List_S = [0] result = 0 p = 0 for i in range(1, M+1): add = 0 add_list = [] while p < N and List[p][0] == i: if List_S[len(List_S)-1] < List[p][1]: List_S.append(List[p][1]) else: add_list.append(List[p][1]) add += 1 p += 1 if len(List_S) != 1: result += List_S.pop() if add > 0: List_S.extend(add_list) List_S.sort() print(result)
p02948
import collections import heapq n, m = list(map(int, input().split())) que = [] for _ in range(n): a, b = list(map(int, input().split())) que.append((-b, a)) que.sort(key=lambda x: x[1]) que = collections.deque(que) h = [] heapq.heapify(h) ans = 0 for i in range(1, m + 1): while len(que) != 0 and que[0][1] <= i: tmp = que.popleft() heapq.heappush(h, tmp) if len(h) == 0: continue tmp = heapq.heappop(h) ans -= tmp[0] print(ans)
import heapq n, m = list(map(int, input().split())) que = [] for _ in range(n): a, b = list(map(int, input().split())) que.append((-b, a)) que.sort(key=lambda x: x[1], reverse=True) def solve(que, m): ans = 0 h = [] heapq.heapify(h) for i in range(1, m + 1): while len(que) != 0 and que[-1][1] <= i: tmp = que.pop() heapq.heappush(h, tmp) if len(h) == 0: continue tmp = heapq.heappop(h) ans -= tmp[0] return ans print((solve(que, m)))
p02948
import heapq n,m=list(map(int,input().split())) arr=[list(map(int,input().split())) for _ in range(n)] arr=sorted(arr,reverse=True,key=lambda x:x[1]) arr=sorted(arr,reverse=True,key=lambda x:x[0]) q=[] tmp=0 cnt=0 for a,b in arr: if tmp!=a: tmp=a cnt=0 if m-tmp<cnt: continue heapq.heappush(q,(-b,a)) cnt+=1 ans=0 cnt=0 box=[0]*(m+1) while len(q)!=0: if cnt>=m: break tmp,date=heapq.heappop(q) if box[date]==0: box[date]=1 ans-=tmp cnt+=1 else: pos=date while 1: if pos==m: break pos+=1 if box[pos]==0: box[pos]=1 ans-=tmp cnt+=1 break print(ans)
import heapq n,m=list(map(int,input().split())) arr=[list(map(int,input().split())) for _ in range(n)] arr2=[[i,0] for i in range(1,m+5)] arr.extend(arr2) arr=sorted(arr,reverse=True,key=lambda x:x[1]) arr=sorted(arr,key=lambda x:x[0]) tmp=arr[0][0] q=[] ans=0 pos=0 while 1: a,b=arr[pos] if a>m: break if a==arr[pos+1][0]: heapq.heappush(q,-b) else: heapq.heappush(q,-b) ans-=heapq.heappop(q) pos+=1 print(ans)
p02948
import heapq n,m=list(map(int,input().split())) arr=[list(map(int,input().split())) for _ in range(n)] arr2=[[i,0] for i in range(1,m+2)] arr.extend(arr2) arr=sorted(arr,reverse=True,key=lambda x:x[1]) arr=sorted(arr,key=lambda x:x[0]) q=[] ans=0 pos=0 while 1: a,b=arr[pos] if a>m: break if a==arr[pos+1][0]: heapq.heappush(q,-b) else: heapq.heappush(q,-b) ans-=heapq.heappop(q) pos+=1 print(ans)
import heapq n,m=list(map(int,input().split())) arr=[list(map(int,input().split())) for _ in range(n)] arr2=[[i,0] for i in range(1,m+2)] arr.extend(arr2) arr=sorted(arr,key=lambda x:x[0]) q=[] ans=0 pos=0 while 1: a,b=arr[pos] if a>m: break if a==arr[pos+1][0]: heapq.heappush(q,-b) else: heapq.heappush(q,-b) ans-=heapq.heappop(q) pos+=1 print(ans)
p02948
import heapq n,m=list(map(int,input().split())) c=[list(map(int,input().split())) for i in range(n)] l=list() v=0 heapq.heapify(l) for i in range(1,m+1): for j in range(n): if c[j][0]==i: heapq.heappush(l,(-1)*c[j][1]) if l: v+=heapq.heappop(l)*(-1) print(v)
import heapq n,m=list(map(int,input().split())) lis=[[] for i in range(m)] for i in range(n): a,b=list(map(int,input().split())) if a-1<m: lis[a-1].append(-b) l=list() v=0 for i in range(m): for j in lis[i]: heapq.heappush(l,j) if l: v+=heapq.heappop(l)*(-1) print(v)
p02948
import heapq N, M = list(map(int, input().split())) jobs = [[] for _ in range(M)] for i in range(N): d, r = list(map(int, input().split())) if d-1 < M: jobs[d-1].append(-r) m = 0 heap = [] for i in range(M): for a in jobs[i]: heapq.heappush(heap, a) if len(heap) > 0: b = heapq.heappop(heap) m += -b print(m)
import heapq def main(): N, M = list(map(int, input().split())) jobs = [[] for _ in range(M)] for i in range(N): d, r = list(map(int, input().split())) if d-1 < M: jobs[d-1].append(-r) m = 0 heap = [] for i in range(M): for a in jobs[i]: heapq.heappush(heap, a) if len(heap) > 0: b = heapq.heappop(heap) m += -b print(m) if __name__ == '__main__': main()
p02948
import heapq N, M = list(map(int, input().split())) job_list = {} ans = 0 kouho = [] for _ in range(N): A, B = list(map(int, input().split())) B = B * (-1) if A in list(job_list.keys()): job_list[A].append(B) else: job_list[A] = [B] for day in range(1, M + 1): try: kouho += job_list[day] heapq.heapify(kouho) except KeyError: pass if len(kouho) != 0: ans += heapq.heappop(kouho) print((ans * (-1)))
import heapq N, M = list(map(int, input().split())) AB_list = [] [heapq.heappush(AB_list, list(map(int, input().split()))) for _ in range(N)] ans = 0 kouho = [] for day in range(1, M + 1): if len(AB_list) != 0: while AB_list[0][0] <= day: heapq.heappush(kouho, heapq.heappop(AB_list)[1]*(-1)) if len(AB_list) == 0: break if len(kouho) != 0: ans += heapq.heappop(kouho) print((ans * (-1)))
p02948
import heapq n, m = list(map(int, input().split())) lis = [] for i in range(m + 1): lis.append([]) for i in range(n): a, b = list(map(int, input().split())) if a <= m: lis[a].append(-b) cnd = [] heapq.heapify(cnd) res = 0 for j in range(1, m + 1): for e in lis[j]: heapq.heappush(cnd, e) if cnd: res += -heapq.heappop(cnd) print(res)
import heapq n, m = list(map(int, input().split())) lis = [[] for _ in range(m + 1)] for i in range(n): a, b = list(map(int, input().split())) if a <= m: lis[a].append(-b) cnd = [] res = 0 for j in range(1, m + 1): for e in lis[j]: heapq.heappush(cnd, e) if cnd: res += -heapq.heappop(cnd) print(res)
p02948
import heapq N, M = list(map(int,input().split())) jobs = [[] for i in range(10 ** 5 + 1)] lst = [] for i in range(N): a, b = list(map(int,input().split())) jobs[a].append((-1) * b) ans = 0 for i in range(1,M+1): lst += jobs[i] if lst: heapq.heapify(lst) k = heapq.heappop(lst) ans += (-1) * k print(ans)
import heapq N, M = list(map(int,input().split())) L = [[] for _ in range(M)] for _ in range(N): A, B = list(map(int,input().split())) try: L[A-1].append(-B) except: pass Q = [] ans = 0 for i in range(M): for j in L[i]: heapq.heappush(Q, j) try: ans -= heapq.heappop(Q) except: pass print(ans)
p02948
from sys import exit, stderr, stdin input = stdin.readline import heapq from collections import defaultdict # setrecursionlimit(10**7) def debug(var, name="hoge"): print(name +":" + str(type(var)) + " = " + repr(var), file=stderr) return def main(): N,M = [int(n) for n in input().split()] # {期限:価値} jobs = defaultdict(list) v = 0 for i in range(N): a,b = [int(n) for n in input().split()] if a <= M: jobs[a].append(-b) cand = [] ans = 0 for i in range(1,M+1): if i in jobs: cand += jobs[i] heapq.heapify(cand) if len(cand) != 0: ans -= cand[0] heapq.heappop(cand) print(ans) if __name__ == "__main__": main()
from sys import exit, stderr, stdin input = stdin.readline import heapq # from collections import defaultdict # setrecursionlimit(10**7) def debug(var, name="hoge"): print(name +":" + str(type(var)) + " = " + repr(var), file=stderr) return def main(): N,M = [int(n) for n in input().split()] # {期限:価値} jobs = [[]]*N v = 0 for i in range(N): jobs[i] = [int(n) for n in input().split()] # 価値降順→期間昇順 jobs = sorted(jobs,key=lambda x:-x[1]) jobs = sorted(jobs,key=lambda x:x[0]) cur = 0 ans = 0 cand = [] for i in range(1,M+1): while(cur < N and i >= jobs[cur][0]): heapq.heappush(cand,-jobs[cur][1]) cur += 1 if len(cand) != 0: ans -= cand[0] heapq.heappop(cand) print(ans) if __name__ == "__main__": main()
p02948
import bisect N,M = list(map(int,input().split())) AB = list(list(map(int,input().split()))for i in range(N)) AB.sort() can = [] p = 0 ans = 0 for i in range(M+1): while p<N and (AB[p][0] <= i) : index = bisect.bisect_left(can,AB[p][1]) can.insert(index,AB[p][1]) p += 1 if can != []: ans += can.pop(-1) print(ans)
import heapq N,M = list(map(int,input().split())) AB = list(list(map(int,input().split()))for i in range(N)) AB.sort() can = [] p = 0 ans = 0 for i in range(M+1): while p<N and (AB[p][0] <= i) : heapq.heappush(can,-AB[p][1]) p += 1 if can != []: ans += heapq.heappop(can) print((-ans))
p02948
from heapq import heappush, heappop import sys input = sys.stdin.readline n, m = list(map(int, input().split())) ab = [tuple(int(i) for i in input().split()) for _ in range(n)] ab.sort() hq = [] result = 0 j = 0 for i in range(1, m + 1): for a, b in ab[j:]: if a > i: break heappush(hq, -b) j += 1 if hq: result -= heappop(hq) print(result)
from heapq import heappush, heappop import sys input = sys.stdin.readline n, m = list(map(int, input().split())) ab = [tuple(int(i) for i in input().split()) for _ in range(n)] ab.sort() hq = [] result = 0 j = 0 for i in range(1, m + 1): while j < n and ab[j][0] <= i: heappush(hq, -ab[j][1]) j += 1 if hq: result -= heappop(hq) print(result)
p02948
from collections import defaultdict N, M = list(map(int, input().split())) d = defaultdict(list) for _ in range(N): A, B = list(map(int, input().split())) d[A].append(B) works = [] for k in range(M, 0, -1): d[k] = sorted(d[k], reverse=True) if M-k+1 >= 1: works.extend(d[k][:M-k+1]) works = sorted(works, reverse=True) works = works[:M-k+1] # print(works) works = sorted(works, reverse=True) print((sum(works[:M])))
# from collections import defaultdict import heapq N, M = list(map(int, input().split())) W = [] for _ in range(N): W.append(tuple(map(int, input().split()))) W = sorted(W, key=lambda x: (x[0], -x[1])) a = [] heapq.heapify(a) # リストaのheap化 ans = 0 c = 0 for day in range(1, M+1): day_c = 0 while c != N and W[c][0] == day: if day_c <= M-day: x = W[c][1] heapq.heappush(a, -x) # heap化されたリストaに要素xを追加 day_c += 1 c += 1 if len(a) >= 1: ans += -heapq.heappop(a) # heap化されたリストaから最小値を削除&その最小値を出力 print(ans)
p02948
import heapq N, M = list(map(int, input().split())) W = [] # 取り組める仕事のリスト for _ in range(N): W.append(tuple(map(int, input().split()))) W = sorted(W, key=lambda x: (x[0], -x[1])) a = [] heapq.heapify(a) # リストaのheap化 ans = 0 c = 0 for day in range(1, M+1): day_c = 0 while c != N and W[c][0] == day: if day_c <= M-day: x = W[c][1] heapq.heappush(a, -x) # heap化されたリストaに要素xを追加 day_c += 1 c += 1 if len(a) >= 1: ans += -heapq.heappop(a) # heap化されたリストaから最小値を削除&その最小値を出力 print(ans)
import heapq N, M = list(map(int, input().split())) AB = [] for _ in range(N): A, B = list(map(int, input().split())) AB.append((A, B)) AB = sorted(AB, key=lambda x: x[0]) hq = [] heapq.heapify(hq) # リストhqのheap化 ans = 0 i = 0 for d in range(1, M+1): while i <= N-1 and AB[i][0] <= d: heapq.heappush(hq, -AB[i][1]) # heap化されたリストhqに要素xを追加 i += 1 if hq: ans -= heapq.heappop(hq) # heap化されたリストhqから最小値を削除&その最小値を出力 print(ans)
p02948
import heapq as hq n,m=list(map(int,input().split())) a=[list(map(int,input().split())) for i in range(n)] for i in range(n): a[i][1]*=-1 hq.heapify(a) k=1 ans=0 q=0 while a and k<=m: p=hq.heappop(a) if p[0]>k: hq.heappush(a,p) k+=1 elif p[0]==k: ans-=p[1] k+=1 elif p[0]<k: p[0]=k hq.heappush(a,p) print(ans)
import heapq as hq n,m=list(map(int,input().split())) a=[list(map(int,input().split())) for i in range(n)] a.sort() for i in range(n): a[i][1]*=-1 b=[] hq.heapify(b) k,ans,i=1,0,0 while k<=m: while i<n and a[i][0]<=k: hq.heappush(b,a[i][1]) i+=1 if b: ans-=hq.heappop(b) k+=1 print(ans)
p02948
import heapq as hq n,m=list(map(int,input().split())) a=[list(map(int,input().split())) for i in range(n)] a.sort() for i in range(n): a[i][1]*=-1 b=[] hq.heapify(b) k,ans,i=1,0,0 while k<=m: while i<n and a[i][0]<=k: hq.heappush(b,a[i][1]) i+=1 if b: ans-=hq.heappop(b) k+=1 print(ans)
import heapq as hq n,m=list(map(int,input().split())) a=[list(map(int,input().split())) for i in range(n)] a.sort() for i in range(n): a[i][1]*=-1 b=[] hq.heapify(b) ans,i=0,0 for k in range(m-1,-1,-1): while i<n and a[i][0]+k<=m: hq.heappush(b,a[i][1]) i+=1 if b: ans-=hq.heappop(b) print(ans)
p02948
from bisect import insort_left as sort import sys def input(): return sys.stdin.readline() n,m = list(map(int,input().split())) list = [[int(i) for i in input().split()] for i in range(n)] temp = [0] ans = 0 a = 0 list = sorted(list) for i in range(1,1+m): for j in range(a,len(list)): if list[j][0] == i: sort(temp,list[j][1]) else: a = j break if len(temp) > 0: ans += temp.pop() print(ans)
import heapq as hp import sys def input(): return sys.stdin.readline() n,m = list(map(int,input().split())) list = [[int(i) for i in input().split()] for i in range(n)] temp = [0] ans = 0 a = 0 j = 0 list = sorted(list) for i in range(1,1+m): for j in range(a,len(list)): if list[j][0] == i: hp.heappush(temp,-list[j][1]) else: a = j break if len(temp) > 0: ans -= hp.heappop(temp) print(ans)
p02948
L=lambda:list(map(int,input().split()));n,m=L();t=sorted([L()for _ in range(n)]+[[float("inf")]]);import heapq as q;h=[];a=j=0 for i in range(t[0][0],m+1): while t[j][0]==i:q.heappush(h,-t[j][1]);j+=1 if h:a-=q.heappop(h) print(a)
n,m=list(map(int,input().split())) ab=[list(map(int,input().split()))for _ in range(n)]+[[100000000,0]] for i in range(m):ab.append([i+1,0]) ab.sort() from heapq import heappop,heappush h=[] j=0 ans=0 for i in range(1,m+1): while ab[j][0]<=i: heappush(h,-ab[j][1]) j+=1 ans-=heappop(h) print(ans)
p02948
from heapq import heappop,heappush n,m = list(map(int,input().split())) AB = [[int(i) for i in input().split()] for j in range(n)] #ab.sort() AB = sorted(AB, key=lambda x: x[0]) #print(ab) tmp = 0 hq = [] ans = 0 for i in range(1,m+1): while tmp <= n-1 and AB[tmp][0] <= i: heappush(hq,-AB[tmp][1]) tmp += 1 if len(hq) > 0: ans += -heappop(hq) print(ans)
import sys input = sys.stdin.readline import heapq import bisect n,m = list(map(int,input().split())) ab = [[int(i) for i in input().split()] for j in range(n)] ab.sort() a_li = [-1] * n b_li = [-1] * n for i in range(n): a_li[i] = ab[i][0] b_li[i] = ab[i][1] kouho = [] bfr_idx = 0 ans = 0 for i in range(m): day = i + 1 idx = bisect.bisect_right(a_li,day) for j in range(idx-bfr_idx): heapq.heappush(kouho,-b_li[bfr_idx+j]) bfr_idx = idx if len(kouho) != 0: ans += -heapq.heappop(kouho) print(ans)
p02948
import heapq class Reverse: def __init__(self, val): self.val = val def __lt__(self, other): return self.val > other.val def __repr__(self): return repr(self.val) class Heapq: def __init__(self, arr, desc=False): if desc: for i in range(len(arr)): arr[i] = Reverse(arr[i]) self.desc = desc self.hq = arr heapq.heapify(self.hq) def pop(self): if len(self.hq) == 0: return None if self.desc: return heapq.heappop(self.hq).val else: return heapq.heappop(self.hq) def push(self, a): if self.desc: heapq.heappush(self.hq, Reverse(a)) else: heapq.heappush(self.hq, a) def top(self): if self.desc: return self.hq[0].val else: return self.hq[0] def __len__(self): return len(self.hq) n, m = list(map(int, input().split(' '))) ab = [] for _ in range(n): ab.append(list(map(int, input().split(' ')))) ans = 0 h = Heapq([], desc=True) for i in range(1, m+1): for j in [x for x in ab if x[0] == i]: h.push(j[1]) ab = list([x for x in ab if x[0] != i]) if len(h) == 0: continue ans += h.pop() print(ans)
import heapq class Reverse: def __init__(self, val): self.val = val def __lt__(self, other): return self.val > other.val def __repr__(self): return repr(self.val) class Heapq: def __init__(self, arr, desc=False): if desc: for i in range(len(arr)): arr[i] = Reverse(arr[i]) self.desc = desc self.hq = arr heapq.heapify(self.hq) def pop(self): if len(self.hq) == 0: return None if self.desc: return heapq.heappop(self.hq).val else: return heapq.heappop(self.hq) def push(self, a): if self.desc: heapq.heappush(self.hq, Reverse(a)) else: heapq.heappush(self.hq, a) def top(self): if self.desc: return self.hq[0].val else: return self.hq[0] def __len__(self): return len(self.hq) n, m = list(map(int, input().split(' '))) ab = [] for _ in range(n): a, b = list(map(int, input().split(' '))) if a > m: continue ab.append((a, b)) ab.sort(reverse=True) ans = 0 h = Heapq([], desc=True) for i in range(1, m+1): while ab and ab[-1][0] <= i: a, b = ab.pop() h.push(b) if h: ans += h.pop() print(ans)
p02948
from operator import itemgetter a,b=list(map(int,input().split())) c=[list(map(int,input().split())) for i in range(a)] c.sort() d=[] sum=0 e=0 for i in range(1,b+1): for j in range(e,a): if c[j][0]<=i: d.append(c[j][1]) else: e=j break d.sort() if len(d)>0: sum+=d[-1] d.pop() print(sum)
import heapq a,b=list(map(int,input().split())) c=[list(map(int,input().split())) for i in range(a)] c.sort() d=[[0]for i in range(b)] sum=0 for i,j in c: if i<=b: d[i-1].append(j) e=[] for i in d: for j in i: heapq.heappush(e,-j) sum-=heapq.heappop(e) print(sum)
p02948
import heapq def main(): n, m = list(map(int, input().split())) a = [list(map(int, input().split())) for i in range(n)] a_t = list(zip(*a)) a_s = set(a_t[0]) cnt = 0 h = [(l[1]*-1) for l in a if l[0] == 1] heapq.heapify(h) if 1 in a_s: cnt += heapq.heappop(h) for i in range(2, m+1): if i in a_s: p = [(l[1]*-1) for l in a if l[0] == i] for j in p: heapq.heappush(h, j) if h !=[]: cnt += heapq.heappop(h) print((-1*cnt)) if __name__ == '__main__': main()
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import heapq def main(): n, m = list(map(int, input().split())) ab = [list(map(int, input().split())) for i in range(n)] a_t = list(zip(*ab)) AB = [[] for _ in range(m+1)] for a, b in zip(*a_t): if a <= m: AB[a].append(b*-1) # print(AB) cnt = 0 h = [] heapq.heapify(h) for i in AB: for j in i: heapq.heappush(h, j) # print('h', h) if h: cnt += heapq.heappop(h) print((-1*cnt)) if __name__ == '__main__': main()
p02948
import heapq n,m = list(map(int, input().split())) job_list = [] for i in range(n): a,b = list(map(int, input().split())) job_list.append((a,b)) job_list.sort(key=lambda x:x[0]) # print(job_list) hq = [] max_income = 0 j = 0 for i in range(1,m+1): while j < n and job_list[j][0] <= i: heapq.heappush(hq, (-job_list[j][1],job_list[j][0])) j += 1 if len(hq) < 1: continue b_i, a_i = heapq.heappop(hq) max_income += -b_i print(max_income)
import sys import heapq input = sys.stdin.readline n,m = list(map(int, input().split())) job_list = [] for i in range(n): a,b = list(map(int, input().split())) job_list.append((a,b)) job_list.sort(key=lambda x:x[0]) # print(job_list) hq = [] max_income = 0 j = 0 for i in range(1,m+1): while j < n and job_list[j][0] <= i: heapq.heappush(hq, (-job_list[j][1],job_list[j][0])) j += 1 if len(hq) < 1: continue b_i, a_i = heapq.heappop(hq) max_income += -b_i print(max_income)
p02948
# 2019-11-16 14:51:41(JST) import sys import collections # import math # from string import ascii_lowercase, ascii_uppercase, digits # from bisect import bisect_left as bi_l, bisect_right as bi_r # import itertools # from functools import reduce # import operator as op # from scipy.misc import comb # float # import numpy as np import heapq def main(): # 10**5連勤とか超人すぎる n, m = [int(x) for x in sys.stdin.readline().split()] job_list = dict((i, list()) for i in range(1, m+1)) for _ in range(n): a, b = [int(x) for x in sys.stdin.readline().split()] if a <= m: job_list[a].append(b) schedule = [0 for _ in range(m+1)] # 当日は報酬0 q = [] for i in range(1, m+1): for reward in job_list[i]: # 期限i日前にできる仕事の報酬一覧をheqp queueに入れる heapq.heappush(q, -reward) # あとで最大値を取り出すために-1かける(heappop()で取り出せるのは最小値) if q: # その日できる仕事が一つでもあれば # 報酬の最小値(マイナスなので元々の最大値)を取り出して-1をかけ、scheduleにいれる schedule[i] = -heapq.heappop(q) # 戻り値を返すと同時にq自体も変更している else: # できる仕事がなければ pass # その日の報酬は0のまま print((sum(schedule))) if __name__ == "__main__": main()
# 2019-11-16 14:51:41(JST) import sys # import collections # import math # from string import ascii_lowercase, ascii_uppercase, digits # from bisect import bisect_left as bi_l, bisect_right as bi_r # import itertools # from functools import reduce # import operator as op # from scipy.misc import comb # float # import numpy as np import heapq def main(): # 10**5連勤とか超人すぎる n, m = [int(x) for x in sys.stdin.readline().split()] job_list = dict((i, list()) for i in range(1, m+1)) for _ in range(n): a, b = [int(x) for x in sys.stdin.readline().split()] if a <= m: job_list[a].append(b) schedule = [0 for _ in range(m+1)] # 当日は報酬0 q = [] for i in range(1, m+1): for reward in job_list[i]: # 期限i日前にできる仕事の報酬一覧をheqp queueに入れる heapq.heappush(q, -reward) # あとで最大値を取り出すために-1かける(heappop()で取り出せるのは最小値) if q: # その日できる仕事が一つでもあれば # 報酬の最小値(マイナスなので元々の最大値)を取り出して-1をかけ、scheduleにいれる schedule[i] = -heapq.heappop(q) # 戻り値を返すと同時にq自体も変更している else: # できる仕事がなければ pass # その日の報酬は0のまま print((sum(schedule))) if __name__ == "__main__": main()
p02948
N,M = list(map(int,input().split())) from collections import defaultdict hist = defaultdict(int) AB = [[0,0]]*N for n in range(N): temp = list(map(int,input().split())) if temp[0] <= M: AB[n] = temp hist[temp[0]] = temp[0]-1 work_schedule = [0]*M sorted_AB = sorted(AB,key=lambda x:(-x[1],x[0])) for work in sorted_AB: while True: if hist[work[0]] >= M: break elif work_schedule[hist[work[0]]] == 0: work_schedule[hist[work[0]]] = work[1] break elif work_schedule[hist[work[0]]] != 0: hist[work[0]] += 1 print((sum(work_schedule)))
from collections import defaultdict import heapq n, m = list(map(int, input().split())) dct = defaultdict(list) for i in range(n): a, b = list(map(int, input().split())) dct[a].append(-b) h = [] res = 0 for i in range(1, m + 1): if dct[i]: for item in dct[i]: heapq.heappush(h, item) if h: res += heapq.heappop(h) print((-res))
p02948
from heapq import heapify, heappop nm = list(map(int, input().split())) n, m = nm[0], nm[1] d = {} ab_list = list() for i in range(n): ab = list(map(int, input().split())) rem_day, reward = ab[0], ab[1] if rem_day in d: d[rem_day].append(-reward) else: d[rem_day] = [-reward] result = 0 candidates = list() for rem in range(1, m + 1): added_candidates = d.get(rem) if added_candidates is not None and len(added_candidates) > 0: candidates.extend(added_candidates) if len(candidates) > 0: heapify(candidates) top = heappop(candidates) result += -top print(result)
from heapq import heapify, heappop, heappush nm = list(map(int, input().split())) n, m = nm[0], nm[1] d = {} ab_list = list() for i in range(n): ab = list(map(int, input().split())) rem_day, reward = ab[0], ab[1] if rem_day in d: d[rem_day].append(-reward) else: d[rem_day] = [-reward] result = 0 candidates = list() for rem in range(1, m + 1): added_candidates = d.get(rem) if added_candidates is not None: for ac in added_candidates: heappush(candidates, ac) if len(candidates) > 0: top = heappop(candidates) result += -top print(result)
p02948
import heapq as hq N, M = list(map(int, input().split())) A = [] for i in range(N): a, b = list(map(int, input().split())) A.append([b * (-1), a * (-1)]) # print(A) ans = 0 Q = [] hq.heapify(Q) for i in range(1, M + 1): for j in (A): if j[1] * (-1) <= i: hq.heappush(Q, j) A.remove([j[0], j[1]]) # print(Q) if Q: ans += hq.heappop(Q)[0] * (-1) print(ans)
import heapq as hq N, M = list(map(int, input().split())) A = [] for i in range(N): a, b = list(map(int, input().split())) A.append([a, b * (-1)]) A.sort() # print(A) ans = 0 j = 0 Q = [] hq.heapify(Q) for i in range(1, M + 1): while j < len(A): if A[j][0] <= i: hq.heappush(Q, A[j][1]) # print(Q) else: break j += 1 if Q: ans += hq.heappop(Q) * (-1) print(ans)
p02948
N,M = list(map(int,input().split())) AB = [list(map(int,input().split())) for _ in range(N)] from operator import itemgetter AB.sort(key = itemgetter(0)) AB.sort(key = itemgetter(1),reverse = True) days = [0]*(M+1) points = list(range(M+1)) from bisect import bisect_right for a,b in AB: point = M - a i = bisect_right(points,point) if i > 0: days[points[i-1]] = b points.pop(i-1) print((sum(days)))
import heapq from operator import itemgetter def main(): N, M = list(map(int,input().split())) AB = [list(map(int,input().split())) for _ in range(N)] AB.sort(key = itemgetter(0)) heap = [] ans = 0 cnt = 0 for i in range(1,M + 1): while cnt < N and AB[cnt][0] <= i: heapq.heappush(heap,-1*AB[cnt][1]) cnt += 1 if len(heap) > 0: ans += heapq.heappop(heap)*-1 print(ans) return if __name__ == "__main__": main()
p02948
from heapq import* N,M,*t=list(map(int,open(0).read().split())) v=[[]for _ in range(M)] for i in range(N): if t[2*i]<=M: v[t[2*i]-1].append(t[2*i+1]) z=0 q=[] for i in range(M): for j in v[i]: heappush(q,-j) b=0 if len(q): b=heappop(q) z-=b print(z)
from heapq import* N,M,*t=list(map(int,open(0).read().split())) v=[[]for _ in range(M)] for i in range(N): if t[2*i]<=M:v[t[2*i]-1].append(t[2*i+1]) z=0 q=[] for i in range(M): for j in v[i]:heappush(q,-j) z-=len(q)and heappop(q) print(z)
p02948
from heapq import* N,M,*t=list(map(int,open(0).read().split())) v=[[]for _ in range(M)] for i in range(N): if t[2*i]<=M:v[t[2*i]-1].append(t[2*i+1]) z=0 q=[] for i in range(M): for j in v[i]:heappush(q,-j) z-=len(q)and heappop(q) print(z)
from heapq import* N,M,*t=list(map(int,open(0).read().split())) v=[[]for _ in'_'*M] for i in range(N): if t[2*i]<=M:v[t[2*i]-1].append(t[2*i+1]) z=0 q=[] for i in v: for j in i:heappush(q,-j) z-=len(q)and heappop(q) print(z)
p02948
from heapq import heappop, heappush N, M = list(map(int, input().split())) W = [] for i in range(N): a = list(map(int, input().split())) heappush(W, a) ans = 0 C = [] for i in range(1,M+1): j = -i while W != [] and W[0][0] <= i: a,b = heappop(W) heappush(C,-b) if C != []: ans += -heappop(C) print(ans)
from heapq import heappop, heappush import sys input = sys.stdin.readline N, M = list(map(int, input().split())) W = [] for i in range(N): a = list(map(int, input().split())) heappush(W, a) ans = 0 C = [] for i in range(1,M+1): j = -i while W != [] and W[0][0] <= i: a,b = heappop(W) heappush(C,-b) if C != []: ans += -heappop(C) print(ans)
p02948
from heapq import heappop, heappush import sys input = sys.stdin.readline N, M = list(map(int, input().split())) W = [] for i in range(N): a = list(map(int, input().split())) heappush(W, a) ans = 0 C = [] for i in range(1,M+1): j = -i while W != [] and W[0][0] <= i: a,b = heappop(W) heappush(C,-b) if C != []: ans += -heappop(C) print(ans)
from heapq import heappop, heappush import sys input = sys.stdin.readline N, M = list(map(int, input().split())) W = [list(map(int, input().split())) for i in range(N)] W.sort() ans = 0 C = [] for i in range(1,M+1): j = -i while W != [] and W[0][0] <= i: a,b = heappop(W) heappush(C,-b) if C != []: ans += -heappop(C) print(ans)
p02948
from heapq import heapify, heappush, heappop maxA = 10**5 N, M = list(map(int, input().split())) works = [[] for _ in range(maxA)] for _ in range(N): A, B = list(map(int, input().split())) works[A-1].append(-B) PQ = [] ans = 0 for dayRest in range(1, M+1): for B in works[dayRest-1]: heappush(PQ, B) if not PQ: continue B = heappop(PQ) ans += -B print(ans)
from heapq import heapify, heappush, heappop import sys input = sys.stdin.readline N, M = list(map(int, input().split())) ABs = [tuple(map(int, input().split())) for _ in range(N)] ABs.sort() PQ = [] iAB = 0 ans = 0 for rest in range(1, M+1): while iAB < N and ABs[iAB][0] <= rest: heappush(PQ, -ABs[iAB][1]) iAB += 1 if PQ: B = -heappop(PQ) ans += B print(ans)
p02948
from heapq import heappop, heappush n,m = list(map(int,input().split())) byte = {} for i in range(n): a,b = list(map(int,input().split())) if a not in byte: byte[a] = [] heappush(byte[a], [-b,a]) ans = 0 res = [] for i in range(1, m+1): if i in byte: heappush(res, heappop(byte[i])) if not res: continue b,a = heappop(res) ans -= b if byte[a]: heappush(res, heappop(byte[a])) print(ans)
from heapq import heappop, heappush n,m = list(map(int,input().split())) byte = {} for i in range(n): a,b = list(map(int,input().split())) if a > m: continue if a not in byte: byte[a] = [] heappush(byte[a], -b) ans = 0 res = [] for i in range(1, m+1): if i in byte: while byte[i]: heappush(res, heappop(byte[i])) if res: ans -= heappop(res) print(ans)
p02948
# -*- coding: utf-8 -*- import sys import math import os import itertools import string import heapq import _collections from collections import Counter from collections import defaultdict from functools import lru_cache import bisect import re import queue class Scanner(): @staticmethod def int(): return int(sys.stdin.readline().rstrip()) @staticmethod def string(): return sys.stdin.readline().rstrip() @staticmethod def map_int(): return [int(x) for x in Scanner.string().split()] @staticmethod def string_list(n): return [eval(input()) for i in range(n)] @staticmethod def int_list_list(n): return [Scanner.map_int() for i in range(n)] @staticmethod def int_cols_list(n): return [int(eval(input())) for i in range(n)] class Math(): @staticmethod def gcd(a, b): if b == 0: return a return Math.gcd(b, a % b) @staticmethod def lcm(a, b): return (a * b) // Math.gcd(a, b) @staticmethod def roundUp(a, b): return -(-a // b) @staticmethod def toUpperMultiple(a, x): return Math.roundUp(a, x) * x @staticmethod def toLowerMultiple(a, x): return (a // x) * x @staticmethod def nearPow2(n): if n <= 0: return 0 if n & (n - 1) == 0: return n ret = 1 while(n > 0): ret <<= 1 n >>= 1 return ret @staticmethod def sign(n): if n == 0: return 0 if n < 0: return -1 return 1 @staticmethod def isPrime(n): if n < 2: return False if n == 2: return True if n % 2 == 0: return False d = int(n ** 0.5) + 1 for i in range(3, d + 1, 2): if n % i == 0: return False return True @staticmethod def divisor(n): i = 1 table = [] while i * i <= n: if n % i == 0: table.append(i) table.append(n // i) i += 1 table = list(set(table)) return table class PriorityQueue: def __init__(self, l=[]): self.__q = l heapq.heapify(self.__q) return def push(self, n): heapq.heappush(self.__q, n) return def pop(self): return heapq.heappop(self.__q) def list(self): return self.__q def length(self): return len(self.__q) sys.setrecursionlimit(1000000) MOD = int(1e09) + 7 INF = int(1e30) def main(): # sys.stdin = open("Sample.txt") N, M = Scanner.map_int() X = Scanner.int_list_list(N) X.sort() ans = 0 tasks = PriorityQueue() for t in reversed(list(range(M))): while X != [] and X[0][0] + t <= M: tasks.push(-X[0][1]) X = X[1:] if tasks.length() > 0: ans += -tasks.pop() print(ans) return if __name__ == "__main__": main()
# -*- coding: utf-8 -*- import sys import math import os import itertools import string import heapq import _collections from collections import Counter from collections import defaultdict from functools import lru_cache import bisect import re import queue class Scanner(): @staticmethod def int(): return int(sys.stdin.readline().rstrip()) @staticmethod def string(): return sys.stdin.readline().rstrip() @staticmethod def map_int(): return [int(x) for x in Scanner.string().split()] @staticmethod def string_list(n): return [eval(input()) for i in range(n)] @staticmethod def int_list_list(n): return [Scanner.map_int() for i in range(n)] @staticmethod def int_cols_list(n): return [int(eval(input())) for i in range(n)] class Math(): @staticmethod def gcd(a, b): if b == 0: return a return Math.gcd(b, a % b) @staticmethod def lcm(a, b): return (a * b) // Math.gcd(a, b) @staticmethod def roundUp(a, b): return -(-a // b) @staticmethod def toUpperMultiple(a, x): return Math.roundUp(a, x) * x @staticmethod def toLowerMultiple(a, x): return (a // x) * x @staticmethod def nearPow2(n): if n <= 0: return 0 if n & (n - 1) == 0: return n ret = 1 while(n > 0): ret <<= 1 n >>= 1 return ret @staticmethod def sign(n): if n == 0: return 0 if n < 0: return -1 return 1 @staticmethod def isPrime(n): if n < 2: return False if n == 2: return True if n % 2 == 0: return False d = int(n ** 0.5) + 1 for i in range(3, d + 1, 2): if n % i == 0: return False return True @staticmethod def divisor(n): i = 1 table = [] while i * i <= n: if n % i == 0: table.append(i) table.append(n // i) i += 1 table = list(set(table)) return table class PriorityQueue: def __init__(self, l=[]): self.__q = l heapq.heapify(self.__q) return def push(self, n): heapq.heappush(self.__q, n) return def pop(self): return heapq.heappop(self.__q) def list(self): return self.__q def length(self): return len(self.__q) sys.setrecursionlimit(1000000) MOD = int(1e09) + 7 INF = int(1e30) def main(): # sys.stdin = open("Sample.txt") N, M = Scanner.map_int() X = Scanner.int_list_list(N) X.sort() ans = 0 tasks = PriorityQueue() i = 0 for t in reversed(list(range(M))): while i < N and X[i][0] + t <= M: tasks.push(-X[i][1]) i += 1 if tasks.length() > 0: ans += -tasks.pop() print(ans) return if __name__ == "__main__": main()
p02948
#-*-coding:utf-8-*- import sys import heapq def pickup_job(tmp_jobs): tmp_jobs = sorted(tmp_jobs,key=lambda x:x[1],reverse=True) worked_job=heapq.heappop(tmp_jobs) return worked_job def main(): N,M = list(map(int, input().split())) job_datas = [list(map(int,input().split())) for _ in range(N)] job_datas=sorted(job_datas) day_counter=M tmp_jobs=[] ans=0 while day_counter >= 0: for job_data in job_datas: if (M-day_counter)>=job_data[0]: tmp_jobs.append(job_data) else: break if len(tmp_jobs)!=0: worked_job=pickup_job(tmp_jobs) ans+=worked_job[1] job_datas.remove(worked_job) tmp_jobs=[] day_counter-=1 print(ans) if __name__ == "__main__": main()
#-*-coding:utf-8-*- import heapq def main2(): n,m=list(map(int,input().split())) jobs_list=[list(map(int,input().split())) for _ in range(n)] #breakするタイミングを作るため、Mより大きい値の仕事を作成 banpei_list=[[i,0] for i in range(1,m+2)] #リストの統合 jobs_list.extend(banpei_list) jobs_list=sorted(jobs_list) ans=0 index=0 que=[] while 1: day,pay=jobs_list[index] #dayがMを超えていればbreak if day>m: break #同じdayのデータが次にもあれば-1倍してheapにpush if day==jobs_list[index+1][0]: heapq.heappush(que,-pay) #仕事の報酬の最大値を答えに加える #前の日のデータからも報酬が良い仕事は選べるようにqueにデータは貯めておく else: heapq.heappush(que,-pay) ans-=heapq.heappop(que) index+=1 print(ans) if __name__ == "__main__": main2()
p02948
import heapq n,m = list(map(int,input().split())) l = [] for i in range(n): ab = list(map(int,input().split())) l.append(ab) l.sort() q = [] ans = 0 for i in range(1,m+1): while len(l) > 0 and l[0][0] == i: tmp = l.pop(0) heapq.heappush(q,-tmp[1]) if len(q) != 0: ans += -heapq.heappop(q) print(ans)
import heapq n,m = list(map(int,input().split())) l = [] for i in range(n): ab = list(map(int,input().split())) l.append(ab) l.sort() q = [] ans = 0 cnt = 0 for i in range(1,m+1): while cnt < n and l[cnt][0] == i: heapq.heappush(q,-l[cnt][1]) cnt += 1 if len(q) != 0: ans += -heapq.heappop(q) print(ans)
p02948
# -*- coding: utf-8 -*- import bisect import heapq import math import random import sys from collections import Counter, defaultdict, deque from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal from functools import lru_cache, reduce from itertools import combinations, combinations_with_replacement, product, permutations from operator import add, mul, sub sys.setrecursionlimit(100000) input = sys.stdin.readline def read_int(): return int(input()) def read_int_n(): return list(map(int, input().split())) def read_float(): return float(input()) def read_float_n(): return list(map(float, input().split())) def read_str(): return input().strip() def read_str_n(): return list(map(str, input().split())) def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, 'sec') return ret return wrap @mt def slv(N, M, AB): ans = 0 cand = [] AB.sort(key=lambda x: x[0], reverse=True) for i in range(1, M+1): while AB and AB[-1][0] <= i: _, b = AB.pop() heapq.heappush(cand, -b) if cand: ans -= heapq.heappop(cand) return ans def main(): N, M = read_int_n() AB = [read_int_n() for _ in range(N)] print(slv(N, M, AB)) if __name__ == '__main__': main()
# -*- coding: utf-8 -*- import sys # sys.setrecursionlimit(10**6) # buff_readline = sys.stdin.buffer.readline buff_readline = sys.stdin.readline readline = sys.stdin.readline INF = 2**62-1 def read_int(): return int(buff_readline()) def read_int_n(): return list(map(int, buff_readline().split())) def read_float(): return float(buff_readline()) def read_float_n(): return list(map(float, buff_readline().split())) def read_str(): return readline().strip() def read_str_n(): return readline().strip().split() def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, 'sec') return ret return wrap class Mod: def __init__(self, m): self.m = m def add(self, a, b): return (a + b) % self.m def sub(self, a, b): return (a - b) % self.m def mul(self, a, b): return ((a % self.m) * (b % self.m)) % self.m def div(self, a, b): return self.mul(a, pow(b, self.m-2, self.m)) def pow(self, a, b): return pow(a, b, self.m) class Bisect: def __init__(self, func): self.__func = func def bisect_left(self, x, lo, hi): while lo < hi: mid = (lo+hi)//2 if self.__func(mid) < x: lo = mid+1 else: hi = mid return lo def bisect_right(self, x, lo, hi): while lo < hi: mid = (lo+hi)//2 if x < self.__func(mid): hi = mid else: lo = mid+1 return lo @mt def slv(N, M, AB): AB.sort(key=lambda x: x[0], reverse=True) from heapq import heappop, heappush q = [] ans = 0 for i in range(1, M+1): while AB and AB[-1][0] <= i: _, b = AB.pop() heappush(q, -b) if q: ans += -heappop(q) return ans def main(): N, M = read_int_n() AB = [read_int_n() for _ in range(N)] print(slv(N, M, AB)) if __name__ == '__main__': main()
p02948
import heapq job = list() salary = list() n,m = list(map(int,input().split())) k = -1 def h_sort(list): h = [] for v in list: heapq.heappush(h, v) return [heapq.heappop(h) for i in range(len(h))] for i in range(n): a,b = list(map(int,input().split())) job.append((a,b)) job = h_sort(job) job.reverse() for x in job: s,t = x if k == -1 or k > t: k = t salary.append(t) if s - m > 0: if salary[0] > k: salary = h_sort(salary) k = -1 del salary[0] else: m -= 1 print((sum(salary)))
import heapq job = list() salary = list() n,m = list(map(int,input().split())) for i in range(n): a,b = list(map(int,input().split())) job.append((a,b)) job.sort(reverse = True) for x in job: s,t = x heapq.heappush(salary,t) if s - m > 0: heapq.heappop(salary) else: m -= 1 print((sum(salary)))
p02948
n, m = list(map(int, input().split())) data = [[int(e) for e in input().split()] for _ in range(n)] d1 = {} for i in range(n): a, b = data[i] if a not in d1: d1[a] = [] d1[a].append(b) result = 0 d2 = {} maxb = -1 for i in range(1, m + 1): if i in d1: for j in d1[i]: if j not in d2: d2[j] = 0 d2[j] += 1 if j > maxb: maxb = j else: if len(d2) == 0: continue result += maxb if d2[maxb] == 1: del d2[maxb] if len(d2) == 0: maxb = 0 else: maxb = max(d2.keys()) else: d2[maxb] -= 1 print(result)
n, m = list(map(int, input().split())) d1 = {} for _ in range(n): a, b = list(map(int, input().split())) if a in d1: d1[a].append(b) else: d1[a] = [b] result = 0 d2 = {} maxb = -1 for i in range(1, m + 1): if i in d1: for j in d1[i]: if j not in d2: d2[j] = 0 d2[j] += 1 if j > maxb: maxb = j else: if len(d2) == 0: continue result += maxb if d2[maxb] == 1: del d2[maxb] if len(d2) == 0: maxb = 0 else: maxb = max(d2.keys()) else: d2[maxb] -= 1 print(result)
p02948
def main(): _map = map _range = range _len = len _max = max _input = input _int = int n, m = _map(_int, _input().split()) d1 = {} for _ in _range(n): a, b = _map(_int, _input().split()) if a in d1: d1[a].append(b) else: d1[a] = [b] result = 0 d2 = {} maxb = -1 for i in _range(1, m + 1): if i in d1: for j in d1[i]: if j not in d2: d2[j] = 1 else: d2[j] += 1 if j > maxb: maxb = j else: if _len(d2) == 0: continue result += maxb if d2[maxb] == 1: del d2[maxb] if _len(d2) == 0: maxb = -1 else: maxb = _max(list(d2.keys())) else: d2[maxb] -= 1 print(result) main()
def main(): _map = map _range = range _len = len _max = max _input = input _int = int n, m = _map(_int, _input().split()) d1 = {} for _ in _range(n): a, b = _map(_int, _input().split()) if a in d1: d1[a].append(b) else: d1[a] = [b] result = 0 d2 = {} maxb = -1 for i in _range(1, m + 1): if i in d1: for j in d1[i]: if j not in d2: d2[j] = 1 else: d2[j] += 1 if j > maxb: maxb = j else: if _len(d2) == 0: continue result += maxb d2[maxb] -= 1 if d2[maxb] == 0: del d2[maxb] if _len(d2) == 0: maxb = -1 else: maxb = _max(list(d2.keys())) print(result) main()
p02948
n,m=list(map(int,input().split())) ab=[] q=0 for i in range(n): p=[int(j) for j in input().split()] q=max(q,p[0]) ab.append(p) from operator import itemgetter ab.sort(key=itemgetter(1)) ab.reverse() ab.sort(key=itemgetter(0)) l=[] import bisect ans=0 for i in range(1,m+1): while True: if ab==[]: break if ab[0][0]<=i: insert_index = bisect.bisect_left(l,ab[0][1]) #2 l.insert(insert_index,ab[0][1]) del ab[0] else: break if l!=[]: ans+=l[-1] del l[-1] print(ans)
n,m=list(map(int,input().split())) ab=[] l=[[] for i in range(m+1)] for i in range(n): tmp=[int(j) for j in input().split()] ab.append(tmp) a,b=tmp if a<=m: l[a].append(b) import heapq q=[] ans=0 for i in range(1,1+m): for j in l[i]: heapq.heappush(q,-j) if q: ans-=heapq.heappop(q) print(ans)
p02948
n,m= list(map(int, input().split())) ab_list=[] for _ in range(n): a, b = list(map(int, input().split())) ab_list.append([a,b]) ab_list.sort() import heapq l = [] heapq.heapify(l) ind=0 ans=0 for i in range(1,m+1): while n>ind and ab_list[ind][0] <= i: heapq.heappush(l,-ab_list[ind][1]) ind+=1 if len(l) !=0: b = -heapq.heappop(l) ans+=b print(ans)
import sys input=sys.stdin.readline n,m= list(map(int, input().split())) ab_list=[] for _ in range(n): a, b = list(map(int, input().split())) ab_list.append([a,b]) ab_list.sort() import heapq l = [] heapq.heapify(l) ind=0 ans=0 for i in range(1,m+1): while n>ind and ab_list[ind][0] <= i: heapq.heappush(l,-ab_list[ind][1]) ind+=1 if len(l) !=0: b = -heapq.heappop(l) ans+=b print(ans)
p02948
import heapq N, M = [int(_) for _ in input().split()] ABs = [[] for _ in range(M + 1)] r = 0 for i in range(N): A, B = [int(x) for x in input().split()] if A <= M: ABs[A].append(-B) for m in range(1, M + 1): B_max = -1 A_max = -1 heapq.heapify(ABs[m]) # print("{0}".format(m)) for a in range(1, m + 1): # print("\t{0}".format(a)) if len(ABs[a]) == 0: continue b = - ABs[a][0] if B_max < b: A_max = a B_max = b if A_max != -1: r -= heapq.heappop(ABs[A_max]) print(r)
import heapq N, M = [int(_) for _ in input().split()] ABs = [[] for _ in range(M + 1)] r = 0 for i in range(N): A, B = [int(x) for x in input().split()] if A <= M: ABs[A].append(-B) q = [] for m in range(1, M + 1): if ABs[m] != 0: for b in ABs[m]: heapq.heappush(q, b) if len(q) != 0: r -= heapq.heappop(q) print(r)
p02948
import sys from collections import defaultdict input = sys.stdin.readline n, m = list(map(int, input().split())) dic = defaultdict(list) total = 0 candidates = [] for i in range(n): a, b = list(map(int, input().split())) dic[a].append(b) for day in range(1, m+1): if day in dic: candidates += dic[day] if candidates: fee = max(candidates) total += fee candidates.remove(fee) print(total)
import sys import heapq from collections import defaultdict input = sys.stdin.readline n, m = list(map(int, input().split())) dic = defaultdict(list) total = 0 candidates = [] heapq.heapify(candidates) for i in range(n): a, b = list(map(int, input().split())) dic[a].append(b) for day in range(1, m+1): if day in dic: for x in dic[day]: heapq.heappush(candidates, -x) if candidates: total -= heapq.heappop(candidates) print(total)
p02948
N, M = list(map(int, input().split())) ABs = [0] * N for i in range(N): ABs[i] = list(map(int, input().split())) ABs = sorted(ABs, reverse=True, key=lambda x: x[1]) rewards = [0] * (M + 1) count = 0 for AB in ABs: day, reward = AB if day <= M: if rewards[day] == 0: rewards[day] = reward count += 1 else: for i in range(day, M + 1): if rewards[i] == 0: rewards[i] = reward count += 1 break if count == M: break result = sum(rewards) print(result)
N, M = list(map(int, input().split())) ABs = [0] * N day_dic = {} for i in range(N): ABs[i] = list(map(int, input().split())) ABs = sorted(ABs, reverse=True, key=lambda x: x[1]) rewards = [0] * (M + 1) count = 0 for AB in ABs: day, reward = AB if day <= M: if rewards[day] == 0: rewards[day] = reward day_dic[day] = day count += 1 else: esti = day_dic.get(day) if esti is None: esti = day for i in range(esti, M + 1): if rewards[i] == 0: rewards[i] = reward day_dic[day] = i count += 1 break if count == M: break result = sum(rewards) print(result)
p02948
import heapq N,M = list(map(int,input().split())) l = [tuple(map(int,input().split())) for _ in range(N)] l = sorted(l,reverse=True) heap = [] ans = 0 for i in range(1,M+1): while l and l[-1][0] == i: heapq.heappush(heap, (l.pop()[1]*(-1))) if heap : ans -= (heapq.heappop(heap)) print(ans)
import heapq N,M = list(map(int,input().split())) l = [tuple(map(int,input().split())) for _ in range(N)] l = sorted(l,reverse=True) heap = [] ans = 0 for i in range(1,M+1): while l and l[-1][0] == i: heapq.heappush(heap, -(l.pop()[1])) if heap : ans -= (heapq.heappop(heap)) print(ans)
p02948
import heapq import os import sys if os.getenv("LOCAL"): sys.stdin = open("_in.txt", "r") sys.setrecursionlimit(2147483647) INF = float("inf") IINF = 10 ** 18 MOD = 10 ** 9 + 7 class PrimalDual: """ 最小費用流 ダイクストラ版 """ def __init__(self, graph=None, residual=None): """ :param list of (list of (int, int, int)) graph: (to, cap, cost) の隣接リスト :param list of (list of (list of (int|list))) residual: (to, cap, cost, rev) の残余グラフ """ assert (graph and not residual) or (not graph and residual) if graph: self.graph = self.residual_graph(graph) else: self.graph = residual @staticmethod def residual_graph(graph): """ 残余グラフ構築 :param list of (list of (int, int, int)) graph: (to, cap, cost) の隣接リスト :rtype: list of (list of (list of (int|list))) :return: (to, cap, cost, rev) の残余グラフ """ ret = [[] for _ in range(len(graph))] for v in range(len(graph)): for u, cap, cost in graph[v]: rev = [v, 0, -cost] edge = [u, cap, cost, rev] rev.append(edge) ret[v].append(edge) ret[u].append(rev) return ret def solve(self, from_v, to_v, flow): """ :param int from_v: :param int to_v: :param int flow: :rtype: int """ total_cost = 0 prevv = [-1] * len(self.graph) preve = [-1] * len(self.graph) # ポテンシャル h = [0] * len(self.graph) remains = flow while remains > 0: dist = [float('inf')] * len(self.graph) dist[from_v] = 0 heap = [(0, from_v)] # Dijkstra while heap: d, v = heapq.heappop(heap) if d > dist[v]: continue for edge in self.graph[v]: u, cap, cost, rev = edge if cap > 0 and dist[v] + cost + h[v] - h[u] < dist[u]: dist[u] = dist[v] + cost + h[v] - h[u] prevv[u] = v preve[u] = edge heapq.heappush(heap, (dist[u], u)) if dist[to_v] == float('inf'): # これ以上流せない break for i, d in enumerate(dist): h[i] += d # 最短路に流せる量 flow = remains v = to_v while v != from_v: cap = preve[v][1] flow = min(cap, flow) v = prevv[v] # 最短路に flow だけ流す v = to_v while v != from_v: preve[v][1] -= flow preve[v][3][1] += flow v = prevv[v] remains -= flow total_cost += flow * h[to_v] return total_cost N, M = list(map(int, sys.stdin.readline().split())) AB = [list(map(int, sys.stdin.readline().split())) for _ in range(N)] S = M T = M + 1 graph = [[] for _ in range(M + 2)] for i in range(M): graph[i].append((T, 1, 0)) for i in range(M - 1): graph[i + 1].append((i, IINF, 0)) for a, b in AB: if M - a >= 0: graph[S].append((M - a, 1, -b)) ans = -PrimalDual(graph).solve(S, T, IINF) print(ans)
import heapq import os import sys if os.getenv("LOCAL"): sys.stdin = open("_in.txt", "r") sys.setrecursionlimit(2147483647) INF = float("inf") IINF = 10 ** 18 MOD = 10 ** 9 + 7 N, M = list(map(int, sys.stdin.readline().split())) AB = [list(map(int, sys.stdin.readline().split())) for _ in range(N)] AB.sort(key=lambda ab: (ab[0], -ab[1])) heap = [] abi = 0 ans = 0 for i in range(1, M + 1): # i までのやつは入れられる while abi < N and AB[abi][0] <= i: a, b = AB[abi] heapq.heappush(heap, (-b, a)) abi += 1 if heap: b, a = heapq.heappop(heap) ans += -b print(ans)
p02948
import heapq N,M = list(map(int,(input().split()))) A = [0]*N B = [0]*N mission = [[] for _ in range(M+1)] work = 0 for i in range(N): if i <= M: heapq.heapify(mission[i]) A[i],B[i] = list(map(int,(input().split()))) if A[i] <= M: mission[A[i]].append(-B[i]) for day in range(1,M+1): mission[day].extend(mission[day-1]) if mission[day]: heapq.heapify(mission[day]) work += heapq.heappop(mission[day]) print((-work))
import heapq N,M = list(map(int,(input().split()))) A = [0]*N B = [0]*N mission = [[] for _ in range(M+1)] work = 0 for i in range(N): if i <= M: heapq.heapify(mission[i]) A[i],B[i] = list(map(int,(input().split()))) if A[i] <= M: mission[A[i]].append(-B[i]) for day in range(1,M+1): for i in range(len(mission[day])): heapq.heappush(mission[0],mission[day][i]) if mission[0]: work += heapq.heappop(mission[0]) print((-work))
p02948
import sys from collections import defaultdict n, m = list(map(int, sys.stdin.readline().split())) a_b = [list(map(int, sys.stdin.readline().split())) for _ in range(n)] dic = defaultdict(list) for ele in a_b: dic[ele[0]].append(ele[1]) L =[] money = 0 for d in range(1, m+1): if dic[d]: L+= dic[d] if L: L.sort() money += L[-1] L.pop() print(money)
import sys import heapq from collections import defaultdict n, m = list(map(int, sys.stdin.readline().split())) a_b = [list(map(int, sys.stdin.readline().split())) for _ in range(n)] dic = defaultdict(list) for ele in a_b: dic[ele[0]].append(ele[1]) #print(dic) L =[] money = 0 for d in range(1, m+1): for e in dic[d]: heapq.heappush(L, -e) if L: money +=heapq.heappop(L) print((-money))
p02948
import heapq as hq 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() #日が短い順に ans = 0 idx = 0 q = [] for i in range(1,m+1): while idx<n and ab[idx][0] ==i: a,mb = ab[idx] hq.heappush(q,mb) idx+=1 if q!=[]: ans += -1*hq.heappop(q) print(ans)
import heapq as hq 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() ans = 0 idx = 0 q = [] for i in range(1,m+1): while idx<n and ab[idx][0] ==i: a,mb = ab[idx] hq.heappush(q,mb) idx+=1 if q!=[]: ans += -1*hq.heappop(q) print(ans)
p02948
import heapq n, m = list(map(int, input().split(' '))) df = [] for _ in range(n): x, y = list(map(int, input().split(' '))) df.append([x, y]) df.sort() h = [] result = 0 counter = 0 for i in range(1, m+1): while counter < len(df) and df[counter][0] <= i: heapq.heappush(h, (-1)*df[counter][1]) counter += 1 if len(h) == 0: continue else: result -= heapq.heappop(h) print(result)
def main(): import heapq n, m = list(map(int, input().split(' '))) df = [] for _ in range(n): x, y = list(map(int, input().split(' '))) df.append([x, y]) df.sort() h = [] result = 0 counter = 0 for i in range(1, m+1): while counter < len(df) and df[counter][0] <= i: heapq.heappush(h, (-1)*df[counter][1]) counter += 1 if len(h) == 0: continue else: result -= heapq.heappop(h) print(result) if __name__ == '__main__': main()
p02948
import heapq from collections import deque N, M=list(map(int,input().split())) AB=[list(map(int,input().split())) for _ in range(N)] AB.sort(key=lambda x:x[0]) d=deque(AB) h=[] ans=0 for i in range(M+1): while d and d[0][0]<=i: heapq.heappush(h, -d.popleft()[1]) if h: ans-=heapq.heappop(h) print(ans)
import heapq from collections import defaultdict N, M = list(map(int, input().split())) d = defaultdict(list) for i in range(N): A, B = list(map(int, input().split())) d[A].append(-B) ans = 0 work = [] for i in range(1, M+1): for j in range(len(d[i])): heapq.heappush(work, d[i][j]) if len(work): ans -= heapq.heappop(work) print(ans)
p02948
import heapq N, M = list(map(int, input().split())) W = [[] for _ in range(100005)] for i in range(N): A, B = list(map(int, input().split())) W[A].append(B) hq = [] ans, ct, idx = 0, 0, 0 for A in range(1, M+1): for B in W[A]: heapq.heappush(hq, -B) if hq: B = heapq.heappop(hq) ans -= B print(ans)
import heapq N, M = list(map(int, input().split())) W = [[] for _ in range(100005)] for i in range(N): A, B = list(map(int, input().split())) W[A].append(B) hq = [] ans = 0 for A in range(1, M+1): for B in W[A]: heapq.heappush(hq, -B) if hq: B = heapq.heappop(hq) ans -= B print(ans)
p02948
import heapq as pq N, M = list(map(int, input().split())) AB = [tuple(map(int, input().split())) for _ in range(N)] day_to_reward = {} for a, b in AB: if day_to_reward.get(a) is None: day_to_reward[a] = [b] else: day_to_reward[a].append(b) ans = 0 candidate = [] for i in range(1, M+1): for reward in day_to_reward.get(i, []): pq.heappush(candidate, - reward) if candidate: work_reward = - pq.heappop(candidate) ans += work_reward print(ans)
import heapq as pq N, M = list(map(int, input().split())) AB = [tuple(map(int, input().split())) for _ in range(N)] day_to_reward = {} for a, b in AB: if day_to_reward.get(a) is None: day_to_reward[a] = [b] else: day_to_reward[a].append(b) ans = 0 candidate = [] for i in range(1, M+1): for reward in day_to_reward.get(i, []): pq.heappush(candidate, - reward) if candidate: ans += - pq.heappop(candidate) print(ans)
p02948
N, M = list(map(int, input().split())) AB = [list(map(int, input().split())) for _ in range(N)] AB = sorted(sorted(AB, key=lambda x: x[0], reverse=False), key=lambda x: x[1], reverse=True) x = [0] * M cnt = 0 cntIN = M preB = 0 for a, b in AB: if preB != b: k = M i = min(M-a, k) while i>=0 and x[i] != 0: i -= 1 else: if i >= 0: x[i] = b k = i - 1 cntIN -= 1 if cntIN <= 0: break print((sum(x)))
N, M = list(map(int, input().split())) AB = [list(map(int, input().split())) for _ in range(N)] AB = sorted(sorted(AB, key=lambda x: x[0], reverse=False), key=lambda x: x[1], reverse=True) x = [0] * M p = [x for x in range(M)] cnt = 0 cntIN = M for a, b in AB: i = M-a s = [] while i >= 0 and p[i] != i: s.append(i) i = p[i] for j in s: p[j] = i - 1 if i >= 0: x[i] = b p[i] = i - 1 cntIN -= 1 if cntIN <= 0: break print((sum(x)))
p02948
N, M = list(map(int, input().split())) AB = [list(map(int, input().split())) for _ in range(N)] AB = sorted(sorted(AB, key=lambda x: x[0], reverse=False), key=lambda x: x[1], reverse=True) x = [0] * M p = [x for x in range(M)] cnt = 0 cntIN = M for a, b in AB: i = M-a s = [] while i >= 0 and p[i] != i: s.append(i) i = p[i] for j in s: p[j] = i - 1 if i >= 0: x[i] = b p[i] = i - 1 cntIN -= 1 if cntIN <= 0: break print((sum(x)))
N, M = list(map(int, input().split())) AB = [(a, b) for _ in range(N) for a, b in [list(map(int, input().split()))] if M - a >= 0] AB = sorted(sorted(AB, key=lambda x: x[0], reverse=False), key=lambda x: x[1], reverse=True) x = [0] * M p = [x for x in range(M)] cnt = 0 cntIN = M for a, b in AB: i = M-a s = [] while i >= 0 and p[i] != i: s.append(i) i = p[i] for j in s: p[j] = i - 1 if i >= 0: x[i] = b p[i] = i - 1 cntIN -= 1 if cntIN <= 0: break print((sum(x)))
p02948
from heapq import * from collections import defaultdict n, m = list(map(int, input().split())) jobs = defaultdict(list) for _ in range(n): a, b = list(map(int, input().split())) jobs[a].append(-b) result = 0 pool = [] for i in range(1, m+1): for j in jobs[i]: heappush(pool, j) if 0 < len(pool): result += -heappop(pool) print(result)
import sys from collections import defaultdict from heapq import * sys.setrecursionlimit(10**8) input = sys.stdin.readline def main(): """ main """ N,M = list(map(int, input().split())) job_list = defaultdict(list) for _ in range(N): a, b = list(map(int, input().split())) job_list[a].append(-b) pool = [] ans = 0 for i in range(1, M+1): for reward in job_list[i]: heappush(pool, reward) if 0 < len(pool): ans -= heappop(pool) print(ans) if __name__ == '__main__': main()
p02948
from heapq import heappop, heappush import sys input = sys.stdin.readline N, M = list(map(int, input().split())) X = sorted([list(map(int, input().split())) for _ in range(N)], key = lambda x: x[0]) hq = [] ans, j = 0, 0 for i in range(1, M + 1): # M - i 日後にするバイトを考える while (j < N) and (X[j][0] <= i): heappush(hq, -X[j][1]) # 候補の追加 j += 1 if len(hq): ans += -heappop(hq) # 候補があるとき、候補から最大値を取り出す print(ans)
from heapq import heappop, heappush N, M = list(map(int, input().split())) X = sorted([list(map(int, input().split())) for _ in range(N)], key = lambda x: x[0]) hq = [] ans, j = 0, 0 for i in range(1, M + 1): # M - i 日後にするバイトを考える while (j < N) and (X[j][0] <= i): heappush(hq, -X[j][1]) # 候補の追加 j += 1 if len(hq): ans += -heappop(hq) # 候補があるとき、候補から最大値を取り出す print(ans)
p02948
import heapq def main(): n, m = [int(e) for e in input().split()] AB = [[int(e) for e in input().split()] for i in range(n)] AB.sort(key=lambda x: x[0]) H = [] ans = 0 mi = 1 for a, b in AB: while a > mi: if len(H) > 0: heapq.heapify(H) ans += -heapq.heappop(H) mi += 1 if mi > m: break if a > m: break H.append(-b) while mi <= m and len(H) > 0: heapq.heapify(H) ans += -heapq.heappop(H) mi += 1 print(ans) if __name__ == '__main__': main()
import heapq def main(): n, m = [int(e) for e in input().split()] AB = [[int(e) for e in input().split()] for i in range(n)] AB.sort(key=lambda x: x[0]) H = [] ans = 0 mi = 1 for a, b in AB: while a > mi: if len(H) > 0: ans += -heapq.heappop(H) mi += 1 if mi > m: break if a > m: break heapq.heappush(H, -b) while mi <= m and len(H) > 0: ans += -heapq.heappop(H) mi += 1 print(ans) if __name__ == '__main__': main()
p02948
def main(): N,M=list(map(int,input().split())) job=[[0,0] for i in range(0,N)] for i in range(0,N): job[i][0],job[i][1]=list(map(int,input().split())) s=0 job=sorted(job,reverse=True) for i in range(0,M): for j in range(0,N): if job[j][0]>M-i: job[j][1]=0 jobsub=[job[k][1] for k in range(0,N)] s+=max(jobsub) job[jobsub.index(max(jobsub))][1]=0 print(s) if __name__ == '__main__': main()
import sys import heapq input=sys.stdin.readline N,M=list(map(int,input().split())) job={i:[] for i in range(1,M+1)} for i in range(0,N): a,b=list(map(int,input().split())) if M>=a: job[a].append(b) ans=0 ablejob=[] heapq.heapify(ablejob) for i in range(1,M+1): for j in job[i]: heapq.heappush(ablejob,-j) if ablejob: m=-heapq.heappop(ablejob) ans+=m print(ans)
p02948
#template def inputlist(): return [int(k) for k in input().split()] #template N,M = inputlist() li = [[] for _ in range(M+1)] for i in range(N): A,B = inputlist() B *= -1 if A > M: continue li[A].append(B) ans = 0 import heapq for i in range(1,M+1): if i >= 2: li[i] += li[i-1] heapq.heapify(li[i]) if len(li[i]) == 0: continue k = heapq.heappop(li[i]) ans += k*(-1) print(ans)
#template def inputlist(): return [int(k) for k in input().split()] #template N,M = inputlist() li = [] for i in range(N): A,B = inputlist() B *= -1 li.append([A,B]) ans = 0 from heapq import * li.sort() k = [] heapify(k) flag = 0 for i in range(1,M+1): if flag == N: for i in range(M+1-i): ans -= heappop(k) break while li[flag][0] <= i: heappush(k,li[flag][1]) flag = flag + 1 if flag == N: break if len(k)>=1: ans -= heappop(k) print(ans)
p02948
#最終日から見る,残り日数条件を満たす中で最大収入のものを選ぶ import heapq N,M=list(map(int,input().split())) AB=[[0,0] for _ in range(N)] for i in range(N): AB[i][0],AB[i][1] = list(map(int,input().split())) AB.sort() ans=0 q=[] heapq.heapify(q) ne=0 #dは残り日数 for d in range(1,M+1): for i in range(ne,N): if AB[i][0]<=d: heapq.heappush(q,AB[i][1]*-1) ne=i+1 if len(q)!=0: a=heapq.heappop(q) ans-=a print(ans)
#最終日から見る,残り日数条件を満たす中で最大収入のものを選ぶ import heapq N,M=list(map(int,input().split())) AB=[[0,0] for _ in range(N)] for i in range(N): AB[i][0],AB[i][1] = list(map(int,input().split())) AB.sort() ans=0 q=[] heapq.heapify(q) ne=0 #dは残り日数 for d in range(1,M+1): for i in range(ne,N): if AB[i][0]<=d: heapq.heappush(q,AB[i][1]*-1) ne=i+1 else: break if len(q)!=0: a=heapq.heappop(q) ans-=a print(ans)
p02948
from collections import defaultdict import heapq n, m = list(map(int, input().split())) m_dict = defaultdict(list) for i in range(n): a, b = list(map(int, input().split())) m_dict[a] = m_dict[a] + [b] job_que = [] total = 0 for j in range(1, m+1): for k in m_dict[j]: heapq.heappush(job_que, -k) if job_que: total -= heapq.heappop(job_que) print(total)
import heapq n, m = list(map(int, input().split())) t_r_list = [] for i in range(n): t_r_list.append(list(map(int, input().split()))) t_r_list.sort() que = [] total = 0 l = 0 for j in range(1, m + 1): while True: if l == n: break if t_r_list[l][0] != j: break heapq.heappush(que, -t_r_list[l][1]) l += 1 if que: total -= heapq.heappop(que) print(total)
p02948
N,M = list(map(int, input().split())) L = [[int(a) for a in input().split()] for _ in range(N)] import heapq L.sort() h = [] ans = [0]*(M) i = 0 j = 0 d = 0 while i < M and d < M: i += 1 while j < N and L[j][0] <= i: heapq.heappush(h, [-L[j][1], L[j][0]]) j += 1 if h: temp = heapq.heappop(h) ans[i-1] = -temp[0] if N < M: ans.sort(reverse=True) c = 0 for i in range(N): c += ans[i] print(c) else: print((sum(ans)))
N,M = list(map(int, input().split())) L = [[int(l) for l in input().split()] for _ in range(N)] import heapq h = [] ans = 0 L.sort() m = 1 i = 0 while i < N and L[i][0] <= M: if L[i][0] == m: heapq.heappush(h, -L[i][1]) i += 1 else: m += 1 if h: ans -= heapq.heappop(h) while m <= M and h: ans -= heapq.heappop(h) m += 1 print(ans)
p02948
import heapq n,m=list(map(int,input().split())) arr=[list(map(int,input().split())) for _ in range(n)] arr2=[[i,0] for i in range(1,m+2)] #すべてのAi=1 to Mについて報酬0の仕事を追加する arr.extend(arr2) arr=sorted(arr,key=lambda x:x[0]) #Aiの小さい順にソート q=[] ans=0 pos=0 while 1: a,b=arr[pos] if a>m: #AiがMより大きければその仕事を請ける意味はない break if a==arr[pos+1][0]: #Aiの値が等しいとき、その仕事を優先度付きキューに追加する heapq.heappush(q,-b) else: heapq.heappush(q,-b) ans-=heapq.heappop(q) #Aiの値が変化したとき、そのAiまででの仕事の報酬の最大値を答えに加える pos+=1 print(ans)
'''tanon 優先度付きキュー t(=0 to M-1)日目に仕事を請けることを考えると、Ai>M-tなるiについては、 その仕事を請けるかどうかを考えなくて良い(∵この仕事を請けても、 Ai>M-t⇔Ai+t>Mより報酬はM日目より後にしかもらえません)。 したがって、tを大きい順に見ていくと、 M-1日目にはAi<=1なるiについて、報酬が最大となる仕事iを請けるべき、 M-2日目にはAi<=2なるiについて、まだ請けていない仕事の中で 報酬が最大となる仕事iを請けるべき、 …0日目にはAi<=Mなるiについて、まだ請けていない仕事の中で報酬が 最大となる仕事iを請けるべきであるといえます。 したがって、元の配列をAiの小さい順にソートしておき、 Ai=1の仕事について優先度付きキューに報酬Biを追加し、 優先度付きキューから取ってきた最大値を答えに加え、 次にAi=2の仕事について優先度付きキューに報酬Biを追加し、 優先度付きキューから取ってきた最大値を答えに加え、 …最後にAi=Mの仕事について優先度付きキューに報酬Biを追加し、 優先度付きキューから取ってきた最大値を答えに加えることで、 求める答えが得られます。 ''' import heapq n,m=list(map(int,input().split())) arr=[list(map(int,input().split())) for _ in range(n)] arr2=[[i,0] for i in range(1,m+2)] #すべてのAi=1 to Mについて報酬0の仕事を追加する #こうすることで、必ずAiに対し、報酬0でも仕事がある⇒空振りがなくなる arr.extend(arr2) arr=sorted(arr,key=lambda x:x[0]) #Aiの小さい順にソート q=[] ans=0 pos=0 while 1: a,b=arr[pos] if a>m: #AiがMより大きければその仕事を請ける意味はない break if a==arr[pos+1][0]: #Aiの値が等しいとき、その仕事を優先度付きキューに追加する heapq.heappush(q,-b)#マイナスを付けて、ヒープキューに入れている else: heapq.heappush(q,-b) ans-=heapq.heappop(q)#ヒープキューから最大値を持ってくるのでマイナス #Aiの値が変化したとき、そのAiまででの仕事の報酬の最大値を答えに加える pos+=1 print(ans)
p02948
#### import #### import sys import math from collections import defaultdict #### 設定 #### sys.setrecursionlimit(10**7) #### 定数 #### mod = 10**9 + 7 #### 読み込み #### def I(): return int(eval(input())) def II(): return list(map(int, input().split())) def III(): return list(map(int, input().split())) def Line(N): read_all = [tuple(map(int, input().split())) for _ in range(N)] return list(map(list,list(zip(*read_all)))) ################# N,M = II() A,B = Line(N) a = [] for i in range(N): a.append([A[i],B[i]]) a.sort(key = lambda x:(-x[1])) from bisect import bisect_left s = [i for i in range(1,M+1)] ans = 0 for i in range(N): if a[i][0]<=M: place = bisect_left(s,a[i][0]) if place <= len(s)-1: s.pop(place) ans += a[i][1] print(ans)
#### import #### import sys import math from collections import defaultdict #### 設定 #### sys.setrecursionlimit(10**7) #### 定数 #### mod = 10**9 + 7 #### 読み込み #### def I(): return int(eval(input())) def II(): return list(map(int, input().split())) def III(): return list(map(int, input().split())) def Line(N): read_all = [tuple(map(int, input().split())) for _ in range(N)] return list(map(list,list(zip(*read_all)))) ################# N,M = II() A,B = Line(N) a = [] for i in range(N): a.append([A[i],B[i]]) a.sort(key = lambda x:(x[0],x[1])) b = [] for i in range(N): b.append([-a[i][1], a[i][0]]) from bisect import bisect_left, bisect_right import heapq ans = 0 h = [] for i in range(1,M+1): left = bisect_left(a,[i,0]) right = bisect_right(a,[i,float('inf')])-1 if right-left>=0: for i in range(left,right+1): heapq.heappush(h,b[i]) x = heapq.heappop(h) ans += -x[0] else: if h: x = heapq.heappop(h) ans += -x[0] print(ans)
p02948
from bisect import bisect_left import sys input = sys.stdin.readline N, M = list(map(int, input().split())) AB = [[int(i) for i in input().split()] for _ in range(N)] AB.sort(key=lambda x: x[1], reverse=True) s = [i for i in range(1, M + 1)] ret = 0 for a, b in AB : if a > s[-1] : continue a = bisect_left(s, a) s.pop(a) ret += b if not s : break print(ret)
from heapq import heappush, heappop N, M = list(map(int, input().split())) AB = [[] for _ in range(M+1)] for _ in range(N) : a, b = list(map(int, input().split())) if a <= M : AB[M-a].append(b) q = [] for a in range(M+1) : for b in AB[a] : heappush(q, b) if len(q) > a + 1 : heappop(q) print((sum(q)))
p02948
import sys import math import itertools import bisect from copy import copy from collections import deque,Counter from decimal import Decimal def s(): return eval(input()) def i(): return int(eval(input())) def S(): return input().split() def I(): return list(map(int,input().split())) def L(): return list(input().split()) def l(): return list(map(int,input().split())) def lcm(a,b): return a*b//math.gcd(a,b) sys.setrecursionlimit(10**9) INF = 10**9 mod = 10**9+7 import heapq N,M = I() L = [l() for _ in range(N)] A = [] B = [] L.sort() ans = 0 for a,b in L: A.append(a) B.append(b) q = [] heapq.heapify(q) for i in range(1,M+1): for j in range(len(A)): if A[0] > i: break else: A.pop(0) heapq.heappush(q,B.pop(0)*-1) if q: ans += heapq.heappop(q)*-1 print(ans)
import sys import math import itertools import bisect from copy import copy from collections import deque,Counter from decimal import Decimal def s(): return eval(input()) def i(): return int(eval(input())) def S(): return input().split() def I(): return list(map(int,input().split())) def L(): return list(input().split()) def l(): return list(map(int,input().split())) def lcm(a,b): return a*b//math.gcd(a,b) sys.setrecursionlimit(10**9) INF = 10**9 mod = 10**9+7 import heapq N,M = I() L = [l() for _ in range(N)] A = deque([]) B = deque([]) L.sort() ans = 0 for a,b in L: A.append(a) B.append(b) q = [] heapq.heapify(q) for i in range(1,M+1): for j in range(len(A)): if A[0] > i: break else: A.popleft() heapq.heappush(q,B.popleft()*-1) if q: ans += heapq.heappop(q)*-1 print(ans)
p02948
N, M = list(map(int, input().split())) AB = [list(map(int, input().split())) for _ in range(N)] AB.sort() B = [[] for _ in range(100005)] tmp = 0 for ab in AB: if ab[0] != tmp: B[ab[0]] = B[tmp].copy() B[ab[0]].append(ab[1]) tmp = ab[0] for i in range(1, M + 1): if len(B[i]) == 0: B[i] = B[i - 1].copy() ans = 0 for i in range(1, M + 1): if len(B[i]) > 0: m = max(B[i]) ans += m for j in range(i, M + 1): if len(B[j]) > 0: B[j].remove(m) print(ans)
N, M = list(map(int, input().split())) AB = [] for i in range(N): ab = list(map(int, input().split())) if ab[0] <= M: AB.append(ab) AB.sort(reverse = True, key = lambda x:x[1]) ans = 0 for i in range(1, M + 1): for j in range(len(AB)): if AB[j][0] <= i: ans += AB.pop(j)[1] break print(ans)
p02948
from heapq import heappush, heappop N, M = list(map(int, input().split())) AB = [] for i in range(N): ab = list(map(int, input().split())) #Ai > M の仕事はいらない if ab[0] <= M: AB.append(ab) #最終日はAi=1の仕事のみ選べる。その前の日はAi<=2, ... #終わりから考えればよさそう AB.sort() hq = [] ans = 0 for i in range(1, M + 1): while len(AB) > 0 and AB[0][0] <= i: heappush(hq, -1 * AB.pop(0)[1]) if len(hq) > 0: m = -1 * heappop(hq) ans += m print(ans)
from heapq import heappush, heappop N, M = list(map(int, input().split())) jobs = [[] for _ in range(M)] for i in range(N): ab = list(map(int, input().split())) #Ai > M の仕事はいらない if ab[0] <= M: jobs[ab[0] - 1].append(ab[1]) #最終日はAi=1の仕事のみ選べる。その前の日はAi<=2, ... #終わりから考えればよさそう hq = [] ans = 0 for i in range(M): for b in jobs[i]: heappush(hq, -1 * b) if len(hq) > 0: m = -1 * heappop(hq) ans += m print(ans)
p02948
import heapq import bisect N, M = list(map(int, input().split())) ABs = [list(map(int, input().split())) for _ in range(N)] lists = [[] for _ in range(10**5 + 1)] availables = [] for A, B in ABs: bisect.insort(lists[A], B) ans = 0 for i in range(1, M + 1): availables = list(heapq.merge(availables, lists[i])) if availables: ans += availables.pop() print(ans)
import heapq N, M = list(map(int, input().split())) ABs = [list(map(int, input().split())) for _ in range(N)] heapq.heapify(ABs) availables = [] ans = 0 for i in range(1, M + 1): while ABs: a = heapq.heappop(ABs) if a[0] == i: heapq.heappush(availables, -a[1]) else: heapq.heappush(ABs, a) break if availables: ans -= heapq.heappop(availables) print(ans)
p02948
from heapq import heappush, heappop N, M = list(map(int, input().split())) Data = [list(map(int, input().split())) for _ in range(N)] heap = [0] * M ans = 0 for i in range(1, M+1): N = len(Data) for j in range(N-1, -1, -1): if (Data[j][0] <= i): heappush(heap, -(Data.pop(j)[1])) ans += -(heappop(heap)) print(ans)
from heapq import heappush, heappop N, M = list(map(int, input().split())) Data = [list(map(int, input().split())) for _ in range(N)] Data.sort(reverse = True) heap = [0] * M ans = 0 for i in range(1, M+1): N = len(Data) for j in range(N-1, -1, -1): if Data[j][0] > i: break if (Data[j][0] <= i): heappush(heap, -(Data.pop(j)[1])) ans += -(heappop(heap)) print(ans)
p02948
import heapq n, m = list(map(int, input().split())) dic = {} for i in range(n): a, b = list(map(int, input().split())) if a >= m+1: continue if a in list(dic.keys()): dic[a].append(b) else: dic[a] = [b] ans = 0 l = [] heapq.heapify(l) for j in range(1, m+1): if j in list(dic.keys()): for x in dic[j]: heapq.heappush(l, -x) if l: ans += -l[0] heapq.heappop(l) print(ans)
import heapq n, m = list(map(int, input().split())) memo = [[] for _ in range(m+1)] for _ in range(n): a, b = list(map(int ,input().split())) if a <= m: memo[a].append(-b) ans = 0 HQ = [] heapq.heapify(HQ) for i in range(1, m+1): for j in memo[i]: heapq.heappush(HQ, j) if HQ: ans += heapq.heappop(HQ) print((-ans))
p02948
import heapq #listから複数index削除するやつ dellist = lambda items, indexes: [item for index, item in enumerate(items) if index not in indexes] [N,M]=[int(x) for x in input().split()] work=[[int(x) for x in input().split()] for _ in range(N)] work.sort() #日付の短い順にsortしておく これいる?いる!!!! tank=[] #候補を入れる res=0 #回答 indexlist=[] #バイトは重複不可なので決定済の労働をindexで管理 for i in range(M+1): #日付を後ろから見ていく for n,item in enumerate(work): #(日付的に働けるかをみて)候補たりうるものを探しheapqに突っ込む if item[0]<=i: heapq.heappush(tank,-item[1]) indexlist.append(n) work=dellist(work,indexlist) indexlist=[] #print("tank1:",tank) if len(tank)==0: #候補がなければ次の日へ #print("continue.") continue else: #候補があればもっとも良いのを回答に足す res -= heapq.heappop(tank) #print("res=",res) #print("tank2:",tank) print(res)
import heapq #listから複数index削除するやつ dellist = lambda items, indexes: [item for index, item in enumerate(items) if index not in indexes] [N,M]=[int(x) for x in input().split()] work=[[int(x) for x in input().split()] for _ in range(N)] work.sort() #日付の短い順にsortしておく これいる?いる!!!! tank=[] #候補を入れる res=0 #回答 j=0 for i in range(M+1): #日付を後ろから見ていく while j<=N-1 and work[j][0]<=i: #forじゃないのはheapqに入れた分はずっと候補なのでもうみなくていいため heapq.heappush(tank,-work[j][1]) j+=1 #print("tank1:",tank) if len(tank)==0: #候補がなければ次の日へ #print("continue.") continue else: #候補があればもっとも良いのを回答に足す res -= heapq.heappop(tank) #print("res=",res) #print("tank2:",tank) print(res)
p02948
from collections import deque N, M = list(map(int, input().split())) ab_list = [ list(map(int, input().split())) for i in range(N)] score_list = [[] for i in range(M+1)] for ab in ab_list: if ab[0] <= M: score_list[ab[0]].append(ab[1]) scores = [deque() for i in range(M+1)] for i in range(len(score_list)): scores[i] = deque(sorted(score_list[i], reverse=True)) ans = 0 for i in range(0, M+1): max_idx = -1 max_n = -1 for j in range(i+1): if len(scores[j]) == 0: continue if max_n < scores[j][0]: max_n = scores[j][0] max_idx = j if max_idx > 0: ans += scores[max_idx].popleft() print(ans)
import heapq N, M = list(map(int, input().split())) ab_list = [ list(map(int, input().split())) for i in range(N)] score_list = [[] for i in range(M+1)] for ab in ab_list: if ab[0] <= M: score_list[ab[0]].append(ab[1]) ans = 0 candidates = [] for i in range(0, M+1): for s in score_list[i]: heapq.heappush(candidates, -s) if len(candidates) == 0: continue ans -= heapq.heappop(candidates) print(ans)
p02948
from collections import defaultdict as dd from collections import deque n,m=list(map(int,input().split())) dic=dd(list) for i in range(1,m+1): dic[i]=[0]*m for i in range(n): a,b=list(map(int,input().split())) dic[a].append(b) for k,v in list(dic.items()): dic[k]=deque(sorted(dic[k],reverse=True)) #print(dic) ans=0 max2nd=[0]*m for i in reversed(list(range(m))): d=m-i a=dic[d][0] max2nd=deque(sorted(max2nd,reverse=True)) b=max2nd[0] if a>=b: ans+=dic[d].popleft() else: ans+=max2nd.popleft() max2nd+=dic[d] print(ans)
from collections import defaultdict as dd from collections import deque n,m=list(map(int,input().split())) dic=dd(list) for i in range(1,m+1): dic[i]=[0] for i in range(n): a,b=list(map(int,input().split())) dic[a].append(b) for k,v in list(dic.items()): dic[k]=deque(sorted(dic[k],reverse=True)) #print(dic) ans=0 max2nd=[0] for i in reversed(list(range(m))): d=m-i a=dic[d][0] max2nd=deque(sorted(max2nd,reverse=True)) b=max2nd[0] if a>=b: ans+=dic[d].popleft() if len(dic[d])==0: dic[d]=[0]; else: ans+=max2nd.popleft() if len(max2nd)==0: max2nd=[0]; max2nd+=dic[d] print(ans)
p02948
import heapq from collections import defaultdict N,M = list(map(int,input().split())) AB = defaultdict(list) for i in range(N): a,b = list(map(int,input().split())) if a not in AB: AB[a] = [-b] else: AB[a] += [-b] work = 0 AB2 = [] for i in range(1,M+1): AB2 += AB[i] heapq.heapify(AB2) if not AB2: continue b = heapq.heappop(AB2) work += -b print(work)
import heapq from collections import defaultdict N,M = list(map(int,input().split())) AB = defaultdict(list) for i in range(N): a,b = list(map(int,input().split())) if a not in AB: AB[a] = [-b] else: AB[a] += [-b] work = 0 AB2 = [] heapq.heapify(AB2) for i in range(1,M+1): for j in AB[i]: heapq.heappush(AB2,j) if not AB2: continue b = heapq.heappop(AB2) work += -b print(work)
p02948