#include <bits/stdc++.h>
typedef long long ll;
struct Line {
ll m, b;
Line() {}
Line(ll m, ll b) : m(m), b(b) {}
ll operator()(ll x) { return m * x + b; }
};
class SparseTable {
public:
ll maxPow;
std::vector<ll> orig;
std::vector<std::vector<ll>> table_max;
SparseTable(const std::vector<ll> &x) {
orig = x;
const ll n = x.size();
maxPow = std::floor(std::log2(n));
table_max = std::vector<std::vector<ll>>(maxPow + 1, std::vector<ll>(n));
for (ll i = 0; i < n; ++i) {
table_max[0][i] = x[i];
}
for (ll power = 1; power <= maxPow; ++power) {
for (ll i = 0; i < n - (1 << (power - 1)); ++i) {
table_max[power][i] =
std::max(table_max[power - 1][i],
table_max[power - 1][i + (1 << (power - 1))]);
}
}
}
ll query_max(ll a, ll b) {
if (a == b) {
return orig[a];
}
ll powToUse = std::ceil(std::log2(b - a + 1)) - 1;
return std::max(table_max[powToUse][a],
table_max[powToUse][b - (1 << powToUse) + 1]);
}
};
class CHT {
private:
long double x_intersect(const Line &l1, const Line &l2) {
return -(l1.b - l2.b) / ((long double)(l1.m - l2.m));
}
public:
std::deque<Line> dq;
void insert(Line l) {
while (dq.size() > 1 &&
x_intersect(dq[0], dq[1]) <= x_intersect(dq[0], l)) {
dq.pop_front();
}
dq.push_front(l);
}
ll query(ll x) {
while (dq.size() > 1 && dq[dq.size() - 1](x) <= dq[dq.size() - 2](x)) {
dq.pop_back();
}
return dq.back()(x);
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n;
std::cin >> n;
std::vector<ll> a(n);
for (auto &i : a) {
std::cin >> i;
}
std::vector<bool> active(n);
std::vector<ll> val(n);
for (ll i = 0, mx = 0; i < n; ++i) {
if (a[i] > mx) {
mx = a[i];
val[i] = mx;
active[i] = true;
}
}
for (ll i = 0, prev = -1; i < n; ++i) {
if (active[i]) {
std::swap(prev, val[i]);
}
}
std::vector<ll> dp(n);
ll suff_max = 0;
ll j = -1;
SparseTable table(a);
CHT convex_hull;
for (ll i = n - 1; i >= 0; --i) {
suff_max = std::max(suff_max, a[i]);
dp[i] = suff_max * (n - i);
if (!convex_hull.dq.empty()) {
dp[i] = std::min(dp[i], -convex_hull.query(i));
}
if (j != -1) {
dp[i] = std::min(dp[i], table.query_max(i, j - 1) * (n - i) + dp[j]);
}
if (active[i]) {
if (j != -1) {
// = (-val) * i + dp[j] + val * n
ll m = -val[j];
ll b = dp[j] + val[j] * n;
// insertion is now largest to smallest based on slope
convex_hull.insert({-m, -b});
}
j = i;
}
}
std::cout << dp[0] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
448 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
856 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
0 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
201 ms |
201148 KB |
Output is correct |
17 |
Correct |
167 ms |
202200 KB |
Output is correct |
18 |
Correct |
166 ms |
200524 KB |
Output is correct |
19 |
Correct |
166 ms |
202016 KB |
Output is correct |
20 |
Correct |
190 ms |
201804 KB |
Output is correct |
21 |
Correct |
169 ms |
202068 KB |
Output is correct |
22 |
Correct |
170 ms |
201548 KB |
Output is correct |
23 |
Correct |
168 ms |
202580 KB |
Output is correct |
24 |
Correct |
172 ms |
202196 KB |
Output is correct |
25 |
Correct |
167 ms |
202572 KB |
Output is correct |
26 |
Correct |
173 ms |
202832 KB |
Output is correct |
27 |
Correct |
160 ms |
201292 KB |
Output is correct |
28 |
Correct |
169 ms |
202068 KB |
Output is correct |
29 |
Correct |
160 ms |
202308 KB |
Output is correct |
30 |
Correct |
165 ms |
202580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
196188 KB |
Output is correct |
2 |
Correct |
147 ms |
196180 KB |
Output is correct |
3 |
Correct |
184 ms |
196180 KB |
Output is correct |
4 |
Correct |
149 ms |
196176 KB |
Output is correct |
5 |
Correct |
148 ms |
196184 KB |
Output is correct |
6 |
Correct |
144 ms |
196176 KB |
Output is correct |
7 |
Correct |
155 ms |
196436 KB |
Output is correct |
8 |
Correct |
143 ms |
196176 KB |
Output is correct |
9 |
Correct |
150 ms |
196176 KB |
Output is correct |
10 |
Correct |
149 ms |
196176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
448 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
856 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
0 ms |
604 KB |
Output is correct |
27 |
Correct |
0 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
0 ms |
604 KB |
Output is correct |
32 |
Correct |
0 ms |
604 KB |
Output is correct |
33 |
Correct |
0 ms |
604 KB |
Output is correct |
34 |
Correct |
0 ms |
460 KB |
Output is correct |
35 |
Correct |
0 ms |
604 KB |
Output is correct |
36 |
Correct |
0 ms |
604 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
0 ms |
600 KB |
Output is correct |
39 |
Correct |
0 ms |
604 KB |
Output is correct |
40 |
Correct |
0 ms |
604 KB |
Output is correct |
41 |
Correct |
0 ms |
604 KB |
Output is correct |
42 |
Correct |
0 ms |
604 KB |
Output is correct |
43 |
Correct |
0 ms |
604 KB |
Output is correct |
44 |
Correct |
0 ms |
460 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
0 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
1 ms |
604 KB |
Output is correct |
49 |
Correct |
0 ms |
612 KB |
Output is correct |
50 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
448 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
856 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
0 ms |
604 KB |
Output is correct |
27 |
Correct |
0 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
201 ms |
201148 KB |
Output is correct |
31 |
Correct |
167 ms |
202200 KB |
Output is correct |
32 |
Correct |
166 ms |
200524 KB |
Output is correct |
33 |
Correct |
166 ms |
202016 KB |
Output is correct |
34 |
Correct |
190 ms |
201804 KB |
Output is correct |
35 |
Correct |
169 ms |
202068 KB |
Output is correct |
36 |
Correct |
170 ms |
201548 KB |
Output is correct |
37 |
Correct |
168 ms |
202580 KB |
Output is correct |
38 |
Correct |
172 ms |
202196 KB |
Output is correct |
39 |
Correct |
167 ms |
202572 KB |
Output is correct |
40 |
Correct |
173 ms |
202832 KB |
Output is correct |
41 |
Correct |
160 ms |
201292 KB |
Output is correct |
42 |
Correct |
169 ms |
202068 KB |
Output is correct |
43 |
Correct |
160 ms |
202308 KB |
Output is correct |
44 |
Correct |
165 ms |
202580 KB |
Output is correct |
45 |
Correct |
146 ms |
196188 KB |
Output is correct |
46 |
Correct |
147 ms |
196180 KB |
Output is correct |
47 |
Correct |
184 ms |
196180 KB |
Output is correct |
48 |
Correct |
149 ms |
196176 KB |
Output is correct |
49 |
Correct |
148 ms |
196184 KB |
Output is correct |
50 |
Correct |
144 ms |
196176 KB |
Output is correct |
51 |
Correct |
155 ms |
196436 KB |
Output is correct |
52 |
Correct |
143 ms |
196176 KB |
Output is correct |
53 |
Correct |
150 ms |
196176 KB |
Output is correct |
54 |
Correct |
149 ms |
196176 KB |
Output is correct |
55 |
Correct |
1 ms |
600 KB |
Output is correct |
56 |
Correct |
0 ms |
604 KB |
Output is correct |
57 |
Correct |
0 ms |
604 KB |
Output is correct |
58 |
Correct |
0 ms |
604 KB |
Output is correct |
59 |
Correct |
0 ms |
460 KB |
Output is correct |
60 |
Correct |
0 ms |
604 KB |
Output is correct |
61 |
Correct |
0 ms |
604 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
0 ms |
600 KB |
Output is correct |
64 |
Correct |
0 ms |
604 KB |
Output is correct |
65 |
Correct |
0 ms |
604 KB |
Output is correct |
66 |
Correct |
0 ms |
604 KB |
Output is correct |
67 |
Correct |
0 ms |
604 KB |
Output is correct |
68 |
Correct |
0 ms |
604 KB |
Output is correct |
69 |
Correct |
0 ms |
460 KB |
Output is correct |
70 |
Correct |
1 ms |
604 KB |
Output is correct |
71 |
Correct |
0 ms |
604 KB |
Output is correct |
72 |
Correct |
1 ms |
604 KB |
Output is correct |
73 |
Correct |
1 ms |
604 KB |
Output is correct |
74 |
Correct |
0 ms |
612 KB |
Output is correct |
75 |
Correct |
0 ms |
604 KB |
Output is correct |
76 |
Correct |
177 ms |
202068 KB |
Output is correct |
77 |
Correct |
152 ms |
202236 KB |
Output is correct |
78 |
Correct |
149 ms |
201904 KB |
Output is correct |
79 |
Correct |
189 ms |
201940 KB |
Output is correct |
80 |
Correct |
156 ms |
202580 KB |
Output is correct |
81 |
Correct |
148 ms |
201812 KB |
Output is correct |
82 |
Correct |
165 ms |
201684 KB |
Output is correct |
83 |
Correct |
174 ms |
202200 KB |
Output is correct |
84 |
Correct |
156 ms |
202060 KB |
Output is correct |
85 |
Correct |
164 ms |
202312 KB |
Output is correct |
86 |
Correct |
160 ms |
201660 KB |
Output is correct |
87 |
Correct |
176 ms |
201540 KB |
Output is correct |
88 |
Correct |
179 ms |
202064 KB |
Output is correct |
89 |
Correct |
153 ms |
201676 KB |
Output is correct |
90 |
Correct |
155 ms |
201808 KB |
Output is correct |
91 |
Correct |
190 ms |
201556 KB |
Output is correct |
92 |
Correct |
171 ms |
202776 KB |
Output is correct |
93 |
Correct |
160 ms |
201940 KB |
Output is correct |
94 |
Correct |
151 ms |
201044 KB |
Output is correct |
95 |
Correct |
161 ms |
202700 KB |
Output is correct |
96 |
Correct |
185 ms |
201424 KB |
Output is correct |
97 |
Correct |
194 ms |
201796 KB |
Output is correct |
98 |
Correct |
151 ms |
202068 KB |
Output is correct |
99 |
Correct |
158 ms |
201556 KB |
Output is correct |
100 |
Correct |
155 ms |
201812 KB |
Output is correct |