//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
//#define endl '\n'
llo n;
llo it[1000001];
vector<pair<llo,llo>> ss[4*1000001];
llo ind[1000001*4];
vector<pair<llo,llo>> pre[1000001];
llo dp[1000001];
void build(llo no,llo l,llo r){
if(l==r){
pre[r].pb({l,no});
}
else{
pre[r].pb({l,no});
llo mid=(l+r)/2;
build(no*2+1,l,mid);
build(no*2+2,mid+1,r);
}
}
void re(llo no,llo l,llo r){
// cout<<no<<":"<<l<<":"<<r<<endl;
for(llo i=l;i<=r;i++){
//cout<<no<<":"<<i<<endl;
pair<llo,llo> xx={(n-i-1),dp[i]};
while(ss[no].size()>=2){
llo aa=ss[no].back().b-xx.b;
llo bb=ss[no].back().a-xx.a;
llo cc=ss[no][ss[no].size()-2].b-ss[no].back().b;
llo dd=ss[no][ss[no].size()-2].a-ss[no].back().a;
aa=-aa;
cc=-cc;
long double kk=aa*dd;
long double mm=bb*cc;
if(kk<mm){
ss[no].pop_back();
continue;
}
else{
break;
}
}
ss[no].pb(xx);
}
}
llo eval(pair<llo,llo> bb,llo cc){
return bb.a*cc+bb.b;
}
llo qq=0;
llo query(llo no,llo l,llo r,llo aa,llo bb){
if(r<aa or l>bb){
return (llo)1e18;
}
if(aa<=l and r<=bb){
if(ss[no].size()==0){
re(no,l,r);
}
// cout<<no<<":"<<endl;
while(ind[no]<(llo)(ss[no].size())-1){
if(eval(ss[no][ind[no]],qq)>=eval(ss[no][ind[no]+1],qq)){
ind[no]++;
}
else{
break;
}
}
return eval(ss[no][ind[no]],qq);
}
else{
llo mid=(l+r)/2;
return min(query(no*2+1,l,mid,aa,bb),query(no*2+2,mid+1,r,aa,bb));
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for(llo i=0;i<n;i++){
cin>>it[i];
}
vector<pair<llo,llo>> ss;
build(0,0,n-1);
for(llo i=0;i<n;i++){
while(ss.size()){
if(ss.back().a<=it[i]){
ss.pop_back();
}
else{
break;
}
}
// cout<<i<<endl;
llo la=-1;
if(ss.size()){
la=ss.back().b;
}
ss.pb({it[i],i});
dp[i]=ss[0].a*n;
if(la!=-1){
dp[i]=min(dp[i],dp[la]);
}
qq=it[i];
if(i>0 and la==-1){
dp[i]=min(dp[i],query(0,0,n-1,max(la,(llo)0),i-1));
}
//cout<<dp[i]<<",";
if(i==n-1){
break;
}
/* for(auto j:pre[i]){
re(j.b,j.a,i);
} */
}
// cout<<endl;
cout<<dp[n-1]<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
117860 KB |
Output is correct |
2 |
Correct |
73 ms |
117732 KB |
Output is correct |
3 |
Correct |
74 ms |
117732 KB |
Output is correct |
4 |
Correct |
74 ms |
117860 KB |
Output is correct |
5 |
Correct |
77 ms |
117732 KB |
Output is correct |
6 |
Correct |
74 ms |
117732 KB |
Output is correct |
7 |
Correct |
75 ms |
117732 KB |
Output is correct |
8 |
Correct |
74 ms |
117732 KB |
Output is correct |
9 |
Correct |
74 ms |
117732 KB |
Output is correct |
10 |
Correct |
74 ms |
117732 KB |
Output is correct |
11 |
Correct |
74 ms |
117864 KB |
Output is correct |
12 |
Correct |
74 ms |
117732 KB |
Output is correct |
13 |
Correct |
81 ms |
117880 KB |
Output is correct |
14 |
Correct |
74 ms |
117732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
118116 KB |
Output is correct |
2 |
Correct |
75 ms |
117988 KB |
Output is correct |
3 |
Correct |
76 ms |
117984 KB |
Output is correct |
4 |
Correct |
74 ms |
117988 KB |
Output is correct |
5 |
Correct |
75 ms |
117988 KB |
Output is correct |
6 |
Correct |
75 ms |
117988 KB |
Output is correct |
7 |
Correct |
75 ms |
118116 KB |
Output is correct |
8 |
Correct |
75 ms |
117988 KB |
Output is correct |
9 |
Correct |
74 ms |
117988 KB |
Output is correct |
10 |
Correct |
74 ms |
117988 KB |
Output is correct |
11 |
Correct |
76 ms |
117988 KB |
Output is correct |
12 |
Correct |
77 ms |
117992 KB |
Output is correct |
13 |
Correct |
75 ms |
117988 KB |
Output is correct |
14 |
Correct |
76 ms |
117988 KB |
Output is correct |
15 |
Correct |
76 ms |
117988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
118116 KB |
Output is correct |
2 |
Correct |
75 ms |
117988 KB |
Output is correct |
3 |
Correct |
76 ms |
117984 KB |
Output is correct |
4 |
Correct |
74 ms |
117988 KB |
Output is correct |
5 |
Correct |
75 ms |
117988 KB |
Output is correct |
6 |
Correct |
75 ms |
117988 KB |
Output is correct |
7 |
Correct |
75 ms |
118116 KB |
Output is correct |
8 |
Correct |
75 ms |
117988 KB |
Output is correct |
9 |
Correct |
74 ms |
117988 KB |
Output is correct |
10 |
Correct |
74 ms |
117988 KB |
Output is correct |
11 |
Correct |
76 ms |
117988 KB |
Output is correct |
12 |
Correct |
77 ms |
117992 KB |
Output is correct |
13 |
Correct |
75 ms |
117988 KB |
Output is correct |
14 |
Correct |
76 ms |
117988 KB |
Output is correct |
15 |
Correct |
76 ms |
117988 KB |
Output is correct |
16 |
Correct |
901 ms |
285648 KB |
Output is correct |
17 |
Correct |
903 ms |
270684 KB |
Output is correct |
18 |
Correct |
895 ms |
301260 KB |
Output is correct |
19 |
Correct |
903 ms |
266596 KB |
Output is correct |
20 |
Correct |
897 ms |
273128 KB |
Output is correct |
21 |
Correct |
894 ms |
271324 KB |
Output is correct |
22 |
Correct |
908 ms |
276076 KB |
Output is correct |
23 |
Correct |
904 ms |
262492 KB |
Output is correct |
24 |
Correct |
905 ms |
272804 KB |
Output is correct |
25 |
Correct |
910 ms |
261596 KB |
Output is correct |
26 |
Correct |
913 ms |
261080 KB |
Output is correct |
27 |
Correct |
901 ms |
278056 KB |
Output is correct |
28 |
Correct |
907 ms |
269784 KB |
Output is correct |
29 |
Correct |
903 ms |
262368 KB |
Output is correct |
30 |
Correct |
916 ms |
262472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
341 ms |
200504 KB |
Output is correct |
2 |
Correct |
334 ms |
200520 KB |
Output is correct |
3 |
Correct |
340 ms |
200396 KB |
Output is correct |
4 |
Correct |
335 ms |
200384 KB |
Output is correct |
5 |
Correct |
338 ms |
200388 KB |
Output is correct |
6 |
Correct |
337 ms |
200392 KB |
Output is correct |
7 |
Correct |
338 ms |
200260 KB |
Output is correct |
8 |
Correct |
334 ms |
200472 KB |
Output is correct |
9 |
Correct |
336 ms |
200512 KB |
Output is correct |
10 |
Correct |
335 ms |
200512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
117860 KB |
Output is correct |
2 |
Correct |
73 ms |
117732 KB |
Output is correct |
3 |
Correct |
74 ms |
117732 KB |
Output is correct |
4 |
Correct |
74 ms |
117860 KB |
Output is correct |
5 |
Correct |
77 ms |
117732 KB |
Output is correct |
6 |
Correct |
74 ms |
117732 KB |
Output is correct |
7 |
Correct |
75 ms |
117732 KB |
Output is correct |
8 |
Correct |
74 ms |
117732 KB |
Output is correct |
9 |
Correct |
74 ms |
117732 KB |
Output is correct |
10 |
Correct |
74 ms |
117732 KB |
Output is correct |
11 |
Correct |
74 ms |
117864 KB |
Output is correct |
12 |
Correct |
74 ms |
117732 KB |
Output is correct |
13 |
Correct |
81 ms |
117880 KB |
Output is correct |
14 |
Correct |
74 ms |
117732 KB |
Output is correct |
15 |
Correct |
77 ms |
118116 KB |
Output is correct |
16 |
Correct |
75 ms |
117988 KB |
Output is correct |
17 |
Correct |
76 ms |
117984 KB |
Output is correct |
18 |
Correct |
74 ms |
117988 KB |
Output is correct |
19 |
Correct |
75 ms |
117988 KB |
Output is correct |
20 |
Correct |
75 ms |
117988 KB |
Output is correct |
21 |
Correct |
75 ms |
118116 KB |
Output is correct |
22 |
Correct |
75 ms |
117988 KB |
Output is correct |
23 |
Correct |
74 ms |
117988 KB |
Output is correct |
24 |
Correct |
74 ms |
117988 KB |
Output is correct |
25 |
Correct |
76 ms |
117988 KB |
Output is correct |
26 |
Correct |
77 ms |
117992 KB |
Output is correct |
27 |
Correct |
75 ms |
117988 KB |
Output is correct |
28 |
Correct |
76 ms |
117988 KB |
Output is correct |
29 |
Correct |
76 ms |
117988 KB |
Output is correct |
30 |
Correct |
73 ms |
117988 KB |
Output is correct |
31 |
Correct |
75 ms |
117992 KB |
Output is correct |
32 |
Correct |
76 ms |
117988 KB |
Output is correct |
33 |
Correct |
75 ms |
117988 KB |
Output is correct |
34 |
Correct |
74 ms |
117992 KB |
Output is correct |
35 |
Correct |
75 ms |
117988 KB |
Output is correct |
36 |
Correct |
75 ms |
117860 KB |
Output is correct |
37 |
Correct |
74 ms |
117860 KB |
Output is correct |
38 |
Correct |
77 ms |
117988 KB |
Output is correct |
39 |
Correct |
74 ms |
117988 KB |
Output is correct |
40 |
Correct |
74 ms |
117988 KB |
Output is correct |
41 |
Correct |
76 ms |
117988 KB |
Output is correct |
42 |
Correct |
74 ms |
117988 KB |
Output is correct |
43 |
Correct |
74 ms |
117988 KB |
Output is correct |
44 |
Correct |
74 ms |
117992 KB |
Output is correct |
45 |
Correct |
74 ms |
117988 KB |
Output is correct |
46 |
Correct |
88 ms |
117988 KB |
Output is correct |
47 |
Correct |
79 ms |
117988 KB |
Output is correct |
48 |
Correct |
74 ms |
117988 KB |
Output is correct |
49 |
Correct |
74 ms |
117988 KB |
Output is correct |
50 |
Correct |
75 ms |
117868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
117860 KB |
Output is correct |
2 |
Correct |
73 ms |
117732 KB |
Output is correct |
3 |
Correct |
74 ms |
117732 KB |
Output is correct |
4 |
Correct |
74 ms |
117860 KB |
Output is correct |
5 |
Correct |
77 ms |
117732 KB |
Output is correct |
6 |
Correct |
74 ms |
117732 KB |
Output is correct |
7 |
Correct |
75 ms |
117732 KB |
Output is correct |
8 |
Correct |
74 ms |
117732 KB |
Output is correct |
9 |
Correct |
74 ms |
117732 KB |
Output is correct |
10 |
Correct |
74 ms |
117732 KB |
Output is correct |
11 |
Correct |
74 ms |
117864 KB |
Output is correct |
12 |
Correct |
74 ms |
117732 KB |
Output is correct |
13 |
Correct |
81 ms |
117880 KB |
Output is correct |
14 |
Correct |
74 ms |
117732 KB |
Output is correct |
15 |
Correct |
77 ms |
118116 KB |
Output is correct |
16 |
Correct |
75 ms |
117988 KB |
Output is correct |
17 |
Correct |
76 ms |
117984 KB |
Output is correct |
18 |
Correct |
74 ms |
117988 KB |
Output is correct |
19 |
Correct |
75 ms |
117988 KB |
Output is correct |
20 |
Correct |
75 ms |
117988 KB |
Output is correct |
21 |
Correct |
75 ms |
118116 KB |
Output is correct |
22 |
Correct |
75 ms |
117988 KB |
Output is correct |
23 |
Correct |
74 ms |
117988 KB |
Output is correct |
24 |
Correct |
74 ms |
117988 KB |
Output is correct |
25 |
Correct |
76 ms |
117988 KB |
Output is correct |
26 |
Correct |
77 ms |
117992 KB |
Output is correct |
27 |
Correct |
75 ms |
117988 KB |
Output is correct |
28 |
Correct |
76 ms |
117988 KB |
Output is correct |
29 |
Correct |
76 ms |
117988 KB |
Output is correct |
30 |
Correct |
901 ms |
285648 KB |
Output is correct |
31 |
Correct |
903 ms |
270684 KB |
Output is correct |
32 |
Correct |
895 ms |
301260 KB |
Output is correct |
33 |
Correct |
903 ms |
266596 KB |
Output is correct |
34 |
Correct |
897 ms |
273128 KB |
Output is correct |
35 |
Correct |
894 ms |
271324 KB |
Output is correct |
36 |
Correct |
908 ms |
276076 KB |
Output is correct |
37 |
Correct |
904 ms |
262492 KB |
Output is correct |
38 |
Correct |
905 ms |
272804 KB |
Output is correct |
39 |
Correct |
910 ms |
261596 KB |
Output is correct |
40 |
Correct |
913 ms |
261080 KB |
Output is correct |
41 |
Correct |
901 ms |
278056 KB |
Output is correct |
42 |
Correct |
907 ms |
269784 KB |
Output is correct |
43 |
Correct |
903 ms |
262368 KB |
Output is correct |
44 |
Correct |
916 ms |
262472 KB |
Output is correct |
45 |
Correct |
341 ms |
200504 KB |
Output is correct |
46 |
Correct |
334 ms |
200520 KB |
Output is correct |
47 |
Correct |
340 ms |
200396 KB |
Output is correct |
48 |
Correct |
335 ms |
200384 KB |
Output is correct |
49 |
Correct |
338 ms |
200388 KB |
Output is correct |
50 |
Correct |
337 ms |
200392 KB |
Output is correct |
51 |
Correct |
338 ms |
200260 KB |
Output is correct |
52 |
Correct |
334 ms |
200472 KB |
Output is correct |
53 |
Correct |
336 ms |
200512 KB |
Output is correct |
54 |
Correct |
335 ms |
200512 KB |
Output is correct |
55 |
Correct |
73 ms |
117988 KB |
Output is correct |
56 |
Correct |
75 ms |
117992 KB |
Output is correct |
57 |
Correct |
76 ms |
117988 KB |
Output is correct |
58 |
Correct |
75 ms |
117988 KB |
Output is correct |
59 |
Correct |
74 ms |
117992 KB |
Output is correct |
60 |
Correct |
75 ms |
117988 KB |
Output is correct |
61 |
Correct |
75 ms |
117860 KB |
Output is correct |
62 |
Correct |
74 ms |
117860 KB |
Output is correct |
63 |
Correct |
77 ms |
117988 KB |
Output is correct |
64 |
Correct |
74 ms |
117988 KB |
Output is correct |
65 |
Correct |
74 ms |
117988 KB |
Output is correct |
66 |
Correct |
76 ms |
117988 KB |
Output is correct |
67 |
Correct |
74 ms |
117988 KB |
Output is correct |
68 |
Correct |
74 ms |
117988 KB |
Output is correct |
69 |
Correct |
74 ms |
117992 KB |
Output is correct |
70 |
Correct |
74 ms |
117988 KB |
Output is correct |
71 |
Correct |
88 ms |
117988 KB |
Output is correct |
72 |
Correct |
79 ms |
117988 KB |
Output is correct |
73 |
Correct |
74 ms |
117988 KB |
Output is correct |
74 |
Correct |
74 ms |
117988 KB |
Output is correct |
75 |
Correct |
75 ms |
117868 KB |
Output is correct |
76 |
Correct |
416 ms |
224160 KB |
Output is correct |
77 |
Correct |
425 ms |
232072 KB |
Output is correct |
78 |
Correct |
429 ms |
230176 KB |
Output is correct |
79 |
Correct |
451 ms |
241752 KB |
Output is correct |
80 |
Correct |
420 ms |
224856 KB |
Output is correct |
81 |
Correct |
448 ms |
239596 KB |
Output is correct |
82 |
Correct |
461 ms |
240112 KB |
Output is correct |
83 |
Correct |
405 ms |
221284 KB |
Output is correct |
84 |
Correct |
446 ms |
235580 KB |
Output is correct |
85 |
Correct |
430 ms |
231420 KB |
Output is correct |
86 |
Correct |
463 ms |
247412 KB |
Output is correct |
87 |
Correct |
446 ms |
244956 KB |
Output is correct |
88 |
Correct |
423 ms |
228988 KB |
Output is correct |
89 |
Correct |
450 ms |
237264 KB |
Output is correct |
90 |
Correct |
434 ms |
238176 KB |
Output is correct |
91 |
Correct |
461 ms |
244552 KB |
Output is correct |
92 |
Correct |
901 ms |
256476 KB |
Output is correct |
93 |
Correct |
898 ms |
267500 KB |
Output is correct |
94 |
Correct |
464 ms |
248916 KB |
Output is correct |
95 |
Correct |
461 ms |
232668 KB |
Output is correct |
96 |
Correct |
451 ms |
244856 KB |
Output is correct |
97 |
Correct |
433 ms |
233036 KB |
Output is correct |
98 |
Correct |
422 ms |
225384 KB |
Output is correct |
99 |
Correct |
426 ms |
237412 KB |
Output is correct |
100 |
Correct |
418 ms |
229316 KB |
Output is correct |