//#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){
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 |
75 ms |
117732 KB |
Output is correct |
2 |
Correct |
74 ms |
117860 KB |
Output is correct |
3 |
Correct |
75 ms |
117732 KB |
Output is correct |
4 |
Correct |
74 ms |
117732 KB |
Output is correct |
5 |
Correct |
76 ms |
117732 KB |
Output is correct |
6 |
Correct |
77 ms |
117732 KB |
Output is correct |
7 |
Correct |
75 ms |
117732 KB |
Output is correct |
8 |
Correct |
80 ms |
117864 KB |
Output is correct |
9 |
Correct |
76 ms |
117736 KB |
Output is correct |
10 |
Correct |
76 ms |
117732 KB |
Output is correct |
11 |
Correct |
75 ms |
117728 KB |
Output is correct |
12 |
Correct |
75 ms |
117732 KB |
Output is correct |
13 |
Correct |
77 ms |
117736 KB |
Output is correct |
14 |
Correct |
76 ms |
117732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
117988 KB |
Output is correct |
2 |
Correct |
81 ms |
117992 KB |
Output is correct |
3 |
Correct |
77 ms |
117988 KB |
Output is correct |
4 |
Correct |
76 ms |
117988 KB |
Output is correct |
5 |
Correct |
80 ms |
117988 KB |
Output is correct |
6 |
Correct |
85 ms |
117988 KB |
Output is correct |
7 |
Correct |
78 ms |
118112 KB |
Output is correct |
8 |
Correct |
77 ms |
117988 KB |
Output is correct |
9 |
Correct |
77 ms |
117988 KB |
Output is correct |
10 |
Correct |
77 ms |
117984 KB |
Output is correct |
11 |
Correct |
77 ms |
117988 KB |
Output is correct |
12 |
Correct |
77 ms |
117988 KB |
Output is correct |
13 |
Correct |
79 ms |
117988 KB |
Output is correct |
14 |
Correct |
79 ms |
117988 KB |
Output is correct |
15 |
Correct |
77 ms |
117988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
117988 KB |
Output is correct |
2 |
Correct |
81 ms |
117992 KB |
Output is correct |
3 |
Correct |
77 ms |
117988 KB |
Output is correct |
4 |
Correct |
76 ms |
117988 KB |
Output is correct |
5 |
Correct |
80 ms |
117988 KB |
Output is correct |
6 |
Correct |
85 ms |
117988 KB |
Output is correct |
7 |
Correct |
78 ms |
118112 KB |
Output is correct |
8 |
Correct |
77 ms |
117988 KB |
Output is correct |
9 |
Correct |
77 ms |
117988 KB |
Output is correct |
10 |
Correct |
77 ms |
117984 KB |
Output is correct |
11 |
Correct |
77 ms |
117988 KB |
Output is correct |
12 |
Correct |
77 ms |
117988 KB |
Output is correct |
13 |
Correct |
79 ms |
117988 KB |
Output is correct |
14 |
Correct |
79 ms |
117988 KB |
Output is correct |
15 |
Correct |
77 ms |
117988 KB |
Output is correct |
16 |
Correct |
905 ms |
286968 KB |
Output is correct |
17 |
Correct |
909 ms |
272724 KB |
Output is correct |
18 |
Correct |
906 ms |
303436 KB |
Output is correct |
19 |
Correct |
912 ms |
268896 KB |
Output is correct |
20 |
Correct |
954 ms |
275344 KB |
Output is correct |
21 |
Correct |
920 ms |
273620 KB |
Output is correct |
22 |
Correct |
910 ms |
278208 KB |
Output is correct |
23 |
Correct |
930 ms |
264792 KB |
Output is correct |
24 |
Correct |
973 ms |
274732 KB |
Output is correct |
25 |
Correct |
943 ms |
263644 KB |
Output is correct |
26 |
Correct |
961 ms |
262996 KB |
Output is correct |
27 |
Correct |
975 ms |
280316 KB |
Output is correct |
28 |
Correct |
937 ms |
272016 KB |
Output is correct |
29 |
Correct |
929 ms |
264468 KB |
Output is correct |
30 |
Correct |
951 ms |
264492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
652 ms |
233260 KB |
Output is correct |
2 |
Correct |
649 ms |
234536 KB |
Output is correct |
3 |
Correct |
658 ms |
234312 KB |
Output is correct |
4 |
Correct |
656 ms |
234280 KB |
Output is correct |
5 |
Correct |
646 ms |
234376 KB |
Output is correct |
6 |
Correct |
655 ms |
234580 KB |
Output is correct |
7 |
Correct |
648 ms |
234408 KB |
Output is correct |
8 |
Correct |
658 ms |
234536 KB |
Output is correct |
9 |
Correct |
648 ms |
234280 KB |
Output is correct |
10 |
Correct |
661 ms |
234280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
117732 KB |
Output is correct |
2 |
Correct |
74 ms |
117860 KB |
Output is correct |
3 |
Correct |
75 ms |
117732 KB |
Output is correct |
4 |
Correct |
74 ms |
117732 KB |
Output is correct |
5 |
Correct |
76 ms |
117732 KB |
Output is correct |
6 |
Correct |
77 ms |
117732 KB |
Output is correct |
7 |
Correct |
75 ms |
117732 KB |
Output is correct |
8 |
Correct |
80 ms |
117864 KB |
Output is correct |
9 |
Correct |
76 ms |
117736 KB |
Output is correct |
10 |
Correct |
76 ms |
117732 KB |
Output is correct |
11 |
Correct |
75 ms |
117728 KB |
Output is correct |
12 |
Correct |
75 ms |
117732 KB |
Output is correct |
13 |
Correct |
77 ms |
117736 KB |
Output is correct |
14 |
Correct |
76 ms |
117732 KB |
Output is correct |
15 |
Correct |
77 ms |
117988 KB |
Output is correct |
16 |
Correct |
81 ms |
117992 KB |
Output is correct |
17 |
Correct |
77 ms |
117988 KB |
Output is correct |
18 |
Correct |
76 ms |
117988 KB |
Output is correct |
19 |
Correct |
80 ms |
117988 KB |
Output is correct |
20 |
Correct |
85 ms |
117988 KB |
Output is correct |
21 |
Correct |
78 ms |
118112 KB |
Output is correct |
22 |
Correct |
77 ms |
117988 KB |
Output is correct |
23 |
Correct |
77 ms |
117988 KB |
Output is correct |
24 |
Correct |
77 ms |
117984 KB |
Output is correct |
25 |
Correct |
77 ms |
117988 KB |
Output is correct |
26 |
Correct |
77 ms |
117988 KB |
Output is correct |
27 |
Correct |
79 ms |
117988 KB |
Output is correct |
28 |
Correct |
79 ms |
117988 KB |
Output is correct |
29 |
Correct |
77 ms |
117988 KB |
Output is correct |
30 |
Correct |
76 ms |
117988 KB |
Output is correct |
31 |
Correct |
77 ms |
117988 KB |
Output is correct |
32 |
Correct |
77 ms |
117984 KB |
Output is correct |
33 |
Correct |
75 ms |
117988 KB |
Output is correct |
34 |
Correct |
76 ms |
117988 KB |
Output is correct |
35 |
Correct |
76 ms |
117988 KB |
Output is correct |
36 |
Correct |
76 ms |
117988 KB |
Output is correct |
37 |
Correct |
75 ms |
117988 KB |
Output is correct |
38 |
Correct |
75 ms |
117988 KB |
Output is correct |
39 |
Correct |
76 ms |
118116 KB |
Output is correct |
40 |
Correct |
75 ms |
117988 KB |
Output is correct |
41 |
Correct |
76 ms |
117988 KB |
Output is correct |
42 |
Correct |
77 ms |
117988 KB |
Output is correct |
43 |
Correct |
78 ms |
117992 KB |
Output is correct |
44 |
Correct |
79 ms |
117988 KB |
Output is correct |
45 |
Correct |
77 ms |
117988 KB |
Output is correct |
46 |
Correct |
77 ms |
117984 KB |
Output is correct |
47 |
Correct |
75 ms |
117988 KB |
Output is correct |
48 |
Correct |
77 ms |
117988 KB |
Output is correct |
49 |
Correct |
75 ms |
117988 KB |
Output is correct |
50 |
Correct |
78 ms |
117988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
117732 KB |
Output is correct |
2 |
Correct |
74 ms |
117860 KB |
Output is correct |
3 |
Correct |
75 ms |
117732 KB |
Output is correct |
4 |
Correct |
74 ms |
117732 KB |
Output is correct |
5 |
Correct |
76 ms |
117732 KB |
Output is correct |
6 |
Correct |
77 ms |
117732 KB |
Output is correct |
7 |
Correct |
75 ms |
117732 KB |
Output is correct |
8 |
Correct |
80 ms |
117864 KB |
Output is correct |
9 |
Correct |
76 ms |
117736 KB |
Output is correct |
10 |
Correct |
76 ms |
117732 KB |
Output is correct |
11 |
Correct |
75 ms |
117728 KB |
Output is correct |
12 |
Correct |
75 ms |
117732 KB |
Output is correct |
13 |
Correct |
77 ms |
117736 KB |
Output is correct |
14 |
Correct |
76 ms |
117732 KB |
Output is correct |
15 |
Correct |
77 ms |
117988 KB |
Output is correct |
16 |
Correct |
81 ms |
117992 KB |
Output is correct |
17 |
Correct |
77 ms |
117988 KB |
Output is correct |
18 |
Correct |
76 ms |
117988 KB |
Output is correct |
19 |
Correct |
80 ms |
117988 KB |
Output is correct |
20 |
Correct |
85 ms |
117988 KB |
Output is correct |
21 |
Correct |
78 ms |
118112 KB |
Output is correct |
22 |
Correct |
77 ms |
117988 KB |
Output is correct |
23 |
Correct |
77 ms |
117988 KB |
Output is correct |
24 |
Correct |
77 ms |
117984 KB |
Output is correct |
25 |
Correct |
77 ms |
117988 KB |
Output is correct |
26 |
Correct |
77 ms |
117988 KB |
Output is correct |
27 |
Correct |
79 ms |
117988 KB |
Output is correct |
28 |
Correct |
79 ms |
117988 KB |
Output is correct |
29 |
Correct |
77 ms |
117988 KB |
Output is correct |
30 |
Correct |
905 ms |
286968 KB |
Output is correct |
31 |
Correct |
909 ms |
272724 KB |
Output is correct |
32 |
Correct |
906 ms |
303436 KB |
Output is correct |
33 |
Correct |
912 ms |
268896 KB |
Output is correct |
34 |
Correct |
954 ms |
275344 KB |
Output is correct |
35 |
Correct |
920 ms |
273620 KB |
Output is correct |
36 |
Correct |
910 ms |
278208 KB |
Output is correct |
37 |
Correct |
930 ms |
264792 KB |
Output is correct |
38 |
Correct |
973 ms |
274732 KB |
Output is correct |
39 |
Correct |
943 ms |
263644 KB |
Output is correct |
40 |
Correct |
961 ms |
262996 KB |
Output is correct |
41 |
Correct |
975 ms |
280316 KB |
Output is correct |
42 |
Correct |
937 ms |
272016 KB |
Output is correct |
43 |
Correct |
929 ms |
264468 KB |
Output is correct |
44 |
Correct |
951 ms |
264492 KB |
Output is correct |
45 |
Correct |
652 ms |
233260 KB |
Output is correct |
46 |
Correct |
649 ms |
234536 KB |
Output is correct |
47 |
Correct |
658 ms |
234312 KB |
Output is correct |
48 |
Correct |
656 ms |
234280 KB |
Output is correct |
49 |
Correct |
646 ms |
234376 KB |
Output is correct |
50 |
Correct |
655 ms |
234580 KB |
Output is correct |
51 |
Correct |
648 ms |
234408 KB |
Output is correct |
52 |
Correct |
658 ms |
234536 KB |
Output is correct |
53 |
Correct |
648 ms |
234280 KB |
Output is correct |
54 |
Correct |
661 ms |
234280 KB |
Output is correct |
55 |
Correct |
76 ms |
117988 KB |
Output is correct |
56 |
Correct |
77 ms |
117988 KB |
Output is correct |
57 |
Correct |
77 ms |
117984 KB |
Output is correct |
58 |
Correct |
75 ms |
117988 KB |
Output is correct |
59 |
Correct |
76 ms |
117988 KB |
Output is correct |
60 |
Correct |
76 ms |
117988 KB |
Output is correct |
61 |
Correct |
76 ms |
117988 KB |
Output is correct |
62 |
Correct |
75 ms |
117988 KB |
Output is correct |
63 |
Correct |
75 ms |
117988 KB |
Output is correct |
64 |
Correct |
76 ms |
118116 KB |
Output is correct |
65 |
Correct |
75 ms |
117988 KB |
Output is correct |
66 |
Correct |
76 ms |
117988 KB |
Output is correct |
67 |
Correct |
77 ms |
117988 KB |
Output is correct |
68 |
Correct |
78 ms |
117992 KB |
Output is correct |
69 |
Correct |
79 ms |
117988 KB |
Output is correct |
70 |
Correct |
77 ms |
117988 KB |
Output is correct |
71 |
Correct |
77 ms |
117984 KB |
Output is correct |
72 |
Correct |
75 ms |
117988 KB |
Output is correct |
73 |
Correct |
77 ms |
117988 KB |
Output is correct |
74 |
Correct |
75 ms |
117988 KB |
Output is correct |
75 |
Correct |
78 ms |
117988 KB |
Output is correct |
76 |
Correct |
937 ms |
332004 KB |
Output is correct |
77 |
Correct |
944 ms |
339176 KB |
Output is correct |
78 |
Correct |
948 ms |
335388 KB |
Output is correct |
79 |
Correct |
972 ms |
341004 KB |
Output is correct |
80 |
Correct |
975 ms |
334020 KB |
Output is correct |
81 |
Correct |
939 ms |
340000 KB |
Output is correct |
82 |
Correct |
949 ms |
337204 KB |
Output is correct |
83 |
Correct |
948 ms |
334540 KB |
Output is correct |
84 |
Correct |
942 ms |
338472 KB |
Output is correct |
85 |
Correct |
958 ms |
338688 KB |
Output is correct |
86 |
Correct |
939 ms |
344620 KB |
Output is correct |
87 |
Correct |
933 ms |
341588 KB |
Output is correct |
88 |
Correct |
949 ms |
337504 KB |
Output is correct |
89 |
Correct |
932 ms |
334728 KB |
Output is correct |
90 |
Correct |
947 ms |
340476 KB |
Output is correct |
91 |
Correct |
947 ms |
337900 KB |
Output is correct |
92 |
Correct |
918 ms |
258696 KB |
Output is correct |
93 |
Correct |
906 ms |
269788 KB |
Output is correct |
94 |
Correct |
922 ms |
342376 KB |
Output is correct |
95 |
Correct |
959 ms |
339916 KB |
Output is correct |
96 |
Correct |
946 ms |
339664 KB |
Output is correct |
97 |
Correct |
944 ms |
338256 KB |
Output is correct |
98 |
Correct |
936 ms |
332396 KB |
Output is correct |
99 |
Correct |
943 ms |
339836 KB |
Output is correct |
100 |
Correct |
930 ms |
335208 KB |
Output is correct |