def min_operations(n, s): """ For a good string, any substring must have count(0) <= count(1). Key insight: To ensure all substrings have this property, we need to ensure that each position's running balance (count(1) - count(0)) never goes below the minimum balance seen so far minus 1. We use a greedy approach: When we encounter a position that violates this rule, we need to convert enough 0s to 1s to fix the balance. """ balance = 0 # Current balance: count(1) - count(0) operations = 0 # Total operations needed for char in s: # Update balance if char == '1': balance += 1 else: # char == '0' balance -= 1 # If balance becomes negative, we need to convert some 0s to 1s if balance < 0: operations += abs(balance) # Convert enough 0s to make balance 0 balance = 0 # After conversion, balance becomes 0 return operations def main(): n = int(input()) s = input().strip() print(min_operations(n, s)) if __name__ == "__main__": main()