#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int MOD = 998244353;
ll binpow(ll a, ll p, int mod = MOD) {
ll res = 1;
while (p) {
if (p & 1) {
(res *= a) %= mod;
}
p >>= 1;
(a *= a) %= mod;
}
return res;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
struct Line {
mutable ll k, m, p;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
static const ll inf = LLONG_MAX;
ll div(ll a, ll b) { // floored division
return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = inf, 0;
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(ll k, ll m) {
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
ll query(ll x) {
assert(!empty());
auto l = *lower_bound(x);
return l.k * x + l.m;
}
};
void solve() {
int n;
cin >> n;
vector<ll> dp(n);
LineContainer cht;
cht.add(-n, 0);
ll mx = 0;
for (int i = 0; i < n; i++) {
ll t;
cin >> t;
mx = max(mx, t);
dp[i] = cht.query(mx);
cht.add(-n + i + 1, dp[i]);
}
cout << -dp[n - 1] << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
for (int tc = 1; tc <= T; tc++) {
// cout << "Case #" << tc << ": ";
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
248 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
324 KB |
Output is correct |
16 |
Correct |
190 ms |
13120 KB |
Output is correct |
17 |
Correct |
203 ms |
13900 KB |
Output is correct |
18 |
Correct |
180 ms |
12488 KB |
Output is correct |
19 |
Correct |
201 ms |
13892 KB |
Output is correct |
20 |
Correct |
205 ms |
13732 KB |
Output is correct |
21 |
Correct |
206 ms |
13892 KB |
Output is correct |
22 |
Correct |
195 ms |
13564 KB |
Output is correct |
23 |
Correct |
219 ms |
14512 KB |
Output is correct |
24 |
Correct |
206 ms |
14012 KB |
Output is correct |
25 |
Correct |
215 ms |
14408 KB |
Output is correct |
26 |
Correct |
217 ms |
14656 KB |
Output is correct |
27 |
Correct |
189 ms |
13108 KB |
Output is correct |
28 |
Correct |
210 ms |
14136 KB |
Output is correct |
29 |
Correct |
222 ms |
14156 KB |
Output is correct |
30 |
Correct |
209 ms |
14396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
200 ms |
17724 KB |
Output is correct |
2 |
Correct |
196 ms |
17740 KB |
Output is correct |
3 |
Correct |
210 ms |
17732 KB |
Output is correct |
4 |
Correct |
199 ms |
17744 KB |
Output is correct |
5 |
Correct |
199 ms |
17852 KB |
Output is correct |
6 |
Correct |
201 ms |
17856 KB |
Output is correct |
7 |
Correct |
214 ms |
17728 KB |
Output is correct |
8 |
Correct |
210 ms |
17740 KB |
Output is correct |
9 |
Correct |
194 ms |
17980 KB |
Output is correct |
10 |
Correct |
200 ms |
17724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
248 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
324 KB |
Output is correct |
30 |
Correct |
1 ms |
320 KB |
Output is correct |
31 |
Correct |
1 ms |
316 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
1 ms |
204 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
2 ms |
320 KB |
Output is correct |
43 |
Correct |
1 ms |
316 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
204 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
248 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
324 KB |
Output is correct |
30 |
Correct |
190 ms |
13120 KB |
Output is correct |
31 |
Correct |
203 ms |
13900 KB |
Output is correct |
32 |
Correct |
180 ms |
12488 KB |
Output is correct |
33 |
Correct |
201 ms |
13892 KB |
Output is correct |
34 |
Correct |
205 ms |
13732 KB |
Output is correct |
35 |
Correct |
206 ms |
13892 KB |
Output is correct |
36 |
Correct |
195 ms |
13564 KB |
Output is correct |
37 |
Correct |
219 ms |
14512 KB |
Output is correct |
38 |
Correct |
206 ms |
14012 KB |
Output is correct |
39 |
Correct |
215 ms |
14408 KB |
Output is correct |
40 |
Correct |
217 ms |
14656 KB |
Output is correct |
41 |
Correct |
189 ms |
13108 KB |
Output is correct |
42 |
Correct |
210 ms |
14136 KB |
Output is correct |
43 |
Correct |
222 ms |
14156 KB |
Output is correct |
44 |
Correct |
209 ms |
14396 KB |
Output is correct |
45 |
Correct |
200 ms |
17724 KB |
Output is correct |
46 |
Correct |
196 ms |
17740 KB |
Output is correct |
47 |
Correct |
210 ms |
17732 KB |
Output is correct |
48 |
Correct |
199 ms |
17744 KB |
Output is correct |
49 |
Correct |
199 ms |
17852 KB |
Output is correct |
50 |
Correct |
201 ms |
17856 KB |
Output is correct |
51 |
Correct |
214 ms |
17728 KB |
Output is correct |
52 |
Correct |
210 ms |
17740 KB |
Output is correct |
53 |
Correct |
194 ms |
17980 KB |
Output is correct |
54 |
Correct |
200 ms |
17724 KB |
Output is correct |
55 |
Correct |
1 ms |
320 KB |
Output is correct |
56 |
Correct |
1 ms |
316 KB |
Output is correct |
57 |
Correct |
1 ms |
332 KB |
Output is correct |
58 |
Correct |
1 ms |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
1 ms |
332 KB |
Output is correct |
61 |
Correct |
1 ms |
332 KB |
Output is correct |
62 |
Correct |
1 ms |
332 KB |
Output is correct |
63 |
Correct |
1 ms |
332 KB |
Output is correct |
64 |
Correct |
1 ms |
332 KB |
Output is correct |
65 |
Correct |
1 ms |
204 KB |
Output is correct |
66 |
Correct |
1 ms |
332 KB |
Output is correct |
67 |
Correct |
2 ms |
320 KB |
Output is correct |
68 |
Correct |
1 ms |
316 KB |
Output is correct |
69 |
Correct |
1 ms |
332 KB |
Output is correct |
70 |
Correct |
1 ms |
332 KB |
Output is correct |
71 |
Correct |
1 ms |
332 KB |
Output is correct |
72 |
Correct |
1 ms |
204 KB |
Output is correct |
73 |
Correct |
1 ms |
332 KB |
Output is correct |
74 |
Correct |
1 ms |
332 KB |
Output is correct |
75 |
Correct |
1 ms |
332 KB |
Output is correct |
76 |
Correct |
203 ms |
14140 KB |
Output is correct |
77 |
Correct |
192 ms |
14160 KB |
Output is correct |
78 |
Correct |
183 ms |
13504 KB |
Output is correct |
79 |
Correct |
184 ms |
13892 KB |
Output is correct |
80 |
Correct |
194 ms |
14400 KB |
Output is correct |
81 |
Correct |
180 ms |
13632 KB |
Output is correct |
82 |
Correct |
188 ms |
13636 KB |
Output is correct |
83 |
Correct |
186 ms |
14104 KB |
Output is correct |
84 |
Correct |
190 ms |
13880 KB |
Output is correct |
85 |
Correct |
191 ms |
14148 KB |
Output is correct |
86 |
Correct |
188 ms |
13496 KB |
Output is correct |
87 |
Correct |
199 ms |
13380 KB |
Output is correct |
88 |
Correct |
191 ms |
13760 KB |
Output is correct |
89 |
Correct |
192 ms |
13632 KB |
Output is correct |
90 |
Correct |
189 ms |
13504 KB |
Output is correct |
91 |
Correct |
186 ms |
13380 KB |
Output is correct |
92 |
Correct |
215 ms |
14668 KB |
Output is correct |
93 |
Correct |
209 ms |
13892 KB |
Output is correct |
94 |
Correct |
185 ms |
13116 KB |
Output is correct |
95 |
Correct |
207 ms |
14400 KB |
Output is correct |
96 |
Correct |
177 ms |
13372 KB |
Output is correct |
97 |
Correct |
196 ms |
13628 KB |
Output is correct |
98 |
Correct |
184 ms |
13892 KB |
Output is correct |
99 |
Correct |
189 ms |
13500 KB |
Output is correct |
100 |
Correct |
185 ms |
13656 KB |
Output is correct |