#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;
int 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;
int 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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
15988 KB |
Output is correct |
2 |
Correct |
72 ms |
15992 KB |
Output is correct |
3 |
Correct |
67 ms |
15952 KB |
Output is correct |
4 |
Correct |
75 ms |
15952 KB |
Output is correct |
5 |
Correct |
76 ms |
15988 KB |
Output is correct |
6 |
Correct |
72 ms |
15952 KB |
Output is correct |
7 |
Correct |
70 ms |
15952 KB |
Output is correct |
8 |
Correct |
74 ms |
15952 KB |
Output is correct |
9 |
Correct |
73 ms |
16004 KB |
Output is correct |
10 |
Correct |
75 ms |
15996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |