Official

A - お買い物の合計金額 / Total Shopping Amount Editorial by admin

Claude 4.5 Opus

Overview

This is a problem to find the remainder when the sum of prices of \(K\) products is divided by \(M\).

Analysis

This problem is very simple and can be solved in the following two steps:

  1. Sum up the prices of all products
  2. Find the remainder when that sum is divided by \(M\)

Verification with a Concrete Example

For example, if \(K = 3\), \(M = 100\), and the product prices are \(L = [150, 80, 35]\): - Total amount: \(150 + 80 + 35 = 265\) yen - Points: \(265 \mod 100 = 65\) points

Points to Note in This Problem

  • Overflow concerns: Since \(K\) is at most \(10^5\) and each \(L_i\) is at most \(10^4\), the maximum sum is approximately \(10^5 \times 10^4 = 10^9\). In Python, there is no integer overflow, so there’s no need to worry about this.
  • A naive approach is sufficient: In this problem, simply calculating the sum and computing the remainder is enough to easily pass both time and memory limits.

Algorithm

  1. Read \(K\), \(M\) and the price list \(L\) from input
  2. Calculate the sum of all product prices using sum(L)
  3. Find the remainder when dividing the sum by \(M\) using the % operator
  4. Output the result

In Python, the built-in function sum() is provided for calculating the sum of a list, so this can be written concisely in one line.

Complexity

  • Time complexity: \(O(K)\)
    • Reading the list takes \(O(K)\)
    • Computing sum(L) takes \(O(K)\)
    • Computing the remainder takes \(O(1)\)
  • Space complexity: \(O(K)\)
    • The list \(L\) storing prices takes \(O(K)\)

Implementation Notes

  • By using Python’s sum() function, you can implement this concisely without writing a loop

  • The % operator computes the remainder (e.g., 7 % 3 returns 1)

  • For reading input, map(int, input().split()) is used to efficiently read space-separated integers

    Source Code

K, M = map(int, input().split())
L = list(map(int, input().split()))
print(sum(L) % M)

This editorial was generated by claude4.5opus.

posted:
last update: