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;
#define endl '\n'
#define debug(x) cerr << #x << " == " << (x) << '\n';
#define all(x) begin(x), end(x)
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
template<typename T>
struct M1 {
using Type = T;
inline const static T Id = numeric_limits<T>::min();
static T op(const T& x, const T& y) { return max(x, y); }
};
template<typename Monoid, bool top_down>
struct MinimumStack {
using M = Monoid;
using T = typename M::Type;
stack<pair<T, T>> st;
T top() const { return st.top().first; }
T minimum() const { return st.empty() ? M::Id : st.top().second; }
void push(T value) {
st.emplace(value, top_down ? M::op(value, minimum()) : M::op(minimum(), value));
}
void pop() { st.pop(); }
bool empty() const { return st.empty(); }
size_t size() const { return size(st); }
};
template<typename Monoid>
struct MinimumQueue {
using M = Monoid;
using T = typename Monoid::Type;
MinimumStack<Monoid, false> in;
MinimumStack<Monoid, true> out;
void move() {
if (out.empty()) while (not in.empty()) {
out.push(in.top());
in.pop();
}
}
T front() { move(); return out.top(); }
T minimum() const { return M::op(out.minimum(), in.minimum()); }
void push(T value) { in.push(value); }
void pop() { move(); out.pop(); }
bool empty() const { return in.empty() && out.empty(); }
size_t size() const { return size(in) + size(out); }
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> v(n);
for (auto& x : v) cin >> x;
MinimumQueue<M1<int>> mq;
int sum = 0, cnt = n / 2;
for (int i = 1; i < 1 + cnt; ++i) {
sum += v[i];
}
mq.push(sum);
for (int i = 1 + cnt; i < n; ++i) {
sum += v[i] - v[i - cnt];
mq.push(sum);
}
int opt = INF;
for (int i = 0; i < n; ++i) {
opt = min(opt, mq.minimum());
mq.pop();
sum += v[i] - v[(i + n - cnt) % n];
mq.push(sum);
}
int ans = accumulate(all(v), 0) - opt;
cout << ans << endl;
exit(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... |