#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 55;
int dp[MAXN], T[MAXN];
int f (array<int, 2> line, int x) {
return line[0] * x + line[1];
}
long double intersect (array<int, 2> line1, array<int, 2> line2) {
return (line2[1] - line1[1]) / (line1[0] - line2[0]);
}
struct Hull {
deque <array<int, 2>> dq;
int query (int 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<int, 2> line) {
while (dq.size() > 1 && intersect(line, dq.back()) <= intersect(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;
int mx = 1, ind = 1;
for (int i = 1;i <= N;i ++) {
if (T[i] >= T[mx]) {
mx = i;
}
while (ind <= mx){
hl.insert({-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 |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
68 ms |
13648 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |