-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path327A.cpp
91 lines (83 loc) · 2.1 KB
/
327A.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<bits/stdc++.h>
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define fi(n) for(ll i=0; i<n; i++)
#define fj(n) for(ll j=0; j<n; j++)
#define fa(n) for(auto i:n)
#define fa2(n) for(auto i:n) cout<<i<<' ';
#define llt ll t; cin>>t; while(t--)
#define lln ll n; cin>>n;
#define llx ll x; cin>>x;
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define pb push_back
#define pbx ll xx; cin>> xx; v.pb(xx);
#define pbs string xx; cin>> xx; v.pb(xx);
#define all(x) x.begin(), x.end()
#define srt(x) sort(all(x))
#define rsrt(x) sort(all(x),greater<ll>())
#define veccin(n) vll v(n); fi(n) cin>>v[i];
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a)*((b)/__gcd(a,b)))
#define endl "\n"
#define nl <<endl
#define sp <<' '
#define sps <<' '<<
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define C(x) cout<< x <<endl;
#define F i.first
#define S i.second
using namespace std;
int main(){
lln;
vll v(n),v0(n,0),v1(n,0);
fi(n){
cin>>v[i];
if(i==0){
if(v[i]==1) v1[0]=1;
else v0[0]=1;
}
else{
if(v[i]==1) v1[i]++;
else v0[i]++;
v1[i]+=v1[i-1];
v0[i]+=v0[i-1];
}
}
//fa2(v1);
ll mx=0,ix=0,jx=0;
// if(n==1){
// if(v[0]==1) cout<<0;
// else cout<<1;
// return 0;
// }
fi(n){
for (int j = i; j < n; ++j)
{
ll temp0,temp1;
if(i==0){
temp0 = v0[j] ;
temp1 = v1[j] ;
}else{
temp0 = v0[j] - v0[i-1];
temp1 = v1[j] - v1[i-1];
}
// cout<<i sps j sps temp0 sps temp1 nl;
if(temp0>temp1){
ll dif=temp0-temp1;
//cout<<i sps j sps dif nl;
if(dif>mx) {
mx=dif;
ix=temp0;
jx=temp1;
// cout<<i sps j sps dif nl;
}
}
}
}
if(v1[n-1]==n) cout<<n-1;
else
cout<< v1[n-1]-jx+ix nl;
}