This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
void solve(){
int n;
cin>>n;
vector<ll> v(n);
for(auto &i:v) cin>>i;
vector<ll> p(n);
p = v;
for(int i=1;i<n;i++) p[i] += p[i-1];
auto getSum = [&](int l,int r){
return p[r] - (l ? p[l-1] : 0);
};
ll ans = 0;
for(int i=0;i<n;i++){
int l = 0;
int r = n - 1;
ll ans1 = 0;
while(l <= r){
int md = l + r >> 1;
int x = i + md;
int y = i - md;
if(x >= n && y < 0){
r = md - 1;
continue;
}
if(x < n && y >= 0){
ans1 = getSum(i,x);
//cout<<"! "<<md<<endl;
l = md + 1;
continue;
}
x %= n;
y += n;
y %= n;
if(x < y){
//cout<<md<<endl;
if(x < i) ans1 = getSum(i,n-1) + getSum(0,x);
else ans1 = getSum(i,x);
l = md + 1;
}
else r = md - 1;
}
l = 0 , r = n - 1;
ll ans2 = 1e18;
while(l <= r){
int md = l + r >> 1;
int x = i + md;
int y = i - md;
if(x >= n && y < 0){
r = md - 1;
continue;
}
if(x < n && y >= 0){
//cout<<"! "<<md<<endl;
ans2 = getSum(y,i);
l = md + 1;
continue;
}
x %= n;
y += n;
y %= n;
if(x < y){
//cout<<md<<endl;
if(y > i) ans2 = getSum(0,i) + getSum(y,n-1);
else ans2 = getSum(y,i);
l = md + 1;
}
else r = md - 1;
}
ans = max(ans , min(ans1 , ans2));
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
return 0;
}
Compilation message (stderr)
hac.cpp: In function 'void solve()':
hac.cpp:22:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
22 | int md = l + r >> 1;
| ~~^~~
hac.cpp:49:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
49 | int md = l + r >> 1;
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |