File size: 1,258 Bytes
3cee1f8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | # p02652 AtCoder Grand Contest 045 - 01 Unbalanced
## Problem Description
Given is a string S, where each character is `0`, `1`, or `?`.
Consider making a string S' by replacing each occurrence of `?` with `0` or `1` (we can choose the character for each `?` independently). Let us define the unbalancedness of S' as follows:
* (The unbalancedness of S') = \max \\{ The absolute difference between the number of occurrences of `0` and `1` between the l-th and r-th character of S (inclusive) :\ 1 \leq l \leq r \leq |S|\\}
Find the minimum possible unbalancedness of S'.
Constraints
* 1 \leq |S| \leq 10^6
* Each character of S is `0`, `1`, or `?`.
Input
Input is given from Standard Input in the following format:
S
Output
Print the minimum possible unbalancedness of S'.
Examples
Input
0??
Output
1
Input
0??0
Output
2
Input
??00????0??0????0?0??00??1???11?1?1???1?11?111???1
Output
4
## Contest Information
- **Contest ID**: 0
- **Problem Index**:
- **Points**: 0.0
- **Rating**: 0
- **Tags**:
- **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
- **Memory Limit**: 1073741824 bytes
## Task
Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases. |