#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
struct Line{
ll m, c;
Line(ll _m, ll _c){
m=_m;c=_c;
}
ll f(ll x){
return m*x+c;
}
ld intersectX(Line l){
return (1.0*l.c-c)/(m-l.m);
}
};
deque<Line> hull;
bool isBetter(Line l){
return (l.intersectX(hull[hull.size()-1])<l.intersectX(hull[hull.size()-2]));
}
void add(Line l){
if (hull.size()>0&&hull.back().m==l.m){
if (l.c<hull.back().c){
hull.pop_back();
hull.push_back(l);
}
return;
}
while (hull.size()>1&&isBetter(l)){
hull.pop_back();
}
hull.push_back(l);
}
ll query(ll x){
while (hull.size()>1&&hull[0].f(x)>hull[1].f(x)){
hull.pop_front();
}
return hull.front().f(x);
}
int main(){
ll n;
cin>>n;
add(Line(n,0));
ll prev = 0;
ll ans;
for (ll i = 1; i<=n; i++){
ll val;
cin>>val;
if (val<prev){
val=prev;
}
/*cout<<"val: "<<val<<'\n';
for (auto h : hull){
cout<<h.m<<' '<<h.c<<'\n';
}*/
prev=val;
ans = query(val);
add(Line{n-i,ans});
//cout<<ans<<'\n';
}
cout<<ans<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
256 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
528 ms |
256 KB |
Output is correct |
17 |
Correct |
608 ms |
256 KB |
Output is correct |
18 |
Correct |
482 ms |
504 KB |
Output is correct |
19 |
Correct |
590 ms |
380 KB |
Output is correct |
20 |
Correct |
581 ms |
376 KB |
Output is correct |
21 |
Correct |
585 ms |
376 KB |
Output is correct |
22 |
Correct |
552 ms |
376 KB |
Output is correct |
23 |
Correct |
638 ms |
360 KB |
Output is correct |
24 |
Correct |
605 ms |
256 KB |
Output is correct |
25 |
Correct |
624 ms |
380 KB |
Output is correct |
26 |
Correct |
683 ms |
256 KB |
Output is correct |
27 |
Correct |
551 ms |
256 KB |
Output is correct |
28 |
Correct |
592 ms |
256 KB |
Output is correct |
29 |
Correct |
610 ms |
376 KB |
Output is correct |
30 |
Correct |
625 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
891 ms |
356 KB |
Output is correct |
2 |
Correct |
855 ms |
356 KB |
Output is correct |
3 |
Correct |
845 ms |
376 KB |
Output is correct |
4 |
Correct |
856 ms |
356 KB |
Output is correct |
5 |
Correct |
857 ms |
376 KB |
Output is correct |
6 |
Correct |
850 ms |
376 KB |
Output is correct |
7 |
Correct |
854 ms |
376 KB |
Output is correct |
8 |
Correct |
846 ms |
356 KB |
Output is correct |
9 |
Correct |
845 ms |
376 KB |
Output is correct |
10 |
Correct |
849 ms |
360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
256 KB |
Output is correct |
21 |
Correct |
1 ms |
256 KB |
Output is correct |
22 |
Correct |
2 ms |
256 KB |
Output is correct |
23 |
Correct |
2 ms |
256 KB |
Output is correct |
24 |
Correct |
2 ms |
256 KB |
Output is correct |
25 |
Correct |
2 ms |
256 KB |
Output is correct |
26 |
Correct |
1 ms |
256 KB |
Output is correct |
27 |
Correct |
1 ms |
256 KB |
Output is correct |
28 |
Correct |
2 ms |
256 KB |
Output is correct |
29 |
Correct |
2 ms |
256 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
256 KB |
Output is correct |
32 |
Correct |
1 ms |
256 KB |
Output is correct |
33 |
Correct |
1 ms |
256 KB |
Output is correct |
34 |
Correct |
1 ms |
256 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
1 ms |
256 KB |
Output is correct |
37 |
Correct |
2 ms |
256 KB |
Output is correct |
38 |
Correct |
1 ms |
256 KB |
Output is correct |
39 |
Correct |
1 ms |
256 KB |
Output is correct |
40 |
Correct |
1 ms |
256 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
42 |
Correct |
1 ms |
256 KB |
Output is correct |
43 |
Correct |
1 ms |
256 KB |
Output is correct |
44 |
Correct |
1 ms |
256 KB |
Output is correct |
45 |
Correct |
1 ms |
256 KB |
Output is correct |
46 |
Correct |
1 ms |
256 KB |
Output is correct |
47 |
Correct |
1 ms |
256 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
1 ms |
256 KB |
Output is correct |
50 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
0 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
0 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
256 KB |
Output is correct |
14 |
Correct |
1 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
256 KB |
Output is correct |
16 |
Correct |
1 ms |
256 KB |
Output is correct |
17 |
Correct |
1 ms |
256 KB |
Output is correct |
18 |
Correct |
1 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
256 KB |
Output is correct |
21 |
Correct |
1 ms |
256 KB |
Output is correct |
22 |
Correct |
2 ms |
256 KB |
Output is correct |
23 |
Correct |
2 ms |
256 KB |
Output is correct |
24 |
Correct |
2 ms |
256 KB |
Output is correct |
25 |
Correct |
2 ms |
256 KB |
Output is correct |
26 |
Correct |
1 ms |
256 KB |
Output is correct |
27 |
Correct |
1 ms |
256 KB |
Output is correct |
28 |
Correct |
2 ms |
256 KB |
Output is correct |
29 |
Correct |
2 ms |
256 KB |
Output is correct |
30 |
Correct |
528 ms |
256 KB |
Output is correct |
31 |
Correct |
608 ms |
256 KB |
Output is correct |
32 |
Correct |
482 ms |
504 KB |
Output is correct |
33 |
Correct |
590 ms |
380 KB |
Output is correct |
34 |
Correct |
581 ms |
376 KB |
Output is correct |
35 |
Correct |
585 ms |
376 KB |
Output is correct |
36 |
Correct |
552 ms |
376 KB |
Output is correct |
37 |
Correct |
638 ms |
360 KB |
Output is correct |
38 |
Correct |
605 ms |
256 KB |
Output is correct |
39 |
Correct |
624 ms |
380 KB |
Output is correct |
40 |
Correct |
683 ms |
256 KB |
Output is correct |
41 |
Correct |
551 ms |
256 KB |
Output is correct |
42 |
Correct |
592 ms |
256 KB |
Output is correct |
43 |
Correct |
610 ms |
376 KB |
Output is correct |
44 |
Correct |
625 ms |
256 KB |
Output is correct |
45 |
Correct |
891 ms |
356 KB |
Output is correct |
46 |
Correct |
855 ms |
356 KB |
Output is correct |
47 |
Correct |
845 ms |
376 KB |
Output is correct |
48 |
Correct |
856 ms |
356 KB |
Output is correct |
49 |
Correct |
857 ms |
376 KB |
Output is correct |
50 |
Correct |
850 ms |
376 KB |
Output is correct |
51 |
Correct |
854 ms |
376 KB |
Output is correct |
52 |
Correct |
846 ms |
356 KB |
Output is correct |
53 |
Correct |
845 ms |
376 KB |
Output is correct |
54 |
Correct |
849 ms |
360 KB |
Output is correct |
55 |
Correct |
1 ms |
384 KB |
Output is correct |
56 |
Correct |
1 ms |
256 KB |
Output is correct |
57 |
Correct |
1 ms |
256 KB |
Output is correct |
58 |
Correct |
1 ms |
256 KB |
Output is correct |
59 |
Correct |
1 ms |
256 KB |
Output is correct |
60 |
Correct |
1 ms |
384 KB |
Output is correct |
61 |
Correct |
1 ms |
256 KB |
Output is correct |
62 |
Correct |
2 ms |
256 KB |
Output is correct |
63 |
Correct |
1 ms |
256 KB |
Output is correct |
64 |
Correct |
1 ms |
256 KB |
Output is correct |
65 |
Correct |
1 ms |
256 KB |
Output is correct |
66 |
Correct |
1 ms |
384 KB |
Output is correct |
67 |
Correct |
1 ms |
256 KB |
Output is correct |
68 |
Correct |
1 ms |
256 KB |
Output is correct |
69 |
Correct |
1 ms |
256 KB |
Output is correct |
70 |
Correct |
1 ms |
256 KB |
Output is correct |
71 |
Correct |
1 ms |
256 KB |
Output is correct |
72 |
Correct |
1 ms |
256 KB |
Output is correct |
73 |
Correct |
1 ms |
384 KB |
Output is correct |
74 |
Correct |
1 ms |
256 KB |
Output is correct |
75 |
Correct |
1 ms |
384 KB |
Output is correct |
76 |
Correct |
609 ms |
6520 KB |
Output is correct |
77 |
Correct |
598 ms |
6376 KB |
Output is correct |
78 |
Correct |
558 ms |
6008 KB |
Output is correct |
79 |
Correct |
579 ms |
6124 KB |
Output is correct |
80 |
Correct |
622 ms |
6904 KB |
Output is correct |
81 |
Correct |
559 ms |
5880 KB |
Output is correct |
82 |
Correct |
555 ms |
5880 KB |
Output is correct |
83 |
Correct |
588 ms |
6264 KB |
Output is correct |
84 |
Correct |
582 ms |
6132 KB |
Output is correct |
85 |
Correct |
624 ms |
6420 KB |
Output is correct |
86 |
Correct |
558 ms |
5896 KB |
Output is correct |
87 |
Correct |
552 ms |
5752 KB |
Output is correct |
88 |
Correct |
581 ms |
6040 KB |
Output is correct |
89 |
Correct |
570 ms |
5988 KB |
Output is correct |
90 |
Correct |
569 ms |
5736 KB |
Output is correct |
91 |
Correct |
533 ms |
5624 KB |
Output is correct |
92 |
Correct |
656 ms |
6888 KB |
Output is correct |
93 |
Correct |
616 ms |
6136 KB |
Output is correct |
94 |
Correct |
518 ms |
5240 KB |
Output is correct |
95 |
Correct |
617 ms |
6632 KB |
Output is correct |
96 |
Correct |
563 ms |
5608 KB |
Output is correct |
97 |
Correct |
564 ms |
6136 KB |
Output is correct |
98 |
Correct |
574 ms |
6248 KB |
Output is correct |
99 |
Correct |
560 ms |
5752 KB |
Output is correct |
100 |
Correct |
557 ms |
5896 KB |
Output is correct |