A.Maxim vs. Yogurt: The Great Discount Battle
We want the minimum cost to buy exactly $$$n$$$ yogurts when:
- A single yogurt costs $$$a$$$ burles.
- A promotion lets you buy two yogurts for $$$b$$$ burles.
You may mix buying single yogurts and buying pairs. Let's reason through the options, compare costs, and derive a compact formula.
Key Observations
- Always-available baseline:
- You can always buy all yogurts one-by-one. That gives the cost.
Cost = n.a
- You can always buy all yogurts one-by-one. That gives the cost.
When is the promotion useful?
- Buying two yogurts with the promotion costs $$$b$$$. Compare this to buying those two separately:$$$2a$$$.
- If $$$b \geq 2a$$$, the promotion is not beneficial (or is equal). Then buying every yogurt individually is at least as cheap:
- $$$b \geq 2a \quad \Rightarrow \quad \text{min cost} = n \cdot a$$$
- If $$$b \lt 2a$$$, the promotion saves money for pairs, so we should use as many promotions as possible.
Case Analysis
Case A: Promotion is not better ($$$b \geq 2a$$$)
- Optimal strategy is to buy everything one by one:
- $$$\text{min cost} = n \cdot a$$$
Case B: Promotion is better ($$$b \lt 2a$$$)
- We should buy as many pairs as possible:
- If $$$n$$$ is even ($$$n = 2k$$$), buy $$$k$$$ pairs: -$$$\text{cost} = k \cdot b = \frac{n}{2} \cdot b$$$
- If $$$n$$$ is odd ($$$n = 2k+1$$$), buy $$$k$$$ pairs for $$$2k$$$ yogurts and one extra single:
- $$$\text{cost} = k \cdot b + a = \left\lfloor \frac{n}{2} \right\rfloor \cdot b + a$$$
- We can combine both cases into a single formula:
- $$$\text{Cost}_{\text{promo}} = \left\lfloor \frac{n}{2} \right\rfloor \cdot b + (n \bmod 2) \cdot a$$$
Final Formula
Now, the answer is the minimum between the baseline and the promotion strategy:
- $$$\text{min cost} = \min\Big( n \cdot a,\ \left\lfloor \frac{n}{2} \right\rfloor \cdot b + (n \bmod 2) \cdot a \Big)$$$
This handles both cases because:
If $$$b \geq 2a$$$, then $$$n \cdot a$$$ will be cheaper.
If $$$b \lt 2a$$$, then the promotion plan will be cheaper.
Example Walkthrough
Let $$$n=5,\ a=4,\ b=7$$$.
- All singles:
- Promotion strategy:
Minimum cost = 18
Complexity
- Each test case is solved in $$$O(1)$$$ time (just arithmetic).
- For $$$t$$$ test cases, total complexity is $$$O(t)$$$.
Key Takeaways
- Always consider the baseline: $$$n \cdot a$$$.
- Promotion is only useful if $$$b \lt 2a$$$.
Compact implementation:
- $$$ \text{pairs} = \left\lfloor \frac{n}{2} \right\rfloor,\quad \text{remainder} = n \bmod 2$$$
- $$$\text{Answer} = \min(n\cdot a,\ \text{pairs}\cdot b + \text{remainder}\cdot a) $$$
We need to determine whether Maxim can compose a letter text $$$s_2$$$ using letters from a newspaper heading $$$s_1$$$. Each letter in $$$s_1$$$ can be used at most once, and uppercase and lowercase letters are distinct. Spaces in the heading can be ignored.
Count Letter Occurrences
For each uppercase or lowercase letter, calculate how many times it appears in both $$$s_1$$$ and $$$s_2$$$.
- Let $$$count_1[c]$$$ be the number of times character $$$c$$$ appears in $$$s_1$$$ (ignoring spaces).
- Let $$$count_2[c]$$$ be the number of times character $$$c$$$ appears in $$$s_2$$$ (ignoring spaces).
Note: Spaces are ignored because Maxim does not cut them out from the heading; he just leaves them blank.
Compare Letter Counts
For every character $$$x$$$ in $$$s_2$$$, check:
- If this is true for all characters, Maxim can cut enough letters to form $$$s_2$$$.
- If this is false for any character, there are not enough letters in $$$s_1$$$ to make $$$s_2$$$, and the answer is
NO.
Determine the Result
- All letters satisfy the condition →
YES - Any letter violates the condition →
NO
Key Considerations
- Uppercase and lowercase letters are distinct.
Aandaare treated separately.
- Spaces are ignored in both strings.
- This method ensures accurate accounting of each letter’s availability.
Complexity Analysis
- Counting letters in $$$s_1$$$ → $$$O(|s_1|)$$$
- Counting letters in $$$s_2$$$ → $$$O(|s_2|)$$$
- Comparing counts → $$$O(1)$$$ per letter (since only 52 letters exist)
Overall complexity: $$$O(|s_1| + |s_2|)$$$, which is efficient for $$$|s_1|, |s_2| \leq 200$$$.



