5. In the country of Lemonia, coins in circulation have denominations of $3n-2$, $6n-1$, $6n+2$, and $6n+5$ piastres, where $n$ is a natural number. A resident of the country went to the bank without any cash on hand. What is the largest amount that the bank will not be able to give him?
Answer: $6 n^{2}-4 n-3$.

Solution. Let's call a natural number $s$ decomposable if the bank can pay the sum $s$ using only coins of value $6 n-1, 6 n+2$, and $6 n+5$ piastres. For any $r=1, \ldots, 3 n-3$, let $m_{r}$ be the smallest decomposable number that gives a remainder of $r$ when divided by $3 n-2$. We will prove two statements.

1) If $r \in \{1, \ldots, 3 n-3\}$ and $s \bmod (3 n-2)=r$, then for $s \geqslant m_{r}$, the bank can pay the sum $s$, and for $s < m_{r}$, it cannot. The first statement is obvious: one needs to pay the sum $m_{r}$ first, and then add some number of coins worth $3 n-2$ piastres. Suppose now that $s < m_{r}$ and the sum $s$ can be paid. In this case, several coins worth $3 n-2$ piastres must be used. Removing all these coins, we get a decomposable sum, smaller than $m_{r}$, and also giving a remainder of $r$ when divided by $3 n-2$. This contradicts the minimality of $m_{r}$.
2) The numbers $3 k \bmod (3 n-2)$ take on the values $\{1, \ldots, 3 n-3\}$ for $k=1, \ldots, 3 n-3$. Indeed, it is sufficient to show that all of them are distinct. If $\left(3 k-3 k^{\prime}\right) \vdots (3 n-2)$, then $\left(k-k^{\prime}\right) \vdots (3 n-2)$. Since $\left|k-k^{\prime}\right| \leqslant 3 n-3$, we get $k=k^{\prime}$.

Obviously, sums that are multiples of $3 n-2$ can be paid. Then, by 1), the answer to the problem will be $\max \left\{m_{1}, \ldots, m_{3 n-3}\right\}-(3 n-2)$. Let's find the largest of the numbers $m_{r}$. Let $r \in \{1, \ldots, 3 n-3\}$. Note that

$$
6 n-1=2(3 n-2)+3, \quad 6 n+2=2(3 n-2)+6 \quad \text{ and } \quad 6 n+5=2(3 n-2)+9.
$$

By 2), there exists a unique number $k \in \{1, \ldots, 3 n-3\}$ for which $3 k \bmod (3 n-2)=r$. Since the remainders of the coins modulo $3 n-2$ take the values 3, 6, and 9, we need to represent $3 k$ as a sum of the minimum number of these numbers. This is achieved as follows: we add nines until we get the largest number not exceeding $3 k$, and then add 3 or 6 if necessary. In this sum, there will be $\left[\frac{k+2}{3}\right]$ terms. Then

$$
m_{r}=3 k+\left[\frac{k+2}{3}\right] \cdot 2(3 n-2) \leqslant 3(3 n-3)+2(n-1)(3 n-2)=6 n^{2}-n-5
$$

and equality is achieved for $k=3 n-3$. Therefore, the answer to the problem is

$$
\left(6 n^{2}-n-5\right)-(3 n-2)=6 n^{2}-4 n-3
$$

## Variant 7