QOJ.ac

QOJ

Limite de temps : 2.0 s Limite de mémoire : 2048 MB Points totaux : 100

#17327. 我不懷念便士

Statistiques

2025 年 11 月,美國鑄造了最後一批一分硬幣(one-cent coins)。加拿大自 2012 年起就已停止鑄造一分硬幣。

儘管市面上仍流通著數十億枚一分硬幣,但預計這些硬幣將隨著時間推移逐漸退出流通。商店預計將繼續以「分」為單位標價,因為信用卡交易是以「分」為單位進行處理的。然而,對於現金交易,商店預計會將總金額四捨五入至最接近的五分(nickel)。具體來說,如果總購買金額的最後一位數字為 3、4、8 或 9 分,總額將向上取整;如果最後一位數字為 1、2、6 或 7 分,總額將向下取整。以 0 或 5 分結尾的交易則不進行四捨五入。

你意識到這提供了一個機會。如果你使用現金支付,並適當地重新排列和分組你的購買項目,你或許能為你購買的所有東西節省一點錢!給定你想要購買的各個商品的價格(以分為單位),請計算出透過僅使用現金支付並最佳化分組購買項目,與使用信用卡購買所有商品時的非四捨五入總價相比,你最多可以節省多少錢。

輸入格式

第一行包含一個整數 $N$ ($1 \le N \le 3 \cdot 10^5$),代表你打算購買的商品數量。

下一行包含 $N$ 個以空格分隔的整數 $p_i$ ($1 \le p_i \le 3000$),代表各商品的價格(以分為單位)。

輸出格式

輸出一個整數:信用卡支付的總價(不進行四捨五入)與透過現金支付並最佳化分組後所能獲得的最低總成本之間的差額。

說明

對於第一個範例,其中一種最佳的分組方式為: $\{78, 999, 350\}, \{59, 173, 2995, 350\}, \{882, 298\}, \{1096\}, \{497\}$。

各組的價格分別為 $1427, 3577, 1180, 1096, 497$,因此總差額為: $(1427 + 3577 + 1180 + 1096 + 497) - (1425 + 3575 + 1180 + 1095 + 495) = 7$。

你透過將商品最佳化分組為數個現金交易,而不是全部使用信用卡支付,節省了 7 分錢。

範例

輸入 1

11
78 59 90 999 173 882 1096 2995 298 497 350

輸出 1

7

輸入 2

2
199 299

輸出 2

-2

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.