#include #include #include using namespace std; int min_operations(int n, const string& s) { /* For a good string, any substring must have count(0) <= count(1). We'll maintain a running balance (count(1) - count(0)) and ensure it never goes negative. If it does, we need to convert enough 0s to 1s. */ int balance = 0; // Current balance: count(1) - count(0) int operations = 0; // Total operations needed for (char c : s) { // Update balance if (c == '1') { balance++; } else { // c == '0' balance--; } // 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; } int main() { int n; string s; cin >> n; cin >> s; cout << min_operations(n, s) << endl; return 0; }