input
stringlengths
20
127k
target
stringlengths
20
119k
problem_id
stringlengths
6
6
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)
import heapq N,M = list(map(int,input().split())) AB = [[] for i in range(M+1)] for i in range(N): a,b = list(map(int,input().split())) if a <= M: AB[a].append(-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
import heapq N,M = list(map(int,input().split())) AB = [[] for i in range(M+1)] for i in range(N): a,b = list(map(int,input().split())) if a <= M: AB[a].append(-b) work = 0 AB2 = [] heapq.heapify(AB2) for i in range(1,M+1): for j in AB[i]: heapq.heappush(AB2,j) if len(AB2) > 0: b = heapq.heappop(AB2) work += -b print(work)
import sys import heapq input = sys.stdin.readline N,M = list(map(int,input().split())) AB = [[] for i in range(M+1)] for i in range(N): a,b = list(map(int,input().split())) if a <= M: AB[a].append(-b) work = 0 AB2 = [] heapq.heapify(AB2) for i in range(1,M+1): for j in AB[i]: heapq.heappush(AB2,j) if len(AB2) > 0: b = heapq.heappop(AB2) work += -b print(work)
p02948
from heapq import heapify, heappush, heappop N,M = list(map(int,input().split())) AB = [] for i in range(N): a,b = list(map(int,input().split())) AB.append((-b,a)) heapify(AB) ans = 0 for i in range(1,M+1): rem = [] while True: if AB: ab = heappop(AB) if ab[1] <= i: ans -= ab[0] for j in rem: heappush(AB,j) break else: rem.append(ab) else: for j in rem: heappush(AB,j) break print(ans)
from heapq import heapify, heappush, heappop N,M = list(map(int,input().split())) AB = [[] for i in range(M+1)] for i in range(N): a,b = list(map(int,input().split())) if a <= M: AB[a].append(-b) AB2 = [] work = 0 heapify(AB2) for i in range(1,M+1): for j in AB[i]: heappush(AB2,j) if len(AB2): b = heappop(AB2) work -= b print(work)
p02948
def main(): import heapq n,m = list(map(int,input().split())) alb = {} for i in range(n): a,b = list(map(int,input().split())) if a not in list(alb.keys()): alb[a] = [b] else: alb[a] += [b] h = [] s = 0 for i in range(1,m+1): if i in list(alb.keys()): for j in range(len(alb[i])): heapq.heappush(h, -1*alb[i][j]) if len(h)>0: m = heapq.heappop(h) s += -1*m print(s) if __name__ == "__main__": main()
def main(): import heapq n,m = list(map(int,input().split())) jobs = {} for i in range(n): a,b = list(map(int,input().split())) if a not in list(jobs.keys()): jobs[a] = [b] else: jobs[a] += [b] ob = [] ans = 0 for i in range(1,m+1): if i in list(jobs.keys()): for j in range(len(jobs[i])): heapq.heappush(ob,-1*jobs[i][j]) if len(ob)>0: ans += heapq.heappop(ob)*-1 print(ans) if __name__ == "__main__": main()
p02948
import heapq n, m = list(map(int,input().split())) joblist = [] for i in range(n): a,b = list(map(int,input().split())) joblist.append([a,b]) joblist.sort(key=lambda x :x[0]) kouho = [] ans =0 for i in range(1,m+1): count=0 if joblist: for j,job in enumerate(joblist): if job[0]==i: heapq.heappush(kouho,(-1*job[1])) if job[0]!=i: if count: for _ in range(count-1): joblist.pop(0) if kouho: ans += -1*heapq.heappop(kouho) print(ans)
import heapq n, m = list(map(int,input().split())) joblist = {} for i in range(n): a,b = list(map(int,input().split())) if a in joblist: joblist[a].append(-1*b) else: joblist[a] = [-1*b] kouho = [] ans =0 for i in range(1,m+1): if i in joblist: for j in joblist[i]: heapq.heappush(kouho,j) if kouho: ans += heapq.heappop(kouho) print((-1*ans))
p02948
import sys sys.setrecursionlimit(10**9) def input(): return sys.stdin.readline().strip() def INT(): return int(eval(input())) def LIST(): return [int(x) for x in input().split()] MOD = 10**9 + 7 N, M = LIST() A = [[] for _ in range(10**4+1)] for _ in range(N): a, b = LIST() if a <= M: A[b].append(a) for a in A: a.sort(reverse=True) off = [False] * M ans = 0 i = 10**4 while i > 0: for a in A[i]: j = M - a while j >= 0: if off[j] == False: off[j] = True ans += i break; else: j -= 1 if off == [True] * M: break; else: i -= 1 print(ans)
import sys import bisect sys.setrecursionlimit(10**9) def input(): return sys.stdin.readline().strip() def INT(): return int(eval(input())) def LIST(): return [int(x) for x in input().split()] MOD = 10**9 + 7 N, M = LIST() A = [[] for _ in range(10**4+1)] for _ in range(N): a, b = LIST() if a <= M: A[b].append(a) for a in A: a.sort(reverse=True) off = list(range(1, M+1)) ans = 0 i = 10**4 while i > 0: for a in A[i]: attempt = bisect.bisect_left(off, a) if attempt < len(off): off.pop(attempt) ans += i if off == []: break; else: i -= 1 print(ans)
p02948
import sys sys.setrecursionlimit(10 ** 6) # input = sys.stdin.readline #### def int1(x): return int(x) - 1 def II(): return int(eval(input())) def MI(): return list(map(int, input().split())) def MI1(): return list(map(int1, input().split())) def LI(): return list(map(int, input().split())) def LI1(): return list(map(int1, input().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return input().split() def printlist(lst, k='\n'): print((k.join(list(map(str, lst))))) INF = float('inf') from math import ceil, floor, log2 from collections import deque from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product import heapq def solve(): n, m = MI() AB = [] for i in range(n): a, b = MI() AB.append((a, b)) AB = sorted(AB, key=lambda x: x[0]) # print(AB) q = [AB[0][1]] pre = AB[0][0] memo = {} for i in range(1, n): a, b = AB[i] if a == pre: q.append(b) else: memo[pre] = deque(sorted(q)) q = [b] pre = a memo[pre] = deque(sorted(q)) # print(memo) ans = 0 for i in range(1, m+1): q = memo.get(i, deque([])) # print(i, hp) if len(q) > 0: ans += q.pop() q.extend(memo.get(i + 1, [])) memo[i+1] = deque(sorted(q)[-m-i:]) print(ans) if __name__ == '__main__': solve()
import sys sys.setrecursionlimit(10 ** 6) # input = sys.stdin.readline #### def int1(x): return int(x) - 1 def II(): return int(eval(input())) def MI(): return list(map(int, input().split())) def MI1(): return list(map(int1, input().split())) def LI(): return list(map(int, input().split())) def LI1(): return list(map(int1, input().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return input().split() def printlist(lst, k='\n'): print((k.join(list(map(str, lst))))) INF = float('inf') from math import ceil, floor, log2 from collections import deque from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product from heapq import heapify, heappush, heappop def solve(): n, m = MI() jobs = [[] for _ in range(m)] for i in range(n): a, b = MI() if m-a >= 0: jobs[m-a].append(b) # print(jobs) hp = [] heapify(hp) ans = 0 for i in range(m-1, -1, -1): js = jobs[i] for j in js: heappush(hp, -j) if len(hp) > 0: ans += -heappop(hp) print(ans) if __name__ == '__main__': solve()
p02948
from collections import Counter, defaultdict import sys sys.setrecursionlimit(10 ** 5 + 10) input = sys.stdin.readline from math import factorial import heapq, bisect def main(): num, max_num = list(map(int, input().split())) data = [list(map(int, input().split())) for i in range(num)] data.sort(key=lambda x:(-x[1])) # for visit = [0 for i in range(max_num + 1)] ans = 0 # print(data) for i in range(num): day, value = data[i] ind_lim = day try: ind_insert = visit[ind_lim:].index(0) visit[ind_insert + ind_lim] = 1 ans += value except: pass # print(visit) print(ans) if __name__ == '__main__': main()
from collections import Counter, defaultdict import sys sys.setrecursionlimit(10 ** 5 + 10) input = sys.stdin.readline from math import factorial import heapq, bisect visit = [] def aaa(ind_lim, dame_num): global visit if ind_lim >= dame_num: return -1 elif visit[ind_lim] == -1: return -1 elif visit[ind_lim] == 0: return ind_lim else: visit[ind_lim] = aaa(visit[ind_lim], dame_num) return visit[ind_lim] def main(): num, max_num = list(map(int, input().split())) data = [list(map(int, input().split())) for i in range(num)] data.sort(key=lambda x:(-x[1])) # for global visit visit = [0 for i in range(max_num + 1)] ans = 0 for i in range(num): day, value = data[i] ind_lim = day # print(i, num, visit) ind_insert = aaa(ind_lim, max_num + 1) if ind_insert != -1: visit[ind_insert] = ind_insert + 1 ans += value # print('uuu') print(ans) if __name__ == '__main__': main()
p02948
# -*- coding: utf-8 -*- import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = float('inf') MOD = 10 ** 9 + 7 class SegTreeIndex: """ 以下のクエリを処理する 1.update: i番目の値をxに更新する 2.get_val: 区間[l, r)の値とindex(同値があった場合は一番右)を得る """ def __init__(self, n, func, init): """ :param n: 要素数(0-indexed) :param func: 値の操作に使う関数(min, max) :param init: 要素の初期値(単位元) """ self.n = n self.func = func self.init = init # nより大きい2の冪数 n2 = 1 while n2 < n: n2 <<= 1 self.n2 = n2 self.tree = [self.init] * (n2 << 1) self.index = [self.init] * (n2 << 1) # 1段目(最下段)の初期化 for i in range(n2): self.index[i+n2] = i # 2段目以降の初期化 for i in range(n2-1, -1, -1): # 全部右の子の値に更新 self.index[i] = self.index[i*2+1] def update(self, i, x): """ i番目の値をxに更新 :param i: index(0-indexed) :param x: update value """ i += self.n2 self.tree[i] = x while i > 1: left, right = min(i, i^1), max(i, i^1) if self.func(self.tree[left], self.tree[right]) == self.tree[right]: self.tree[i >> 1] = self.tree[right] self.index[i >> 1] = self.index[right] else: self.tree[i >> 1] = self.tree[left] self.index[i >> 1] = self.index[left] i >>= 1 def get_val(self, a, b): """ [a, b)の値を得る :param a: index(0-indexed) :param b: index(0-indexed) """ return self._get_val(a, b, 1, 0, self.n2) def _get_val(self, a, b, k, l, r): """ [a, b)の値を得る内部関数 :param k: 現在調べている区間のtree内index :param l, r: kが表す区間の左右端index [l, r) :return: kが表す区間と[a, b)の共通区間内での最小値。共通区間を持たない場合は初期値 """ # 範囲外なら初期値 if r <= a or b <= l: return (self.init, -1) # [a,b)が完全に[l,r)を包含するならtree[k]の値を採用 if a <= l and r <= b: return (self.tree[k], self.index[k]) # 一部だけ範囲内なら2つに分けて再帰的に調査 m = (l + r) // 2 left = self._get_val(a, b, k << 1, l, m) right = self._get_val(a, b, (k << 1) + 1, m, r) if self.func(left[0], right[0]) == right[0]: return right else: return left N, M = MAP() days = [[] for i in range(10**5+1)] for i in range(N): a, b = MAP() days[a].append(b) sti = SegTreeIndex(10**5+1, max, -INF) for i in range(10**5+1): days[i].sort() if len(days[i]): sti.update(i, days[i][-1]) ans = 0 for i in range(M-1, -1, -1): val, idx = sti.get_val(0, M-i+1) if val == -INF: continue ans += val days[idx].pop(-1) if len(days[idx]): sti.update(idx, days[idx][-1]) else: sti.update(idx, -INF) print(ans)
# -*- coding: utf-8 -*- import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = float('inf') MOD = 10 ** 9 + 7 class SegTreeIndex: """ 以下のクエリを処理する 1.update: i番目の値をxに更新する 2.get_val: 区間[l, r)の値とindex(同値があった場合は一番左)を得る """ def __init__(self, n, func, init): """ :param n: 要素数(0-indexed) :param func: 値の操作に使う関数(min, max) :param init: 要素の初期値(単位元) """ self.n = n self.func = func self.init = init # nより大きい2の冪数 n2 = 1 while n2 < n: n2 <<= 1 self.n2 = n2 self.tree = [self.init] * (n2 << 1) self.index = [self.init] * (n2 << 1) # 1段目(最下段)の初期化 for i in range(n2): self.index[i+n2] = i # 2段目以降の初期化 for i in range(n2-1, -1, -1): # 全部左の子の値に更新 self.index[i] = self.index[i*2] def update(self, i, x): """ i番目の値をxに更新 :param i: index(0-indexed) :param x: update value """ i += self.n2 self.tree[i] = x while i > 1: left, right = min(i, i^1), max(i, i^1) if self.func(self.tree[left], self.tree[right]) == self.tree[left]: self.tree[i >> 1] = self.tree[left] self.index[i >> 1] = self.index[left] else: self.tree[i >> 1] = self.tree[right] self.index[i >> 1] = self.index[right] i >>= 1 def get_val(self, a, b): """ [a, b)の値を得る :param a: index(0-indexed) :param b: index(0-indexed) """ return self._get_val(a, b, 1, 0, self.n2) def _get_val(self, a, b, k, l, r): """ [a, b)の値を得る内部関数 :param k: 現在調べている区間のtree内index :param l, r: kが表す区間の左右端index [l, r) :return: kが表す区間と[a, b)の共通区間内での最小値。共通区間を持たない場合は初期値 """ # 範囲外なら初期値 if r <= a or b <= l: return (self.init, -1) # [a,b)が完全に[l,r)を包含するならtree[k]の値を採用 if a <= l and r <= b: return (self.tree[k], self.index[k]) # 一部だけ範囲内なら2つに分けて再帰的に調査 m = (l + r) // 2 left = self._get_val(a, b, k << 1, l, m) right = self._get_val(a, b, (k << 1) + 1, m, r) if self.func(left[0], right[0]) == left[0]: return left else: return right N, M = MAP() days = [[] for i in range(10**5+1)] for i in range(N): a, b = MAP() days[a].append(b) sti = SegTreeIndex(10**5+1, max, -INF) for i in range(10**5+1): days[i].sort() if len(days[i]): sti.update(i, days[i][-1]) ans = 0 for i in range(M-1, -1, -1): val, idx = sti.get_val(0, M-i+1) if val == -INF: continue ans += val days[idx].pop(-1) if len(days[idx]): sti.update(idx, days[idx][-1]) else: sti.update(idx, -INF) print(ans)
p02948
# -*- coding: utf-8 -*- import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = float('inf') MOD = 10 ** 9 + 7 class SegTreeIndex: """ 以下のクエリを処理する 1.update: i番目の値をxに更新する 2.get_val: 区間[l, r)の値とindex(同値があった場合は一番左)を得る """ def __init__(self, n, func, init): """ :param n: 要素数(0-indexed) :param func: 値の操作に使う関数(min, max) :param init: 要素の初期値(単位元) """ self.n = n self.func = func self.init = init # nより大きい2の冪数 n2 = 1 while n2 < n: n2 <<= 1 self.n2 = n2 self.tree = [self.init] * (n2 << 1) self.index = [self.init] * (n2 << 1) # 1段目(最下段)の初期化 for i in range(n2): self.index[i+n2] = i # 2段目以降の初期化 for i in range(n2-1, -1, -1): # 全部左の子の値に更新 self.index[i] = self.index[i*2] def update(self, i, x): """ i番目の値をxに更新 :param i: index(0-indexed) :param x: update value """ i += self.n2 self.tree[i] = x while i > 1: left, right = min(i, i^1), max(i, i^1) if self.func(self.tree[left], self.tree[right]) == self.tree[left]: self.tree[i >> 1] = self.tree[left] self.index[i >> 1] = self.index[left] else: self.tree[i >> 1] = self.tree[right] self.index[i >> 1] = self.index[right] i >>= 1 def get_val(self, a, b): """ [a, b)の値を得る :param a: index(0-indexed) :param b: index(0-indexed) """ return self._get_val(a, b, 1, 0, self.n2) def _get_val(self, a, b, k, l, r): """ [a, b)の値を得る内部関数 :param k: 現在調べている区間のtree内index :param l, r: kが表す区間の左右端index [l, r) :return: kが表す区間と[a, b)の共通区間内での最小値。共通区間を持たない場合は初期値 """ # 範囲外なら初期値 if r <= a or b <= l: return (self.init, -1) # [a,b)が完全に[l,r)を包含するならtree[k]の値を採用 if a <= l and r <= b: return (self.tree[k], self.index[k]) # 一部だけ範囲内なら2つに分けて再帰的に調査 m = (l + r) // 2 left = self._get_val(a, b, k << 1, l, m) right = self._get_val(a, b, (k << 1) + 1, m, r) if self.func(left[0], right[0]) == left[0]: return left else: return right N, M = MAP() days = [[] for i in range(10**5+1)] for i in range(N): a, b = MAP() days[a].append(b) sti = SegTreeIndex(10**5+1, max, -INF) for i in range(10**5+1): days[i].sort() if len(days[i]): sti.update(i, days[i][-1]) ans = 0 for i in range(M-1, -1, -1): val, idx = sti.get_val(0, M-i+1) if val == -INF: continue ans += val days[idx].pop(-1) if len(days[idx]): sti.update(idx, days[idx][-1]) else: sti.update(idx, -INF) print(ans)
# -*- coding: utf-8 -*- import sys from heapq import heappush, heappop def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(eval(input())) def MAP(): return list(map(int, input().split())) def LIST(): return list(map(int, input().split())) def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') sys.setrecursionlimit(10 ** 9) INF = float('inf') MOD = 10 ** 9 + 7 N, M = MAP() # 日数別に報酬の値を格納 days = [[] for i in range(10**5+1)] for i in range(N): a, b = MAP() days[a].append(b) que = [] ans = 0 # 制約の厳しい後半から確認していく for i in range(M-1, -1, -1): a = M-i # 残日数aで使えるようになるタスクを追加 for b in days[a]: heappush(que, (-b, a)) # キューに中身があったら最大のものを取得 if len(que): b, a = heappop(que) ans += -b print(ans)
p02948
#!/usr/bin/env python import sys n,m= [ int(x) for x in sys.stdin.readline().split() ] baito=[ [] for _ in range(m) ] for _ in range(n): ai,bi = [ int(x) for x in sys.stdin.readline().split() ] ai-=1 if ai<m: baito[ai].append(bi) # tot=0 cur=[] for i in range(m): ref= baito[i] if ref: cur+= ref cur.sort() # if not cur: continue tot+= cur.pop() # print( tot )
#!/usr/bin/env python import sys n,m= [ int(x) for x in sys.stdin.readline().split() ] baito=[ [] for _ in range(m) ] for _ in range(n): ai,bi = [ int(x) for x in sys.stdin.readline().split() ] ai-=1 if ai<m: baito[ai].append(bi) # for i in range(m): if baito[i]: baito[i].sort() # tot=0 cur=[] for i in range(m): ref= baito[i] if ref: if not cur: cur= ref else: if ref[-1]<=cur[0]: cur= ref + cur elif cur[-1]<=ref[0]: cur= cur + ref else: cur+= ref cur.sort() # if not cur: continue tot+= cur.pop() # print( tot )
p02948
#!/usr/bin/env python import sys import heapq n,m= [ int(x) for x in sys.stdin.readline().split() ] baito=[ [] for _ in range(m) ] for _ in range(n): ai,bi = [ int(x) for x in sys.stdin.readline().split() ] ai-=1 if ai<m: baito[ai].append(-bi) # tot=0 cur=[] for i in range(m): ref= baito[i] if ref: if not cur: cur= ref else: cur+= ref heapq.heapify(cur) # if not cur: continue tot+= heapq.heappop(cur) # print(( -tot ))
#!/usr/bin/env python import sys import heapq n,m= [ int(x) for x in sys.stdin.readline().split() ] baito=[ [] for _ in range(m) ] for _ in range(n): ai,bi = [ int(x) for x in sys.stdin.readline().split() ] ai-=1 if ai<m: baito[ai].append(-bi) # tot=0 cur=[] for i in range(m): ref= baito[i] if ref: heapq.heapify(ref) while ref: heapq.heappush(cur,heapq.heappop(ref)) # if not cur: continue tot+= heapq.heappop(cur) # print(( -tot ))
p02948
import heapq n, m = list(map(int, input().split())) byte = [[] for _ in range(100001)] for _ in range(n): a, b = list(map(int, input().split())) byte[a].append(b) search = [] day = 1 ans = 0 while day-1 != m: for b in byte[day]: heapq.heappush(search, -b) if not search: day += 1 continue tmp = -heapq.heappop(search) ans += tmp day += 1 print(ans)
import heapq n, m = list(map(int, input().split())) byte = [[] for _ in range(m+2)] for _ in range(n): a, b = list(map(int, input().split())) if a > m+1: continue byte[a].append(b) search = [] day = 1 ans = 0 while day-1 != m: for b in byte[day]: heapq.heappush(search, -b) if not search: day += 1 continue tmp = -heapq.heappop(search) ans += tmp day += 1 print(ans)
p02948
# D - Summer Vacation 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 heapq import heappop, heappush sr = lambda: sys.stdin.readline().rstrip() ir = lambda: int(sr()) lr = lambda: list(map(int, sr().split())) N, M = lr() works = [[] for _ in range(M+1)] # M-1日後から選んでいく, for _ in range(N): a, b = lr() if a > M: continue works[a].append(-b) heap = [] answer = 0 for i in range(1, M+1): for x in works[i]: heappush(heap, x) if len(heap) == 0: continue answer += heappop(heap) answer *= -1 print(answer) # 08
p02948
import sys from collections import deque sr = lambda: sys.stdin.readline().rstrip() ir = lambda: int(sr()) lr = lambda: list(map(int, sr().split())) N, M = lr() AB = [lr() for _ in range(N)] #Bが報酬 AB = [(a, b) for a, b in AB if a<=M] AB.sort(reverse=True, key=lambda x: x[1]) AB = deque(AB) answer = 0 used = set() out = set() while AB: x, y = AB.popleft() if x > M or x in out: continue else: z = x while z in used: z += 1 if z > M: out.add(x) continue used.add(z) answer += y print(answer)
import sys from heapq import heappush, heappop sr = lambda: sys.stdin.readline().rstrip() ir = lambda: int(sr()) lr = lambda: list(map(int, sr().split())) N, M = lr() works = [[] for _ in range(M+1)] #Bが報酬 for _ in range(N): a, b = lr() if a <= M: works[a].append(b) cand = [] answer = 0 for i in range(1, M+1): for work in works[i]: heappush(cand, -work) if cand: answer -= heappop(cand) print(answer)
p02948
import sys from heapq import heappush, heappop, merge, heapify sr = lambda: sys.stdin.readline().rstrip() ir = lambda: int(sr()) lr = lambda: list(map(int, sr().split())) N, M = lr() works = [[] for _ in range(M+1)] #Bが報酬 for _ in range(N): a, b = lr() if a <= M: works[a].append(-b) cand = [] answer = 0 for i in range(1, M+1): cand = list(merge(cand, works[i])) heapify(cand) if cand: answer -= heappop(cand) print(answer)
import sys from heapq import heapify, heappop, heappush sr = lambda: sys.stdin.readline().rstrip() ir = lambda: int(sr()) lr = lambda: list(map(int, sr().split())) N, M = lr() jobs = [[] for _ in range(M+1)] for _ in range(N): a, b = lr() if a > M: continue jobs[a].append(b) answer = 0 candidate = [] for i in range(1, M+1): for job in jobs[i]: heappush(candidate, -job) if candidate: answer += heappop(candidate) answer *= -1 print(answer) # 31
p02948
from collections import defaultdict, deque import sys import math sys.setrecursionlimit(100000) MIN = -10 ** 9 MAX = 10 ** 9 + 7 def main(): # N = int(input()) N, M = [int(a) for a in input().split()] AB = [ [int(a) for a in input().split()] for _ in range(N) ] AB = sorted(AB, key=lambda it: it[1]) ARR = [[] for _ in range(M + 1)] for a, b in AB: if a > M: continue ARR[a].append(b) # print("ARR", ARR) res = 0 q = [] for i in range(1, M + 1): current = ARR[i] if len(current) != 0: if len(q) != 0: if current[-1] > q[-1]: res += current.pop() else: res += q.pop() else: res += current.pop() q += current q.sort() # q = sorted(q + current) q = q[-(M - i):] else: if len(q) != 0: res += q.pop() else: pass print(res) if __name__ == '__main__': main()
from collections import defaultdict, deque import sys import heapq import math sys.setrecursionlimit(100000) MIN = -10 ** 9 MAX = 10 ** 9 + 7 def main(): # N = int(input()) N, M = [int(a) for a in input().split()] AB = [ [int(a) for a in input().split()] for _ in range(N) ] # AB = sorted(AB, key=lambda it: it[1]) ARR = [[] for _ in range(M + 1)] for a, b in AB: if a > M: continue ARR[a].append(b) # print("ARR", ARR) res = 0 q = [] for i in range(1, M + 1): current = ARR[i] for a in current: heapq.heappush(q, -a) if len(q) != 0: res -= heapq.heappop(q) print(res) if __name__ == '__main__': main()
p02948
import sys, math, itertools, bisect, copy, re from collections import Counter, deque, defaultdict from itertools import accumulate, permutations, combinations, takewhile, compress, cycle # from functools import reduce # from math import ceil, floor, log10, log2, factorial # from pprint import pprint INF = float('inf') MOD = 10 ** 9 + 7 EPS = 10 ** -7 sys.setrecursionlimit(1000000) # N = int(input()) # N,M = [int(x) for x in input().split()] # V = [[0] * 100 for _ in range(100)] # A = [int(input()) for _ in range(N)] # DP = [[0] * 100 for _ in range(100)] # DP = defaultdict(lambda: float('inf')) N, M = [int(x) for x in input().split()] AB = [] for _ in range(N): A, B = [int(x) for x in input().split()] AB.append((A, B)) AB = sorted(AB, key=lambda x: (-x[1], -x[0])) ans = 0 used = set() for m in range(M, -1, -1): for i in range(N): a, b = AB[i] if a + m <= M and i not in used: ans += b used.add(i) # print("m:{}, a:{},b{}".format(m, a, b)) break print(ans)
import sys, math, itertools, bisect, copy, re from collections import Counter, deque, defaultdict from itertools import accumulate, permutations, combinations, takewhile, compress, cycle # from functools import reduce # from math import ceil, floor, log10, log2, factorial # from pprint import pprint import heapq INF = float('inf') MOD = 10 ** 9 + 7 EPS = 10 ** -7 sys.setrecursionlimit(1000000) # N = int(input()) # N,M = [int(x) for x in input().split()] # V = [[0] * 100 for _ in range(100)] # A = [int(input()) for _ in range(N)] # DP = [[0] * 100 for _ in range(100)] # DP = defaultdict(lambda: float('inf')) N, M = [int(x) for x in input().split()] AB = [] for _ in range(N): A, B = [int(x) for x in input().split()] AB.append((A, B)) AB = sorted(AB, key=lambda x: x[0]) idx = 0 available = [] ans = 0 for d in range(1, M + 1): while idx < N and AB[idx][0] <= d: heapq.heappush(available, -AB[idx][1]) idx += 1 if len(available) != 0: ans += (-heapq.heappop(available)) print(ans)
p02948
import math, heapq from operator import itemgetter from collections import defaultdict INF = float("inf") MOD = int(1e9 + 7) N, M = list(map(int, input().split())) AB = [list(map(int, input().split())) for _ in range(N)] ab = sorted(AB) v, que = 0, [] for n in range(1, M + 1): while ab: a, b = ab.pop(0) if a <= n: heapq.heappush(que, -b) else: ab.insert(0, (a, b)) break if que: v += -heapq.heappop(que) print(v)
import math, heapq from operator import itemgetter from collections import defaultdict INF = float("inf") MOD = int(1e9 + 7) def main(): N, M = list(map(int, input().split())) AB = defaultdict(list) for _ in range(N): a, b = list(map(int, input().split())) AB[a].append(b) v, que = 0, [] for m in range(1, M + 1): if m in AB: for b in AB[m]: heapq.heappush(que, -b) if que: v += -heapq.heappop(que) print(v) main()
p02948
import math, heapq from operator import itemgetter as ig from collections import defaultdict as dd # 定数 INF = float("inf") MOD = int(1e9 + 7) # ヒープ class heapque: def __init__(self, *args): self.que = [] for arg in args: self.push(arg) def push(self, v): heapq.heappush(self.que, v) def pop(self): return heapq.heappop(self.que) # 最大公約数 / 最小公倍数 def gcd(v1, v2): if v2 == 0: return v1 return gcd(v2, v1 % v2) def lcm(v1, v2): return (v1 // gcd(v1, v2)) * v2 # 二分探索 def bsr(a, v, lo=0, hi=None): if hi == None: hi = len(a) - 1 if hi < lo: return lo mi = (lo + hi) // 2 if v < a[mi]: return bsr(a, v, lo, mi - 1) else: return bsr(a, v, mi + 1, hi) # Union-Find木 class uft: def __init__(self, n): self.height = [1] * n self.group = [-1] * n def root(self, v): if self.group[v] < 0: return v self.group[v] = self.root(self.group[v]) return self.group[v] def size(self, v): return - self.group[self.root(v)] def merge(self, v1, v2): v1, v2 = self.root(v1), self.root(v2) if v1 == v2: return if self.height[v1] < self.height[v2]: self.group[v2] += self.group[v1] self.group[v1] = v2 self.height[v2] = max(self.height[v1] + 1, self.height[v2]) else: self.group[v1] += self.group[v2] self.group[v2] = v1 self.height[v1] = max(self.height[v1] , self.height[v2] + 1) # グラフ class graph: def __init__(self, n): self.n = n self.graph = [[] for _ in range(n)] def append(self, v1, v2, cost=1): self.graph[v1].append((v2, cost)) # 最短経路:ダイクストラ法(Dijkstra's Algorithm) def dks(self, v): costs = [INF] * self.n costs[v] = 0 done = [False] * self.n heap = heapque((0, v)) while heap.que: c_cost, c_index = heap.pop() if done[c_index]: continue done[c_index] = True for n_index, n_cost in self.graph[c_index]: if c_cost + n_cost < costs[n_index]: costs[n_index] = c_cost + n_cost heap.push((costs[n_index], n_index)) return costs def main(): N, M = list(map(int, input().split())) AB = [list(map(int, input().split())) for _ in range(N)] ab = sorted(AB) heap = heapque() v = 0 for m in range(1, M + 1): while ab: if ab[0][0] <= m: _, b = ab.pop(0) heap.push(-b) else: break if heap.que: v += -heap.pop() print(v) main()
import math, heapq from operator import itemgetter as ig from collections import defaultdict as dd # 定数 INF = float("inf") MOD = int(1e9 + 7) # ヒープ class heapque: def __init__(self, *args): self.que = [] for arg in args: self.push(arg) def push(self, v): heapq.heappush(self.que, v) def pop(self): return heapq.heappop(self.que) # 最大公約数 / 最小公倍数 def gcd(v1, v2): if v2 == 0: return v1 return gcd(v2, v1 % v2) def lcm(v1, v2): return (v1 // gcd(v1, v2)) * v2 # 二分探索 def bsr(a, v, lo=0, hi=None): if hi == None: hi = len(a) - 1 if hi < lo: return lo mi = (lo + hi) // 2 if v < a[mi]: return bsr(a, v, lo, mi - 1) else: return bsr(a, v, mi + 1, hi) # Union-Find木 class uft: def __init__(self, n): self.height = [1] * n self.group = [-1] * n def root(self, v): if self.group[v] < 0: return v self.group[v] = self.root(self.group[v]) return self.group[v] def size(self, v): return - self.group[self.root(v)] def merge(self, v1, v2): v1, v2 = self.root(v1), self.root(v2) if v1 == v2: return if self.height[v1] < self.height[v2]: self.group[v2] += self.group[v1] self.group[v1] = v2 self.height[v2] = max(self.height[v1] + 1, self.height[v2]) else: self.group[v1] += self.group[v2] self.group[v2] = v1 self.height[v1] = max(self.height[v1] , self.height[v2] + 1) # グラフ class graph: def __init__(self, n): self.n = n self.graph = [[] for _ in range(n)] def append(self, v1, v2, cost=1): self.graph[v1].append((v2, cost)) # 最短経路:ダイクストラ法(Dijkstra's Algorithm) def dks(self, v): costs = [INF] * self.n costs[v] = 0 done = [False] * self.n heap = heapque((0, v)) while heap.que: c_cost, c_index = heap.pop() if done[c_index]: continue done[c_index] = True for n_index, n_cost in self.graph[c_index]: if c_cost + n_cost < costs[n_index]: costs[n_index] = c_cost + n_cost heap.push((costs[n_index], n_index)) return costs def main(): N, M = list(map(int, input().split())) AB = [list(map(int, input().split())) for _ in range(N)] ab = dd(list) for a, b in AB: ab[a] += [b] heap = heapque() v = 0 for m in range(1, M + 1): if m in ab: for b in ab[m]: heap.push(-b) if heap.que: v += -heap.pop() print(v) main()
p02948
import sys from collections import deque def main(): input = sys.stdin.readline N, M = list(map(int, input().split())) q = deque() for _ in range(N): a, b = list(map(int, input().split())) if a > M: continue q.append((a, b)) ans = 0 cnt = 1 while cnt <= M: q_ = deque() max_a, max_b = 0, 0 while q: a, b = q.pop() if a > cnt: q_.append((a, b)) continue elif b > max_b: q_.append((max_a, max_b)) max_a, max_b = a, b else: q_.append((a, b)) ans += max_b cnt += 1 q = q_ return ans if __name__ == '__main__': print((main()))
import sys from collections import defaultdict from heapq import heappop, heappush def main(): input = sys.stdin.readline 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) ans = 0 heap = [] for i in range(1, M+1): for b in d[i]: heappush(heap, b) if heap: ans -= heappop(heap) return ans if __name__ == '__main__': print((main()))
p02948
import sys input = sys.stdin.readline def main(): n,m = list(map(int,input().split())) money = [[] for _ in range(10**5)] dp = [0] * m for _ in range(n): a,b = list(map(int,input().split())) money[a-1].append(b) max1 = [] for i in range(m): max1 += money[i] if max1: dp[i] = max1.pop(max1.index(max(max1))) if i >= 1: dp[i] += dp[i-1] print((dp[-1])) if __name__ == '__main__': main()
import sys import heapq imput = sys.stdin.readline def main(): n,m = list(map(int,input().split())) money = [[] for _ in range(10**5)] dp = [0] * m hq = [] heapq.heapify(hq) for _ in range(n): a,b = list(map(int,input().split())) money[a-1] += [b] for i in range(m): for j in money[i]: heapq.heappush(hq,-j) if hq: dp[i] += -heapq.heappop(hq) if i >= 1: dp[i] += dp[i-1] print((dp[-1])) if __name__ == '__main__': main()
p02948
import logging logging.basicConfig(level=logging.INFO, format="%(message)s") #logging.disable(logging.CRITICAL) def main(): N, M = list(map(int, input().split())) A_B = [list(map(int, input().split()))[::-1] for _ in range(N)] logging.info("Hello!") sorted_A_B = sorted(A_B, reverse=True) logging.info(sorted_A_B) total = 0 for i in range(N): if i >= M: break if sorted_A_B[i][1] + i <= M and sorted_A_B[i][0] != -1: total += sorted_A_B[i][0] sorted_A_B[i][0] = -1 else: for k in range(i, N): if sorted_A_B[k][1] + i <= M and sorted_A_B[k][0] != -1: total += sorted_A_B[k][0] sorted_A_B[k][0] = -1 print(total) if __name__ == "__main__": main()
import logging import heapq logging.basicConfig(level=logging.INFO, format="%(message)s") #logging.disable(logging.CRITICAL) def main(): N, M = list(map(int, input().split())) A_B = [list(map(int, input().split())) for _ in range(N)] logging.info("Hello!") A_B.sort() logging.info(A_B) heap_Q = [] curr = 0 total = 0 for i in range(1, M + 1): while curr < N and A_B[curr][0] <= i: heapq.heappush(heap_Q, A_B[curr][1] * (-1)) curr += 1 if heap_Q: total += (-1) * heapq.heappop(heap_Q) print(total) if __name__ == "__main__": main()
p02948
from collections import Counter,defaultdict import sys,heapq,bisect,math,itertools,string,queue,datetime mod = 10**9+7 def inp(): return int(sys.stdin.readline()) def inpl(): return list(map(int, sys.stdin.readline().split())) def inpl_str(): return list(sys.stdin.readline().split()) n,m = inpl() aa = [[] for i in range(m)] ans = 0 for i in range(n): a,b = inpl() if a <= m: aa[a-1].append(b) for i in range(m): if aa[i]: aa[i].sort() ans += aa[i].pop() if i < m-1: for j in aa[i]: aa[i+1].append(j) print(ans)
from collections import Counter,defaultdict import sys,heapq,bisect,math,itertools,string,queue,datetime mod = 10**9+7 def inp(): return int(sys.stdin.readline()) def inpl(): return list(map(int, sys.stdin.readline().split())) def inpl_str(): return list(sys.stdin.readline().split()) n,m = inpl() aa = [[] for i in range(m)] ans = 0 max_b = [0] * m for i in range(n): a,b = inpl() if a <= m: aa[a-1].append(b) for i in range(m): if aa[i]: max_b[i] = max(aa[i]) for i in range(m): mmm = 0 max_i = -1 for j in range(i+1): if max_b[j] > mmm: mmm = max_b[j] max_i = j ans += mmm if len(aa[max_i]) and max_i != -1: aa[max_i].remove(mmm) if aa[max_i]: max_b[max_i] = max(aa[max_i]) else : max_b[max_i] = 0 print(ans)
p02948
from collections import Counter,defaultdict import sys,heapq,bisect,math,itertools,string,queue,datetime mod = 10**9+7 def inp(): return int(sys.stdin.readline()) def inpl(): return list(map(int, sys.stdin.readline().split())) def inpl_str(): return list(sys.stdin.readline().split()) n,m = inpl() aa = [[] for i in range(m)] ans = 0 max_b = [0] * m for i in range(n): a,b = inpl() if a <= m: aa[a-1].append(b) for i in range(m): if aa[i]: max_b[i] = max(aa[i]) for i in range(m): mmm = 0 max_i = -1 for j in range(i+1): if max_b[j] > mmm: mmm = max_b[j] max_i = j ans += mmm if len(aa[max_i]) and max_i != -1: aa[max_i].remove(mmm) if aa[max_i]: max_b[max_i] = max(aa[max_i]) else : max_b[max_i] = 0 print(ans)
from collections import Counter,defaultdict import sys,heapq,bisect,math,itertools,string,queue,datetime mod = 10**9+7 def inp(): return int(sys.stdin.readline()) def inpl(): return list(map(int, sys.stdin.readline().split())) def inpl_str(): return list(sys.stdin.readline().split()) ans = 0 hq = [] n,m = inpl() ab = [[] for i in range(m)] for i in range(n): a,b = inpl() if a <= m: ab[a-1].append(b) heapq.heapify(hq) for i in range(m): for j in ab[i]: heapq.heappush(hq,-j) if hq: ans -= heapq.heappop(hq) print(ans)
p02948
from collections import deque import sys input = sys.stdin.readline def main(): N,M = list(map(int,input().split())) paylist = [[] for _ in range(M)] for _ in range(N): a,b = list(map(int,input().split())) if M >= a: paylist[a-1].append(b) work = [] payment = 0 for i in range(M): work += paylist[i] work.sort() if work != []: payment += work.pop() print(payment) if __name__ == "__main__": main()
import heapq import sys input = sys.stdin.readline def main(): N,M = list(map(int,input().split())) paylist = [[] for _ in range(M)] for _ in range(N): a,b = list(map(int,input().split())) if M >= a: heapq.heappush(paylist[a-1],-b) work = [] payment = 0 for i in range(M): for j in paylist[i]: heapq.heappush(work,j) if work: payment += heapq.heappop(work) print((-payment)) if __name__ == "__main__": main()
p02948
n,m = list(map(int,input().split())) lis = [[] for _ in range(10**5)] for _ in range(n): a,b = list(map(int,input().split())) lis[a-1].append(b) ans = 0 li = [] for i in range(m): li += lis[i] li.sort(reverse=True) try: ans += li[0] del(li[0]) except: continue print(ans)
from heapq import heappush,heappop,heapify n,m = list(map(int,input().split())) lis = [[] for i in range(m+1)] for _ in range(n): a,b = list(map(int,input().split())) if a <= m: lis[a].append(-b) ans = 0 num = [] heapify(num) for j in range(1,m+1): for nu in lis[j]: heappush(num,nu) if len(num) > 0: work = heappop(num) ans += -work print(ans)
p02948
import heapq n,m = list(map(int,input().split())) lb = [[] for _ in range(10**5+5)] for _ in range(n): a,b = list(map(int,input().split())) lb[a].append(b) res = 0 q = [] heapq.heapify(q) for i in range(1,m+1): for b in lb[i]: heapq.heappush(q,b * (-1)) if q: res += heapq.heappop(q) * (-1) print(res)
import heapq def main(): n,m = list(map(int,input().split())) lb = [[] for _ in range(10**5+5)] for _ in range(n): a,b = list(map(int,input().split())) lb[a].append(b) res = 0 q = [] heapq.heapify(q) for i in range(1,m+1): for b in lb[i]: heapq.heappush(q,b * (-1)) if q: res += heapq.heappop(q) * (-1) print(res) main()
p02948
#coding utf-8 import heapq N,M=list(map(int,input().split())) AB= [[int(i) for i in input().split()] for i in range(N)] work=[] heapq.heapify(work) ans=0 for i in range(1,M+1): for j in range(N): if AB[j][0]==i: heapq.heappush(work,-AB[j][1]) if work: tmp=heapq.heappop(work) ans-=tmp print(ans)
#coding utf-8 import heapq N,M=list(map(int,input().split())) AB= [[int(i) for i in input().split()] for i in range(N)] AB.sort(reverse=True) work=[] heapq.heapify(work) ans=0 for i in range(1,M+1): while AB and AB[-1][0] <=i: tmp=AB.pop() heapq.heappush(work,-tmp[1]) if work: tmp=heapq.heappop(work) ans-=tmp print(ans)
p02948
# -*- coding: utf-8 -*- """ Created on Sun Apr 12 02:06:07 2020 @author: Kanaru Sato """ import heapq n,m = list(map(int, input().split())) albeit = [] for i in range(m+1): albeit.append([]) for i in range(n): a,b = list(map(int, input().split())) if a <= m: albeit[a].append(-1*b) ##print(albeit) for i in range(m): heapq.heapify(albeit[i+1]) ##print(albeit) sum = 0 for i in range(m): day = i+1 #day:1->m ##print("day=",day) minlist = [] for j in range(day): if len(albeit[j+1]) >= 1: minlist.append(albeit[j+1][0]) else: minlist.append(0) ##print(minlist) m = min(minlist) if m != 0: Maxalbeitindex = minlist.index(m) sum += -1*heapq.heappop(albeit[Maxalbeitindex+1]) ##print(sum) print(sum)
# -*- coding: utf-8 -*- """ Created on Sun Apr 12 02:06:07 2020 @author: Kanaru Sato """ import heapq n,m = list(map(int, input().split())) albeit = [] for i in range(m+1): albeit.append([]) for i in range(n): a,b = list(map(int, input().split())) if a <= m: albeit[a].append(-1*b) ##print(albeit) Heap = [0] sum = 0 for i in range(m): day = i+1 for money in albeit[day]: heapq.heappush(Heap, money) if Heap[0] != 0: daymax = heapq.heappop(Heap)*-1 sum += daymax print(sum)
p02948
import heapq n, m = list(map(int, input().split())) ba=[] for i in range(n): a, b = list(map(int, input().split())) ba.append([-b, a]) heapq.heapify(ba) ans=0 for i in range(m - 1, -1, -1): not_use = [] while ba: l = heapq.heappop(ba) if l[1] + i <= m: ans += l[0] * -1 break else: not_use.append(l) for j in not_use: heapq.heappush(ba, j) print(ans)
import heapq n, m = list(map(int, input().split())) l = [[] for _ in range(m)] for i in range(n): a, b = list(map(int, input().split())) if a <= m: l[a-1].append(b) a = [] ans = 0 for i in range(m): for j in l[i]: heapq.heappush(a, (-1) * j) if a: ans += heapq.heappop(a) * (-1) print(ans)
p02948
import heapq n ,m = list(map(int,input().split())) li = [] for i in range(n): x, y= list(map(int,input().split())) li.append([x,y]) li.sort() heap = [] ans = 0 for i in range(m): bar = True while bar: if len(li) == 0: break if i >= li[0][0] - 1: price = li.pop(0) heapq.heappush(heap,-price[1]) else: bar = False try: ans += -heapq.heappop(heap) except IndexError: ans += 0 print(ans)
import heapq n ,m = list(map(int,input().split())) li = [] for i in range(n): x, y= list(map(int,input().split())) li.append([x,y]) li.sort() heap = [] ans = 0 j = 0 for i in range(1, m + 1): while j < n and li[j][0] <= i: heapq.heappush(heap,-li[j][1]) j += 1 if heap: ans += -heapq.heappop(heap) print(ans)
p02948
n,m = list(map(int,input().split())) work = [list(map(int, input().split())) for i in range(n)] from operator import itemgetter work = sorted(work,key=itemgetter(1),reverse=True) count = 0 for i in range(1,m+1): for j in range(len(work)): if work[j][0] <= i: count += work[j][1] del work[j] break print(count)
import heapq n,m = list(map(int,input().split())) a = [0] * n b = [0] * n info = [[] for _ in range(m+1)] for i in range(n): a[i], b[i] = list(map(int,input().split())) if a[i] <= m: info[a[i]].append(b[i]) l = [] count = 0 for j in range(1,m+1): for x in info[j]: heapq.heappush(l,-x) if l != []: count += -heapq.heappop(l) print(count)
p02948
import heapq N, M = list(map(int, input().split())) AB = [list(map(int, input().split())) for _ in range(N)] pay_list = [] heapq.heapify(pay_list) ans = 0 for i in range(1, M+1): # 実行可能なバイトリストを抽出 for j in range(len(AB)-1, -1, -1): a = AB[j][0] b = AB[j][1] if a <= i: # heappopで最大値を取り出したいため、補正値の(-1)を乗じておく heapq.heappush(pay_list, b*(-1)) AB.pop(j) # 最大の報酬の値を取り出して足す if len(pay_list) > 0: ans += heapq.heappop(pay_list) * (-1) print(ans)
import heapq N, M = list(map(int, input().split())) AB = [list(map(int, input().split())) for _ in range(N)] pay_list = [] heapq.heapify(pay_list) ans = 0 # AB = sorted(AB, key=lambda x: x[0]) AB = sorted(AB, reverse=True) for i in range(1, M+1): # 実行可能なバイトリストを抽出 for j in range(len(AB)-1, -1, -1): a = AB[j][0] b = AB[j][1] if a <= i: # heappopで最大値を取り出したいため、補正値の(-1)を乗じておく heapq.heappush(pay_list, b*(-1)) AB.pop(j) else: break # 最大の報酬の値を取り出して足す if len(pay_list) > 0: ans += heapq.heappop(pay_list) * (-1) print(ans)
p02948
import heapq N, M = list(map(int, input().split())) AB = [list(map(int, input().split())) for _ in range(N)] pay_list = [] heapq.heapify(pay_list) ans = 0 # AB = sorted(AB, key=lambda x: x[0]) AB = sorted(AB, reverse=True) for i in range(1, M+1): # 実行可能なバイトリストを抽出 for j in range(len(AB)-1, -1, -1): a = AB[j][0] b = AB[j][1] if a <= i: # heappopで最大値を取り出したいため、補正値の(-1)を乗じておく heapq.heappush(pay_list, b*(-1)) AB.pop(j) else: break # 最大の報酬の値を取り出して足す if len(pay_list) > 0: ans += heapq.heappop(pay_list) * (-1) print(ans)
import heapq N, M = list(map(int, input().split())) AB = [list(map(int, input().split())) for _ in range(N)] pay_list = [] heapq.heapify(pay_list) ans = 0 AB = sorted(AB, reverse=True) for i in range(1, M+1): # 実行可能なバイトリストを抽出 for j in range(len(AB)-1, -1, -1): a = AB[j][0] b = AB[j][1] if a <= i: # heappopで最大値を取り出したいため、補正値の(-1)を乗じておく heapq.heappush(pay_list, b*(-1)) AB.pop(j) else: break # 最大の報酬の値を取り出して足す if len(pay_list) > 0: ans += heapq.heappop(pay_list) * (-1) print(ans)
p02948
N,M = list(map(int,input().split())) AB = [list(map(int,input().split())) for _ in range(N)] import heapq AB.sort() heap = [] heapq.heapify(heap) ans = 0 i = 0 day = 1 while day <= M: while i < N and AB[i][0] <= day: heapq.heappush(heap,-AB[i][1]) i += 1 day += 1 if not(heap):continue ans -= heapq.heappop(heap) print(ans)
import heapq N,M = list(map(int,input().split())) AB = [list(map(int,input().split())) for _ in range (N)] AB.sort() ans = 0 q = [] heapq.heapify(q) idx = 0 for i in range(1,M+1): while idx<N and AB[idx][0]<=i: heapq.heappush(q, -AB[idx][1]) idx += 1 if q: p = -heapq.heappop(q) ans += p print(ans)
p02948
import collections N,M = list(map(int,input().split())) allhiyatoi = [[] for j in range(M+1)] for i in range(N): temp = list(map(int,input().split())) if temp[0] > M: continue allhiyatoi[temp[0]].append(temp[1]) temp = [] for i in allhiyatoi: temp.append(collections.deque(sorted(i))) allhiyatoi = temp reward = 0 for i in range(M+1): D = -1 maxrw = -1 for j,day in enumerate(allhiyatoi): if j > i: break if len(day) == 0: continue rw = max(day) if maxrw < rw: D = j maxrw = rw if D == -1: continue reward += allhiyatoi[D].pop() print(reward)
from heapq import heappush, heappop N,M = list(map(int,input().split())) allhiyatoi = [[] for j in range(M+1)] for i in range(N): temp = list(map(int,input().split())) if temp[0] > M: continue allhiyatoi[temp[0]].append(temp[1]) reward = 0 Q = [] for i in allhiyatoi: for j in i: heappush(Q,j*-1) if len(Q) == 0: continue reward -= (heappop(Q)) print(reward)
p02948
#解説の方法 N,M = list(map(int,input().split())) AB = [[] for _ in range(10**5+1)]#報酬振り込み日数ごとに報酬を格納 for _ in range(N): a,b = list(map(int,input().split())) AB[a].append(b) q = [] ans =0 for i in range(1,M+1): q += AB[i]#後ろから見るイメージ 残りi日で請けれる仕事をリストに追加する if len(q)>0: q.sort() ans += q.pop() print(ans)
import heapq #解説の方法 N,M = list(map(int,input().split())) AB = [[] for _ in range(10**5+1)]#報酬振り込み日数ごとに報酬を格納 for _ in range(N): a,b = list(map(int,input().split())) AB[a].append(-b) q = [] heapq.heapify(q) ans =0 for i in range(1,M+1): for b in AB[i]: heapq.heappush(q,b) if len(q)>0: ans += -heapq.heappop(q) print(ans)
p02948
class Job(object): def __init__(self, day, reward): self.day = day self.reward = reward self.is_used = False class MyJobs(object): def __init__(self, num_elements_in_array): self.heapm = {} def append(self, priority, value): if not priority in list(self.heapm.keys()): self.heapm[priority] = [] self.heapm[priority].append(value) def get_best(self, priority): day = -1 reward = -1 for i in range(priority + 1): if not (i in list(self.heapm.keys())): continue for j in range(len(self.heapm[i])): if self.heapm[i][j].is_used: continue cur = self.heapm[i][j].reward if not (0 > day): best = self.heapm[day][reward].reward if (0 > day) or (cur > best): day = i reward = j if (0 > day): return 0 self.heapm[day][reward].is_used = True return self.heapm[day][reward].reward (n, m) = list(map(int, input().split())) jobs = MyJobs(m) for _ in range(n): (a, b) = list(map(int, input().split())) jobs.append(a - 1, Job(a - 1, b)) sum = 0 for i in range(m): sum += jobs.get_best(i) print(sum)
import heapq (n, m) = list(map(int, input().split())) jobs = {} for _ in range(n): (a, b) = list(map(int, input().split())) if not a in jobs: jobs[a] = [] jobs[a].append(-b) heap = [] sum = 0 for i in range(1, m + 1): if i in jobs: for j in jobs[i]: heapq.heappush(heap, j) if 0 < len(heap): sum -= heapq.heappop(heap) print(sum)
p02948
from queue import PriorityQueue N, M = list(map(int, input().split())) # N件の仕事, M日後までに得られる報酬 jobs = [[] for _ in range(M)] # 締め切りごとに仕事を格納する二重配列 for i in range(N): # N件の仕事を見て行く a, b = list(map(int, input().split())) if a > M: # a日後にもらえる、そのaが期日のMよりも大きかったらスルー continue jobs[M-a].append(b) # 報酬がもらえるまでに時間がかかる仕事は、小さいindexに格納されていく # PriorityQueueに追加するときはjobsの後ろから見ていく ans = 0 pq = PriorityQueue() for i in range(M-1, -1, -1): # jobsの後ろから見ていく for b in jobs[i]: pq.put(-b) # min-heapだからマイナスに追加する if not pq.empty(): ans += -(pq.get()) print(ans)
from queue import PriorityQueue N, M = list(map(int, input().split())) # N件のアルバイト, M日後までに欲しい jobs_list = [[] for _ in range(M+1)] for i in range(N): a, b = list(map(int, input().split())) if a > M: # M日後にもらえる仕事ならok continue jobs_list[a].append(b) pq = PriorityQueue() ans = 0 for jobs in jobs_list: for job in jobs: pq.put(-job) # min-heapであるから、マイナスにして追加する if not pq.empty(): # pqに要素があるなら仕事を一つ行う ans += -(pq.get()) print(ans)
p02948
N, M = list(map(int, input().split())) AB = {} for _ in range(N): a, b = list(map(int, input().split())) if a in AB: AB[a].append(b) else: AB[a] = [b] count = 0 current = [] for day in range(1, M + 1): if day in AB: current.extend(AB[day]) current.sort(reverse=True) #print("day{}:{}".format(day, current)) if len(current) != 0: count += current.pop(0) print(count)
import heapq N, M = list(map(int, input().split())) AB = {} for _ in range(N): a, b = list(map(int, input().split())) if a in AB: AB[a].append(b) else: AB[a] = [b] count = 0 current = [] for day in range(1, M + 1): if day in AB: for work in AB[day]: heapq.heappush(current, -work) #print("day{}:{}".format(day, current)) if len(current) > 0: count += -heapq.heappop(current) print(count)
p02948
import bisect n,m = list(map(int,input().split())) data = [tuple(map(int,input().split())) for i in range(n)] data.sort(key = lambda tup:tup[0]) salary =[] ans=0 prev=0 memo=0 for day in range(m): for i in range(memo,n): if data[memo][0]<=(day+1): bisect.insort(salary,data[memo][1]) # salary.append(data[memo][1]) memo+=1 else: break # ans+=salary.pop(max) if len(salary)!=0: ans+=salary.pop() print(ans)
import heapq n,m = list(map(int,input().split())) AB =[tuple(map(int,input().split())) for i in range(n)] ans,memo=0,0 hq=[] AB.sort(key=lambda tup:tup[0]) for day in range(m): for i in range(memo,n): if AB[i][0]<=(day+1): heapq.heappush(hq,-AB[i][1]) memo+=1 else: break if len(hq): ans += -heapq.heappop(hq) print(ans)
p02948
import heapq N, M = list(map(int, input().split())) AB = {} q = [] ans = 0 for i in range(N): A, B = list(map(int, input().split())) if A in AB: AB[A].append(-B) else: AB[A] = [-B] for i in range(1, M+1): if i in AB: for B in AB[i]: heapq.heappush(q, B) if len(q): ans -= heapq.heappop(q) print(ans)
import sys from heapq import heappush, heappop input = sys.stdin.readline def main(): N, M = list(map(int, input().split())) A, B = [[] for i in range(M)], [] for i in range(N): a, b = list(map(int, input().split())) if a > M: continue A[a-1].append(-b) ans = 0 for i in range(M): for a in A[i]: heappush(B, a) if not B: continue ans += heappop(B) print((-ans)) if __name__ == '__main__': main()
p02948
from heapq import* (N,M),*t=[list(map(int,s.split()))for s in open(0)] z,*q=[0]*99*M v=[[]for _ in q] 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)] z,*q=[0]*50*M v=[[]for _ in q] 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
from heapq import* (N,M),*t=[list(map(int,s.split()))for s in open(0)] z,*q=[0]*50*M v=[[]for _ in q] for a,b in t:v[a-1]+=b, for i in v[:M]:[heappush(q,-j)for j in i];z-=heappop(q) print(z)
from heapq import* (N,M),*t=[list(map(int,s.split()))for s in open(0)] q=[0]*50*M v=[[]for _ in q] for a,b in t:v[a-1]+=b, print((sum(([heappush(q,-j)for j in i],-heappop(q))[1]for i in v[:M])))
p02948
from collections import deque N, M = list(map(int,input().split())) class WorkList: def __init__(self): self.payList = [[] for _ in range(10001)] self.payList_sorted = [] def add(self, a, b): if a <= M: self.payList[10001 - b].append(a) def sort_task(self): self.payList_sorted = [deque(sorted(l)) for l in self.payList] def get_task(self, deadline): for i, q in enumerate(self.payList_sorted): if len(q) > 0 and q[0] <= deadline: q.popleft() return 10001 - i return 0 w = WorkList() for _ in range(N): A, B = list(map(int, input().split())) w.add(A, B) w.sort_task() ans = 0 for d in range(M + 1): ans += w.get_task(d) print(ans)
import bisect from collections import deque N, M = list(map(int,input().split())) work = [list(map(int,input().split())) for _ in range(N)] wq = deque(sorted(work)) paytree = [] ans = 0 for day in range(M + 1): while len(wq) > 0 and wq[0][0] <= day: bisect.insort_right(paytree,wq.popleft()[1]) if len(paytree) > 0: ans += paytree.pop(len(paytree) - 1) print(ans)
p02948
import bisect from collections import deque N, M = list(map(int,input().split())) work = [list(map(int,input().split())) for _ in range(N)] wq = deque(sorted(work)) paytree = [] ans = 0 for day in range(M + 1): while len(wq) > 0 and wq[0][0] <= day: bisect.insort_left(paytree,wq.popleft()[1]) if len(paytree) > 0: ans += paytree.pop(len(paytree) - 1) print(ans)
import heapq from collections import deque N, M = list(map(int,input().split())) work = [list(map(int,input().split())) for _ in range(N)] wq = deque(sorted(work)) paytree = [] ans = 0 for day in range(M + 1): while len(wq) > 0 and wq[0][0] <= day: heapq.heappush(paytree, wq.popleft()[1] * (-1)) if len(paytree) > 0: ans -= heapq.heappop(paytree) print(ans)
p02948
N, M = [int(x) for x in input().split()] AB = [0] * N for i in range(N): A, B = [int(x) for x in input().split()] AB[i] = (A, B) # 期間が短い報酬が高い def key(ab): a, b = ab return -b, a AB = sorted(((a, b) for a, b in AB if a <=M), key=key) #print(BA) def calc(jobs, M): m = 0 for d, (a, b) in enumerate(jobs): if d + a <= M: m += b return m def check(N, M, AB): from itertools import permutations return max(calc(jobs, M) for jobs in permutations(AB)) def solve(N, M, AB): pass def solve(N, M, AB): # a = 1 -> M - a d = M i = 0 score = 0 scheduled = set() for d in range(0, M+1)[::-1]: for i, (a, b) in enumerate(AB): if i in scheduled: continue #print("d=%d"%d,(a,b),d+a,end="") if d + a <= M: score += b #print(" score=%d"%score) d -= 1 scheduled.add(i) break return score #print("check", check(N, M, AB)) print((solve(N, M, AB)))
import heapq N, M = [int(x) for x in input().split()] AB = [0] * N for i in range(N): AB[i] = [int(x) for x in input().split()] AB = sorted(AB) jobs = set() answer = 0 nextpush = 0 candidates = [] for day in range(M)[::-1]: while nextpush < N: a, b = AB[nextpush] if day + a <= M: heapq.heappush(candidates, (-b, a)) nextpush+=1 else: break if candidates: b, a = heapq.heappop(candidates) answer -= b print(answer)
p02948
import heapq N, M = [int(x) for x in input().split()] AB = [0] * N for i in range(N): AB[i] = [int(x) for x in input().split()] AB = sorted(AB) jobs = set() answer = 0 nextpush = 0 candidates = [] for day in range(M)[::-1]: while nextpush < N: a, b = AB[nextpush] if day + a <= M: heapq.heappush(candidates, (-b, a)) nextpush+=1 else: break if candidates: b, a = heapq.heappop(candidates) answer -= b print(answer)
# 優先度キューを使う import heapq N, M = [int(x) for x in input().split()] AB = [0] * N for i in range(N): AB[i] = [int(x) for x in input().split()] # 毎日、候補となる集合を再計算すると間に合わない # M-1日後から当日に向けて計算してけば、 # day 日時点で day + a = M となる仕事だけを候補に追加することで # day + 1日までに追加した候補を再利用できる。 # さらに、候補からpopした要素は次回から考慮しないため、設問の条件とも一致する。 # 仕事の候補、徐々に追加していく candidates = [] # nextpush 次に候補としてpush するか判定するcandidates の添え字 nextpush = 0 # 期間の短いものから候補に入れていくので期間でソートしておく # AB[i][0] が期間なので、キー指定不要 AB = sorted(AB) # 回答 answer = 0 for day in range(M)[::-1]: while nextpush < N: a, b = AB[nextpush] if day + a == M: # 給料が高い順ものを優先して取り出したいので、-b をキューにいれる。 heapq.heappush(candidates, -b) nextpush += 1 else: # AB はソート済みなので、これ以降の要素に候補に追加できるものはない # 次回は nextpush から再開する。 break if candidates: # 仕事が出来ない日もあるかも・・・ b = heapq.heappop(candidates) # マイナスで入れてるので、符号を逆にして戻す answer -= b print(answer)
p02948
import heapq n, m = list(map(int, input().split())) baitos = [] for _ in range(n): baitos.append(list(map(int, input().split()))) baitos = sorted(baitos) # print(sorted(baitos)) # print(baitos) money = 0 avail = [] heapq.heapify(avail) for day in range(m): while baitos: if baitos[0][0] > day + 1: break else: heapq.heappush(avail, -baitos[0][1]) baitos.remove(baitos[0]) # for baito in baitos: # print(baito) # if baito[0] > day + 1: # print("break") # break # else: # heapq.heappush(avail, -baito[1]) # baitos.remove(baito) # print("baitos", baitos) # print("avail", avail) if avail: money += -heapq.heappop(avail) # print(day, money) # money += max[1] # baitos.remove(max) print(money)
import heapq n, m = list(map(int, input().split())) baitos = [list(map(int, input().split())) for _ in range(n)] baitos = sorted(baitos) # print(sorted(baitos)) # print(baitos) money = 0 avail = [] # heapq.heapify(avail) index = 0 for day in range(m): while index < n and baitos[index][0] <= day + 1: heapq.heappush(avail, -baitos[index][1]) index += 1 # baitos.remove(baitos[0]) # for baito in baitos: # print(baito) # if baito[0] > day + 1: # print("break") # break # else: # heapq.heappush(avail, -baito[1]) # baitos.remove(baito) # print("baitos", baitos) # print("avail", avail) if avail: money += -heapq.heappop(avail) # print(day, money) # money += max[1] # baitos.remove(max) print(money)
p02948
def slove(): import sys, heapq input = sys.stdin.readline n, m = list(map(int, input().rstrip('\n').split())) ab = [] for i in range(n): a, b = list(map(int, input().rstrip('\n').split())) ab.append([a, b]) heapq.heapify(ab) s_q = [] heapq.heapify(s_q) d = m r = 0 while True: while True: if len(ab) != 0: p = heapq.heappop(ab) if p[0] + d <= m: heapq.heappush(s_q, -p[1]) else: heapq.heappush(ab, p) break else: break if len(s_q) != 0: mr = -heapq.heappop(s_q) r += mr d -= 1 if d == -1: break print(r) if __name__ == '__main__': slove()
def slove(): import sys, heapq input = sys.stdin.readline n, m = list(map(int, input().rstrip('\n').split())) ab = [list(map(int, input().rstrip('\n').split())) for _ in range(n)] heapq.heapify(ab) s_q = [] heapq.heapify(s_q) r = 0 for d in range(1, m + 1): while True: if len(ab) != 0: p = heapq.heappop(ab) if p[0] <= d: heapq.heappush(s_q, -p[1]) else: heapq.heappush(ab, p) break else: break if len(s_q) != 0: mr = -heapq.heappop(s_q) r += mr d -= 1 print(r) if __name__ == '__main__': slove()
p02948
def slove(): import sys, heapq input = sys.stdin.readline n, m = list(map(int, input().rstrip('\n').split())) ab = [list(map(int, input().rstrip('\n').split())) for _ in range(n)] heapq.heapify(ab) s_q = [] heapq.heapify(s_q) r = 0 for d in range(1, m + 1): while True: if len(ab) != 0: p = heapq.heappop(ab) if p[0] <= d: heapq.heappush(s_q, -p[1]) else: heapq.heappush(ab, p) break else: break if len(s_q) != 0: mr = -heapq.heappop(s_q) r += mr d -= 1 print(r) if __name__ == '__main__': slove()
def slove(): import sys import heapq input = sys.stdin.readline n, m = list(map(int, input().rstrip('\n').split())) l = [list(map(int, input().rstrip('\n').split())) for _ in range(n)] r = [] heapq.heapify(l) heapq.heapify(r) t = 0 for i in range(m + 1): while True: if len(l) != 0: a, b = heapq.heappop(l) if a <= i: heapq.heappush(r, -b) else: heapq.heappush(l, [a, b]) break else: break if len(r) != 0: t += -heapq.heappop(r) print(t) if __name__ == '__main__': slove()
p02948
def slove(): import sys import heapq input = sys.stdin.readline n, m = list(map(int, input().rstrip('\n').split())) ls =[] v = [] heapq.heapify(v) for i in range(n): a, b = list(map(int, input().rstrip('\n').split())) ls.append([a, b]) ls.sort(reverse=True) r = 0 for i in range(1, m + 1): while True: if len(ls) != 0: a, b = ls.pop() if a <= i: heapq.heappush(v, -b) else: ls.append([a, b]) break else: break if len(v) != 0: r += -heapq.heappop(v) print(r) if __name__ == '__main__': slove()
import sys import heapq def solve(): input = sys.stdin.readline mod = 10 ** 9 + 7 n, m = list(map(int, input().rstrip('\n').split())) ab = [list(map(int, input().rstrip('\n').split())) for _ in range(n)] ab.sort(reverse=True) ls = [] heapq.heapify(ls) t = 0 for i in range(1, m + 1): while True: if len(ab) != 0: if ab[-1][0] <= i: heapq.heappush(ls, -ab.pop()[1]) else: break else: break if len(ls) != 0: t -= heapq.heappop(ls) print(t) if __name__ == '__main__': solve()
p02948
import heapq from collections import defaultdict def main(): N, M = list(map(int, input().split())) ans = 0 d = defaultdict(list) for _ in range(N): A, B = list(map(int, input().split())) d[A].append(-B) cand = [] heapq.heapify(cand) for day in range(M-1,-1,-1): diff = M - day if d[diff]: cand += d[diff] heapq.heapify(cand) if cand: ans -= heapq.heappop(cand) print(ans) if __name__ == "__main__": main()
import heapq from collections import defaultdict def main(): N, M = list(map(int, input().split())) ans = 0 d = defaultdict(list) for _ in range(N): A, B = list(map(int, input().split())) d[A].append(-B) cand = [] heapq.heapify(cand) for day in range(M-1,-1,-1): diff = M - day for money in d[diff]: heapq.heappush(cand, money) if cand: ans -= heapq.heappop(cand) print(ans) if __name__ == "__main__": main()
p02948
from collections import defaultdict n, m = list(map(int, input().split())) ab = sorted([list(map(int, input().split())) for i in range(n)]) c = [[]] * m a, b = ab[0] last = a ad = [b] for i in range(1, n): a, b = ab[i] if a == last: ad.append(b) else: if last <= m: c[last - 1] = ad ad = [b] last = a if a <= m: c[a - 1] = ad choice = defaultdict(int) ans = 0 for i in range(m): for j in c[i]: choice[j] += 1 if choice: use = max(choice) ans += use choice[use] -= 1 if choice[use] <= 0: del choice[use] print(ans)
import heapq n, m = list(map(int, input().split())) ab = sorted([list(map(int, input().split())) for i in range(n)]) c = [[]] * m a, b = ab[0] last = a ad = [b] for i in range(1, n): a, b = ab[i] if a == last: ad.append(b) else: if last <= m: c[last - 1] = ad ad = [b] last = a if a <= m: c[a - 1] = ad a = [3, 0, 2, 6] choice = [] heapq.heapify(choice) ans = 0 for i in range(m): for j in c[i]: heapq.heappush(choice, -j) if choice: ans -= heapq.heappop(choice) print(ans)
p02948
import bisect n,m=list(map(int,input().split())) l=[[] for _ in range(m)] for _ in range(n): a,b=list(map(int,input().split())) if a<=m: l[a-1].append(b) ans=0 w=[] for i in range(m): for j in l[i]: bisect.insort_right(w,j) if w!=[]: ans+=w.pop(-1) 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())) if a<=m: l[a-1].append(b) ans=0 w=[] for i in range(m): for j in l[i]: heapq.heappush(w,-j) if w!=[]: ans-=heapq.heappop(w) print(ans)
p02948
# -*- coding: utf-8 -*- """ D - Summer Vacation https://atcoder.jp/contests/abc137/tasks/abc137_d """ import sys from heapq import heapify, heappop from bisect import bisect_left def solve(N, M, jobs): days = [d for d in range(1, M+1)] heapify(jobs) ans = 0 while jobs: B, A = heappop(jobs) i = bisect_left(days, -A) if i != len(days): days.pop(i) ans -= B return ans def main(args): N, M = list(map(int, input().split())) jobs = [] for _ in range(N): A, B = list(map(int, input().split())) jobs.append([-B, -A]) ans = solve(N, M, jobs) print(ans) if __name__ == '__main__': main(sys.argv[1:])
# -*- coding: utf-8 -*- """ D - Summer Vacation https://atcoder.jp/contests/abc137/tasks/abc137_d """ import sys from heapq import heapify, heappop, heappush def solve(N, M, jobs): heapify(jobs) availables = [] ans = 0 for day in range(1, M+1): while jobs and jobs[0][0] <= day: A, B = heappop(jobs) heappush(availables, (-B)) if availables: ans -= heappop(availables) return ans def main(args): N, M = list(map(int, input().split())) jobs = [] for _ in range(N): A, B = list(map(int, input().split())) jobs.append((A, B)) ans = solve(N, M, jobs) print(ans) if __name__ == '__main__': main(sys.argv[1:])
p02948
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from heapq import heappop, heapify, heappush def main(): N, M = list(map(int, readline().split())) tasks = [[] for _ in range(M)] for _ in range(N): a, b = list(map(int, readline().split())) if M-a<0: continue tasks[M-a].append(b) s = 0 h = [] for i in reversed(list(range(M))): for b in tasks[i]: heappush(h, -b) if len(h)>0: s+=-heappop(h) print(s) if __name__ == '__main__': main()
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from heapq import heappop, heapify, heappush def main(): N, M = list(map(int, readline().split())) tasks = [[] for _ in range(M)] for _ in range(N): a, b = list(map(int, readline().split())) if M-a<0: continue tasks[M-a].append(b) s = 0 h = [] for i in [i for i in range(M)][::-1]: for b in tasks[i]: heappush(h, -b) if len(h)>0: s+=-heappop(h) print(s) if __name__ == '__main__': main()
p02948
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from heapq import heappop, heapify, heappush def main(): N, M = list(map(int, readline().split())) tasks = [[] for _ in range(M)] for _ in range(N): a, b = list(map(int, readline().split())) if M-a<0: continue tasks[M-a].append(b) s = 0 h = [] for i in [i for i in range(M)][::-1]: for b in tasks[i]: heappush(h, -b) if len(h)>0: s+=-heappop(h) print(s) if __name__ == '__main__': main()
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from heapq import heappop, heappush def main(): N, M = list(map(int, readline().split())) tasks = [[] for _ in range(M)] for _ in range(N): a, b = list(map(int, readline().split())) if M-a<0: continue tasks[M-a].append(b) s = 0 h = [] for i in [i for i in range(M)][::-1]: for b in tasks[i]: heappush(h, -b) if len(h)>0: s+=-heappop(h) print(s) if __name__ == '__main__': main()
p02948
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from heapq import heappop, heappush def main(): N, M = list(map(int, readline().split())) tasks = [[] for _ in range(M)] for _ in range(N): a, b = list(map(int, readline().split())) if M-a<0: continue tasks[M-a].append(b) s = 0 h = [] for i in [i for i in range(M)][::-1]: for b in tasks[i]: heappush(h, -b) if len(h)>0: s+=-heappop(h) print(s) if __name__ == '__main__': main()
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from heapq import heappop, heappush def main(): N, M = list(map(int, readline().split())) tasks = [[] for _ in range(M)] for _ in range(N): a, b = list(map(int, readline().split())) if M-a<0: continue tasks[M-a].append(b) s = 0 h = [] for i in [i for i in range(M)][::-1]: for b in tasks[i]: heappush(h, -b) if len(h)>0: s-=heappop(h) print(s) if __name__ == '__main__': main()
p02948
from operator import itemgetter N, M = list(map(int, input().split())) AB = [list(map(int, input().split())) for _ in range(N)] AB.sort(reverse=True, key=itemgetter(1)) res = [0 for _ in range(M)] for i, (a, b) in enumerate(AB): p = M - a while p >= 0 and res[p] != 0: p -= 1 if p >= 0: res[p] = b print((sum(res)))
import heapq N, M = list(map(int, input().split())) AB = [[] for _ in range(M)] for _ in range(N): a, b = list(map(int, input().split())) if a - 1 < M: AB[a - 1].append(-b) res = 0 cand = [] for i in range(M): AB[i].sort(reverse=True) for b in AB[i]: heapq.heappush(cand, b) if cand: mb = heapq.heappop(cand) res -= mb print(res)
p02948
n,m = list(map(int,input().split())) from collections import defaultdict dic = defaultdict(list) l = [] for i in range(n): a,b=list(map(int,input().split())) dic[a].append(b) s = 0 t = [0] for i in range(m): t += dic[i+1] if len(t) ==0 : M = 0 else: M = max(t) s +=M t.remove(M) print(s)
n,m = list(map(int,input().split())) import heapq from collections import defaultdict dic = defaultdict(list) l = [] heapq.heapify(l) for i in range(n): a,b=list(map(int,input().split())) b = -b dic[a].append(b) s = 0 t = [0] for i in range(m): for i in dic[i+1]: heapq.heappush(l,i) if len(l) ==0 : M = 0 else: # print(l) M = heapq.heappop(l) s += -M print(s)
p02948
from operator import itemgetter n, m = list(map(int, input().split())) abab = [list(map(int, input().split())) for _ in range(n)] abab = sorted(sorted(abab, key=itemgetter(0), reverse=True), key=itemgetter(1), reverse=True) gyara = [0 for _ in range(m+1)] for a, b in abab : for i in range(a, m+1) : if gyara[i] == 0 : gyara[i] = b break print((sum(gyara)))
from heapq import * n, m = list(map(int, input().split())) abab = [list(map(int, input().split())) for _ in range(n)] bites = [[] for _ in range(m)] for a, b in abab : if a <= m : bites[a-1].append(-b) ans = 0 board = [] for bite in bites: for b in bite : heappush(board, b) if board : ans -= heappop(board) print(ans)
p02948
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)
from heapq import heappush, heappop n, m = list(map(int, input().split())) jobs = {} for _ in range(n): a, b = list(map(int, input().split())) if a > m: continue if a in jobs: jobs[a].append(b) else: jobs[a] = [b] result = 0 candidates = [] for a in range(1, m + 1): if a in jobs: for b in jobs[a]: heappush(candidates, -b) else: if len(candidates) == 0: continue result += -heappop(candidates) print(result)
p02948
from heapq import heappush, heappop n, m = list(map(int, input().split())) jobs = {} for _ in range(n): a, b = list(map(int, input().split())) if a > m: continue if a in jobs: jobs[a].append(b) else: jobs[a] = [b] result = 0 candidates = [] for a in range(1, m + 1): if a in jobs: for b in jobs[a]: heappush(candidates, -b) else: if len(candidates) == 0: continue result += -heappop(candidates) print(result)
from heapq import heappop, heappush import sys input = sys.stdin.readline def calculate(N,M,arr): ret = 0 j = 0 res = [] # 1,2,3,4,5 # ↑ # 4,3,2,1 # M = 4 # range(1,5) => 1,2,3,4 for i in range(1,M+1): # i = 1,2,3 # 比如M=4的话,第三天(最多1天),第二天(最多2天),第一天(最多3天) # 记下来循环打工信息资源,以j为标记 # j为资源编号 # arr[j][0]为该编号资源的天数 # 候补的资源,满足所有天数,因为是满足后续的天数来算的 while (j < N) and (arr[j][0] <= i): heappush(res, -arr[j][1]) j = j + 1 #j找到的话就往上顶一格,一共这一天,或者前一天寻找的时候以下一个资源天为开始 if len(res) > 0: ret -= heappop(res) return ret N,M = list(map(int,input().split())) # 后面的逻辑是基于前面的从小到大的排序的 list = [list(map(int,input().split())) for i in range(N)] arr = sorted(list,key=lambda x:x[0]) result = calculate(N,M,arr) print(result)
p02948
#Binary Indexed Tree (1-index!!!!!!!!) class BinaryIndexedTree(): def __init__(self, size): self.__node = [0] * (size+1) self.size = size #node[index]にvalueを足して、BITを更新 O(logN) def add(self, index, value): while index <= self.size: self.__node[index] += value index += index&-index #indexまでの和を返す O(logN) def sum(self, index): ret = 0 #零元・単位元 while index > 0: ret += self.__node[index] index -= index&-index return ret def update_max(self, index, value): while index <= self.size: self.__node[index] = max(self.__node[index], value) index += index & -index #indexまでの最大値を返す def query_max(self, index): ret = 0 while index > 0: ret = max(ret, self.__node[index]) index -= index & -index return ret #0-indexでの添字を受け取って、1-indexでの添字での値を返す def get_node(self, index): return self.__node[index] def show(self): #print(self.__node) for day in range(1,self.size+1): print(("day,",day,self.sum(day))) # #    ⋀_⋀  #   (・ω・) # ./ U ∽ U\ # │* 合 *│ # │* 格 *│ # │* 祈 *│ # │* 願 *│ # │*   *│ #  ̄ # import sys sys.setrecursionlimit(10**6) input=sys.stdin.readline from math import floor,ceil,sqrt,factorial,log #log2ないyp from heapq import heappop, heappush, heappushpop from collections import Counter,defaultdict from itertools import accumulate,permutations,combinations,product,combinations_with_replacement from bisect import bisect_left,bisect_right from copy import deepcopy from operator import itemgetter inf=float('inf') mod = 10**9+7 def INT_(n): return int(n)-1 def MI(): return list(map(int,input().split())) def MF(): return list(map(float, input().split())) def MI_(): return list(map(INT_,input().split())) def LI(): return list(MI()) def LI_(): return [int(x) - 1 for x in input().split()] def LF(): return list(MF()) def LIN(n:int): return [I() for _ in range(n)] def LLIN(n: int): return [LI() for _ in range(n)] def LLIN_(n: int): return [LI_() for _ in range(n)] def LLI(): return [list(map(int, l.split() )) for l in eval(input())] def I(): return int(eval(input())) def F(): return float(eval(input())) def ST(): return input().replace('\n', '') def main(): N,M= MI() works = LLIN(N) works.sort(key = itemgetter(1),reverse = True) BIT = BinaryIndexedTree(M) used = set() #print(works) for day in range(1,M+1): #print(day) BIT.add(day,1) ans = 0 for day, money in works: #BIT.show() date = M-day+1 while date in used: date-=1 if date > 0 and BIT.sum(date)>0: BIT.add(date, -1) used.add(date) ans += money print(ans) if __name__ == '__main__': main()
import sys sys.setrecursionlimit(10**6) input=sys.stdin.readline from math import floor,ceil,sqrt,factorial,log #log2ないyp from heapq import heappop, heappush, heappushpop from collections import Counter,defaultdict from itertools import accumulate,permutations,combinations,product,combinations_with_replacement from bisect import bisect_left,bisect_right from copy import deepcopy from operator import itemgetter inf=float('inf') mod = 10**9+7 def INT_(n): return int(n)-1 def MI(): return list(map(int,input().split())) def MF(): return list(map(float, input().split())) def MI_(): return list(map(INT_,input().split())) def LI(): return list(MI()) def LI_(): return [int(x) - 1 for x in input().split()] def LF(): return list(MF()) def LIN(n:int): return [I() for _ in range(n)] def LLIN(n: int): return [LI() for _ in range(n)] def LLIN_(n: int): return [LI_() for _ in range(n)] def LLI(): return [list(map(int, l.split() )) for l in eval(input())] def I(): return int(eval(input())) def F(): return float(eval(input())) def ST(): return input().replace('\n', '') def main(): N,M= MI() works = [] for _ in range(N): date,money = list(map(int,input().split())) works.append((-money,date)) works.sort(key = itemgetter(1),reverse = True) ans =0 pri_queue = [] for date in range(M): while works and works[-1][1] -1 <= date: heappush(pri_queue,works.pop()) if pri_queue: ans += -heappop(pri_queue)[0] print(ans) if __name__ == '__main__': main()
p02948
n, m = list(map(int, input().split(" "))) xys = [tuple(map(int, input().split(" "))) for i in range(n)] xys = sorted(xys, key=lambda x: (-x[0], x[1])) dp = [[0 for x in range(m)] for y in range(n)] for i in range(n): a, b = xys[i] # print(i, a, b) if i == 0: for j in range(0, m - a + 1): dp[i][j] = b else: dp[i][0] = max(dp[i - 1][0], b) for j in range(1, m - a + 1): dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1] + b) # print(dp[i]) # print(dp) print((max(max(x) for x in dp)))
n, m = list(map(int, input().split(" "))) xys = [tuple(map(int, input().split(" "))) for i in range(n)] xys = sorted(xys, key=lambda x: (x[0], -x[1])) ans = 0 for i in range(1, m + 1): cm = -1 cmi = -1 for j in range(len(xys)): if xys[j][0] <= i and xys[j][1] > cm: cm = xys[j][1] cmi = j if cmi >= 0: ans += cm xys.pop(cmi) print(ans)
p02948
n,m=list(map(int,input().split())) l=sorted([list(map(int,input().split())) for _ in [0]*n],key=lambda x:[x[0],-x[1]])+[[float('inf'),0]] for i in range(n):l[i][0]-=1 w=[n]*m for i in range(m): j=0 while l[j][0]<=i: if l[j][1]>l[w[i]][1] and j not in w: w[i]=j j+=1 print((sum(l[w[i]][1] for i in range(m))))
from heapq import heappop, heappush n,m=list(map(int,input().split())) a=[] for i in range(n): heappush(a,list(map(int,input().split()))) heap=[] s=0 for i in range(m): while 1: if len(a)==0:break else: if a[0][0]==i+1:heappush(heap,-heappop(a)[1]) else:break if len(heap)>0:s+=-heappop(heap) print(s)
p02948
import sys,collections as cl,bisect as bs sys.setrecursionlimit(100000) mod = 10**9+7 Max = sys.maxsize def cmb(n, r): if n - r < r: r = n - r if r == 0: return 1 if r == 1: return n numerator = [n - r + k + 1 for k in range(r)] denominator = [k + 1 for k in range(r)] for p in range(2,r+1): pivot = denominator[p - 1] if pivot > 1: offset = (n - r) % p for k in range(p-1,r,p): numerator[k - offset] /= pivot denominator[k] /= pivot result = 1 for k in range(r): if numerator[k] > 1: result *= int(numerator[k]) return result def l(): #intのlist return list(map(int,input().split())) def m(): #複数文字 return map(int,input().split()) def onem(): #Nとかの取得 return int(input()) def s(x): #圧縮 a = [] aa = x[0][0] su = [x[0]] for i in range(len(x)-1): if aa != x[i+1][0]: a.append(su) aa = x[i+1][0] su = [x[i+1]] else: su.append(x[i+1]) a.append(su) return a def jo(x): #listをスペースごとに分ける return " ".join(map(str,x)) def max2(x): #他のときもどうように作成可能 return max(map(max,x)) def binary_search_recursion(a,b): arry = a target = b arry_len = len(arry) def recursion(left=0,right=arry_len-1): if left > right: return print("not found") mid = (left+right) // 2 if arry[mid] > target: return recursion(left,mid-1) elif arry[mid] < target: return recursion(mid+1,right) else: return print("found: " + str(mid+1) + " 番目にありました") recursion() n,m = l() a = [] for i in range(n): z,x = l() a.append([z,x]) a.sort(key = lambda x:(x[0],-x[1])) kkk = s(a) co = 0 p = 0 al = [] for i in range(m): k = i + 1 if p <= len(kkk)-1: if k >= kkk[p][0][0]: for ppp in kkk[p]: al.append(ppp[1]) p += 1 al.sort(reverse = True) if len(al) != 0: an = al.pop(0) co += an print(co)
import sys,collections as cl,bisect as bs,heapq as hq sys.setrecursionlimit(100000) mod = 10**9+7 Max = sys.maxsize def cmb(n, r): if n - r < r: r = n - r if r == 0: return 1 if r == 1: return n numerator = [n - r + k + 1 for k in range(r)] denominator = [k + 1 for k in range(r)] for p in range(2,r+1): pivot = denominator[p - 1] if pivot > 1: offset = (n - r) % p for k in range(p-1,r,p): numerator[k - offset] /= pivot denominator[k] /= pivot result = 1 for k in range(r): if numerator[k] > 1: result *= int(numerator[k]) return result def l(): #intのlist return list(map(int,input().split())) def m(): #複数文字 return list(map(int,input().split())) def onem(): #Nとかの取得 return int(eval(input())) def s(x): #圧縮 a = [] aa = x[0][0] su = [x[0]] for i in range(len(x)-1): if aa != x[i+1][0]: a.append(su) aa = x[i+1][0] su = [x[i+1]] else: su.append(x[i+1]) a.append(su) return a def jo(x): #listをスペースごとに分ける return " ".join(map(str,x)) def max2(x): #他のときもどうように作成可能 return max(list(map(max,x))) n,m = l() a = [] for i in range(n): z,x = l() a.append([z,x]) a.sort(key = lambda x:(x[0],-x[1])) kkk = s(a) co = 0 p = 0 al = [] for i in range(m): k = i + 1 if p <= len(kkk)-1: if k >= kkk[p][0][0]: for ppp in kkk[p]: hq.heappush(al,-ppp[1]) p += 1 if len(al) != 0: an = hq.heappop(al) co -= an print(co)
p02948
import heapq N,M = list(map(int,input().split())) AB = [list(map(int,input().split())) for _ in range(N)] d_AB = {a:[] for a,b in AB} for a,b in AB: d_AB[a].append(-b) H = [] money = 0 for i in range(1,M+1): if i in list(d_AB.keys()): H += d_AB[i] else: H += [0] #print(H) heapq.heapify(H) money += heapq.heappop(H) print((-money))
import heapq N,M = list(map(int,input().split())) AB = [list(map(int,input().split())) for _ in range(N)] d_AB = {a:[] for a,b in AB} for a,b in AB: d_AB[a].append(-b) #print(d_AB) H = [] heapq.heapify(H) money = 0 for i in range(1,M+1): if i in list(d_AB.keys()): for ab in d_AB[i]: heapq.heappush(H,ab) #print(H) money += heapq.heappop(H) #print(money) else: money += heapq.heappushpop(H,0) print((-money))
p02948
N, M = list(map(int, input().split())) AB = {} for i in range(N): a, b = list(map(int, input().split())) if a in list(AB.keys()): AB[a].append(b) else: AB[a] = [b] dp = [0] * (M+1) for i in range(1, M+1): tmp = 0 km = -1 tm = -1 for k in range(i+1): if k not in list(AB.keys()): continue for t in range(len(AB[k])): if tmp < AB[k][t]: tmp = AB[k][t] km = k tm = t if km >= 0: dp[i] = dp[i-1] + tmp AB[km][tm] = 0 else: dp[i] = dp[i-1] print((dp[len(dp)-1]))
from heapq import heappush, heappop N, M = list(map(int, input().split())) AB = [list(map(int, input().split())) for _ in range(N)] AB.sort(reverse=True) hp = [] ret = 0 for i in range(1, M+1): # push while len(AB) > 0 and AB[len(AB)-1][0] <= i: heappush(hp, AB.pop()[1]*-1) # pop if len(hp) > 0: ret += heappop(hp)*-1 print(ret)
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())) c=[list(map(int,input().split())) for i in range(n)] ans, available = 0, [] heapq.heapify(available) heapq.heapify(c) for i in range(1, m+1): while len(c)!=0 and c[0][0]<=i: now = heapq.heappop(c) heapq.heappush(available,-now[1]) if len(available) != 0: ans -= heapq.heappop(available) print(ans)
p02948
from operator import itemgetter n, m = list(map(int, input().split())) ab = [list(map(int, input().split())) for _ in range(n)] d = [0] * (m+1) ab.sort(key=itemgetter(1), reverse=True) for a, b in ab: x = m - a while x >= 0: if d[x] == 0: d[x] = b break x -= 1 print((sum(d)))
from heapq import heappop, heappush n, m = list(map(int, input().split())) ab = [list(map(int, input().split())) for _ in range(n)] h = [] ans = 0 ab.sort() i = 0 for d in range(1, m+1): while i < n: if ab[i][0] <= d: heappush(h, - ab[i][1]) i += 1 else: break if len(h) > 0: ans -= heappop(h) print(ans)
p02948
import heapq N, M = list(map(int, input().split())) rewards = [None] * N for i in range(N): a, b = list(map(int, input().split())) rewards[i] = (b, a) heap = [] total = 0 for day in range(1, M + 1): target_rewards = list([x for x in rewards if (day - 1) < x[1] <= day]) for reward in target_rewards: heapq.heappush(heap, (-reward[0], reward[1])) if len(heap) > 0: max_reward = heapq.heappop(heap) total += -max_reward[0] print(total)
import heapq N, M = list(map(int, input().split())) rewards = [[] for _ in range(M + 1)] for i in range(N): a, b = list(map(int, input().split())) if a <= M: rewards[a].append(b) heap = [] total = 0 for day in range(1, M + 1): for reward in rewards[day]: heapq.heappush(heap, -reward) if len(heap) > 0: max_reward = heapq.heappop(heap) total += -max_reward print(total)
p02948
import bisect 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() day = 1 i = 0 maxi = [] ans = 0 while day <= M: if i < N and AB[i][0] == day: ind = bisect.bisect_left(maxi,AB[i][1]) maxi.insert(ind,AB[i][1]) i += 1 else: day += 1 if len(maxi) > 0: ans += maxi.pop(-1) print (ans)
import heapq 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() day = 1 i = 0 lis = [] ans = 0 while day <= M: if i < N and AB[i][0] == day: heapq.heappush(lis,-1 * AB[i][1]) i += 1 else: day += 1 if len(lis) > 0: ans -= heapq.heappop(lis) print (ans)
p02948
def work(l): l.sort(key=lambda x:x[1],reverse=True) salary = l.pop(0) return salary[1] n,m = list(map(int, input().split())) a = [list(map(int, input().split())) for i in range(n)] queue = [] ans = 0 i = 0 while i < m: i += 1 for j in range(len(a)): if a[j][0] == i: queue.append(a[j]) if len(queue)>0: ans += work(queue) print(ans)
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) #解法 rewards = 0 heap = [] for i in range(M): for reward in jobs[i]: heapq.heappush(heap, reward) if len(heap) >0: reword = heapq.heappop(heap) rewards += -reword print(rewards)
p02948
import sys import heapq N, M = list(map(int, input().split())) jobs = [] for line in sys.stdin: A, B = list(map(int, line.split())) if A > M: continue jobs.append((A, B)) jobs.sort(reverse=True) q = [] ans = 0 for i in range(1, M + 1): while jobs and jobs[-1][0] <= i: A, B = jobs.pop() heapq.heappush(q, -B) if q: ans += -heapq.heappop(q) print(ans)
import sys import heapq N, M = list(map(int, input().split())) possiblyValidJobs = {} for line in sys.stdin: A, B = list(map(int, line.split())) if A > M: continue if A in possiblyValidJobs: possiblyValidJobs[A].append(B) else: possiblyValidJobs[A] = [B] validJobsOnThisDay = [] ans = 0 for daysBeforeDeadline in range(1, M + 1): if daysBeforeDeadline in possiblyValidJobs: for wage in possiblyValidJobs[daysBeforeDeadline]: heapq.heappush(validJobsOnThisDay, -wage) if validJobsOnThisDay: ans += -heapq.heappop(validJobsOnThisDay) print(ans)
p02948
import heapq N, M = list(map(int, input().split())) ab = [list(map(int, input().split())) for _ in range(N)] ab = sorted(ab, key=lambda x:x[0]) day = 0 pay = 0 q = [] for i in range(1, M+1): for j in range(len(ab)): if ab[0][0] <= i: a, b = ab.pop(0) heapq.heappush(q, (-b, a)) else: break if len(q) > 0: pay += - heapq.heappop(q)[0] print(pay)
import heapq N, M = list(map(int, input().split())) ab = [list(map(int, input().split())) for _ in range(N)] day = {i+1: [] for i in range(M)} for a, b in ab: if a <= M: day[a] += [-b] q = [] heapq.heapify(q) pay = 0 for i in range(M): for b in day[i+1]: heapq.heappush(q, b) if len(q) > 0: pay += -heapq.heappop(q) print(pay)
p02948
import heapq import copy N,M = list(map(int,input().split())) baito = [] for i in range(N): baito.append(list(map(int, input().split()))) baito.sort(key=lambda x: x[0]) que = [] ans = 0 for i in range(1, M+1): tmp = copy.copy(baito) for val in baito: if val[0] <= i: heapq.heappush(que, val[1]*-1) tmp.pop(0) else: break baito = tmp if len(que) != 0: ans += heapq.heappop(que)*-1 print(ans)
import heapq N,M = list(map(int,input().split())) baito = [[] for i in range(M+1)] for i in range(N): A,B = list(map(int,input().split())) if A <= M: baito[A].append(B) que = [] ans = 0 for i in range(1, M+1): if len(baito[i]) != 0: for val in baito[i]: heapq.heappush(que, val*-1) if len(que) != 0: ans += heapq.heappop(que)*-1 print(ans)
p02948
from heapq import heapify, heappop, heappush N,M = list(map(int, input().split())) AB = [] for i in range(N): a,b = list(map(int, input().split())) if a<=M: AB.append((a,-b)) else: N-=1 AB.sort() Q = [] heapify(Q) ans = 0 i = 0 for day in range(M): while i<N and AB[i][0] == day+1: heappush(Q, AB[i][1]) i+=1 if Q: ans -= heappop(Q) print(ans)
from heapq import heapify, heappop, heappush 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() Q = [] ans = 0 i = 0 for day in range(M): while i<N and AB[i][0] == day+1: heappush(Q, AB[i][1]) i+=1 if Q: ans -= heappop(Q) print(ans)
p02948
import collections import heapq inputs = [int(x) for x in input().split()] n, m = inputs[0], inputs[1] conditions = {} for i in range(n): inputs = [int(x) for x in input().split()] if inputs[0] in list(conditions.keys()): conditions[inputs[0]].append(inputs[1]) else: conditions[inputs[0]] = collections.deque([inputs[1]]) for day in list(conditions.keys()): conditions[day] = sorted(conditions[day], reverse=True) ans = 0 candidates = [] for reversed_day in range(1, m + 1): for d in range(1, reversed_day+1): if d in list(conditions.keys()): heapq.heappush(candidates, -conditions[d].pop(0)) if len(conditions[d]) == 0: del conditions[d] if len(candidates) == 0: continue ans -= heapq.heappop(candidates) print(ans)
import collections import heapq inputs = [int(x) for x in input().split()] n, m = inputs[0], inputs[1] conditions = collections.deque([]) for i in range(n): conditions.append([int(x) for x in input().split()]) conditions = sorted(conditions) cnt, ans = 0, 0 candidates = [] for reversed_day in range(1, m + 1): while cnt < len(conditions) and conditions[cnt][0] <= reversed_day: heapq.heappush(candidates, -conditions[cnt][1]) cnt += 1 if len(candidates) == 0: continue ans -= heapq.heappop(candidates) print(ans)
p02948
import queue N, M = list(map(int, input().split())) AB = [[] for _ in range(M + 1)] for n in range(N): a, b = list(map(int, input().split())) if a > M: continue AB[a].append(0-b) earn = 0 q = queue.PriorityQueue() for m in range(1, M + 1): for job in AB[m]: q.put(job) if not q.empty(): earn += q.get() print((0-earn))
import heapq N, M = list(map(int, input().split())) AB = [[] for _ in range(M + 1)] for n in range(N): a, b = list(map(int, input().split())) if a > M: continue AB[a].append(0-b) earn = 0 q = [] for m in range(1, M + 1): for job in AB[m]: heapq.heappush(q, job) if q != []: earn += heapq.heappop(q) print((0-earn))
p02948
N, M = list(map(int, input().split())) from collections import deque AB = deque(sorted([list(map(int, input().split())) for i in range(N)])) from heapq import * q = [] heapify(q) ans = 0 remain_days = 1 while remain_days <= M: for a, b in list(AB): if a <= remain_days: heappush(q, -1 * AB.popleft()[1]) if q: v = heappop(q) ans += -1 * v remain_days += 1 print(ans)
N, M = list(map(int, input().split())) jobs = sorted([list(map(int, input().split())) for i in range(N)], reverse=True) from heapq import * q = [] heapify(q) ans = 0 for i in range(1, M+1): while jobs and jobs[-1][0] <= i: heappush(q, -1*jobs.pop()[1]) if q: ans += -1 * heappop(q) print(ans)
p02948
import heapq N, M = list(map(int, input().split())) w = [[] for i in range(M)] for i in range(N): a, b = list(map(int, input().split())) try: w[a-1].append(-b) except: pass ans = 0 a = [] for i in range(M): a += w[i] heapq.heapify(a) try: ans += -heapq.heappop(a) except: pass print(ans)
import heapq N, M = list(map(int, input().split())) w = [[] for i in range(M)] for i in range(N): a, b = list(map(int, input().split())) try: w[a-1].append(-b) except: pass ans = 0 a = [] heapq.heapify(a) for i in range(M): for x in w[i]: heapq.heappush(a, x) try: ans += -heapq.heappop(a) except: pass print(ans)
p02948
import bisect N, M = list(map(int, input().split())) income = [[] for _ in range(M+1)] # income[i]はi日後に報酬が得られる全ての仕事についての報酬のリスト for _ in range(N): a, b = list(map(int, input().split())) if a <= M: bisect.insort_left(income[a], b) # 後ろから考える。残りi日のときに終了までに報酬が得られる仕事のうち報酬最大のものを選ぶ ans = 0 for rest_days in range(1, M+1): if income[rest_days]: ans += income[rest_days].pop(-1) if rest_days < M: income[rest_days+1] = sorted(income[rest_days+1] + income[rest_days]) print(ans)
import heapq N, M = list(map(int, input().split())) income = [[] for _ in range(M+1)] # income[i]はi日後に報酬が得られる全ての仕事についての報酬のリスト for _ in range(N): a, b = list(map(int, input().split())) if a <= M: income[a].append(-b) # 後ろから考える。残りi日のときに終了までに報酬が得られる仕事のうち報酬最大のものを選ぶ ans = 0 tmp = [] for rest_days in range(1, M+1): tmp = list(heapq.merge(tmp, income[rest_days])) heapq.heapify(tmp) if tmp: ans += heapq.heappop(tmp) print((-ans))
p02948
from collections import defaultdict import heapq n, m = list(map(int, input().split())) d = defaultdict(list) for i in range(n): a, b = list(map(int, input().split())) d[a-1].append(-b) cnt = 0 a = [] for i in range(m): a += d[i] heapq.heapify(a) if a: cnt += -heapq.heappop(a) print(cnt)
from collections import defaultdict import heapq n, m = list(map(int, input().split())) d = defaultdict(list) for i in range(n): a, b = list(map(int, input().split())) d[a-1].append(-b) cnt = 0 a = [] heapq.heapify(a) for i in range(m): for j in d[i]: heapq.heappush(a, j) if a: cnt += -heapq.heappop(a) print(cnt)
p02948
import bisect n, m = list(map(int, input().split())) ls = [list(map(int, input().split())) for _ in range(n)] ls.sort(key=lambda x: x[0]) la = [x[0] for x in ls] lb = [x[1] for x in ls] selected = [] res = 0 for i in range(1, m + 1): k = bisect.bisect(la, i) bs = lb[:k] index = -1 temp = 0 for j, b in enumerate(bs): if j in selected: continue if temp < b: index = j temp = b if index >= 0: selected.append(index) res += temp print(res)
import heapq n, m = list(map(int, input().split())) ls = [list(map(int, input().split())) for _ in range(n)] ls.sort(key=lambda x: x[0]) q = [] res = 0 index = 0 for i in range(1, m + 1): while index < n and ls[index][0] <= i: heapq.heappush(q, -ls[index][1]) index += 1 if len(q) > 0: res += -heapq.heappop(q) print(res)
p02948
import bisect import sys input = sys.stdin.readline inpl = lambda: list(map(int,input().split())) class SegmentTree: def __init__(self, value, N=0, comp=lambda x,y: x<=y, reverse=False): M = max(len(value),N) N = 2**(len(bin(M))-3) if N < M: N *= 2 self.N = N self.node = [0] * (2*N-1) for i in range(N): self.node[N-1+i] = i self.value = [None] * N for i, v in enumerate(value): self.value[i] = v self.comp = lambda x, y: True if y is None else False if x is None else comp(x,y)^reverse for i in range(N-2,-1,-1): left_i, right_i = self.node[2*i+1], self.node[2*i+2] left_v, right_v = self.value[left_i], self.value[right_i] if self.comp(left_v, right_v): self.node[i] = left_i else: self.node[i] = right_i def set_input(self, n, v): self.value[n] = v i = (self.N-1) + n while i > 0: i = (i-1)//2 left_i, right_i = self.node[2*i+1], self.node[2*i+2] left_v, right_v = self.value[left_i], self.value[right_i] if self.comp(left_v, right_v): new_i = left_i else: new_i = right_i if new_i == self.node[i] and new_i != n: break else: self.node[i] = new_i def get_input(self, n): if n is None: return None else: return self.value[n] def get_output(self, a=0, b=-1): return self.get_input(self.get_output_index(a,b)) def get_output_index(self, a=0, b=-1, k=0, l=0, r=-1): if b < 0: b = self.N if r < 0: r = self.N if a <= l and r <= b: return self.node[k] elif r <= a or b <= l: return None else: left_i = self.get_output_index(a,b,2*k+1,l,(l+r)//2) right_i = self.get_output_index(a,b,2*k+2,(l+r)//2,r) left_v, right_v = self.get_input(left_i), self.get_input(right_i) if left_v is None and right_v is None: return None elif self.comp(left_v, right_v): return left_i else: return right_i N, M = inpl() AB = [ inpl() for _ in range(N) ] AB.sort() A = [ ab[0] for ab in AB ] B = [ ab[1] for ab in AB ] sg = SegmentTree(B, reverse=True) ans = 0 n = 0 for m in range(1,M+1): n = bisect.bisect_right(A, m, lo=n) p = sg.get_output_index(0, n) if p is not None: ans += sg.get_input(p) sg.set_input(p, None) print(ans)
import sys input = sys.stdin.readline inpl = lambda: list(map(int,input().split())) class SegmentTree: def __init__(self, value, N=0, comp=lambda x,y: x<=y, reverse=False): M = max(len(value),N) N = 2**(len(bin(M))-3) if N < M: N *= 2 self.N = N self.node = [0] * (2*N-1) for i in range(N): self.node[N-1+i] = i self.value = [None] * N for i, v in enumerate(value): self.value[i] = v self.comp = lambda x, y: True if y is None else False if x is None else comp(x,y)^reverse for i in range(N-2,-1,-1): left_i, right_i = self.node[2*i+1], self.node[2*i+2] left_v, right_v = self.value[left_i], self.value[right_i] if self.comp(left_v, right_v): self.node[i] = left_i else: self.node[i] = right_i def set_input(self, n, v): self.value[n] = v i = (self.N-1) + n while i > 0: i = (i-1)//2 left_i, right_i = self.node[2*i+1], self.node[2*i+2] left_v, right_v = self.value[left_i], self.value[right_i] if self.comp(left_v, right_v): new_i = left_i else: new_i = right_i if new_i == self.node[i] and new_i != n: break else: self.node[i] = new_i def get_input(self, n): if n is None: return None else: return self.value[n] def get_output(self, a=0, b=-1): return self.get_input(self.get_output_index(a,b)) def get_output_index(self, a=0, b=-1, k=0, l=0, r=-1): if b < 0: b = self.N if r < 0: r = self.N if a <= l and r <= b: return self.node[k] elif r <= a or b <= l: return None else: left_i = self.get_output_index(a,b,2*k+1,l,(l+r)//2) right_i = self.get_output_index(a,b,2*k+2,(l+r)//2,r) left_v, right_v = self.get_input(left_i), self.get_input(right_i) if left_v is None and right_v is None: return None elif self.comp(left_v, right_v): return left_i else: return right_i N, M = inpl() AB = [ inpl() for _ in range(N) ] AB.sort() A = [ ab[0] for ab in AB ] B = [ ab[1] for ab in AB ] sg = SegmentTree(B, reverse=True) ans = 0 n = 0 for m in range(1,M+1): while n < N and A[n] <= m: n += 1 p = sg.get_output_index(0, n) if p is not None: ans += sg.get_input(p) sg.set_input(p, None) print(ans)
p02948
import sys import heapq input = sys.stdin.readline n,m = list(map(int,input().split())) alwork = [() for i in range(10**5+1)] w = [] hpop = heapq.heappop hpush = heapq.heappush ans = 0 for i in range(n): a,b = list(map(int,input().split())) alwork[a] += (-b,) for i in range(1,m+1): for j in alwork[i]: hpush(w,j) if w: ans += hpop(w) print((-ans))
import sys import heapq input = sys.stdin.readline n,m = list(map(int,input().split())) l=[tuple(map(int,input().split())) for i in range(n)] l.sort(reverse=True) w = [] hpop = heapq.heappop hpush = heapq.heappush ans = 0 for i in range(1,m+1): while l and l[-1][0]==i: hpush(w,-l.pop()[1]) if w: ans += hpop(w) print((-ans))
p02948
import heapq N,M = (int(x) for x in input().split()) AB = [] sum = 0 for i in range(N): a,b = (int(x) for x in input().split()) AB.append([a,b]) AB = sorted(AB , key = lambda x : x[0]) heap = [] j = 0 for i in range(M+1): while j<N and AB[j][0] < i+1: heapq.heappush(heap,-AB[j][1]) j += 1 if len(heap): heapq.heapify(heap) sum += -heapq.heappop(heap) print(sum)
import heapq N,M = (int(x) for x in input().split()) AB = [] sum = 0 for i in range(N): a,b = (int(x) for x in input().split()) AB.append([a,b]) AB = sorted(AB , key = lambda x : x[0]) heap = [] heapq.heapify(heap) j = 0 for i in range(M+1): while j<N and AB[j][0] < i+1: heapq.heappush(heap,-AB[j][1]) j += 1 if len(heap): sum += -heapq.heappop(heap) print(sum)
p02948
if __name__ == '__main__': n, m = list(map(int, input().split())) ab = [list(map(int, input().split())) for i in range(n)] ab = sorted(ab, key=lambda x: x[1], reverse=True) free_day = set(list(range(m + 1))) answer = 0 for i in range(n): a = ab[i][0] b = ab[i][1] work_days = {day for day in free_day if m-day >= a} if len(work_days) != 0: free_day.remove(max(work_days)) answer += b print(answer)
from heapq import heappush, heappop # https://atcoder.jp/contests/abc137/submissions/6840855 を参考にした。 q = [] if __name__ == '__main__': n, m = list(map(int, input().split())) ab = [list(map(int, input().split())) for i in range(n)] ab = sorted(ab, reverse=True) q = [] answer = 0 for i in range(m-1, -1, -1): while ab and i + ab[-1][0] <= m: (a, b) = ab.pop() heappush(q, -b) if q: answer -= heappop(q) print(answer)
p02948
# -*- coding: utf-8 -*- N, M = list(map(int, input().split())) Wlist = [] for i in range(N): A, B = list(map(int, input().split())) Wlist.append([A,B]) Wlist.sort(key=lambda x:x[0]) Ans = 0 for day in range(1,M+1): Best = 0 BestIdx = -1 for i, W in enumerate(Wlist): if W[0] > day: break if Best < W[1]: Best = W[1] BestIdx = i if BestIdx >= 0: Ans += Best Wlist[BestIdx][1] = 0 print(Ans)
# -*- coding: utf-8 -*- import heapq N, M = list(map(int, input().split())) Wlist = [] for i in range(N): A, B = list(map(int, input().split())) Wlist.append([A,B]) Wlist.sort(key=lambda x:x[0]) h = [] Ans = 0 Index = 0 for day in range(1,M+1): while Index < N: if Wlist[Index][0] <= day: heapq.heappush(h, -Wlist[Index][1]) Index += 1 else: break if len(h): Ans += -heapq.heappop(h) print(Ans)
p02948
import sys from collections import deque input = sys.stdin.readline def main(): n,m=list(map(int,input().split())) ab=[list(map(int,input().split())) for _ in range(n)] ab.sort() c=[0]*n ans=0 for i in range(1,m+1): mb=0 mnum=-1 for j in range(n): if ab[j][0]<=i: if mb<ab[j][1] and c[j]==0: mb=ab[j][1] mnum=j else: break ans+=mb if mnum!=-1: c[mnum]=1 print(ans) if __name__ == '__main__': main()
import sys from heapq import heappush, heappop input = sys.stdin.readline def main(): n,m=list(map(int,input().split())) ab=[] for i in range(n): a,b=list(map(int,input().split())) if a>m: continue ab.append((a,b)) ab.sort(reverse=True) q=[] ans=0 for i in range(1,m+1): while ab and ab[-1][0]<=i: a,b=ab.pop() heappush(q,-b) if q: b=-heappop(q) ans+=b print(ans) if __name__ == '__main__': main()
p02948
import heapq N, M = list(map(int, input().split())) AB = [list(map(int, input().split())) for i in range(N)] AB.sort() ans = 0 candidates = [] now = 0 for i in range(M+1): for j in range(now, N): if AB[j][0] <= i: heapq.heappush(candidates, -AB[j][1]) now = j + 1 else: now = j break if candidates: ans -= heapq.heappop(candidates) print(ans)
# イベントソートでもできると思う import heapq N, M = list(map(int, input().split())) event_list = [[m, 10**5] for m in range(1, M+1)] for i in range(N): a, b = list(map(int, input().split())) event_list.append([a, b]) event_list.sort() jobs = [] ans = 0 for event in event_list: if event[1] < 10**5: heapq.heappush(jobs, -event[1]) else: if jobs: ans -= heapq.heappop(jobs) print(ans)
p02948
""" 案件を報酬を受け取るまでにかかる時間で降順ソートしておく。 """ class segTree: def __init__(self,n,identity): if bin(n).count("1") == 1: self.leaves = 2**(n.bit_length()-1) else: self.leaves = 2**n.bit_length() self.identity = identity self.tree = [[self.identity,-1] for i in range(self.leaves*2)] for i in range(self.leaves,self.leaves*2): self.tree[i] = [self.identity,i-self.leaves] def merge(self,x,y): #ここにマージ処理を書く if x[0] < y[0]: return x[::1] else: return y[::1] def update(self,i,x): i = i+self.leaves self.tree[i][0] = x i >>= 1 while i > 0: self.tree[i] = self.merge(self.tree[i*2],self.tree[i*2+1]) i >>= 1 def query(self,l,r): #[l:r)のクエリ結果を返す l = l + self.leaves r = r + self.leaves res = [self.identity,-1] while l < r: if l&1: res = self.merge(res,self.tree[l]) l += 1 if r&1: r -= 1 res = self.merge(res,self.tree[r]) l >>= 1 r >>= 1 return res N,M = list(map(int,input().split())) M += 1 sgt = segTree(N,float("INF")) AB = [list(map(int,input().split())) for _ in range(N)] AB.sort(key=lambda x: -x[0]) today = 1 for i in range(N): a,b = AB[i] due = M-a #M日までに報酬を受け取るためには、何日までに働かなくてはいけないかを求める。 if today <= due: #今日の日付がdue以下であれば、今日その仕事をこなす sgt.update(today-1,b) today += 1 else: if due < 1: continue #今日の日付がdueを過ぎてしまっている場合は、due以前の労働で報酬がb以下の仕事のうち、報酬がもっとも低い仕事を塗り替える。 tmp = sgt.query(0,due) if tmp[0] < b: sgt.update(tmp[1],b) ans = 0 for i in range(N): if sgt.tree[i+sgt.leaves][0] != float("INF"): ans += sgt.tree[i+sgt.leaves][0] print(ans)
""" Aの値が大きいものからheapqにぶち込んでいけばよい。 """ from heapq import heappop,heappush N,M = list(map(int,input().split())) AB = [list(map(int,input().split())) for _ in range(N)] AB.sort(reverse=True) que = [] for a,b in AB: if M >= a: heappush(que,b) M -= 1 elif que: if que[0] < b: heappop(que) heappush(que,b) print((sum(que)))
p02948
import sys readline = sys.stdin.readline # 逆順に見ていく # A_iの値が小さい順にheapqに追加していく N,M = list(map(int,readline().split())) from collections import defaultdict tasks = defaultdict(list) for i in range(N): a,b = list(map(int,readline().split())) tasks[a].append(b) import heapq q = [] heapq.heapify(q) ans = 0 for i in range(1,M + 1): for task in tasks[i]: heapq.heappush(q,-task) if q: t = heapq.heappop(q) ans += (-t) print(ans)
import sys readline = sys.stdin.readline N,M = list(map(int,readline().split())) from collections import defaultdict tasks = defaultdict(list) for i in range(N): a,b = list(map(int,readline().split())) tasks[a].append(b) import heapq as hq q = [] ans = 0 for i in range(1, M + 1): newtask = tasks[i] for t in newtask: hq.heappush(q,-t) if q: ans += -hq.heappop(q) print(ans)
p02948
from collections import defaultdict import heapq N,M=list(map(int,input().split())) x=defaultdict(list) for i in range(N): a,b=list(map(int,input().split())) x[a].append(-b) y=[] an=0 for i in range(M+1): y+=x[i] heapq.heapify(y) try: an-=heapq.heappop(y) except: pass print(an)
import heapq N,M=list(map(int,input().split())) x={} for i in range(M+1): x[i]=[] for i in range(N): a,b=list(map(int,input().split())) if a>M: continue x[a].append(-b) y=[] an=0 for i in range(M+1): for n in x[i]: heapq.heappush(y,n) try: an-=heapq.heappop(y) except: pass print(an)
p02948
import heapq n,m=list(map(int,input().split())) c=[[] for i in range(m+1)] for i in range(n): a,b=list(map(int,input().split())) if a<=m: c[a].append(-b) d=[] heapq.heapify(d) ans=0 for i in range(1,m+1): for j in range(len(c[i])): heapq.heappush(d,c[i][j]) if d!=[]: ans+=heapq.heappop(d) print((-ans))
import heapq n,m=list(map(int,input().split())) c=[[] for i in range(m+1)] for i in range(n): a,b=list(map(int,input().split())) if a<=m: c[a].append(-b) d=[] heapq.heapify(d) ans=0 for i in range(1,m+1): for j in c[i]: heapq.heappush(d,j) if d!=[]: ans+=heapq.heappop(d) print((-ans))
p02948
mod = 10 ** 9 + 7 from collections import deque import heapq def main(): N, M = iip(False) A = [] for i in range(N): A.append(iip(True)) ret = solve(N, M, A) print(ret) def solve(N, M, A): A = sorted(A, key=lambda x: x[1], reverse=True) sch = list(range(M)) ret = 0 for nouki, money in A: day = M - nouki if day < 0: continue id = koenai_saidai_x_index(sch, day) #print(sch) #print(nouki, money, id) if id is False: continue sch.pop(id) ret += money return ret #####################################################ライブラリ集ここから def split_print_space(s): print((" ".join([str(i) for i in s]))) def split_print_enter(s): print(("\n".join([str(i) for i in s]))) def koenai_saidai_x_index(sorted_list, n): l = 0 r = len(sorted_list) if len(sorted_list) == 0: return False if sorted_list[0] > n: return False while r-l > 1: x = (l+r)//2 if sorted_list[x] == n: return x elif sorted_list[x] > n: r = x else: l = x return l def searchsorted(sorted_list, n, side): if side not in ["right", "left"]: raise Exception("sideはrightかleftで指定してください") l = 0 r = len(sorted_list) if n > sorted_list[-1]: # print(sorted_list) return len(sorted_list) if n < sorted_list[0]: return 0 while r - l > 1: x = (l + r) // 2 if sorted_list[x] > n: r = x elif sorted_list[x] < n: l = x else: if side == "left": r = x elif side == "right": l = x if side == "left": if sorted_list[l] == n: return r - 1 else: return r if side == "right": if sorted_list[l] == n: return l + 1 else: return l def iip(listed): ret = [int(i) for i in input().split()] if len(ret) == 1 and not listed: return ret[0] return ret def soinsuu_bunkai(n): ret = [] for i in range(2, int(n ** 0.5) + 1): while n % i == 0: n //= i ret.append(i) if i > n: break if n != 1: ret.append(n) return ret def conbination(n, r, mod, test=False): if n <= 0: return 0 if r == 0: return 1 if r < 0: return 0 if r == 1: return n ret = 1 for i in range(n - r + 1, n + 1): ret *= i ret = ret % mod bunbo = 1 for i in range(1, r + 1): bunbo *= i bunbo = bunbo % mod ret = (ret * inv(bunbo, mod)) % mod if test: # print(f"{n}C{r} = {ret}") pass return ret def inv(n, mod): return power(n, mod - 2) def power(n, p): if p == 0: return 1 if p % 2 == 0: return (power(n, p // 2) ** 2) % mod if p % 2 == 1: return (n * power(n, p - 1)) % mod if __name__ == "__main__": main()
mod = 10 ** 9 + 7 from collections import deque import heapq def main(): N, M = iip(False) A = [] for i in range(N): A.append(iip(True)) ret = solve(N, M, A) print((-ret)) def solve(N, M, A): A = deque(sorted(A, key=lambda x: x[0], reverse=False)) jobs = [] heapq.heapify(jobs) ret = 0 for day in range(M): nokori = day + 1 dn = 0 #print(nokori) #print(A) for job in A: if job[0] == nokori: heapq.heappush(jobs, -job[1]) dn += 1 else: break for i in range(dn): A.popleft() #print(jobs) if jobs: #print(jobs) ret += heapq.heappop(jobs) #print(nokori) #print(jobs) #print(A) return ret #####################################################ライブラリ集ここから def split_print_space(s): print((" ".join([str(i) for i in s]))) def split_print_enter(s): print(("\n".join([str(i) for i in s]))) def koenai_saidai_x_index(sorted_list, n): l = 0 r = len(sorted_list) if len(sorted_list) == 0: return False if sorted_list[0] > n: return False while r-l > 1: x = (l+r)//2 if sorted_list[x] == n: return x elif sorted_list[x] > n: r = x else: l = x return l def searchsorted(sorted_list, n, side): if side not in ["right", "left"]: raise Exception("sideはrightかleftで指定してください") l = 0 r = len(sorted_list) if n > sorted_list[-1]: # print(sorted_list) return len(sorted_list) if n < sorted_list[0]: return 0 while r - l > 1: x = (l + r) // 2 if sorted_list[x] > n: r = x elif sorted_list[x] < n: l = x else: if side == "left": r = x elif side == "right": l = x if side == "left": if sorted_list[l] == n: return r - 1 else: return r if side == "right": if sorted_list[l] == n: return l + 1 else: return l def iip(listed): ret = [int(i) for i in input().split()] if len(ret) == 1 and not listed: return ret[0] return ret def soinsuu_bunkai(n): ret = [] for i in range(2, int(n ** 0.5) + 1): while n % i == 0: n //= i ret.append(i) if i > n: break if n != 1: ret.append(n) return ret def conbination(n, r, mod, test=False): if n <= 0: return 0 if r == 0: return 1 if r < 0: return 0 if r == 1: return n ret = 1 for i in range(n - r + 1, n + 1): ret *= i ret = ret % mod bunbo = 1 for i in range(1, r + 1): bunbo *= i bunbo = bunbo % mod ret = (ret * inv(bunbo, mod)) % mod if test: # print(f"{n}C{r} = {ret}") pass return ret def inv(n, mod): return power(n, mod - 2) def power(n, p): if p == 0: return 1 if p % 2 == 0: return (power(n, p // 2) ** 2) % mod if p % 2 == 1: return (n * power(n, p - 1)) % mod if __name__ == "__main__": main()
p02948
# -*- coding: utf-8 -*- import sys import copy import collections from bisect import bisect_left from bisect import bisect_right from collections import defaultdict from heapq import heappop, heappush, heapify import math import itertools import random # NO, PAY-PAY #import numpy as np #import statistics #from statistics import mean, median,variance,stdev INF = float('inf') def inputInt(): return int(eval(input())) def inputMap(): return list(map(int, input().split())) def inputList(): return list(map(int, input().split())) def main(): N,M = inputMap() ab = [] for i in range(N): a,b = inputMap() if a > M: continue ab.append((a,b)) ab = sorted(ab, key=lambda x:(x[0],-x[1])) pq = [] heapify(pq) ans = 0 for i in range(1,M+1): ind = -1 for j,val in enumerate(ab): a,b = val ind = -1 if a > i: break if i == a: heappush(pq, -b) ind = j if len(pq) > 0: tmp = heappop(pq) * (-1) ans += tmp if ind != -1: ab = ab[ind+1:] print(ans) if __name__ == "__main__": main()
# -*- coding: utf-8 -*- import sys import copy import collections from bisect import bisect_left from bisect import bisect_right from collections import defaultdict from heapq import heappop, heappush, heapify import math import itertools import random # NO, PAY-PAY #import numpy as np #import statistics #from statistics import mean, median,variance,stdev INF = float('inf') def inputInt(): return int(eval(input())) def inputMap(): return list(map(int, input().split())) def inputList(): return list(map(int, input().split())) def main(): N,M = inputMap() ab = [] heapify(ab) for i in range(N): a,b = inputMap() if a > M: continue heappush(ab, (a,b)) ans = 0 pq = [] heapify(pq) for i in range(1,M+1): while True: if len(ab) == 0 or ab[0][0] != i: break a,b = heappop(ab) heappush(pq, -b) if len(pq) > 0: ans = ans + (heappop(pq) * (-1)) print(ans) if __name__ == "__main__": main()
p02948