| Codeforces Round 1136 (Div. 1) |
|---|
| Finished |
There is a significant difference between the easy and hard versions of this problem. Please read the statement carefully.
Yukari Yakumo has placed $$$x$$$ boundary stones of power $$$1$$$ and $$$y$$$ boundary stones of power $$$2$$$ on the ground.
Yukari has a spell $$$s$$$ consisting of the characters + and - ($$$|s| = m$$$). Yukari chooses a prefix of $$$s$$$ (possibly empty) and then performs every character of this prefix in order.
When performing the $$$i$$$-th character of the prefix, Yukari chooses a stone that is currently on the ground. The chosen stone changes as follows:
If there is no stone on the ground, the remaining characters cannot be performed, and the number of stones on the ground at that moment is $$$0$$$.
Yukari chooses the prefix and the stone for every spell so that the number of stones on the ground is as large as possible. Find this maximum number of stones.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows.
Each test case is described as follows.
The first line contains three integers $$$x$$$, $$$y$$$, and $$$m$$$ ($$$0 \le x, y \le 50$$$, $$$1 \le x + y \le 50$$$, $$$1 \le m \le 50$$$) — the number of stones of power $$$1$$$, the number of stones of power $$$2$$$, and the length of the spell.
The second line contains a string $$$s$$$ of length $$$m$$$ consisting of the characters + and - — the spell.
For each test case, output a single integer — the maximum number of stones Yukari can leave on the ground.
41 2 5-+-+-1 1 5-+-+-0 1 5-----0 50 10++++++++++
53160
In the first test case, $$$x = 1$$$, $$$y = 2$$$, so the stones have powers $$$1$$$, $$$2$$$, $$$2$$$; $$$s = \mathtt{-+-+-}$$$. One optimal play is:
In the second test case, $$$x = 1$$$, $$$y = 1$$$, so the stones have powers $$$1$$$, $$$2$$$; $$$s = \mathtt{-+-+-}$$$. The best play removes the stone of power $$$1$$$ with the first -, copies the remaining stone of power $$$2$$$ ($$$2, 2$$$), lowers one of them back to $$$1$$$ and copies the other one again, so after four spells the stones are $$$1, 2, 2$$$ and the answer is $$$3$$$.
In the third test case, $$$x = 0$$$, $$$y = 1$$$, so the only stone has power $$$2$$$; every spell is -, so the number of stones never exceeds $$$1$$$ (take the empty prefix), and the answer is $$$1$$$.
In the fourth test case, $$$y = 50$$$, so all stones have power $$$2$$$; every + spell copies a stone, hence the answer is $$$50 + 10 = 60$$$.
| Name |
|---|


