Best Name
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Bohdan, Danylo and Maksym are trying to come up with a team name for an interesting competition. According to the rules of the competition, the team name must consist of lowercase Latin letters and have a length of at most \(n\). Since the guys are not very creative, all the options they suggested were quite strange and immediately discarded. Therefore, they asked you to come up with a name for them.
It is also known that for each occurrence of the string
bohdan in the name, Bohdan will pay you \(b\) hryvnias, for each occurrence of the
string danylo, Danylo will pay \(d\) hryvnias, and Maksym will pay you \(m\) hryvnias for each occurrence of the
string maksym.
What is the maximum amount of money you can get if you come up with an optimal team name?
Input
The single line contains four integers \(b\), \(d\), \(m\) and \(n\) — the amounts of hryvnias that Bohdan, Danylo, and Maksym will pay you respectively, and the length of the team name.
Output
Print a single integer in one line — the answer to the problem.
Constraints
\(0 \le b, d, m \le 100\),
\(5\) tests: \(1 \le n \le 100\),
\(5\) tests: \(1 \le n \le 10^3\),
\(7\) tests: \(1 \le n \le 10^6\),
\(8\) tests: \(1 \le n \le 10^9\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 2 1 2 11 | 4 |
Notes
In the example, \(b = 2\), \(d = 1\), \(m =
2\) and \(n = 11\). You can
construct the name maksymaksym and get 4 hryvnias from
Maksym (two for each occurrence).
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Best Name - Editorial
One can notice that an optimal string is a concatenation of strings
of the form bohdan, danylo,
maksym and bohdanylo. Without loss of
generality, assume \(d \le b\), so
there is no point in using the string danylo in our name
(unless it is part of the string bohdanylo).
Consider two options:
Do not use the string
maksymin the name. Suppose that in an optimal name the stringbohdanoccurs at least 9 times andbohdanyloat least 6 times. Then replacing nine stringsbohdanwith six stringsbohdanylo(or vice versa) does not decrease the profit, so there is an optimal name with fewer than 9 stringsbohdanor fewer than 6 stringsbohdanylo. So we go over the number of stringsbohdanfrom 0 to 8 and give the rest of the space to the stringbohdanylo(and vice versa). This way we get the optimal name for the team.Use the string
maksymat least once. Then it is easy to see that the stringsmaksymgo next to each other, and \(1 \le k\) stringsmaksymtake exactly \(5k + 1\) characters. As in the previous case, one can show that there is an optimal name where the number of stringsmaksym,bohdanorbohdanylodoes not exceed, for example, 20 (there are stricter bounds too). So by going over the number of strings of one of these types, we reduce the problem to the previous case (see the code for a better understanding).
Complexity: \(O(1)\) time and memory.
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|