codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1443B.md (2347B)


      1 Source: https://codeforces.com/problemset/problem/1443/B
      2 
      3 B. Saving the City
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 Bertown is a city with $n$ buildings in a straight line.
     22 
     23 The city's security service discovered that some buildings were mined. A map was compiled, which is a string of length $n$, where the $i$-th character is "1" if there is a mine under the building number $i$ and "0" otherwise.
     24 
     25 Bertown's best sapper knows how to activate mines so that the buildings above them are not damaged. When a mine under the building numbered $x$ is activated, it explodes and activates two adjacent mines under the buildings numbered $x-1$ and $x+1$ (if there were no mines under the building, then nothing happens). Thus, it is enough to activate any one mine on a continuous segment of mines to activate all the mines of this segment. For manual activation of one mine, the sapper takes $a$ coins. He can repeat this operation as many times as you want.
     26 
     27 Also, a sapper can place a mine under a building if it wasn't there. For such an operation, he takes $b$ coins. He can also repeat this operation as many times as you want.
     28 
     29 The sapper can carry out operations in any order.
     30 
     31 You want to blow up all the mines in the city to make it safe. Find the minimum number of coins that the sapper will have to pay so that after his actions there are no mines left in the city.
     32 
     33 Input
     34 
     35 The first line contains one positive integer $t$ ($1 \le t \le 10^5$) — the number of test cases. Then $t$ test cases follow.
     36 
     37 Each test case begins with a line containing two integers $a$ and $b$ ($1 \le a, b \le 1000$) — the cost of activating and placing one mine, respectively.
     38 
     39 The next line contains a map of mines in the city — a string consisting of zeros and ones.
     40 
     41 The sum of the string lengths for all test cases does not exceed $10^5$.
     42 
     43 Output
     44 
     45 For each test case, output one integer — the minimum number of coins that the sapper will have to pay.
     46 
     47 Example
     48 
     49 Input
     50 
     51 ```
     52 2
     53 1 1
     54 01000010
     55 5 1
     56 01101110
     57 ```
     58 
     59 Output
     60 
     61 ```
     62 2
     63 6
     64 ```
     65 
     66 Note
     67 
     68 In the second test case, if we place a mine under the fourth building and then activate it, then all mines on the field are activated. The cost of such operations is six, $b=1$ coin for placing a mine and $a=5$ coins for activating.