fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. const int MOD = 1e9 + 7;
  7.  
  8. void solve(){
  9. int n;
  10. cin >> n;
  11. string s;
  12. cin >> s;
  13. int ans= 0;
  14. int top = 0, bottom = 0;
  15. for(int i = 0; i < n; i++){
  16. if(s[i] == '1'){
  17. bottom++;
  18. ans = max(ans, bottom);
  19. top = bottom;
  20. bottom = 0;
  21. }else{
  22. top += i;
  23. bottom += n - i - 1;
  24. ans = max({ans, top, bottom});
  25. }
  26. }
  27. cout << ans << "\n";
  28.  
  29. }
  30.  
  31. int main(){
  32. ios_base::sync_with_stdio(false);
  33. cin.tie(nullptr);
  34.  
  35. int t = 1;
  36. cin >> t;
  37.  
  38. for(int i = 1; i <= t; i++){
  39. solve();
  40. }
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5300KB
stdin
6
3
000
4
0010
7
1011001
4
0001
2
11
1
0
stdout
3
9
10
7
1
0