#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define int long long
#define ll long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<ll,ll> pii;
typedef pair<ll,pii> ipii;
const int MAXN = 1e6+10;
const ll INF = 1e18+10;
const int MOD = 998244353;
const int LOG = 20;
const int ADD = 1e10;
int n, x;
int a[MAXN];
vector <pii> vec;
int dp[MAXN], cnt[MAXN], val[MAXN];
int pr[MAXN];
struct Line {
int m, c;
int getY(int x){
return m*x+c;
}
pii cut(Line oth){
return pii(oth.c-c, m-oth.m);
}
};
vector <Line> ch;
bool cek(Line a, Line b, Line c){
int A = b.cut(a).fi, B = b.cut(a).se;
int C = c.cut(b).fi, D = c.cut(b).se;
// A/B < C/D
if(A/B == C/D){
return (A%B)*D > (C%D)*B;
}
return A/B > C/D;
}
void add(Line nw){
while(ch.size()>=2 &&
!cek(ch[ch.size()-2], ch.back(), nw) ) ch.pop_back();
ch.pb(nw);
// for(auto in : ch) cout << in.m << ' ' << in.c << " mc\n";
// cout << "done\n";
}
int que(int x){
if(ch.size() == 1) return ch[0].getY(x);
int l=1, r=ch.size()-1, mid=0, cnt=INF;
while(r-l>=5){
mid = md;
int le = ch[mid-1].getY(x), ri = ch[mid].getY(x);
cnt = min(cnt, min(le, ri));
if(le > ri) l = mid+2;
else r = mid-2;
}
for(int i=max(0ll,l-1); i<=min(r+1,(int)ch.size()-1); i++)
cnt = min(cnt, ch[i].getY(x));
return cnt;
}
signed main(){
cin >> n;
for(int i=1; i<=n; i++) cin >> a[i];
vec.pb({-1,-1});
for(int i=1; i<=n; i++){
int te = a[i], id = i;
while(i+1<=n && te>=a[i+1]) i++;
vec.pb({te, i-id+1});
}
n = vec.size()-1;
for(int i=1; i<=n; i++){
val[i] = vec[i].fi, cnt[i] = vec[i].se;
pr[i] = pr[i-1] + cnt[i];
}
for(int i=1; i<=n; i++) dp[i] = INF;
Line nw; nw.m = -val[n]; nw.c = val[n]*pr[n];
add(nw);
for(int i=n; i>=1; i--){
dp[i] = que(pr[i-1]);
Line nw; nw.m = -val[i-1]; nw.c = dp[i] + val[i-1]*pr[n];
add(nw);
}
// for(int i=n; i>=1; i--){
// for(int j=i; j<=n; j++){
// dp[i] = min(dp[i], dp[j+1] + val[j]*pr[n] - val[j]*pr[i-1]);
// }
// }
cout << dp[1] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
856 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
392 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
856 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
392 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
179 ms |
31932 KB |
Output is correct |
17 |
Correct |
199 ms |
35260 KB |
Output is correct |
18 |
Correct |
155 ms |
26036 KB |
Output is correct |
19 |
Correct |
197 ms |
34868 KB |
Output is correct |
20 |
Correct |
192 ms |
37048 KB |
Output is correct |
21 |
Correct |
195 ms |
35260 KB |
Output is correct |
22 |
Correct |
181 ms |
32736 KB |
Output is correct |
23 |
Correct |
220 ms |
42936 KB |
Output is correct |
24 |
Correct |
200 ms |
39196 KB |
Output is correct |
25 |
Correct |
211 ms |
39596 KB |
Output is correct |
26 |
Correct |
226 ms |
45324 KB |
Output is correct |
27 |
Correct |
185 ms |
29876 KB |
Output is correct |
28 |
Correct |
198 ms |
36024 KB |
Output is correct |
29 |
Correct |
209 ms |
40088 KB |
Output is correct |
30 |
Correct |
243 ms |
43952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
256 ms |
17748 KB |
Output is correct |
2 |
Correct |
254 ms |
17768 KB |
Output is correct |
3 |
Correct |
272 ms |
17936 KB |
Output is correct |
4 |
Correct |
245 ms |
17748 KB |
Output is correct |
5 |
Correct |
242 ms |
17748 KB |
Output is correct |
6 |
Correct |
254 ms |
17908 KB |
Output is correct |
7 |
Correct |
267 ms |
17952 KB |
Output is correct |
8 |
Correct |
269 ms |
17840 KB |
Output is correct |
9 |
Correct |
260 ms |
17712 KB |
Output is correct |
10 |
Correct |
259 ms |
17744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
856 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
392 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
452 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
856 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
392 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
179 ms |
31932 KB |
Output is correct |
31 |
Correct |
199 ms |
35260 KB |
Output is correct |
32 |
Correct |
155 ms |
26036 KB |
Output is correct |
33 |
Correct |
197 ms |
34868 KB |
Output is correct |
34 |
Correct |
192 ms |
37048 KB |
Output is correct |
35 |
Correct |
195 ms |
35260 KB |
Output is correct |
36 |
Correct |
181 ms |
32736 KB |
Output is correct |
37 |
Correct |
220 ms |
42936 KB |
Output is correct |
38 |
Correct |
200 ms |
39196 KB |
Output is correct |
39 |
Correct |
211 ms |
39596 KB |
Output is correct |
40 |
Correct |
226 ms |
45324 KB |
Output is correct |
41 |
Correct |
185 ms |
29876 KB |
Output is correct |
42 |
Correct |
198 ms |
36024 KB |
Output is correct |
43 |
Correct |
209 ms |
40088 KB |
Output is correct |
44 |
Correct |
243 ms |
43952 KB |
Output is correct |
45 |
Correct |
256 ms |
17748 KB |
Output is correct |
46 |
Correct |
254 ms |
17768 KB |
Output is correct |
47 |
Correct |
272 ms |
17936 KB |
Output is correct |
48 |
Correct |
245 ms |
17748 KB |
Output is correct |
49 |
Correct |
242 ms |
17748 KB |
Output is correct |
50 |
Correct |
254 ms |
17908 KB |
Output is correct |
51 |
Correct |
267 ms |
17952 KB |
Output is correct |
52 |
Correct |
269 ms |
17840 KB |
Output is correct |
53 |
Correct |
260 ms |
17712 KB |
Output is correct |
54 |
Correct |
259 ms |
17744 KB |
Output is correct |
55 |
Correct |
1 ms |
348 KB |
Output is correct |
56 |
Correct |
1 ms |
452 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
1 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
65 |
Correct |
1 ms |
348 KB |
Output is correct |
66 |
Correct |
1 ms |
344 KB |
Output is correct |
67 |
Correct |
1 ms |
348 KB |
Output is correct |
68 |
Correct |
1 ms |
348 KB |
Output is correct |
69 |
Correct |
1 ms |
348 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
1 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
1 ms |
348 KB |
Output is correct |
74 |
Correct |
1 ms |
348 KB |
Output is correct |
75 |
Correct |
1 ms |
348 KB |
Output is correct |
76 |
Correct |
170 ms |
14164 KB |
Output is correct |
77 |
Correct |
177 ms |
14312 KB |
Output is correct |
78 |
Correct |
154 ms |
13612 KB |
Output is correct |
79 |
Correct |
163 ms |
13772 KB |
Output is correct |
80 |
Correct |
177 ms |
14384 KB |
Output is correct |
81 |
Correct |
179 ms |
13604 KB |
Output is correct |
82 |
Correct |
182 ms |
13648 KB |
Output is correct |
83 |
Correct |
167 ms |
14160 KB |
Output is correct |
84 |
Correct |
175 ms |
13956 KB |
Output is correct |
85 |
Correct |
167 ms |
14200 KB |
Output is correct |
86 |
Correct |
158 ms |
13544 KB |
Output is correct |
87 |
Correct |
175 ms |
13396 KB |
Output is correct |
88 |
Correct |
162 ms |
13908 KB |
Output is correct |
89 |
Correct |
176 ms |
13764 KB |
Output is correct |
90 |
Correct |
167 ms |
13648 KB |
Output is correct |
91 |
Correct |
159 ms |
13500 KB |
Output is correct |
92 |
Correct |
209 ms |
39356 KB |
Output is correct |
93 |
Correct |
191 ms |
35652 KB |
Output is correct |
94 |
Correct |
171 ms |
13064 KB |
Output is correct |
95 |
Correct |
178 ms |
14508 KB |
Output is correct |
96 |
Correct |
155 ms |
13392 KB |
Output is correct |
97 |
Correct |
167 ms |
13652 KB |
Output is correct |
98 |
Correct |
167 ms |
14016 KB |
Output is correct |
99 |
Correct |
178 ms |
13648 KB |
Output is correct |
100 |
Correct |
160 ms |
13652 KB |
Output is correct |