#include <bits/stdc++.h>
typedef long long ll;
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 pow_to_use = std::ceil(std::log2(b - a + 1)) - 1;
return std::max(table_max[pow_to_use][a],
table_max[pow_to_use][b - (1 << pow_to_use) + 1]);
}
};
struct Line {
ll m, b;
Line() {}
Line(ll m, ll b) : m(m), b(b) {}
ll operator()(ll x) { return m * x + b; }
};
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), val(n);
std::vector<bool> active(n);
for (ll i = 0, mx = 0, prev = -1; i < n; ++i) {
std::cin >> a[i];
if (a[i] > mx) {
mx = a[i];
val[i] = prev;
active[i] = true;
prev = mx;
}
}
std::vector<ll> dp(n);
SparseTable table(a);
CHT convex_hull;
for (ll i = n - 1, j = -1, suff_max = 0; 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) {
convex_hull.insert({val[j], -dp[j] - val[j] * n});
}
j = i;
}
}
std::cout << dp[0] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 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 |
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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 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 |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 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 |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
152 ms |
196320 KB |
Output is correct |
17 |
Correct |
161 ms |
196180 KB |
Output is correct |
18 |
Correct |
161 ms |
196212 KB |
Output is correct |
19 |
Correct |
178 ms |
196180 KB |
Output is correct |
20 |
Correct |
151 ms |
196204 KB |
Output is correct |
21 |
Correct |
161 ms |
196212 KB |
Output is correct |
22 |
Correct |
161 ms |
196180 KB |
Output is correct |
23 |
Correct |
151 ms |
196176 KB |
Output is correct |
24 |
Correct |
161 ms |
196200 KB |
Output is correct |
25 |
Correct |
180 ms |
196220 KB |
Output is correct |
26 |
Correct |
163 ms |
196180 KB |
Output is correct |
27 |
Correct |
151 ms |
196192 KB |
Output is correct |
28 |
Correct |
152 ms |
196180 KB |
Output is correct |
29 |
Correct |
158 ms |
196200 KB |
Output is correct |
30 |
Correct |
160 ms |
196176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
143 ms |
196212 KB |
Output is correct |
2 |
Correct |
150 ms |
196212 KB |
Output is correct |
3 |
Correct |
133 ms |
196176 KB |
Output is correct |
4 |
Correct |
150 ms |
196192 KB |
Output is correct |
5 |
Correct |
152 ms |
196440 KB |
Output is correct |
6 |
Correct |
144 ms |
196180 KB |
Output is correct |
7 |
Correct |
194 ms |
196180 KB |
Output is correct |
8 |
Correct |
146 ms |
196200 KB |
Output is correct |
9 |
Correct |
138 ms |
196204 KB |
Output is correct |
10 |
Correct |
144 ms |
196180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 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 |
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 |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
0 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 |
604 KB |
Output is correct |
29 |
Correct |
0 ms |
604 KB |
Output is correct |
30 |
Correct |
0 ms |
604 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 |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
0 ms |
604 KB |
Output is correct |
37 |
Correct |
0 ms |
600 KB |
Output is correct |
38 |
Correct |
0 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
0 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
0 ms |
640 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
0 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 |
0 ms |
604 KB |
Output is correct |
49 |
Correct |
0 ms |
604 KB |
Output is correct |
50 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 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 |
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 |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
604 KB |
Output is correct |
23 |
Correct |
0 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
0 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 |
604 KB |
Output is correct |
29 |
Correct |
0 ms |
604 KB |
Output is correct |
30 |
Correct |
152 ms |
196320 KB |
Output is correct |
31 |
Correct |
161 ms |
196180 KB |
Output is correct |
32 |
Correct |
161 ms |
196212 KB |
Output is correct |
33 |
Correct |
178 ms |
196180 KB |
Output is correct |
34 |
Correct |
151 ms |
196204 KB |
Output is correct |
35 |
Correct |
161 ms |
196212 KB |
Output is correct |
36 |
Correct |
161 ms |
196180 KB |
Output is correct |
37 |
Correct |
151 ms |
196176 KB |
Output is correct |
38 |
Correct |
161 ms |
196200 KB |
Output is correct |
39 |
Correct |
180 ms |
196220 KB |
Output is correct |
40 |
Correct |
163 ms |
196180 KB |
Output is correct |
41 |
Correct |
151 ms |
196192 KB |
Output is correct |
42 |
Correct |
152 ms |
196180 KB |
Output is correct |
43 |
Correct |
158 ms |
196200 KB |
Output is correct |
44 |
Correct |
160 ms |
196176 KB |
Output is correct |
45 |
Correct |
143 ms |
196212 KB |
Output is correct |
46 |
Correct |
150 ms |
196212 KB |
Output is correct |
47 |
Correct |
133 ms |
196176 KB |
Output is correct |
48 |
Correct |
150 ms |
196192 KB |
Output is correct |
49 |
Correct |
152 ms |
196440 KB |
Output is correct |
50 |
Correct |
144 ms |
196180 KB |
Output is correct |
51 |
Correct |
194 ms |
196180 KB |
Output is correct |
52 |
Correct |
146 ms |
196200 KB |
Output is correct |
53 |
Correct |
138 ms |
196204 KB |
Output is correct |
54 |
Correct |
144 ms |
196180 KB |
Output is correct |
55 |
Correct |
0 ms |
604 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 |
604 KB |
Output is correct |
60 |
Correct |
1 ms |
604 KB |
Output is correct |
61 |
Correct |
0 ms |
604 KB |
Output is correct |
62 |
Correct |
0 ms |
600 KB |
Output is correct |
63 |
Correct |
0 ms |
604 KB |
Output is correct |
64 |
Correct |
1 ms |
604 KB |
Output is correct |
65 |
Correct |
0 ms |
604 KB |
Output is correct |
66 |
Correct |
1 ms |
604 KB |
Output is correct |
67 |
Correct |
0 ms |
640 KB |
Output is correct |
68 |
Correct |
1 ms |
604 KB |
Output is correct |
69 |
Correct |
1 ms |
604 KB |
Output is correct |
70 |
Correct |
0 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 |
0 ms |
604 KB |
Output is correct |
74 |
Correct |
0 ms |
604 KB |
Output is correct |
75 |
Correct |
0 ms |
604 KB |
Output is correct |
76 |
Correct |
149 ms |
196208 KB |
Output is correct |
77 |
Correct |
158 ms |
196436 KB |
Output is correct |
78 |
Correct |
148 ms |
196180 KB |
Output is correct |
79 |
Correct |
146 ms |
196204 KB |
Output is correct |
80 |
Correct |
161 ms |
196180 KB |
Output is correct |
81 |
Correct |
158 ms |
196176 KB |
Output is correct |
82 |
Correct |
177 ms |
196176 KB |
Output is correct |
83 |
Correct |
149 ms |
196176 KB |
Output is correct |
84 |
Correct |
159 ms |
196020 KB |
Output is correct |
85 |
Correct |
148 ms |
196212 KB |
Output is correct |
86 |
Correct |
151 ms |
196108 KB |
Output is correct |
87 |
Correct |
159 ms |
196180 KB |
Output is correct |
88 |
Correct |
140 ms |
196196 KB |
Output is correct |
89 |
Correct |
140 ms |
196180 KB |
Output is correct |
90 |
Correct |
136 ms |
196212 KB |
Output is correct |
91 |
Correct |
138 ms |
196176 KB |
Output is correct |
92 |
Correct |
164 ms |
196216 KB |
Output is correct |
93 |
Correct |
162 ms |
196180 KB |
Output is correct |
94 |
Correct |
147 ms |
196008 KB |
Output is correct |
95 |
Correct |
143 ms |
196180 KB |
Output is correct |
96 |
Correct |
137 ms |
196180 KB |
Output is correct |
97 |
Correct |
135 ms |
196180 KB |
Output is correct |
98 |
Correct |
134 ms |
196176 KB |
Output is correct |
99 |
Correct |
141 ms |
196032 KB |
Output is correct |
100 |
Correct |
137 ms |
196208 KB |
Output is correct |