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;
typedef long long ll;
const int MAXN = 1e6 + 55;
ll dp[MAXN], T[MAXN];
ll f (array<ll, 2> line, ll x) {
return line[0] * x + line[1];
}
long double inters (array<ll, 2> line1, array<ll, 2> line2) {
return (line2[1] - line1[1]) / (line1[0] - line2[0]);
}
struct Hull {
deque<array<ll, 2>> dq;
ll query (ll x) {
while ((int)dq.size() > 1 && f(dq[0], x) > f(dq[1], x)) dq.pop_front();
return f(dq[0], x);
}
void insert (array<ll, 2> line) {
while (dq.size() > 1 && inters(line, dq.back()) <= inters(line, dq[dq.size() - 2])) dq.pop_back();
dq.push_back(line);
}
};
int32_t main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
for (int i = 1;i <= N;i ++) {
cin >> T[i];
}
Hull hl;
ll mx = 1, ind = 1;
for (int i = 1;i <= N;i ++) {
if (T[i] >= T[mx]) {
mx = i;
}
while (ind <= mx){
hl.insert({-1LL * ind, dp[ind - 1]});
ind ++;
}
if (i > 1) {
dp[i] = min(T[mx] * N, hl.query(T[mx]) + T[mx] * (N + 1));
} else {
dp[i] = T[1] * N;
}
}
cout << dp[N] << "\n";
}
# | 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... |