#GD0000205. Giải cứu thành phố (Saving the City)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Giải cứu thành phố (Saving the City)
Saving the City
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Bertown contains a straight row of buildings. A binary string describes mines: 1 means a mine is already present under that building and 0 means there is none.
A sapper may manually activate an existing mine for coins. The blast propagates to mines under adjacent buildings, so one activation removes a whole contiguous block of mines. Alternatively, the sapper may place a new mine at a 0 position for coins, possibly connecting two blocks of existing mines.
Operations may be performed in any order. Find the minimum total cost of detonating all original mines. If there are no mines initially, the cost is zero.
Input
- The first line contains two positive integers and : the manual activation and mine placement costs.
- The second line contains a binary string describing buildings from left to right. Let .
Output
Print one integer: the minimum number of coins needed to detonate every mine originally present.
Subtasks
- Subtask 1 (20 points): ; ; contains only
0and1. - Subtask 2 (30 points): ; ; contains only
0and1. - Subtask 3 (50 points): ; ; contains only
0and1.
Examples
Input
5 1
01101110
Output
6
Explanation
Here , , and the city is 01101110. Mines form two blocks at positions 2–3 and 5–7, separated by a single zero at position 4. Activating both blocks separately costs . Placing one mine at position 4 costs and connects them, so one activation for suffices. The minimum total is .