/**
* author: Haunted_Cpp
**/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
template<typename T> ostream &operator << (ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream &operator << (ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream &operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif
class CHT {
private:
typedef long long i64;
struct Node {
Node *l, *r;
pair<i64, i64> line;
Node() {
l = r = nullptr;
line = {1e9, 1e9};
}
};
Node *root;
const int LO, HI;
long long f(const pair<i64, i64> line, int x) {
return 1LL * x * line.first + line.second;
}
void add_line(pair<i64, i64> line, int l, int r, Node *seg) {
const int mid = l + (r - l) / 2;
bool esq = f(seg -> line, l) > f(line, l);
bool middle = f(seg -> line, mid) > f(line, mid);
if (middle) {
swap(line, seg -> line);
}
if (l == r) return;
if (esq == middle) {
if (!seg -> r) seg -> r = new Node;
add_line(line, mid + 1, r, seg -> r);
} else {
if (!seg -> l) seg -> l = new Node;
add_line(line, l, mid, seg -> l);
}
}
long long query(int l, int r, int where, Node *seg) {
if (!seg) return 1e18;
if (l == r) return f(seg -> line, where);
const int mid = l + (r - l) / 2;
if (where > mid) {
return min(query(mid + 1, r, where, seg -> r), f(seg -> line, where));
}
return min(query(l, mid, where, seg -> l), f(seg -> line, where));
}
public:
CHT() : LO(0), HI(1e9 + 7) {
root = new Node;
}
void add_line(pair<i64, i64> line) {
add_line(line, LO, HI, root);
}
long long get_best(int where) {
return query(LO, HI, where, root);
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> arr(n + 1);
for (int i = 1; i <= n; i++) cin >> arr[i];
CHT seg;
vector<long long> dp(n + 1);
seg.add_line({0, 0});
int mx = 0;
for (int i = 1; i <= n; i++) {
mx = max(mx, arr[i]);
dp[i] = 1LL * mx * n + seg.get_best(mx);
seg.add_line({1LL * -i, dp[i]});
}
cout << dp[n] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
360 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
276 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
640 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
331 ms |
13688 KB |
Output is correct |
17 |
Correct |
337 ms |
13944 KB |
Output is correct |
18 |
Correct |
340 ms |
13688 KB |
Output is correct |
19 |
Correct |
346 ms |
13568 KB |
Output is correct |
20 |
Correct |
377 ms |
13688 KB |
Output is correct |
21 |
Correct |
335 ms |
13816 KB |
Output is correct |
22 |
Correct |
347 ms |
13688 KB |
Output is correct |
23 |
Correct |
358 ms |
14072 KB |
Output is correct |
24 |
Correct |
333 ms |
13816 KB |
Output is correct |
25 |
Correct |
330 ms |
13944 KB |
Output is correct |
26 |
Correct |
328 ms |
13820 KB |
Output is correct |
27 |
Correct |
334 ms |
13688 KB |
Output is correct |
28 |
Correct |
331 ms |
13832 KB |
Output is correct |
29 |
Correct |
331 ms |
13816 KB |
Output is correct |
30 |
Correct |
334 ms |
13900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
347 ms |
12424 KB |
Output is correct |
2 |
Correct |
357 ms |
21840 KB |
Output is correct |
3 |
Correct |
340 ms |
21752 KB |
Output is correct |
4 |
Correct |
366 ms |
21752 KB |
Output is correct |
5 |
Correct |
336 ms |
21752 KB |
Output is correct |
6 |
Correct |
358 ms |
21880 KB |
Output is correct |
7 |
Correct |
348 ms |
21752 KB |
Output is correct |
8 |
Correct |
343 ms |
21752 KB |
Output is correct |
9 |
Correct |
350 ms |
21752 KB |
Output is correct |
10 |
Correct |
360 ms |
21880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
360 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
276 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
640 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
1 ms |
384 KB |
Output is correct |
31 |
Correct |
1 ms |
384 KB |
Output is correct |
32 |
Correct |
1 ms |
384 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
1 ms |
384 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
1 ms |
416 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
1 ms |
512 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
1 ms |
384 KB |
Output is correct |
50 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
360 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
276 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
640 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
1 ms |
512 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
331 ms |
13688 KB |
Output is correct |
31 |
Correct |
337 ms |
13944 KB |
Output is correct |
32 |
Correct |
340 ms |
13688 KB |
Output is correct |
33 |
Correct |
346 ms |
13568 KB |
Output is correct |
34 |
Correct |
377 ms |
13688 KB |
Output is correct |
35 |
Correct |
335 ms |
13816 KB |
Output is correct |
36 |
Correct |
347 ms |
13688 KB |
Output is correct |
37 |
Correct |
358 ms |
14072 KB |
Output is correct |
38 |
Correct |
333 ms |
13816 KB |
Output is correct |
39 |
Correct |
330 ms |
13944 KB |
Output is correct |
40 |
Correct |
328 ms |
13820 KB |
Output is correct |
41 |
Correct |
334 ms |
13688 KB |
Output is correct |
42 |
Correct |
331 ms |
13832 KB |
Output is correct |
43 |
Correct |
331 ms |
13816 KB |
Output is correct |
44 |
Correct |
334 ms |
13900 KB |
Output is correct |
45 |
Correct |
347 ms |
12424 KB |
Output is correct |
46 |
Correct |
357 ms |
21840 KB |
Output is correct |
47 |
Correct |
340 ms |
21752 KB |
Output is correct |
48 |
Correct |
366 ms |
21752 KB |
Output is correct |
49 |
Correct |
336 ms |
21752 KB |
Output is correct |
50 |
Correct |
358 ms |
21880 KB |
Output is correct |
51 |
Correct |
348 ms |
21752 KB |
Output is correct |
52 |
Correct |
343 ms |
21752 KB |
Output is correct |
53 |
Correct |
350 ms |
21752 KB |
Output is correct |
54 |
Correct |
360 ms |
21880 KB |
Output is correct |
55 |
Correct |
1 ms |
384 KB |
Output is correct |
56 |
Correct |
1 ms |
384 KB |
Output is correct |
57 |
Correct |
1 ms |
384 KB |
Output is correct |
58 |
Correct |
1 ms |
384 KB |
Output is correct |
59 |
Correct |
1 ms |
384 KB |
Output is correct |
60 |
Correct |
1 ms |
384 KB |
Output is correct |
61 |
Correct |
1 ms |
384 KB |
Output is correct |
62 |
Correct |
1 ms |
416 KB |
Output is correct |
63 |
Correct |
1 ms |
384 KB |
Output is correct |
64 |
Correct |
1 ms |
384 KB |
Output is correct |
65 |
Correct |
1 ms |
384 KB |
Output is correct |
66 |
Correct |
1 ms |
384 KB |
Output is correct |
67 |
Correct |
1 ms |
384 KB |
Output is correct |
68 |
Correct |
1 ms |
512 KB |
Output is correct |
69 |
Correct |
1 ms |
384 KB |
Output is correct |
70 |
Correct |
1 ms |
384 KB |
Output is correct |
71 |
Correct |
1 ms |
384 KB |
Output is correct |
72 |
Correct |
1 ms |
384 KB |
Output is correct |
73 |
Correct |
1 ms |
384 KB |
Output is correct |
74 |
Correct |
1 ms |
384 KB |
Output is correct |
75 |
Correct |
1 ms |
384 KB |
Output is correct |
76 |
Correct |
318 ms |
18168 KB |
Output is correct |
77 |
Correct |
314 ms |
18264 KB |
Output is correct |
78 |
Correct |
319 ms |
17660 KB |
Output is correct |
79 |
Correct |
311 ms |
17876 KB |
Output is correct |
80 |
Correct |
314 ms |
18552 KB |
Output is correct |
81 |
Correct |
310 ms |
17784 KB |
Output is correct |
82 |
Correct |
308 ms |
17656 KB |
Output is correct |
83 |
Correct |
331 ms |
18424 KB |
Output is correct |
84 |
Correct |
306 ms |
18040 KB |
Output is correct |
85 |
Correct |
325 ms |
18312 KB |
Output is correct |
86 |
Correct |
310 ms |
17732 KB |
Output is correct |
87 |
Correct |
325 ms |
17664 KB |
Output is correct |
88 |
Correct |
318 ms |
17928 KB |
Output is correct |
89 |
Correct |
310 ms |
17784 KB |
Output is correct |
90 |
Correct |
314 ms |
17680 KB |
Output is correct |
91 |
Correct |
331 ms |
17528 KB |
Output is correct |
92 |
Correct |
330 ms |
19804 KB |
Output is correct |
93 |
Correct |
339 ms |
19192 KB |
Output is correct |
94 |
Correct |
310 ms |
17108 KB |
Output is correct |
95 |
Correct |
306 ms |
18552 KB |
Output is correct |
96 |
Correct |
317 ms |
17400 KB |
Output is correct |
97 |
Correct |
310 ms |
17788 KB |
Output is correct |
98 |
Correct |
305 ms |
18040 KB |
Output is correct |
99 |
Correct |
316 ms |
17664 KB |
Output is correct |
100 |
Correct |
311 ms |
17784 KB |
Output is correct |