Problem solution · Python

ABC376 E — Max × Sum

ABC376 E — Max × Sum: a Python solution using heap or priority queue. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Heap or priority queue
Source
KATO-Hiro AtCoder Solutions
Length
107 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Heap or priority queue

For ABC376 E — Max × Sum, the implementation repeatedly takes the currently best candidate from a heap while inserting newly available choices.

  1. Define the priority key and whether the smallest or largest item should lead.
  2. Push each candidate when it becomes eligible.
  3. 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.

Source

Code and credit

This code comes from KATO-Hiro AtCoder Solutions by KATO-Hiro and is used under the CC0-1.0 licence.

Full codeABC376 E — Max × Sum · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*- from heapq import heapify, heappop, heappushfrom typing import List, Optional  class DeletableHeapq:    """Alternatives to ordered set (set) in C++.     Landau notation: O(log(n))     See:    https://qiita.com/physharp/items/f9229ab879cac9a944d7    https://prd-xxx.hateblo.jp/entry/2019/06/24/235844    """     def __init__(self, descending_order=False) -> None:        self.q: List[int] = []  # Body        self.p: List[int] = []  # For deleting        self.descending_order = descending_order        self.sign = -1 if descending_order else 1     def build(self, a: List[int]) -> None:        """Build a priority-queue q from an array."""         if self.descending_order:            a = [-ai for ai in a]         self.q = a        heapify(self.q)     def push(self, number: int) -> None:        """Add a number to the priority-queue."""        heappush(self.q, number * self.sign)     def erase(self, number: int) -> None:        """Pseudo-erase a number to the priority-queue."""        heappush(self.p, number * self.sign)        self.clean()     def clean(self) -> None:        """Remove top elements from q, p."""         while self.p and self.q[0] == self.p[0]:            heappop(self.q)            heappop(self.p)     def pop(self, exc=None) -> Optional[int]:        """Pop a top value from the priority-queue."""        self.clean()         if self.q:            return heappop(self.q) * self.sign        return exc     def top(self, exc=None) -> Optional[int]:        """Get a top value from the priority-queue.         Landau notation: O(1)         Note:        descending_order=False: min value.        descending_order=True : max value.        """         if self.q:            return self.q[0] * self.sign        return exc  def solve():    n, k = map(int, input().split())     a = list(map(int, input().split()))    b = list(map(int, input().split()))    ab = sorted([(ai, bi) for ai, bi in zip(a, b)])     hq = DeletableHeapq(descending_order=True)    summed = 0    inf = 10**18    ans = inf     for ai, bi in ab:        hq.push(bi)        summed += bi         if len(hq.q) == k:            ans = min(ans, ai * summed)            summed -= hq.pop()     print(ans)  def main():    import sys     input = sys.stdin.readline     t = int(input())     for _ in range(t):        solve()  if __name__ == "__main__":    main() 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗