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>
#define ll long long
#define pb push_back
#define task "tests"
#define pll pair<ll, ll>
#define pi pair<ll, pll>
#define fi first
#define se second
using namespace std;
const ll mod = 1e9+7;
const ll N = 5e5+5;
const int base = 313;
ll n, m, t, k, T, ans, q, tong, c[N], a[N], b[N], h[N], P[N][21];
vector<ll> adj[N], kq;
ll pw(ll k, ll n)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n & 1)total = total * k % mod;
k = k * k % mod;
}
return total;
}
ll sum(ll l, ll r)
{
if(l <= r)return b[r] - b[l-1];
return b[r] + b[n] - b[l-1];
}
ll mx(ll l, ll r)
{
ll log = log2(r-l+1);
return max(P[l][log], P[r-(1<<log)+1][log]);
}
ll get(ll l, ll r)
{
if(l <= r)return mx(l, r);
return max(mx(1, r), mx(l, n));
}
void sol()
{
cin >> n;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
b[i] = b[i-1] + a[i];
}
for(int i = 1; i <= n; i ++)
{
int j = i + n/2 - 1;
if(j > n)j -= n;
P[i][0] = sum(i, j);
}
for(int j = 1; j <= 20; j ++)
{
for(int i = 1; i+(1<<j)-1 <= n; i ++)P[i][j] = max(P[i][j-1], P[i+(1<<(j-1))][j-1]);
}
for(int i = 1; i <= n; i ++)
{
int l = i + 1, r = i - n / 2;
if(l > n)l -= n;
if(r <= 0)r += n;
ans = max(ans, b[n]- get(l, r));
}
cout << ans;
}
int main()
{
if(fopen(task".in", "r"))
{
freopen(task".in", "r", stdin);
freopen(task".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
Compilation message (stderr)
hac.cpp: In function 'int main()':
hac.cpp:72:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen(task".in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
hac.cpp:73:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |