This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
4
7 6 8 4
*/
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename K>
using ordered_map = tree<T, K, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
ll h[n];
for (int i = 0; i < n; i++)
cin >> h[i];
ll k = (n + 1) / 2;
int sz = n + k;
ll s[sz + 1];
s[0] = 0;
for (int i = 1; i <= sz; i++)
s[i] = s[i - 1] + h[i % n];
vector<ll>X;
for (int i = k; i <= sz; i++)
X.push_back(s[i] - s[i - k]);
deque<pair<ll, ll>>A;
for (int i = 0; i < k; i++)
{
while (!A.empty() && A.back().first >= X[i])
A.pop_back();
A.push_back({X[i], i});
}
ll ans = A[0].first;
for (int i = k; i < (int)X.size(); i++)
{
while (!A.empty() && A.back().first >= X[i])
A.pop_back();
A.push_back({X[i], i});
while (A[0].second <= i - k)
A.pop_front();
ans = max(ans, A[0].first);
}
cout << ans << "\n";
return 0;
}
# | 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... |