#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e15;
const int N = 1502;
template<typename T>
struct li_chao_min {
static const T identity = numeric_limits<T>::max();
struct Line {
T m, c;
Line() {
m = 0;
c = identity;
}
Line(T m, T c) : m(m), c(c) {}
T val(T x) { return m * x + c; }
};
struct Node {
Line line;
Node *lc, *rc;
Node() : lc(0), rc(0) {}
};
T L, R, eps;
deque<Node> buffer;
Node* root;
Node* new_node() {
buffer.emplace_back();
return &buffer.back();
}
li_chao_min() {}
li_chao_min(T _L, T _R, T _eps) {
init(_L, _R, _eps);
}
void init(T _L, T _R, T _eps) {
L = _L;
R = _R;
eps = _eps;
root = new_node();
}
void insert(Node* &cur, T l, T r, Line line) {
if (!cur) {
cur = new_node();
cur->line = line;
return;
}
T mid = l + (r - l) / 2;
if (r - l <= eps) return;
if (line.val(mid) < cur->line.val(mid))
swap(line, cur->line);
if (line.val(l) < cur->line.val(l)) insert(cur->lc, l, mid, line);
else insert(cur->rc, mid + 1, r, line);
}
T query(Node* &cur, T l, T r, T x) {
if (!cur) return identity;
T mid = l + (r - l) / 2;
T res = cur->line.val(x);
if (r - l <= eps) return res;
if (x <= mid) return min(res, query(cur->lc, l, mid, x));
else return min(res, query(cur->rc, mid + 1, r, x));
}
void insert(T m, T c) { insert(root, L, R, Line(m, c)); }
T query(T x) { return query(root, L, R, x); }
};
signed main() {
std::ios::sync_with_stdio(0);
std::cout.tie(0);
std::cin.tie(0);
int n; cin >> n;
vector<int> a(n + 1), dp(n + 1);
for(int i = 1; i <= n; i++) cin >> a[i];
li_chao_min<int> lct(1, 1000000000, 0);
dp[1] = a[1] * n;
lct.insert(n, 0); //querying all n at one time
int mx = a[1];
for(int i = 2; i <= n; i++) {
mx = max(mx, a[i]);
lct.insert(n - i + 1, dp[i - 1]);
dp[i] = lct.query(mx);
//we can qry for mx cuz the x coordinate has to be atleast mx (all under same time mx)
}
cout << dp[n] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
284 ms |
16168 KB |
Output is correct |
17 |
Correct |
295 ms |
16348 KB |
Output is correct |
18 |
Correct |
266 ms |
16252 KB |
Output is correct |
19 |
Correct |
283 ms |
16232 KB |
Output is correct |
20 |
Correct |
280 ms |
16224 KB |
Output is correct |
21 |
Correct |
304 ms |
16224 KB |
Output is correct |
22 |
Correct |
294 ms |
16240 KB |
Output is correct |
23 |
Correct |
285 ms |
16304 KB |
Output is correct |
24 |
Correct |
279 ms |
16252 KB |
Output is correct |
25 |
Correct |
286 ms |
16564 KB |
Output is correct |
26 |
Correct |
285 ms |
16224 KB |
Output is correct |
27 |
Correct |
284 ms |
16228 KB |
Output is correct |
28 |
Correct |
312 ms |
16328 KB |
Output is correct |
29 |
Correct |
294 ms |
16316 KB |
Output is correct |
30 |
Correct |
283 ms |
16272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
324 ms |
15964 KB |
Output is correct |
2 |
Correct |
341 ms |
15980 KB |
Output is correct |
3 |
Correct |
316 ms |
16072 KB |
Output is correct |
4 |
Correct |
317 ms |
15956 KB |
Output is correct |
5 |
Correct |
308 ms |
16068 KB |
Output is correct |
6 |
Correct |
316 ms |
15880 KB |
Output is correct |
7 |
Correct |
314 ms |
15968 KB |
Output is correct |
8 |
Correct |
303 ms |
15948 KB |
Output is correct |
9 |
Correct |
319 ms |
15956 KB |
Output is correct |
10 |
Correct |
320 ms |
15968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
2 ms |
332 KB |
Output is correct |
42 |
Correct |
2 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
284 ms |
16168 KB |
Output is correct |
31 |
Correct |
295 ms |
16348 KB |
Output is correct |
32 |
Correct |
266 ms |
16252 KB |
Output is correct |
33 |
Correct |
283 ms |
16232 KB |
Output is correct |
34 |
Correct |
280 ms |
16224 KB |
Output is correct |
35 |
Correct |
304 ms |
16224 KB |
Output is correct |
36 |
Correct |
294 ms |
16240 KB |
Output is correct |
37 |
Correct |
285 ms |
16304 KB |
Output is correct |
38 |
Correct |
279 ms |
16252 KB |
Output is correct |
39 |
Correct |
286 ms |
16564 KB |
Output is correct |
40 |
Correct |
285 ms |
16224 KB |
Output is correct |
41 |
Correct |
284 ms |
16228 KB |
Output is correct |
42 |
Correct |
312 ms |
16328 KB |
Output is correct |
43 |
Correct |
294 ms |
16316 KB |
Output is correct |
44 |
Correct |
283 ms |
16272 KB |
Output is correct |
45 |
Correct |
324 ms |
15964 KB |
Output is correct |
46 |
Correct |
341 ms |
15980 KB |
Output is correct |
47 |
Correct |
316 ms |
16072 KB |
Output is correct |
48 |
Correct |
317 ms |
15956 KB |
Output is correct |
49 |
Correct |
308 ms |
16068 KB |
Output is correct |
50 |
Correct |
316 ms |
15880 KB |
Output is correct |
51 |
Correct |
314 ms |
15968 KB |
Output is correct |
52 |
Correct |
303 ms |
15948 KB |
Output is correct |
53 |
Correct |
319 ms |
15956 KB |
Output is correct |
54 |
Correct |
320 ms |
15968 KB |
Output is correct |
55 |
Correct |
1 ms |
332 KB |
Output is correct |
56 |
Correct |
1 ms |
332 KB |
Output is correct |
57 |
Correct |
1 ms |
332 KB |
Output is correct |
58 |
Correct |
1 ms |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
332 KB |
Output is correct |
61 |
Correct |
1 ms |
332 KB |
Output is correct |
62 |
Correct |
1 ms |
332 KB |
Output is correct |
63 |
Correct |
1 ms |
332 KB |
Output is correct |
64 |
Correct |
1 ms |
332 KB |
Output is correct |
65 |
Correct |
1 ms |
332 KB |
Output is correct |
66 |
Correct |
2 ms |
332 KB |
Output is correct |
67 |
Correct |
2 ms |
332 KB |
Output is correct |
68 |
Correct |
1 ms |
332 KB |
Output is correct |
69 |
Correct |
1 ms |
332 KB |
Output is correct |
70 |
Correct |
1 ms |
332 KB |
Output is correct |
71 |
Correct |
1 ms |
332 KB |
Output is correct |
72 |
Correct |
1 ms |
332 KB |
Output is correct |
73 |
Correct |
1 ms |
332 KB |
Output is correct |
74 |
Correct |
1 ms |
332 KB |
Output is correct |
75 |
Correct |
1 ms |
332 KB |
Output is correct |
76 |
Correct |
296 ms |
15936 KB |
Output is correct |
77 |
Correct |
251 ms |
15968 KB |
Output is correct |
78 |
Correct |
259 ms |
15972 KB |
Output is correct |
79 |
Correct |
247 ms |
16068 KB |
Output is correct |
80 |
Correct |
284 ms |
15976 KB |
Output is correct |
81 |
Correct |
249 ms |
15968 KB |
Output is correct |
82 |
Correct |
243 ms |
15964 KB |
Output is correct |
83 |
Correct |
284 ms |
16096 KB |
Output is correct |
84 |
Correct |
242 ms |
15964 KB |
Output is correct |
85 |
Correct |
260 ms |
16036 KB |
Output is correct |
86 |
Correct |
248 ms |
15976 KB |
Output is correct |
87 |
Correct |
244 ms |
15964 KB |
Output is correct |
88 |
Correct |
252 ms |
15976 KB |
Output is correct |
89 |
Correct |
244 ms |
16072 KB |
Output is correct |
90 |
Correct |
258 ms |
15968 KB |
Output is correct |
91 |
Correct |
246 ms |
15948 KB |
Output is correct |
92 |
Correct |
287 ms |
16228 KB |
Output is correct |
93 |
Correct |
285 ms |
16236 KB |
Output is correct |
94 |
Correct |
242 ms |
15952 KB |
Output is correct |
95 |
Correct |
258 ms |
16072 KB |
Output is correct |
96 |
Correct |
266 ms |
16088 KB |
Output is correct |
97 |
Correct |
248 ms |
15948 KB |
Output is correct |
98 |
Correct |
249 ms |
16068 KB |
Output is correct |
99 |
Correct |
250 ms |
15948 KB |
Output is correct |
100 |
Correct |
252 ms |
15972 KB |
Output is correct |