fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5.  
  6.  
  7. const int M = 1000000007;
  8. const int N = 3e5+9;
  9. const int INF = 2e9+1;
  10. const int MAXN = 100000;
  11. const int LINF = 2000000000000000001;
  12.  
  13. //_ ***************************** START Below *******************************
  14.  
  15.  
  16.  
  17. vector<int> a;
  18.  
  19. int consistency(int n){
  20.  
  21. int ones = 0;
  22. for(int i=0; i<n; i++) if(a[i] == 1) ones++;
  23. if(ones == 0) return -1;
  24.  
  25. int s = 0, e = 0;
  26. int zeroes = 0;
  27. int ans = INF;
  28.  
  29. while(e<n){
  30. if(a[e] == 0) zeroes++;
  31.  
  32. if(e-s+1 < ones){
  33. e++;
  34. }
  35. else{
  36. ans = min(ans, zeroes);
  37. if(a[s] == 0) zeroes--;
  38. s++;
  39. e++;
  40. }
  41. }
  42.  
  43. return ans;
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. int practice(int n){
  55.  
  56. return 0;
  57. }
  58.  
  59.  
  60.  
  61.  
  62. void solve() {
  63.  
  64. int n;
  65. cin >> n;
  66. a.resize(n);
  67. for(int i=0; i<n; i++) cin >> a[i];
  68.  
  69. cout << consistency(n) << endl;
  70.  
  71. // cout << consistency(n) << " " << practice(n) << endl;
  72.  
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  
  79.  
  80. int32_t main() {
  81. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  82.  
  83. int t = 1;
  84. while (t--) {
  85. solve();
  86. }
  87.  
  88. return 0;
  89. }
Success #stdin #stdout 0.01s 5320KB
stdin
6
1 0 1 0 1 1
stdout
1