from heapq import heappush, heappop def solve(N, S): # Convert string to array: '0' -> +1, '1' -> -1 A = [1 if c == '0' else -1 for c in S] # Priority queue to store indices of '0's (max heap using negative indices) pq = [] # Running prefix sum prefix_sum = 0 # List to store prefix sums up to current position prefix_sums = [0] # Initial prefix sum before first character # Count of operations operations = 0 # Process each character for i in range(N): # Update prefix sum with current character's value prefix_sum += A[i] # If current character is '0', add its index to priority queue if A[i] == 1: heappush(pq, -i) # Negative index for max heap behavior # Check if current prefix sum violates the "good" condition min_prev = min(prefix_sums) while prefix_sum > min_prev and pq: # Get the rightmost '0' index idx = -heappop(pq) # Flip '0' to '1': +1 becomes -1, reducing prefix sum by 2 A[idx] = -1 prefix_sum -= 2 operations += 1 # Store the current prefix sum prefix_sums.append(prefix_sum) # Optional: Keep prefix_sums manageable by removing oldest if unnecessary if len(prefix_sums) > i - 1: prefix_sums.pop(0) return operations # Input handling N = int(input()) S = input().strip() # Output the result print(solve(N, S))