Welcome to the Editorial of IPC-2.
A. Binary Cleanup
Author:justplaygame
greedy
Each operation removes one '1' and one '0' where the '1' is to the left of the '0'. So, every '1' needs a '0' somewhere to its right to be removed.
Think from the end of the string — the rightmost '1' can only be removed if there is at least one '0' after it.
Try scanning from the end while counting '0's. If at any moment the number of '1's exceeds the number of '0's to their right, it’s impossible to remove all '1's.
We iterate from the end of the string toward the beginning. Let’s maintain how many '0's are available to remove '1's. When we see '0', it becomes available for pairing — we can think of it as “adding one removable slot”.
When we see '1', it must pair with a '0' to its right. If a '0' is available, we consume one of them. If not, this '1' can never be removed — so the answer is NO.
If we finish scanning the string without violating this condition, then every '1' can be paired with some '0' to its right, and we can remove all '1's → YES.
Essentially, the check is: While going from right to left, the number of '1's should never exceed the number of '0's seen so far. If it ever does, removal becomes impossible.
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tt = 1; cin >> tt;
while( tt-- ){
int n , c = 0; cin >> n;
bool ans = 1;
string s; cin >> s;
for( int i = n-1 ; i >= 0 ; i-- ){
c += ( s[i] == '1' );
ans &= ( ( n - i - c ) >= c );
}
cout << ( ( ans )? "YES" : "NO" ) << '\n';
}
}




