Untitled

mail@pastecode.io avatar
unknown
plain_text
24 days ago
785 B
10
Indexable
Never
#include <bits/stdc++.h>
using namespace std;

int n; int a[1000000];

int main() {
    cin >> n; for(int i = 0; i < n; i++) cin >> a[i];
    int ma = 0, cnt = 0, l = 0, r = 0, le = 0, ri = 0;
    for(int i = 0; i < n; i++){
        if(a[i] == 0){
            ++cnt;
            if(cnt == 1) l = i;
            r = i;
        }
        else{
            if(cnt>0) --cnt;
        }
        if(ma < cnt){
            le = l;
            ri = r;
            ma = cnt;
        }
    }
    
    int ans = 0;
    for(int i = 0; i < le; i++){
        if(a[i]) ++ans;
    }
    for(int i = le; i <= ri; i++){
        if(!a[i]) ++ans;
    }
    for(int i = ri+1; i < n; i++){
        if(a[i]) ++ans;
    }
    
    //cout << le << " "<< ri << endl;
    
    cout << ans; 
      
    return 0;
}
Leave a Comment