#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e15;
const int N = 1502;
using ll = long long;
ll eval(array<ll, 2> a, ll x) {
return a[0]*x + a[1];
}
struct LiChao {
struct Node {
Node *l, *r;
array<ll, 2> f;
Node() : l(0), r(0), f{0, -(1ll<<62)} {}
};
deque<Node> buf;
template<class... Args>
Node *newnode(Args... args) {
return &buf.emplace_back(args...);
}
void update(Node *&v, ll l, ll r, array<ll, 2> f) {
if(!v) {
v = newnode(), v->f = f;
return;
}
ll mid = l + (r-l)/2;
if(eval(f, mid) < eval(v->f, mid)) swap(f, v->f);
if(l == r) return;
if(eval(f, l) < eval(v->f, l))
update(v->l, l, mid, f);
else
update(v->r, mid+1, r, f);
}
ll query(Node *v, ll l, ll r, ll x) {
if(!v) return (1ll<<62);
ll mid = l + (r-l)/2;
return min(eval(v->f, x),
x <= mid ? query(v->l, l, mid, x) : query(v->r, mid+1, r, x));
}
void update(array<ll, 2> f) {
update(root, -(n-1), n-1, f);
}
ll query(ll x) {
return query(root, -(n-1), n-1, x);
}
ll n;
Node *root;
LiChao(ll _n) : n(_n), root(0) {}
};
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];
LiChao cht(1e9 + 3);
dp[1] = a[1] * n;
cht.update({n, 0}); //querying all n at one time
int mx = a[1];
for(int i = 2; i <= n; i++) {
mx = max(mx, a[i]);
cht.update({n - i + 1, dp[i - 1]});
dp[i] = cht.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 |
192 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 |
2 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 |
2 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 |
2 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 |
2 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 |
2 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 |
2 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 |
278 ms |
16360 KB |
Output is correct |
17 |
Correct |
310 ms |
16232 KB |
Output is correct |
18 |
Correct |
258 ms |
16232 KB |
Output is correct |
19 |
Correct |
282 ms |
16224 KB |
Output is correct |
20 |
Correct |
318 ms |
16196 KB |
Output is correct |
21 |
Correct |
288 ms |
16236 KB |
Output is correct |
22 |
Correct |
283 ms |
16264 KB |
Output is correct |
23 |
Correct |
289 ms |
16472 KB |
Output is correct |
24 |
Correct |
294 ms |
16324 KB |
Output is correct |
25 |
Correct |
318 ms |
16408 KB |
Output is correct |
26 |
Correct |
294 ms |
16360 KB |
Output is correct |
27 |
Correct |
280 ms |
16216 KB |
Output is correct |
28 |
Correct |
285 ms |
16232 KB |
Output is correct |
29 |
Correct |
289 ms |
16324 KB |
Output is correct |
30 |
Correct |
294 ms |
16356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
345 ms |
15964 KB |
Output is correct |
2 |
Correct |
332 ms |
16068 KB |
Output is correct |
3 |
Correct |
322 ms |
15948 KB |
Output is correct |
4 |
Correct |
333 ms |
15948 KB |
Output is correct |
5 |
Correct |
332 ms |
15972 KB |
Output is correct |
6 |
Correct |
334 ms |
15984 KB |
Output is correct |
7 |
Correct |
343 ms |
15964 KB |
Output is correct |
8 |
Correct |
325 ms |
15948 KB |
Output is correct |
9 |
Correct |
385 ms |
15956 KB |
Output is correct |
10 |
Correct |
339 ms |
16004 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 |
192 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
2 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 |
2 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 |
2 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 |
340 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 |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 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 |
192 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
2 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 |
2 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 |
2 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 |
278 ms |
16360 KB |
Output is correct |
31 |
Correct |
310 ms |
16232 KB |
Output is correct |
32 |
Correct |
258 ms |
16232 KB |
Output is correct |
33 |
Correct |
282 ms |
16224 KB |
Output is correct |
34 |
Correct |
318 ms |
16196 KB |
Output is correct |
35 |
Correct |
288 ms |
16236 KB |
Output is correct |
36 |
Correct |
283 ms |
16264 KB |
Output is correct |
37 |
Correct |
289 ms |
16472 KB |
Output is correct |
38 |
Correct |
294 ms |
16324 KB |
Output is correct |
39 |
Correct |
318 ms |
16408 KB |
Output is correct |
40 |
Correct |
294 ms |
16360 KB |
Output is correct |
41 |
Correct |
280 ms |
16216 KB |
Output is correct |
42 |
Correct |
285 ms |
16232 KB |
Output is correct |
43 |
Correct |
289 ms |
16324 KB |
Output is correct |
44 |
Correct |
294 ms |
16356 KB |
Output is correct |
45 |
Correct |
345 ms |
15964 KB |
Output is correct |
46 |
Correct |
332 ms |
16068 KB |
Output is correct |
47 |
Correct |
322 ms |
15948 KB |
Output is correct |
48 |
Correct |
333 ms |
15948 KB |
Output is correct |
49 |
Correct |
332 ms |
15972 KB |
Output is correct |
50 |
Correct |
334 ms |
15984 KB |
Output is correct |
51 |
Correct |
343 ms |
15964 KB |
Output is correct |
52 |
Correct |
325 ms |
15948 KB |
Output is correct |
53 |
Correct |
385 ms |
15956 KB |
Output is correct |
54 |
Correct |
339 ms |
16004 KB |
Output is correct |
55 |
Correct |
1 ms |
340 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 |
1 ms |
332 KB |
Output is correct |
67 |
Correct |
1 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 |
297 ms |
15964 KB |
Output is correct |
77 |
Correct |
271 ms |
15960 KB |
Output is correct |
78 |
Correct |
255 ms |
15872 KB |
Output is correct |
79 |
Correct |
276 ms |
15956 KB |
Output is correct |
80 |
Correct |
282 ms |
15960 KB |
Output is correct |
81 |
Correct |
250 ms |
15972 KB |
Output is correct |
82 |
Correct |
252 ms |
15948 KB |
Output is correct |
83 |
Correct |
262 ms |
15972 KB |
Output is correct |
84 |
Correct |
256 ms |
15900 KB |
Output is correct |
85 |
Correct |
260 ms |
16088 KB |
Output is correct |
86 |
Correct |
249 ms |
16100 KB |
Output is correct |
87 |
Correct |
246 ms |
15948 KB |
Output is correct |
88 |
Correct |
259 ms |
15972 KB |
Output is correct |
89 |
Correct |
279 ms |
15964 KB |
Output is correct |
90 |
Correct |
257 ms |
15976 KB |
Output is correct |
91 |
Correct |
252 ms |
15948 KB |
Output is correct |
92 |
Correct |
291 ms |
16316 KB |
Output is correct |
93 |
Correct |
285 ms |
16268 KB |
Output is correct |
94 |
Correct |
245 ms |
15968 KB |
Output is correct |
95 |
Correct |
258 ms |
15948 KB |
Output is correct |
96 |
Correct |
256 ms |
15980 KB |
Output is correct |
97 |
Correct |
312 ms |
15948 KB |
Output is correct |
98 |
Correct |
256 ms |
15976 KB |
Output is correct |
99 |
Correct |
269 ms |
15960 KB |
Output is correct |
100 |
Correct |
265 ms |
15964 KB |
Output is correct |