Official

A - サービス券 / Service Tickets Editorial by admin

gpt-6-astra-medium

Overview

For each customer, find the minimum of the number of discount vouchers calculated from their payment amount and the upper limit \(M\), and take the sum over all customers.

Analysis

Without the upper limit, the number of vouchers given to the \(i\)-th customer would be \(\left\lfloor A_i / 100 \right\rfloor\). Here, \(\lfloor x \rfloor\) denotes the value rounded down to the nearest integer.

However, since at most \(M\) vouchers can be given to a single person, the actual number of vouchers given is

\(\min\left(\left\lfloor A_i / 100 \right\rfloor, M\right)\)

For example, when \(M = 3\):

  • A customer who pays \(250\) yen receives \(2\) vouchers.
  • A customer who pays \(580\) yen receives \(3\) vouchers due to the limit, even though the calculation gives \(5\).
  • A customer who pays \(80\) yen receives none, as it is less than \(100\) yen.

In this case, the total is \(2 + 3 + 0 = 5\) vouchers.

Summing up all customers’ payments first and then dividing by \(100\) does not yield the correct answer. For instance, if there are two customers who each pay \(50\) yen, the number of vouchers given to each is \(0\). Furthermore, the limit \(M\) must be applied to each customer individually, not to the total.

Therefore, the problem can be solved by calculating the number of vouchers for each customer individually and adding them up. Since \(N \leq 100\), processing all customers one by one is sufficiently fast.

Algorithm

  1. Read \(N\), \(M\), and each customer’s payment amount.
  2. Initialize a variable total, representing the total number of vouchers, to \(0\).
  3. For each customer, do the following:
    • Find the quotient of the payment amount divided by \(100\).
    • Add the smaller of that quotient and \(M\) to total.
  4. Output total.

The required answer can be expressed by the following formula:

\(\displaystyle \sum_{i=1}^{N} \min\left(\left\lfloor A_i / 100 \right\rfloor, M\right)\)

Complexity

  • Time complexity: \(O(N)\)
    A constant-time calculation is performed once for each customer.
  • Space complexity: \(O(N)\)
    In the provided code, the input is read all at once and stored in the list data. Excluding the storage for the input, the additional working memory is \(O(1)\).

Implementation Notes

  • In Python, // represents integer division. Since the payment amounts are positive integers, A // 100 computes the quotient rounded down.

  • Writing min(A // 100, M) allows you to apply the limit per person.

  • The limit applies to each customer. Be careful not to write min(total, M) after summing them up.

    Source Code

def main():
    import sys
    data = sys.stdin.read().split()
    N = int(data[0])
    M = int(data[1])
    total = 0
    for i in range(N):
        A = int(data[2 + i])
        total += min(A // 100, M)
    print(total)

main()

This editorial was generated by gpt-6-astra-medium.