Official

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

Gemini 3.0 Flash

Overview

This is a problem where you need to find the total sum of prices of \(K\) items and calculate the remainder (points) when dividing it by \(M\).

Analysis

This problem can be solved by simply summing up all \(K\) given prices \(L_1, L_2, \ldots, L_K\) and outputting the remainder when divided by \(M\).

Checking the constraints, the number of items \(K\) is at most \(10^5\), and each item’s price \(L_i\) is at most \(10^4\). Therefore, the maximum total amount is approximately \(10^5 \times 10^4 = 10^9\). Since Python automatically handles very large integers, there is no concern about overflow when computing this sum directly.

Also, since \(K\) can be as large as \(10^5\), reading all input at once and processing it together is faster in terms of execution time compared to processing inputs one by one.

Algorithm

The problem can be solved with the following steps:

  1. Read \(K\) and \(M\) from the input.
  2. Read the \(K\) prices \(L_i\) as a list.
  3. Calculate the sum \(S\) of all elements in the list.
  4. Calculate the remainder of \(S\) divided by \(M\) (\(S \pmod M\)) and output it.

Complexity

  • Time complexity: \(O(K)\)
    • Reading \(K\) numbers and scanning through them once to compute the sum takes time proportional to the number of inputs \(K\).
  • Space complexity: \(O(K)\)
    • If all prices \(L_i\) are stored in a list at once, the memory usage is proportional to \(K\).

Implementation Notes

  • Fast input: When reading a large amount of data in Python, using sys.stdin.read().split() allows you to read the entire standard input at once and quickly obtain a list split by spaces and newlines.

  • Efficient sum calculation: By combining the map function and sum function as in sum(map(int, data)), you can compute the sum more concisely and faster than writing an explicit loop.

    Source Code

import sys

def main():
    # 標準入力からすべてのデータを読み込み、スペースや改行で分割します
    data = sys.stdin.read().split()
    if not data:
        return
    
    # K: 商品の個数, M: ポイント計算用の除数
    K = int(data[0])
    M = int(data[1])
    
    # L: 各商品の価格(インデックス2以降のデータ)
    # mapを使用して整数に変換し、sumで合計を求めます
    total_price = sum(map(int, data[2:]))
    
    # 合計金額をMで割った余りを出力します
    print(total_price % M)

if __name__ == "__main__":
    main()

This editorial was generated by gemini-3-flash-preview.

posted:
last update: