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;
struct Line {
mutable long long int k, m, p;
bool operator<(const Line& o) const { return k > o.k; }
bool operator<(long long int x) const { return p < x; }
};
struct LineContainer : multiset<Line, less< > > {
static const long long int inf = LLONG_MAX;
long long int div(long long int a, long long int b) {
return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = inf, 0;
if (x->k == y->k)
{
x->p = x->m < y->m ? inf : -inf;
}
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(long long int k, long long int m) {
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
long long int get(long long int x) {
assert(!empty());
auto l = *lower_bound(x);
return l.k * x + l.m;
}
} st;
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
long long int n;
cin >> n;
vector<pair<long long int, int>> v;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (v.empty()) {
v.push_back({x, 1});
}
else {
if (v.back().first < x) v.push_back({x, 1});
else {
v[v.size() - 1].second++;
}
}
}
long long int c = 0, d = 0;
st.add(0, 0);
for (auto w : v) {
c += w.second;
d = 1ll * w.first * n + st.get(w.first);
st.add(-c, d);
}
cout << d;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |