Suppose denominations of banknotes follow the infinity Hyperinflation sequence: \$ $1, $2, $5, $10, $20, $50, $100, $200, $500, $1000, $2000, $5000, \cdots \$. How many banknotes are required, at minimum, to pay a \$$n\$ bill?
Consider Alice needs to pay \$ $992 \$ to Bob. It is possible for Alice to use 7 banknotes \$ $500, $200, $200, $50, $20, $20, $2 \$ to pay the bill, but that uses a lot of banknotes. We can see that a better solution is: Alice pays 2 banknotes (\$ $1000, $2 \$), and Bob gives her \$ $10 \$ in change. So, we only need 3 banknotes here.
Formal Definition
Banknotes follow an infinite sequence \$b_i\$: $$ b_n=10\cdot b_{n-3} $$ with base cases $$ b_1=1, b_2=2, b_3=5 $$
When Alice pays \$ $x \$ to Bob, Alice pays \$ a_i \$ banknotes with denominations \$b_i\$. \$ a_i \in \mathbb{Z} \$ And $$ \sum a_ib_i=x $$
\$ a_i \$ may be negative which means Bob gives Alice these banknotes in change.
You are going to calculate: $$ f\left(x\right)=\min_{\sum a_ib_i=x} \sum\left|a_i\right| $$
Input / Output
Input a non-negative number representing the amount of money to pay. Output the minimum number of banknotes required.
Rules
- This is code-golf: Shortest codes in bytes win.
- Your program should be able to handle inputs \$ 0 \le n < 100{,}000 \$ at least. Your algorithm should work for arbitrary large numbers in theory.
- As this questions is only focused on integers, floating point errors are not allowed.
Testcases
Input -> Output
0 -> 0
1 -> 1
2 -> 1
3 -> 2
4 -> 2
5 -> 1
6 -> 2
7 -> 2
8 -> 2
9 -> 2
10 -> 1
11 -> 2
12 -> 2
13 -> 3
14 -> 3
15 -> 2
16 -> 3
17 -> 3
18 -> 2
19 -> 2
20 -> 1
40 -> 2
41 -> 3
42 -> 3
43 -> 3
44 -> 3
45 -> 2
46 -> 3
47 -> 3
48 -> 2
49 -> 2
50 -> 1
90 -> 2
91 -> 3
92 -> 3
93 -> 3
94 -> 3
95 -> 2
96 -> 3
97 -> 3
98 -> 2
99 -> 2
100 -> 1
980 -> 2
981 -> 3
982 -> 3
983 -> 4
984 -> 4
985 -> 3
986 -> 4
987 -> 4
988 -> 3
989 -> 3
990 -> 2
991 -> 3
992 -> 3
993 -> 3
994 -> 3
995 -> 2
996 -> 3
997 -> 3
998 -> 2
999 -> 2
1000 -> 1
1341 -> 6
2531 -> 5
3301 -> 5
4624 -> 6
5207 -> 4
6389 -> 6
6628 -> 7
6933 -> 6
7625 -> 6
8899 -> 4
13307 -> 7
23790 -> 5
33160 -> 7
33325 -> 8
40799 -> 5
55641 -> 7
66472 -> 8
77825 -> 6
89869 -> 6
98023 -> 5