#include <bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define getbit(x, i) (((x) >> (i)) & 1)
#define bit(x) (1 << (x))
#define popcount __builtin_popcountll
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
long long rand(long long l, long long r) {
return l + rd() % (r - l + 1);
}
const int N = 1e6 + 5;
const int mod = 1e9 + 7; // 998244353;
const int lg = 25; // lg + 1
const int inf = 1e9; // INT_MAX;
const long long llinf = 1e18; // LLONG_MAX;
template<class X, class Y>
bool minimize(X &a, Y b) {
return a > b ? (a = b, true) : false;
}
template<class X, class Y>
bool maximize(X &a, Y b) {
return a < b ? (a = b, true) : false;
}
template<class X, class Y>
bool add(X &a, Y b) {
a += b;
while (a >= mod) a -= mod;
while (a < 0) a += mod;
return true;
}
int n;
int a[N];
int f[N];
double F(int j, int k) {
return (f[j] - f[k]) / (j - k);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
a[i] = max(a[i], a[i - 1]);
}
memset(f, 0, sizeof f);
f[0] = 0;
deque<int> q;
q.push_back(0);
for (int i = 1; i <= n; ++i) {
while (q.size() >= 2 && F(q[0], q[1]) < a[i]) q.pop_front();
int j = q.front();
f[i] = a[i] * (n - j) + f[j];
while (q.size() >= 2 && F(q[q.size() - 2], q[q.size() - 1]) > F(q[q.size() - 1], i)) q.pop_back();
q.push_back(i);
}
cout << f[n];
return 0;
}
/*
5
1 3 2 6 3
27
7
1 1 2 2 2 2 2
14
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
1 ms |
8792 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8676 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
9048 KB |
Output is correct |
13 |
Correct |
2 ms |
8792 KB |
Output is correct |
14 |
Correct |
2 ms |
9048 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
2 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8676 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
1 ms |
8796 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
1 ms |
9048 KB |
Output is correct |
13 |
Correct |
2 ms |
8792 KB |
Output is correct |
14 |
Correct |
2 ms |
9048 KB |
Output is correct |
15 |
Correct |
1 ms |
8796 KB |
Output is correct |
16 |
Correct |
88 ms |
20956 KB |
Output is correct |
17 |
Correct |
94 ms |
21840 KB |
Output is correct |
18 |
Correct |
79 ms |
20496 KB |
Output is correct |
19 |
Correct |
95 ms |
21912 KB |
Output is correct |
20 |
Correct |
100 ms |
21584 KB |
Output is correct |
21 |
Correct |
92 ms |
21840 KB |
Output is correct |
22 |
Correct |
90 ms |
21316 KB |
Output is correct |
23 |
Correct |
108 ms |
22352 KB |
Output is correct |
24 |
Correct |
95 ms |
21788 KB |
Output is correct |
25 |
Correct |
97 ms |
22348 KB |
Output is correct |
26 |
Correct |
101 ms |
22724 KB |
Output is correct |
27 |
Correct |
93 ms |
20932 KB |
Output is correct |
28 |
Correct |
98 ms |
22012 KB |
Output is correct |
29 |
Correct |
97 ms |
22188 KB |
Output is correct |
30 |
Correct |
107 ms |
22356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
25684 KB |
Output is correct |
2 |
Correct |
97 ms |
25680 KB |
Output is correct |
3 |
Correct |
97 ms |
25664 KB |
Output is correct |
4 |
Correct |
96 ms |
25668 KB |
Output is correct |
5 |
Correct |
96 ms |
25556 KB |
Output is correct |
6 |
Correct |
100 ms |
25544 KB |
Output is correct |
7 |
Correct |
96 ms |
25776 KB |
Output is correct |
8 |
Correct |
96 ms |
25684 KB |
Output is correct |
9 |
Correct |
97 ms |
25684 KB |
Output is correct |
10 |
Correct |
101 ms |
26312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
1 ms |
8792 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8656 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
21 |
Correct |
1 ms |
8796 KB |
Output is correct |
22 |
Correct |
1 ms |
8676 KB |
Output is correct |
23 |
Correct |
2 ms |
8796 KB |
Output is correct |
24 |
Correct |
1 ms |
8796 KB |
Output is correct |
25 |
Correct |
1 ms |
8796 KB |
Output is correct |
26 |
Correct |
1 ms |
9048 KB |
Output is correct |
27 |
Correct |
2 ms |
8792 KB |
Output is correct |
28 |
Correct |
2 ms |
9048 KB |
Output is correct |
29 |
Correct |
1 ms |
8796 KB |
Output is correct |
30 |
Correct |
1 ms |
8796 KB |
Output is correct |
31 |
Correct |
2 ms |
8796 KB |
Output is correct |
32 |
Correct |
1 ms |
8796 KB |
Output is correct |
33 |
Correct |
1 ms |
8660 KB |
Output is correct |
34 |
Correct |
1 ms |
8796 KB |
Output is correct |
35 |
Correct |
2 ms |
8792 KB |
Output is correct |
36 |
Correct |
1 ms |
8796 KB |
Output is correct |
37 |
Correct |
2 ms |
8796 KB |
Output is correct |
38 |
Correct |
1 ms |
8796 KB |
Output is correct |
39 |
Correct |
1 ms |
8796 KB |
Output is correct |
40 |
Correct |
2 ms |
8792 KB |
Output is correct |
41 |
Correct |
1 ms |
8796 KB |
Output is correct |
42 |
Correct |
2 ms |
8792 KB |
Output is correct |
43 |
Correct |
1 ms |
9048 KB |
Output is correct |
44 |
Correct |
1 ms |
8796 KB |
Output is correct |
45 |
Correct |
1 ms |
8796 KB |
Output is correct |
46 |
Correct |
2 ms |
8660 KB |
Output is correct |
47 |
Correct |
2 ms |
8792 KB |
Output is correct |
48 |
Correct |
2 ms |
8792 KB |
Output is correct |
49 |
Correct |
2 ms |
8792 KB |
Output is correct |
50 |
Correct |
1 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8796 KB |
Output is correct |
2 |
Correct |
1 ms |
8796 KB |
Output is correct |
3 |
Correct |
1 ms |
8796 KB |
Output is correct |
4 |
Correct |
1 ms |
8796 KB |
Output is correct |
5 |
Correct |
1 ms |
8796 KB |
Output is correct |
6 |
Correct |
1 ms |
8796 KB |
Output is correct |
7 |
Correct |
1 ms |
8796 KB |
Output is correct |
8 |
Correct |
1 ms |
8796 KB |
Output is correct |
9 |
Correct |
1 ms |
8796 KB |
Output is correct |
10 |
Correct |
1 ms |
8792 KB |
Output is correct |
11 |
Correct |
1 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
1 ms |
8656 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
2 ms |
8796 KB |
Output is correct |
18 |
Correct |
2 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
21 |
Correct |
1 ms |
8796 KB |
Output is correct |
22 |
Correct |
1 ms |
8676 KB |
Output is correct |
23 |
Correct |
2 ms |
8796 KB |
Output is correct |
24 |
Correct |
1 ms |
8796 KB |
Output is correct |
25 |
Correct |
1 ms |
8796 KB |
Output is correct |
26 |
Correct |
1 ms |
9048 KB |
Output is correct |
27 |
Correct |
2 ms |
8792 KB |
Output is correct |
28 |
Correct |
2 ms |
9048 KB |
Output is correct |
29 |
Correct |
1 ms |
8796 KB |
Output is correct |
30 |
Correct |
88 ms |
20956 KB |
Output is correct |
31 |
Correct |
94 ms |
21840 KB |
Output is correct |
32 |
Correct |
79 ms |
20496 KB |
Output is correct |
33 |
Correct |
95 ms |
21912 KB |
Output is correct |
34 |
Correct |
100 ms |
21584 KB |
Output is correct |
35 |
Correct |
92 ms |
21840 KB |
Output is correct |
36 |
Correct |
90 ms |
21316 KB |
Output is correct |
37 |
Correct |
108 ms |
22352 KB |
Output is correct |
38 |
Correct |
95 ms |
21788 KB |
Output is correct |
39 |
Correct |
97 ms |
22348 KB |
Output is correct |
40 |
Correct |
101 ms |
22724 KB |
Output is correct |
41 |
Correct |
93 ms |
20932 KB |
Output is correct |
42 |
Correct |
98 ms |
22012 KB |
Output is correct |
43 |
Correct |
97 ms |
22188 KB |
Output is correct |
44 |
Correct |
107 ms |
22356 KB |
Output is correct |
45 |
Correct |
98 ms |
25684 KB |
Output is correct |
46 |
Correct |
97 ms |
25680 KB |
Output is correct |
47 |
Correct |
97 ms |
25664 KB |
Output is correct |
48 |
Correct |
96 ms |
25668 KB |
Output is correct |
49 |
Correct |
96 ms |
25556 KB |
Output is correct |
50 |
Correct |
100 ms |
25544 KB |
Output is correct |
51 |
Correct |
96 ms |
25776 KB |
Output is correct |
52 |
Correct |
96 ms |
25684 KB |
Output is correct |
53 |
Correct |
97 ms |
25684 KB |
Output is correct |
54 |
Correct |
101 ms |
26312 KB |
Output is correct |
55 |
Correct |
1 ms |
8796 KB |
Output is correct |
56 |
Correct |
2 ms |
8796 KB |
Output is correct |
57 |
Correct |
1 ms |
8796 KB |
Output is correct |
58 |
Correct |
1 ms |
8660 KB |
Output is correct |
59 |
Correct |
1 ms |
8796 KB |
Output is correct |
60 |
Correct |
2 ms |
8792 KB |
Output is correct |
61 |
Correct |
1 ms |
8796 KB |
Output is correct |
62 |
Correct |
2 ms |
8796 KB |
Output is correct |
63 |
Correct |
1 ms |
8796 KB |
Output is correct |
64 |
Correct |
1 ms |
8796 KB |
Output is correct |
65 |
Correct |
2 ms |
8792 KB |
Output is correct |
66 |
Correct |
1 ms |
8796 KB |
Output is correct |
67 |
Correct |
2 ms |
8792 KB |
Output is correct |
68 |
Correct |
1 ms |
9048 KB |
Output is correct |
69 |
Correct |
1 ms |
8796 KB |
Output is correct |
70 |
Correct |
1 ms |
8796 KB |
Output is correct |
71 |
Correct |
2 ms |
8660 KB |
Output is correct |
72 |
Correct |
2 ms |
8792 KB |
Output is correct |
73 |
Correct |
2 ms |
8792 KB |
Output is correct |
74 |
Correct |
2 ms |
8792 KB |
Output is correct |
75 |
Correct |
1 ms |
8796 KB |
Output is correct |
76 |
Correct |
94 ms |
22100 KB |
Output is correct |
77 |
Correct |
97 ms |
22028 KB |
Output is correct |
78 |
Correct |
88 ms |
21332 KB |
Output is correct |
79 |
Correct |
89 ms |
21708 KB |
Output is correct |
80 |
Correct |
96 ms |
22424 KB |
Output is correct |
81 |
Correct |
93 ms |
21572 KB |
Output is correct |
82 |
Correct |
86 ms |
21616 KB |
Output is correct |
83 |
Correct |
95 ms |
21812 KB |
Output is correct |
84 |
Correct |
89 ms |
21840 KB |
Output is correct |
85 |
Correct |
99 ms |
22328 KB |
Output is correct |
86 |
Correct |
85 ms |
21324 KB |
Output is correct |
87 |
Correct |
86 ms |
21328 KB |
Output is correct |
88 |
Correct |
92 ms |
21680 KB |
Output is correct |
89 |
Correct |
96 ms |
21648 KB |
Output is correct |
90 |
Correct |
90 ms |
21372 KB |
Output is correct |
91 |
Correct |
84 ms |
21332 KB |
Output is correct |
92 |
Correct |
101 ms |
22476 KB |
Output is correct |
93 |
Correct |
107 ms |
21832 KB |
Output is correct |
94 |
Correct |
88 ms |
20816 KB |
Output is correct |
95 |
Correct |
93 ms |
22704 KB |
Output is correct |
96 |
Correct |
87 ms |
21188 KB |
Output is correct |
97 |
Correct |
98 ms |
21592 KB |
Output is correct |
98 |
Correct |
88 ms |
21700 KB |
Output is correct |
99 |
Correct |
88 ms |
21412 KB |
Output is correct |
100 |
Correct |
91 ms |
21540 KB |
Output is correct |