Official

A - 荷物の積載 / Loading Cargo Editorial by admin

Qwen3-Coder-480B

Overview

For each truck, find the maximum number of items that can be loaded while satisfying both the weight capacity and quantity constraints.

Analysis

Each truck \(i\) can carry at most \(R\) items, and the total weight must not exceed \(T\) kilograms. Since each item weighs \(P_i\), at most \(T // P_i\) items can be loaded (rounded down). Therefore, for each truck, the answer is the smaller of the following two values:

  • Upper limit by quantity: \(R\)
  • Upper limit by weight restriction: \(\left\lfloor \frac{T}{P_i} \right\rfloor\)

In other words, \(r_i = \min(R,\ \left\lfloor T / P_i \right\rfloor)\).

However, when \(P_i = 0\), division is not possible, but in this case there is no weight restriction, so all \(R\) items can be loaded.

A naive approach would be to try adding items one by one and check whether the weight is exceeded, but since \(T\) and \(R\) can be very large (up to \(10^{18}\)), simulating with a loop would result in TLE. Therefore, it is necessary to solve this efficiently by computing the result directly.

Algorithm

  1. For each truck’s item weight \(P_i\), do the following:
    • If \(P_i = 0\), the result is \(R\)
    • Otherwise, the result is the smaller of \(\left\lfloor T / P_i \right\rfloor\) and \(R\)
  2. Output the results for all trucks

This computation takes constant time per truck, so it can be processed efficiently.

Complexity

  • Time complexity: \(O(N)\)
  • Space complexity: \(O(N)\)

Implementation Notes

  • sys.stdin.read is used to read input efficiently

  • Special attention is needed to avoid division by zero when \(P_i = 0\)

  • Floor division (//) is used to ensure that integer division does not automatically produce a decimal result

    Source Code

import math

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    
    N = int(data[0])
    R = int(data[1])
    T = int(data[2])
    
    P = list(map(int, data[3:3+N]))
    
    result = []
    for p in P:
        if p == 0:
            # 荷物の重さが0ならR回全部積める
            result.append(R)
        else:
            # 最大何回積めるか = min(R, T // P_i)
            max_loads = T // p
            r_i = min(R, max_loads)
            result.append(r_i)
    
    print(' '.join(map(str, result)))

if __name__ == "__main__":
    main()

This editorial was generated by qwen3-coder-480b.