#P9517. Minimum Bottles to Pick Up
Minimum Bottles to Pick Up
Minimum Bottles to Pick Up
You are given n bottles, numbered from \(1\) to \(n\) from left to right. Each bottle may either be empty or contain water.
You are allowed to choose a pair \((l, r)\) (with \(l \le r\)) and drink all the water from the bottles numbered \(l, l+1, \dots, r\) in one go. Your goal is to drink all the water on the table at once. In order to achieve this, you might have to pick up some bottles even if they are empty along the way. Determine the minimum number of bottles you need to pick up such that your chosen contiguous segment covers all the bottles that contain water.
Note: It is possible that you do not need to pick up any bottle if there is no water.
Hint: Find the leftmost and rightmost bottle that contains water. The answer is \(\text{rightmost index} - \text{leftmost index} + 1\) (if there is any water).
inputFormat
The input consists of two lines:
- The first line contains an integer \(n\) representing the number of bottles.
- The second line contains a string of length \(n\) consisting of characters '0' and '1'. A character '1' indicates that the corresponding bottle contains water, while '0' indicates that it is empty.
outputFormat
Output a single integer which is the minimum number of bottles that need to be picked up in order to drink all of the water. If there is no water at all, output 0.
sample
5
00101
3
</p>