Problem solution · Python

ABC390 E — Vitamin Balance

ABC390 E — Vitamin Balance: a Python solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Dynamic programming
Source
KATO-Hiro AtCoder Solutions
Length
86 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

Dynamic programming

For ABC390 E — Vitamin Balance, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 86 lines of Python from the credited upstream file abc390_e.py.
  • The implementation visibly relies on sequence storage, ordered lookup, cached states.
  • No explicit loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

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 codeABC390 E — Vitamin Balance · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*- from bisect import bisect_leftfrom typing import List  def bisect_ge(sorted_array: List[int], value: int):    """Find the smallest element >= x and its index, or None if it doesn't exist."""     if sorted_array[-1] >= value:        index: int = bisect_left(sorted_array, value)         return index, sorted_array[index]     return None, None  def main():    import sys     input = sys.stdin.readline     n, x = map(int, input().split())    foods = [list() for _ in range(3)]     for _ in range(n):        vi, ai, ci = map(int, input().split())        vi -= 1        foods[vi].append((ai, ci))     vitamins = list()     # 各ビタミンについて、ナップサック問題    for food in foods:        dp = [0 for _ in range(x + 1)]         for fi in food:            ndp = [0 for _ in range(x + 1)]            aj, cj = fi             for j in range(x + 1):                ndp[j] = max(ndp[j], dp[j])                nj = j + cj                 if nj > x:                    continue                 ndp[nj] = max(ndp[nj], dp[j] + aj)             dp = ndp         vitamins.append(dp)     # 最小値の最大化 = 二分探索    ac, wa = 0, 10**9     def f(wj):        total = 0         for i in range(3):            if vitamins[i][x] < wj:                return False             j, _ = bisect_ge(vitamins[i], wj)             if j is None:                return False             total += j         return total <= x     while abs(wa - ac) > 1:        wj = (ac + wa) // 2         if f(wj):            ac = wj        else:            wa = wj     print(ac)  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 ↗