- Define the priority key and whether the smallest or largest item should lead.
- Push each candidate when it becomes eligible.
- Discard stale entries when necessary and process the best live candidate.
Code notes
- 107 lines of Python from the credited upstream file abc376_e.py.
- The implementation visibly relies on sequence storage, ordered lookup, work queue.
- No explicit loop blocks detected.
Complexity
Count heap pushes and pops; each normally contributes a logarithmic factor in the heap size.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3from heapq import heapify, heappop, heappush4from typing import List, Optional5 6 7class DeletableHeapq:8 """Alternatives to ordered set (set) in C++.9 10 Landau notation: O(log(n))11 12 See:13 https:qiita.com/physharp/items/f9229ab879cac9a944d714 https:prd-xxx.hateblo.jp/entry/2019/06/24/23584415 """16 17 def __init__(self, descending_order=False) -> None:18 self.q: List[int] = [] 19 self.p: List[int] = [] 20 self.descending_order = descending_order21 self.sign = -1 if descending_order else 122 23 def build(self, a: List[int]) -> None:24 """Build a priority-queue q from an array."""25 26 if self.descending_order:27 a = [-ai for ai in a]28 29 self.q = a30 heapify(self.q)31 32 def push(self, number: int) -> None:33 """Add a number to the priority-queue."""34 heappush(self.q, number * self.sign)35 36 def erase(self, number: int) -> None:37 """Pseudo-erase a number to the priority-queue."""38 heappush(self.p, number * self.sign)39 self.clean()40 41 def clean(self) -> None:42 """Remove top elements from q, p."""43 44 while self.p and self.q[0] == self.p[0]:45 heappop(self.q)46 heappop(self.p)47 48 def pop(self, exc=None) -> Optional[int]:49 """Pop a top value from the priority-queue."""50 self.clean()51 52 if self.q:53 return heappop(self.q) * self.sign54 return exc55 56 def top(self, exc=None) -> Optional[int]:57 """Get a top value from the priority-queue.58 59 Landau notation: O(1)60 61 Note:62 descending_order=False: min value.63 descending_order=True : max value.64 """65 66 if self.q:67 return self.q[0] * self.sign68 return exc69 70 71def solve():72 n, k = map(int, input().split())73 74 a = list(map(int, input().split()))75 b = list(map(int, input().split()))76 ab = sorted([(ai, bi) for ai, bi in zip(a, b)])77 78 hq = DeletableHeapq(descending_order=True)79 summed = 080 inf = 10**1881 ans = inf82 83 for ai, bi in ab:84 hq.push(bi)85 summed += bi86 87 if len(hq.q) == k:88 ans = min(ans, ai * summed)89 summed -= hq.pop()90 91 print(ans)92 93 94def main():95 import sys96 97 input = sys.stdin.readline98 99 t = int(input())100 101 for _ in range(t):102 solve()103 104 105if __name__ == "__main__":106 main()107