#include <bits/stdc++.h>
using namespace std;
#define oo 1e15
#define fi first
#define se second
#define sp(iiii) setprecision(iiii)
#define IO ios_base::sync_with_stdio(false); cin.tie(0)
#define ms(aaaa,xxxx) memset(aaaa,xxxx,sizeof(aaaa))
#define cntbit(xxxx) __builtin_popcount(xxxx)
#define getbit(xxxx,aaaa) ((xxxx>>(aaaa-1))&1)
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<pair<int,int>,int> piii;
typedef pair<long long,long long> pll;
typedef pair<pair<long long,long long>,long long> plll;
typedef pair<pair<long long,long long>,pair<long long,long long>> pllll;
typedef pair<pair<long long,long long>,bool> pllb;
const ll base=361;
const ll mod=998244353;
const ld eps=1e-5;
const ll maxn=1e9+9;
ll n,i,j,a[1000009],f[1000009],sizet;
struct segtree_ele{
pll line;
ll l,r;
} segtree[30000009];
ll getval(pll line,ll gt) {
return line.fi*gt+line.se;
}
ll getnode(ll &x) {
if (x!=0) {
return x;
}
sizet++;
x=sizet;
segtree[x].l=0;
segtree[x].r=0;
segtree[x].line={0,-oo};
return x;
}
void update(ll id,ll l,ll r,pll line) {
if (line.fi<segtree[id].line.fi) {
swap(segtree[id].line,line);
}
if (line.fi==segtree[id].line.fi) {
segtree[id].line.se=max(segtree[id].line.se,line.se);
return;
}
if (r-l==1) {
return;
}
ll mid=(r+l)/2;
if (getval(line,l)>getval(segtree[id].line,l)) {
swap(line,segtree[id].line);
update(getnode(segtree[id].l),l,mid,line);
}
else {
if (getval(line,mid)>getval(segtree[id].line,mid)) {
swap(segtree[id].line,line);
update(getnode(segtree[id].l),l,mid,line);
}
else {
update(getnode(segtree[id].r),mid,r,line);
}
}
}
ll get(ll id,ll l,ll r,ll gt) {
if (r-l==1) {
return getval(segtree[id].line,gt);
}
ll mid=(r+l)/2;
if (gt<mid) {
return max(getval(segtree[id].line,gt),get(getnode(segtree[id].l),l,mid,gt));
}
else {
return max(getval(segtree[id].line,gt),get(getnode(segtree[id].r),mid,r,gt));
}
}
int main(){
IO;
sizet=0;
getnode(n);
cin>>n;
for (i=1;i<=n;i++) {
cin>>a[i];
a[i]=max(a[i-1],a[i]);
}
update(1,1,maxn,{0,0});
for (i=1;i<=n;i++) {
f[i]=-get(1,1,maxn,a[i])+n*a[i];
update(1,1,maxn,{i,-f[i]});
}
cout<<f[n]<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
416 ms |
939588 KB |
Output is correct |
2 |
Correct |
410 ms |
939632 KB |
Output is correct |
3 |
Correct |
410 ms |
939560 KB |
Output is correct |
4 |
Correct |
411 ms |
939544 KB |
Output is correct |
5 |
Correct |
415 ms |
939588 KB |
Output is correct |
6 |
Correct |
412 ms |
939524 KB |
Output is correct |
7 |
Correct |
411 ms |
939588 KB |
Output is correct |
8 |
Correct |
417 ms |
939572 KB |
Output is correct |
9 |
Correct |
405 ms |
939608 KB |
Output is correct |
10 |
Correct |
415 ms |
939588 KB |
Output is correct |
11 |
Correct |
408 ms |
939656 KB |
Output is correct |
12 |
Correct |
411 ms |
939600 KB |
Output is correct |
13 |
Correct |
414 ms |
939704 KB |
Output is correct |
14 |
Correct |
410 ms |
939640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
410 ms |
939684 KB |
Output is correct |
2 |
Correct |
408 ms |
939648 KB |
Output is correct |
3 |
Correct |
417 ms |
939592 KB |
Output is correct |
4 |
Correct |
417 ms |
939568 KB |
Output is correct |
5 |
Correct |
439 ms |
939672 KB |
Output is correct |
6 |
Correct |
414 ms |
939600 KB |
Output is correct |
7 |
Correct |
413 ms |
939728 KB |
Output is correct |
8 |
Correct |
414 ms |
939576 KB |
Output is correct |
9 |
Correct |
415 ms |
939568 KB |
Output is correct |
10 |
Correct |
459 ms |
939776 KB |
Output is correct |
11 |
Correct |
415 ms |
939588 KB |
Output is correct |
12 |
Correct |
411 ms |
939720 KB |
Output is correct |
13 |
Correct |
404 ms |
939668 KB |
Output is correct |
14 |
Correct |
411 ms |
939584 KB |
Output is correct |
15 |
Correct |
410 ms |
939732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
410 ms |
939684 KB |
Output is correct |
2 |
Correct |
408 ms |
939648 KB |
Output is correct |
3 |
Correct |
417 ms |
939592 KB |
Output is correct |
4 |
Correct |
417 ms |
939568 KB |
Output is correct |
5 |
Correct |
439 ms |
939672 KB |
Output is correct |
6 |
Correct |
414 ms |
939600 KB |
Output is correct |
7 |
Correct |
413 ms |
939728 KB |
Output is correct |
8 |
Correct |
414 ms |
939576 KB |
Output is correct |
9 |
Correct |
415 ms |
939568 KB |
Output is correct |
10 |
Correct |
459 ms |
939776 KB |
Output is correct |
11 |
Correct |
415 ms |
939588 KB |
Output is correct |
12 |
Correct |
411 ms |
939720 KB |
Output is correct |
13 |
Correct |
404 ms |
939668 KB |
Output is correct |
14 |
Correct |
411 ms |
939584 KB |
Output is correct |
15 |
Correct |
410 ms |
939732 KB |
Output is correct |
16 |
Correct |
887 ms |
958980 KB |
Output is correct |
17 |
Correct |
944 ms |
961156 KB |
Output is correct |
18 |
Correct |
845 ms |
959684 KB |
Output is correct |
19 |
Correct |
903 ms |
961096 KB |
Output is correct |
20 |
Correct |
898 ms |
960900 KB |
Output is correct |
21 |
Correct |
931 ms |
960980 KB |
Output is correct |
22 |
Correct |
905 ms |
960716 KB |
Output is correct |
23 |
Correct |
956 ms |
961656 KB |
Output is correct |
24 |
Correct |
937 ms |
961076 KB |
Output is correct |
25 |
Correct |
948 ms |
961568 KB |
Output is correct |
26 |
Correct |
953 ms |
961968 KB |
Output is correct |
27 |
Correct |
898 ms |
960288 KB |
Output is correct |
28 |
Correct |
897 ms |
961172 KB |
Output is correct |
29 |
Correct |
920 ms |
961380 KB |
Output is correct |
30 |
Correct |
939 ms |
961580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
832 ms |
959240 KB |
Output is correct |
2 |
Correct |
830 ms |
964888 KB |
Output is correct |
3 |
Correct |
843 ms |
964868 KB |
Output is correct |
4 |
Correct |
846 ms |
964928 KB |
Output is correct |
5 |
Correct |
850 ms |
965060 KB |
Output is correct |
6 |
Correct |
840 ms |
965000 KB |
Output is correct |
7 |
Correct |
832 ms |
965156 KB |
Output is correct |
8 |
Correct |
886 ms |
964940 KB |
Output is correct |
9 |
Correct |
835 ms |
965096 KB |
Output is correct |
10 |
Correct |
862 ms |
964960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
416 ms |
939588 KB |
Output is correct |
2 |
Correct |
410 ms |
939632 KB |
Output is correct |
3 |
Correct |
410 ms |
939560 KB |
Output is correct |
4 |
Correct |
411 ms |
939544 KB |
Output is correct |
5 |
Correct |
415 ms |
939588 KB |
Output is correct |
6 |
Correct |
412 ms |
939524 KB |
Output is correct |
7 |
Correct |
411 ms |
939588 KB |
Output is correct |
8 |
Correct |
417 ms |
939572 KB |
Output is correct |
9 |
Correct |
405 ms |
939608 KB |
Output is correct |
10 |
Correct |
415 ms |
939588 KB |
Output is correct |
11 |
Correct |
408 ms |
939656 KB |
Output is correct |
12 |
Correct |
411 ms |
939600 KB |
Output is correct |
13 |
Correct |
414 ms |
939704 KB |
Output is correct |
14 |
Correct |
410 ms |
939640 KB |
Output is correct |
15 |
Correct |
410 ms |
939684 KB |
Output is correct |
16 |
Correct |
408 ms |
939648 KB |
Output is correct |
17 |
Correct |
417 ms |
939592 KB |
Output is correct |
18 |
Correct |
417 ms |
939568 KB |
Output is correct |
19 |
Correct |
439 ms |
939672 KB |
Output is correct |
20 |
Correct |
414 ms |
939600 KB |
Output is correct |
21 |
Correct |
413 ms |
939728 KB |
Output is correct |
22 |
Correct |
414 ms |
939576 KB |
Output is correct |
23 |
Correct |
415 ms |
939568 KB |
Output is correct |
24 |
Correct |
459 ms |
939776 KB |
Output is correct |
25 |
Correct |
415 ms |
939588 KB |
Output is correct |
26 |
Correct |
411 ms |
939720 KB |
Output is correct |
27 |
Correct |
404 ms |
939668 KB |
Output is correct |
28 |
Correct |
411 ms |
939584 KB |
Output is correct |
29 |
Correct |
410 ms |
939732 KB |
Output is correct |
30 |
Correct |
440 ms |
939728 KB |
Output is correct |
31 |
Correct |
410 ms |
939616 KB |
Output is correct |
32 |
Correct |
429 ms |
939648 KB |
Output is correct |
33 |
Correct |
414 ms |
939680 KB |
Output is correct |
34 |
Correct |
431 ms |
939564 KB |
Output is correct |
35 |
Correct |
421 ms |
939608 KB |
Output is correct |
36 |
Correct |
418 ms |
939592 KB |
Output is correct |
37 |
Correct |
420 ms |
939588 KB |
Output is correct |
38 |
Correct |
423 ms |
939716 KB |
Output is correct |
39 |
Correct |
423 ms |
939608 KB |
Output is correct |
40 |
Correct |
413 ms |
939648 KB |
Output is correct |
41 |
Correct |
433 ms |
939720 KB |
Output is correct |
42 |
Correct |
422 ms |
939604 KB |
Output is correct |
43 |
Correct |
415 ms |
939572 KB |
Output is correct |
44 |
Correct |
415 ms |
939652 KB |
Output is correct |
45 |
Correct |
424 ms |
939784 KB |
Output is correct |
46 |
Correct |
428 ms |
939588 KB |
Output is correct |
47 |
Correct |
417 ms |
939620 KB |
Output is correct |
48 |
Correct |
417 ms |
939588 KB |
Output is correct |
49 |
Correct |
418 ms |
939592 KB |
Output is correct |
50 |
Correct |
417 ms |
939596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
416 ms |
939588 KB |
Output is correct |
2 |
Correct |
410 ms |
939632 KB |
Output is correct |
3 |
Correct |
410 ms |
939560 KB |
Output is correct |
4 |
Correct |
411 ms |
939544 KB |
Output is correct |
5 |
Correct |
415 ms |
939588 KB |
Output is correct |
6 |
Correct |
412 ms |
939524 KB |
Output is correct |
7 |
Correct |
411 ms |
939588 KB |
Output is correct |
8 |
Correct |
417 ms |
939572 KB |
Output is correct |
9 |
Correct |
405 ms |
939608 KB |
Output is correct |
10 |
Correct |
415 ms |
939588 KB |
Output is correct |
11 |
Correct |
408 ms |
939656 KB |
Output is correct |
12 |
Correct |
411 ms |
939600 KB |
Output is correct |
13 |
Correct |
414 ms |
939704 KB |
Output is correct |
14 |
Correct |
410 ms |
939640 KB |
Output is correct |
15 |
Correct |
410 ms |
939684 KB |
Output is correct |
16 |
Correct |
408 ms |
939648 KB |
Output is correct |
17 |
Correct |
417 ms |
939592 KB |
Output is correct |
18 |
Correct |
417 ms |
939568 KB |
Output is correct |
19 |
Correct |
439 ms |
939672 KB |
Output is correct |
20 |
Correct |
414 ms |
939600 KB |
Output is correct |
21 |
Correct |
413 ms |
939728 KB |
Output is correct |
22 |
Correct |
414 ms |
939576 KB |
Output is correct |
23 |
Correct |
415 ms |
939568 KB |
Output is correct |
24 |
Correct |
459 ms |
939776 KB |
Output is correct |
25 |
Correct |
415 ms |
939588 KB |
Output is correct |
26 |
Correct |
411 ms |
939720 KB |
Output is correct |
27 |
Correct |
404 ms |
939668 KB |
Output is correct |
28 |
Correct |
411 ms |
939584 KB |
Output is correct |
29 |
Correct |
410 ms |
939732 KB |
Output is correct |
30 |
Correct |
887 ms |
958980 KB |
Output is correct |
31 |
Correct |
944 ms |
961156 KB |
Output is correct |
32 |
Correct |
845 ms |
959684 KB |
Output is correct |
33 |
Correct |
903 ms |
961096 KB |
Output is correct |
34 |
Correct |
898 ms |
960900 KB |
Output is correct |
35 |
Correct |
931 ms |
960980 KB |
Output is correct |
36 |
Correct |
905 ms |
960716 KB |
Output is correct |
37 |
Correct |
956 ms |
961656 KB |
Output is correct |
38 |
Correct |
937 ms |
961076 KB |
Output is correct |
39 |
Correct |
948 ms |
961568 KB |
Output is correct |
40 |
Correct |
953 ms |
961968 KB |
Output is correct |
41 |
Correct |
898 ms |
960288 KB |
Output is correct |
42 |
Correct |
897 ms |
961172 KB |
Output is correct |
43 |
Correct |
920 ms |
961380 KB |
Output is correct |
44 |
Correct |
939 ms |
961580 KB |
Output is correct |
45 |
Correct |
832 ms |
959240 KB |
Output is correct |
46 |
Correct |
830 ms |
964888 KB |
Output is correct |
47 |
Correct |
843 ms |
964868 KB |
Output is correct |
48 |
Correct |
846 ms |
964928 KB |
Output is correct |
49 |
Correct |
850 ms |
965060 KB |
Output is correct |
50 |
Correct |
840 ms |
965000 KB |
Output is correct |
51 |
Correct |
832 ms |
965156 KB |
Output is correct |
52 |
Correct |
886 ms |
964940 KB |
Output is correct |
53 |
Correct |
835 ms |
965096 KB |
Output is correct |
54 |
Correct |
862 ms |
964960 KB |
Output is correct |
55 |
Correct |
440 ms |
939728 KB |
Output is correct |
56 |
Correct |
410 ms |
939616 KB |
Output is correct |
57 |
Correct |
429 ms |
939648 KB |
Output is correct |
58 |
Correct |
414 ms |
939680 KB |
Output is correct |
59 |
Correct |
431 ms |
939564 KB |
Output is correct |
60 |
Correct |
421 ms |
939608 KB |
Output is correct |
61 |
Correct |
418 ms |
939592 KB |
Output is correct |
62 |
Correct |
420 ms |
939588 KB |
Output is correct |
63 |
Correct |
423 ms |
939716 KB |
Output is correct |
64 |
Correct |
423 ms |
939608 KB |
Output is correct |
65 |
Correct |
413 ms |
939648 KB |
Output is correct |
66 |
Correct |
433 ms |
939720 KB |
Output is correct |
67 |
Correct |
422 ms |
939604 KB |
Output is correct |
68 |
Correct |
415 ms |
939572 KB |
Output is correct |
69 |
Correct |
415 ms |
939652 KB |
Output is correct |
70 |
Correct |
424 ms |
939784 KB |
Output is correct |
71 |
Correct |
428 ms |
939588 KB |
Output is correct |
72 |
Correct |
417 ms |
939620 KB |
Output is correct |
73 |
Correct |
417 ms |
939588 KB |
Output is correct |
74 |
Correct |
418 ms |
939592 KB |
Output is correct |
75 |
Correct |
417 ms |
939596 KB |
Output is correct |
76 |
Correct |
855 ms |
961304 KB |
Output is correct |
77 |
Correct |
854 ms |
961268 KB |
Output is correct |
78 |
Correct |
834 ms |
960708 KB |
Output is correct |
79 |
Correct |
822 ms |
960944 KB |
Output is correct |
80 |
Correct |
836 ms |
961560 KB |
Output is correct |
81 |
Correct |
857 ms |
960768 KB |
Output is correct |
82 |
Correct |
834 ms |
960728 KB |
Output is correct |
83 |
Correct |
837 ms |
961356 KB |
Output is correct |
84 |
Correct |
818 ms |
961096 KB |
Output is correct |
85 |
Correct |
841 ms |
961252 KB |
Output is correct |
86 |
Correct |
830 ms |
960664 KB |
Output is correct |
87 |
Correct |
836 ms |
960600 KB |
Output is correct |
88 |
Correct |
834 ms |
961024 KB |
Output is correct |
89 |
Correct |
820 ms |
960928 KB |
Output is correct |
90 |
Correct |
832 ms |
960652 KB |
Output is correct |
91 |
Correct |
829 ms |
960548 KB |
Output is correct |
92 |
Correct |
950 ms |
961740 KB |
Output is correct |
93 |
Correct |
972 ms |
960980 KB |
Output is correct |
94 |
Correct |
845 ms |
960208 KB |
Output is correct |
95 |
Correct |
832 ms |
961568 KB |
Output is correct |
96 |
Correct |
818 ms |
960476 KB |
Output is correct |
97 |
Correct |
874 ms |
960904 KB |
Output is correct |
98 |
Correct |
837 ms |
961092 KB |
Output is correct |
99 |
Correct |
821 ms |
960712 KB |
Output is correct |
100 |
Correct |
841 ms |
960840 KB |
Output is correct |