#include <bits/stdc++.h>
using namespace std;
const int N = 1000100;
const long long inf = (long long) 1e18;
vector<int> xs;
struct Line {
long long k;
long long n;
Line(long long _k = 0, long long _n = inf) : k(_k), n(_n) {}
long long val(long long x) {
return k * x + n;
}
} seg[8 * N];
void Modify(int id, int l, int r, Line ln) {
int mid = (l + r) >> 1;
if (ln.val(xs[mid]) < seg[id].val(xs[mid])) {
swap(ln, seg[id]);
}
if (l == r) {
return;
}
if (ln.val(xs[l]) < seg[id].val(xs[l])) {
Modify(id * 2, l, mid, ln);
} else {
Modify(id * 2 + 1, mid + 1, r, ln);
}
}
long long Query(int id, int l, int r, int p) {
if (l == r) {
return seg[id].val(xs[p]);
}
int mid = (l + r) >> 1;
if (p <= mid) {
return min(seg[id].val(xs[p]), Query(id * 2, l, mid, p));
} else {
return min(seg[id].val(xs[p]), Query(id * 2 + 1, mid + 1, r, p));
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
xs.push_back(a[i]);
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
int k = (int) xs.size();
vector<long long> dp(n, inf);
int ptr = 0;
int idx = 0;
for (int i = 0; i < n; i++) {
if (a[i] >= a[idx]) {
idx = i;
}
while (ptr <= idx) {
Modify(1, 0, k - 1, Line(n - ptr, (ptr == 0 ? 0LL : dp[ptr - 1])));
ptr += 1;
}
int p = (int) (lower_bound(xs.begin(), xs.end(), a[idx]) - xs.begin());
dp[i] = Query(1, 0, k - 1, p);
}
cout << dp[n - 1] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
125524 KB |
Output is correct |
2 |
Correct |
15 ms |
125504 KB |
Output is correct |
3 |
Correct |
15 ms |
125532 KB |
Output is correct |
4 |
Correct |
14 ms |
125532 KB |
Output is correct |
5 |
Correct |
16 ms |
125664 KB |
Output is correct |
6 |
Correct |
16 ms |
125532 KB |
Output is correct |
7 |
Correct |
14 ms |
125680 KB |
Output is correct |
8 |
Correct |
15 ms |
125532 KB |
Output is correct |
9 |
Correct |
15 ms |
125532 KB |
Output is correct |
10 |
Correct |
14 ms |
125532 KB |
Output is correct |
11 |
Correct |
14 ms |
125580 KB |
Output is correct |
12 |
Correct |
15 ms |
125532 KB |
Output is correct |
13 |
Correct |
15 ms |
125532 KB |
Output is correct |
14 |
Correct |
16 ms |
125608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
125532 KB |
Output is correct |
2 |
Correct |
16 ms |
125532 KB |
Output is correct |
3 |
Correct |
15 ms |
125640 KB |
Output is correct |
4 |
Correct |
15 ms |
125532 KB |
Output is correct |
5 |
Correct |
16 ms |
125532 KB |
Output is correct |
6 |
Correct |
16 ms |
125532 KB |
Output is correct |
7 |
Correct |
18 ms |
125532 KB |
Output is correct |
8 |
Correct |
15 ms |
125532 KB |
Output is correct |
9 |
Correct |
15 ms |
125532 KB |
Output is correct |
10 |
Correct |
15 ms |
125532 KB |
Output is correct |
11 |
Correct |
18 ms |
125532 KB |
Output is correct |
12 |
Correct |
17 ms |
125664 KB |
Output is correct |
13 |
Correct |
16 ms |
125532 KB |
Output is correct |
14 |
Correct |
18 ms |
125532 KB |
Output is correct |
15 |
Correct |
15 ms |
125532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
125532 KB |
Output is correct |
2 |
Correct |
16 ms |
125532 KB |
Output is correct |
3 |
Correct |
15 ms |
125640 KB |
Output is correct |
4 |
Correct |
15 ms |
125532 KB |
Output is correct |
5 |
Correct |
16 ms |
125532 KB |
Output is correct |
6 |
Correct |
16 ms |
125532 KB |
Output is correct |
7 |
Correct |
18 ms |
125532 KB |
Output is correct |
8 |
Correct |
15 ms |
125532 KB |
Output is correct |
9 |
Correct |
15 ms |
125532 KB |
Output is correct |
10 |
Correct |
15 ms |
125532 KB |
Output is correct |
11 |
Correct |
18 ms |
125532 KB |
Output is correct |
12 |
Correct |
17 ms |
125664 KB |
Output is correct |
13 |
Correct |
16 ms |
125532 KB |
Output is correct |
14 |
Correct |
18 ms |
125532 KB |
Output is correct |
15 |
Correct |
15 ms |
125532 KB |
Output is correct |
16 |
Correct |
240 ms |
146496 KB |
Output is correct |
17 |
Correct |
241 ms |
147396 KB |
Output is correct |
18 |
Correct |
235 ms |
145912 KB |
Output is correct |
19 |
Correct |
260 ms |
147144 KB |
Output is correct |
20 |
Correct |
251 ms |
147144 KB |
Output is correct |
21 |
Correct |
271 ms |
147144 KB |
Output is correct |
22 |
Correct |
239 ms |
146768 KB |
Output is correct |
23 |
Correct |
321 ms |
147912 KB |
Output is correct |
24 |
Correct |
257 ms |
147144 KB |
Output is correct |
25 |
Correct |
229 ms |
147656 KB |
Output is correct |
26 |
Correct |
233 ms |
147904 KB |
Output is correct |
27 |
Correct |
252 ms |
146380 KB |
Output is correct |
28 |
Correct |
225 ms |
147264 KB |
Output is correct |
29 |
Correct |
258 ms |
147780 KB |
Output is correct |
30 |
Correct |
254 ms |
147656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
176 ms |
150984 KB |
Output is correct |
2 |
Correct |
180 ms |
150976 KB |
Output is correct |
3 |
Correct |
179 ms |
150988 KB |
Output is correct |
4 |
Correct |
199 ms |
150972 KB |
Output is correct |
5 |
Correct |
176 ms |
150984 KB |
Output is correct |
6 |
Correct |
163 ms |
151188 KB |
Output is correct |
7 |
Correct |
161 ms |
150984 KB |
Output is correct |
8 |
Correct |
166 ms |
151232 KB |
Output is correct |
9 |
Correct |
179 ms |
150988 KB |
Output is correct |
10 |
Correct |
168 ms |
151084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
125524 KB |
Output is correct |
2 |
Correct |
15 ms |
125504 KB |
Output is correct |
3 |
Correct |
15 ms |
125532 KB |
Output is correct |
4 |
Correct |
14 ms |
125532 KB |
Output is correct |
5 |
Correct |
16 ms |
125664 KB |
Output is correct |
6 |
Correct |
16 ms |
125532 KB |
Output is correct |
7 |
Correct |
14 ms |
125680 KB |
Output is correct |
8 |
Correct |
15 ms |
125532 KB |
Output is correct |
9 |
Correct |
15 ms |
125532 KB |
Output is correct |
10 |
Correct |
14 ms |
125532 KB |
Output is correct |
11 |
Correct |
14 ms |
125580 KB |
Output is correct |
12 |
Correct |
15 ms |
125532 KB |
Output is correct |
13 |
Correct |
15 ms |
125532 KB |
Output is correct |
14 |
Correct |
16 ms |
125608 KB |
Output is correct |
15 |
Correct |
16 ms |
125532 KB |
Output is correct |
16 |
Correct |
16 ms |
125532 KB |
Output is correct |
17 |
Correct |
15 ms |
125640 KB |
Output is correct |
18 |
Correct |
15 ms |
125532 KB |
Output is correct |
19 |
Correct |
16 ms |
125532 KB |
Output is correct |
20 |
Correct |
16 ms |
125532 KB |
Output is correct |
21 |
Correct |
18 ms |
125532 KB |
Output is correct |
22 |
Correct |
15 ms |
125532 KB |
Output is correct |
23 |
Correct |
15 ms |
125532 KB |
Output is correct |
24 |
Correct |
15 ms |
125532 KB |
Output is correct |
25 |
Correct |
18 ms |
125532 KB |
Output is correct |
26 |
Correct |
17 ms |
125664 KB |
Output is correct |
27 |
Correct |
16 ms |
125532 KB |
Output is correct |
28 |
Correct |
18 ms |
125532 KB |
Output is correct |
29 |
Correct |
15 ms |
125532 KB |
Output is correct |
30 |
Correct |
16 ms |
125532 KB |
Output is correct |
31 |
Correct |
14 ms |
125672 KB |
Output is correct |
32 |
Correct |
15 ms |
125532 KB |
Output is correct |
33 |
Correct |
14 ms |
125748 KB |
Output is correct |
34 |
Correct |
16 ms |
125532 KB |
Output is correct |
35 |
Correct |
14 ms |
125532 KB |
Output is correct |
36 |
Correct |
14 ms |
125532 KB |
Output is correct |
37 |
Correct |
15 ms |
125708 KB |
Output is correct |
38 |
Correct |
14 ms |
125532 KB |
Output is correct |
39 |
Correct |
15 ms |
125648 KB |
Output is correct |
40 |
Correct |
15 ms |
125532 KB |
Output is correct |
41 |
Correct |
15 ms |
125624 KB |
Output is correct |
42 |
Correct |
14 ms |
125532 KB |
Output is correct |
43 |
Correct |
18 ms |
125532 KB |
Output is correct |
44 |
Correct |
16 ms |
125648 KB |
Output is correct |
45 |
Correct |
15 ms |
125720 KB |
Output is correct |
46 |
Correct |
14 ms |
125532 KB |
Output is correct |
47 |
Correct |
15 ms |
125532 KB |
Output is correct |
48 |
Correct |
15 ms |
125532 KB |
Output is correct |
49 |
Correct |
15 ms |
125608 KB |
Output is correct |
50 |
Correct |
14 ms |
125532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
125524 KB |
Output is correct |
2 |
Correct |
15 ms |
125504 KB |
Output is correct |
3 |
Correct |
15 ms |
125532 KB |
Output is correct |
4 |
Correct |
14 ms |
125532 KB |
Output is correct |
5 |
Correct |
16 ms |
125664 KB |
Output is correct |
6 |
Correct |
16 ms |
125532 KB |
Output is correct |
7 |
Correct |
14 ms |
125680 KB |
Output is correct |
8 |
Correct |
15 ms |
125532 KB |
Output is correct |
9 |
Correct |
15 ms |
125532 KB |
Output is correct |
10 |
Correct |
14 ms |
125532 KB |
Output is correct |
11 |
Correct |
14 ms |
125580 KB |
Output is correct |
12 |
Correct |
15 ms |
125532 KB |
Output is correct |
13 |
Correct |
15 ms |
125532 KB |
Output is correct |
14 |
Correct |
16 ms |
125608 KB |
Output is correct |
15 |
Correct |
16 ms |
125532 KB |
Output is correct |
16 |
Correct |
16 ms |
125532 KB |
Output is correct |
17 |
Correct |
15 ms |
125640 KB |
Output is correct |
18 |
Correct |
15 ms |
125532 KB |
Output is correct |
19 |
Correct |
16 ms |
125532 KB |
Output is correct |
20 |
Correct |
16 ms |
125532 KB |
Output is correct |
21 |
Correct |
18 ms |
125532 KB |
Output is correct |
22 |
Correct |
15 ms |
125532 KB |
Output is correct |
23 |
Correct |
15 ms |
125532 KB |
Output is correct |
24 |
Correct |
15 ms |
125532 KB |
Output is correct |
25 |
Correct |
18 ms |
125532 KB |
Output is correct |
26 |
Correct |
17 ms |
125664 KB |
Output is correct |
27 |
Correct |
16 ms |
125532 KB |
Output is correct |
28 |
Correct |
18 ms |
125532 KB |
Output is correct |
29 |
Correct |
15 ms |
125532 KB |
Output is correct |
30 |
Correct |
240 ms |
146496 KB |
Output is correct |
31 |
Correct |
241 ms |
147396 KB |
Output is correct |
32 |
Correct |
235 ms |
145912 KB |
Output is correct |
33 |
Correct |
260 ms |
147144 KB |
Output is correct |
34 |
Correct |
251 ms |
147144 KB |
Output is correct |
35 |
Correct |
271 ms |
147144 KB |
Output is correct |
36 |
Correct |
239 ms |
146768 KB |
Output is correct |
37 |
Correct |
321 ms |
147912 KB |
Output is correct |
38 |
Correct |
257 ms |
147144 KB |
Output is correct |
39 |
Correct |
229 ms |
147656 KB |
Output is correct |
40 |
Correct |
233 ms |
147904 KB |
Output is correct |
41 |
Correct |
252 ms |
146380 KB |
Output is correct |
42 |
Correct |
225 ms |
147264 KB |
Output is correct |
43 |
Correct |
258 ms |
147780 KB |
Output is correct |
44 |
Correct |
254 ms |
147656 KB |
Output is correct |
45 |
Correct |
176 ms |
150984 KB |
Output is correct |
46 |
Correct |
180 ms |
150976 KB |
Output is correct |
47 |
Correct |
179 ms |
150988 KB |
Output is correct |
48 |
Correct |
199 ms |
150972 KB |
Output is correct |
49 |
Correct |
176 ms |
150984 KB |
Output is correct |
50 |
Correct |
163 ms |
151188 KB |
Output is correct |
51 |
Correct |
161 ms |
150984 KB |
Output is correct |
52 |
Correct |
166 ms |
151232 KB |
Output is correct |
53 |
Correct |
179 ms |
150988 KB |
Output is correct |
54 |
Correct |
168 ms |
151084 KB |
Output is correct |
55 |
Correct |
16 ms |
125532 KB |
Output is correct |
56 |
Correct |
14 ms |
125672 KB |
Output is correct |
57 |
Correct |
15 ms |
125532 KB |
Output is correct |
58 |
Correct |
14 ms |
125748 KB |
Output is correct |
59 |
Correct |
16 ms |
125532 KB |
Output is correct |
60 |
Correct |
14 ms |
125532 KB |
Output is correct |
61 |
Correct |
14 ms |
125532 KB |
Output is correct |
62 |
Correct |
15 ms |
125708 KB |
Output is correct |
63 |
Correct |
14 ms |
125532 KB |
Output is correct |
64 |
Correct |
15 ms |
125648 KB |
Output is correct |
65 |
Correct |
15 ms |
125532 KB |
Output is correct |
66 |
Correct |
15 ms |
125624 KB |
Output is correct |
67 |
Correct |
14 ms |
125532 KB |
Output is correct |
68 |
Correct |
18 ms |
125532 KB |
Output is correct |
69 |
Correct |
16 ms |
125648 KB |
Output is correct |
70 |
Correct |
15 ms |
125720 KB |
Output is correct |
71 |
Correct |
14 ms |
125532 KB |
Output is correct |
72 |
Correct |
15 ms |
125532 KB |
Output is correct |
73 |
Correct |
15 ms |
125532 KB |
Output is correct |
74 |
Correct |
15 ms |
125608 KB |
Output is correct |
75 |
Correct |
14 ms |
125532 KB |
Output is correct |
76 |
Correct |
244 ms |
147396 KB |
Output is correct |
77 |
Correct |
259 ms |
147396 KB |
Output is correct |
78 |
Correct |
238 ms |
146884 KB |
Output is correct |
79 |
Correct |
223 ms |
147140 KB |
Output is correct |
80 |
Correct |
242 ms |
147908 KB |
Output is correct |
81 |
Correct |
221 ms |
146884 KB |
Output is correct |
82 |
Correct |
230 ms |
146888 KB |
Output is correct |
83 |
Correct |
255 ms |
147388 KB |
Output is correct |
84 |
Correct |
223 ms |
147140 KB |
Output is correct |
85 |
Correct |
260 ms |
147696 KB |
Output is correct |
86 |
Correct |
251 ms |
146884 KB |
Output is correct |
87 |
Correct |
224 ms |
146632 KB |
Output is correct |
88 |
Correct |
259 ms |
147140 KB |
Output is correct |
89 |
Correct |
240 ms |
146872 KB |
Output is correct |
90 |
Correct |
246 ms |
146880 KB |
Output is correct |
91 |
Correct |
235 ms |
146628 KB |
Output is correct |
92 |
Correct |
260 ms |
147912 KB |
Output is correct |
93 |
Correct |
230 ms |
147144 KB |
Output is correct |
94 |
Correct |
238 ms |
146240 KB |
Output is correct |
95 |
Correct |
275 ms |
147652 KB |
Output is correct |
96 |
Correct |
238 ms |
146628 KB |
Output is correct |
97 |
Correct |
240 ms |
146888 KB |
Output is correct |
98 |
Correct |
246 ms |
147128 KB |
Output is correct |
99 |
Correct |
216 ms |
146884 KB |
Output is correct |
100 |
Correct |
235 ms |
147008 KB |
Output is correct |