AlgoLensArena
ContestlərProblemlərOtaqlar
Vizualizator
← Məsələlər

BAPC19-040.Efficient Exchange

Medium · 1 s · 256 MB

  • icpc
  • bapc
  • 2019

You have recently acquired a new job at the Bank for Acquiring Peculiar Currencies. Here people can make payments, and deposit or withdraw money in all kinds of strange currencies. At your first day on the job you help a customer from Nijmegia, a small insignificant country famous for its enormous coins with values equal to powers of 10, that is, 1,10,100,10001, 10, 100, 10001,10,100,1000, etc. This customer wants to make a rather large payment, and you are not looking forward to the prospect of carrying all those coins to and from the vault.

You therefore decide to think things over first. You have an enormous supply of Nijmegian coins in reserve, as does the customer (most citizens from Nijmegia are extremely strong). You now want to minimize the total number of coins that are exchanged, in either direction, to make the exact payment the customer has to make.

For example, if the customer wants to pay 83 coins there are many ways to make the exchange. Here are three possibilities:

• Option 1. The customer pays 888 coins of value 101010, and 333 coins of value 111. This requires exchanging 8+3=118 + 3 = 118+3=11 coins.

• Option 2. The customer pays a coin of value 100100100, and you return a coin of value 101010, and 777 coins of value 111. This requires exchanging 1+1+7=91 + 1 + 7 = 91+1+7=9 coins.

• Option 3. The customer pays a coin of value 100100100, and 333 coins of value 111. You return 222 coins of value 101010. This requires exchanging 1+3+2=61 + 3 + 2 = 61+3+2=6 coins.

It turns out the last way of doing it requires the least coins possible.

Giriş formatı

• A single integer 0≤n<1010000\leq n < 10^{1000}0≤n<101000, the amount the customer from Nijmegia has to pay.

Çıxış formatı

• Output the minimum number of coins that have to be exchanged to make the required payment.

Qeyd

Source: Benelux Algorithm Programming Contest 2019 · Author: Raymond van Bommel · License: CC BY-SA 4.0 · Original: https://bapc.eu · Statement adapted for AlgoLens and shared under the same license.

Nümunələr

Giriş #1

83

Çıxış #1

6

Giriş #2

13

Çıxış #2

4

Giriş #3

0

Çıxış #3

0

Giriş #4

12345678987654321

Çıxış #4

42