#include "bits/stdc++.h"
#include <bit>
using ll = long long;
using namespace std;
const int N = 1'000'007;
const int LEVELS = 20;
int max_strips[LEVELS][N];
int getMaxStrip(int l,int r){
int level = bit_width((unsigned)(r-l+1))-1;
int size = 1<<level;
int k = r-size+1;
return max(max_strips[level][k],max_strips[level][l]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
ll total = 0;
vector<int> strip_sums;
for (int i = 0; i<n; i++){
cin >>a[i];
total+=a[i];
}
int k = n/2;
int curr = 0;
for (int i = 0; i < k; i++){
curr+=a[i];
}
int l = 0, r = k-1;
for (int i = 0; i < n; i++){
strip_sums.push_back(curr);
r = (r+1)%n;
curr+=a[r];
curr-=a[l];
l = (l+1)%n;
}
for (int i = 0; i < n; i++){
max_strips[0][i] = strip_sums[i];
}
for (int i = 1; i < LEVELS; i++){
for (int j = 0; j+(1<<i) <= n; j++){
max_strips[i][j] = max(max_strips[i-1][j], max_strips[i-1][j+(1<<(i-1))]);
}
}
int smallest = INT_MAX;
for (int i = 0; i < n; i++){
int l = (i+1)%n;
int r = (i+ceil((double)n/2));
r%=n;
if (l > r){
smallest = min(smallest,max(getMaxStrip(0,r),getMaxStrip(l, n-1)));
}
else{
smallest = min(smallest,getMaxStrip(l,r));
}
}
cout << total-smallest;
}
| # | 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... |