#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 1000000;
const ll INF = 1000000000000000000LL;
ll a[N+5];
ll dp[N+5];
struct line{
ll k, n;
ll eval(int x){ return k*x + n; }
} seg[4*N+5];
ll query(int node, int l, int r, int i){
ll g = seg[node].eval(i);
if(l == r) return g;
int mid = (l+r)/2;
if(i <= mid) return min(g, query(node*2, l, mid, i));
return min(g, query(node*2+1, mid+1, r, i));
}
stack <tuple <line, int, int>> rev;
void brisi(int i){
while(!rev.empty() && get<2>(rev.top()) == i){
seg[get<1>(rev.top())] = get<0>(rev.top());
rev.pop();
}
}
void upd(int node, int l, int r, line g, int i){
int mid = (l+r)/2;
if(g.eval(mid) < seg[node].eval(mid)){
rev.push({seg[node], node, i});
swap(g, seg[node]);
}
if(l == r) return;
if(seg[node].eval(l) > g.eval(l)) upd(node*2, l, mid, g, i);
else upd(node*2+1, mid+1, r, g, i);
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n;
cin >> n;
for(int i=1; i<=n; i++){
cin >> a[i];
}
reverse(a+1, a+1+n);
for(int i=1; i<=4*n; i++) seg[i].n = INF;
stack <int> st;
for(int i=1; i<=n; i++){
while(!st.empty() && a[st.top()] <= a[i]){
brisi(st.top());
st.pop();
}
line h;
h.k = a[i];
h.n = 0;
if(!st.empty()){
h.n = dp[st.top()];
}
upd(1, 1, n, h, i);
st.push(i);
dp[i] = query(1, 1, n, i);
}
cout << dp[n] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
405 ms |
249608 KB |
Output is correct |
17 |
Correct |
446 ms |
269248 KB |
Output is correct |
18 |
Correct |
412 ms |
207608 KB |
Output is correct |
19 |
Correct |
389 ms |
249684 KB |
Output is correct |
20 |
Correct |
408 ms |
288568 KB |
Output is correct |
21 |
Correct |
415 ms |
265800 KB |
Output is correct |
22 |
Correct |
393 ms |
251684 KB |
Output is correct |
23 |
Correct |
422 ms |
315964 KB |
Output is correct |
24 |
Correct |
472 ms |
292636 KB |
Output is correct |
25 |
Correct |
402 ms |
286504 KB |
Output is correct |
26 |
Correct |
416 ms |
319876 KB |
Output is correct |
27 |
Correct |
376 ms |
227900 KB |
Output is correct |
28 |
Correct |
404 ms |
277720 KB |
Output is correct |
29 |
Correct |
438 ms |
293692 KB |
Output is correct |
30 |
Correct |
434 ms |
313556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
276 ms |
88260 KB |
Output is correct |
2 |
Correct |
309 ms |
88156 KB |
Output is correct |
3 |
Correct |
281 ms |
88216 KB |
Output is correct |
4 |
Correct |
307 ms |
88212 KB |
Output is correct |
5 |
Correct |
298 ms |
88264 KB |
Output is correct |
6 |
Correct |
289 ms |
88436 KB |
Output is correct |
7 |
Correct |
277 ms |
88196 KB |
Output is correct |
8 |
Correct |
279 ms |
88180 KB |
Output is correct |
9 |
Correct |
298 ms |
88232 KB |
Output is correct |
10 |
Correct |
357 ms |
88164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
472 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
352 KB |
Output is correct |
31 |
Correct |
1 ms |
456 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
1 ms |
460 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
472 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
405 ms |
249608 KB |
Output is correct |
31 |
Correct |
446 ms |
269248 KB |
Output is correct |
32 |
Correct |
412 ms |
207608 KB |
Output is correct |
33 |
Correct |
389 ms |
249684 KB |
Output is correct |
34 |
Correct |
408 ms |
288568 KB |
Output is correct |
35 |
Correct |
415 ms |
265800 KB |
Output is correct |
36 |
Correct |
393 ms |
251684 KB |
Output is correct |
37 |
Correct |
422 ms |
315964 KB |
Output is correct |
38 |
Correct |
472 ms |
292636 KB |
Output is correct |
39 |
Correct |
402 ms |
286504 KB |
Output is correct |
40 |
Correct |
416 ms |
319876 KB |
Output is correct |
41 |
Correct |
376 ms |
227900 KB |
Output is correct |
42 |
Correct |
404 ms |
277720 KB |
Output is correct |
43 |
Correct |
438 ms |
293692 KB |
Output is correct |
44 |
Correct |
434 ms |
313556 KB |
Output is correct |
45 |
Correct |
276 ms |
88260 KB |
Output is correct |
46 |
Correct |
309 ms |
88156 KB |
Output is correct |
47 |
Correct |
281 ms |
88216 KB |
Output is correct |
48 |
Correct |
307 ms |
88212 KB |
Output is correct |
49 |
Correct |
298 ms |
88264 KB |
Output is correct |
50 |
Correct |
289 ms |
88436 KB |
Output is correct |
51 |
Correct |
277 ms |
88196 KB |
Output is correct |
52 |
Correct |
279 ms |
88180 KB |
Output is correct |
53 |
Correct |
298 ms |
88232 KB |
Output is correct |
54 |
Correct |
357 ms |
88164 KB |
Output is correct |
55 |
Correct |
1 ms |
352 KB |
Output is correct |
56 |
Correct |
1 ms |
456 KB |
Output is correct |
57 |
Correct |
1 ms |
468 KB |
Output is correct |
58 |
Correct |
1 ms |
468 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
468 KB |
Output is correct |
61 |
Correct |
1 ms |
468 KB |
Output is correct |
62 |
Correct |
1 ms |
468 KB |
Output is correct |
63 |
Correct |
1 ms |
460 KB |
Output is correct |
64 |
Correct |
1 ms |
468 KB |
Output is correct |
65 |
Correct |
1 ms |
468 KB |
Output is correct |
66 |
Correct |
1 ms |
468 KB |
Output is correct |
67 |
Correct |
1 ms |
468 KB |
Output is correct |
68 |
Correct |
1 ms |
596 KB |
Output is correct |
69 |
Correct |
1 ms |
468 KB |
Output is correct |
70 |
Correct |
1 ms |
468 KB |
Output is correct |
71 |
Correct |
1 ms |
468 KB |
Output is correct |
72 |
Correct |
1 ms |
468 KB |
Output is correct |
73 |
Correct |
1 ms |
468 KB |
Output is correct |
74 |
Correct |
1 ms |
468 KB |
Output is correct |
75 |
Correct |
1 ms |
464 KB |
Output is correct |
76 |
Correct |
353 ms |
84588 KB |
Output is correct |
77 |
Correct |
339 ms |
84752 KB |
Output is correct |
78 |
Correct |
335 ms |
83984 KB |
Output is correct |
79 |
Correct |
371 ms |
84296 KB |
Output is correct |
80 |
Correct |
353 ms |
84932 KB |
Output is correct |
81 |
Correct |
332 ms |
84084 KB |
Output is correct |
82 |
Correct |
332 ms |
84160 KB |
Output is correct |
83 |
Correct |
382 ms |
84560 KB |
Output is correct |
84 |
Correct |
357 ms |
84332 KB |
Output is correct |
85 |
Correct |
369 ms |
84784 KB |
Output is correct |
86 |
Correct |
348 ms |
84008 KB |
Output is correct |
87 |
Correct |
331 ms |
83896 KB |
Output is correct |
88 |
Correct |
360 ms |
84324 KB |
Output is correct |
89 |
Correct |
361 ms |
84148 KB |
Output is correct |
90 |
Correct |
347 ms |
83996 KB |
Output is correct |
91 |
Correct |
350 ms |
83812 KB |
Output is correct |
92 |
Correct |
423 ms |
278340 KB |
Output is correct |
93 |
Correct |
437 ms |
266276 KB |
Output is correct |
94 |
Correct |
340 ms |
83496 KB |
Output is correct |
95 |
Correct |
354 ms |
84940 KB |
Output is correct |
96 |
Correct |
357 ms |
83788 KB |
Output is correct |
97 |
Correct |
347 ms |
84084 KB |
Output is correct |
98 |
Correct |
351 ms |
84436 KB |
Output is correct |
99 |
Correct |
334 ms |
84044 KB |
Output is correct |
100 |
Correct |
356 ms |
84020 KB |
Output is correct |