#include <bits/stdc++.h>
#define err(args...) {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; }
template <typename X, typename Y> istream& operator>>(istream& is, pair<X, Y>& p) { return is >> p.first >> p.second; }
template <typename Iter, typename T> int gt(Iter L, Iter R, T v) { return upper_bound(L, R, v) - L; }
template <typename Iter, typename T> int ge(Iter L, Iter R, T v) { return lower_bound(L, R, v) - L; }
template <typename Iter, typename T> int lt(Iter L, Iter R, T v) { return lower_bound(L, R, v) - L - 1; }
template <typename Iter, typename T> int le(Iter L, Iter R, T v) { return upper_bound(L, R, v) - L - 1; }
template <typename Bst, typename T> typename Bst::const_iterator gt(const Bst& bst, T v) { return bst.upper_bound(v); }
template <typename Bst, typename T> typename Bst::const_iterator ge(const Bst& bst, T v) { return bst.lower_bound(v); }
template <typename Bst, typename T> typename Bst::const_iterator lt(const Bst& bst, T v) { return bst.lower_bound(v) == bst.begin() ? bst.end() : --bst.lower_bound(v); }
template <typename Bst, typename T> typename Bst::const_iterator le(const Bst& bst, T v) { return bst.upper_bound(v) == bst.begin() ? bst.end() : --bst.upper_bound(v); }
// maintain an upper/lower hull L of lines y = mx + b
// query for y(x) such that y(x) is minimum/maximum over all lines y in L
// assumes lines are inserted in non-increasing/non-decreasing order of m
// minimization by default, call constructor with greater<ll>() for maximization
struct cht {
struct line {
ll m, b, intersection;
ll eval(ll x) const { return m * x + b; }
ll cross(const line& l) const { return (l.b - b) / (m - l.m); }
bool operator<(const line& l) const { return m > l.m; }
bool operator<(const ll x) const { return intersection < x; }
friend bool operator<(const ll x, const line& l) { return x < l.intersection; }
};
multiset<line, less<>> h;
const function<bool(ll, ll)> cmp;
cht(const function<bool(ll, ll)>& cmp = less<ll>()) : cmp(cmp) {}
const line& l1() { return *h.rbegin(); }
const line& l2() { return *++h.rbegin(); }
void pop() { h.erase(--h.end()); }
void add(ll m, ll b) {
if(h.empty()) {
h.insert({m, b, numeric_limits<ll>::min()});
} else if(m != l1().m or cmp(b, l1().b)) {
if(m == l1().m) pop();
while(h.size() >= 2 and l1().cross({m, b}) <= l1().cross(l2())) pop();
h.insert({m, b, l1().cross({m, b})});
}
}
ll opt(ll x) {
assert(!h.empty());
ll y1 = lt(h, x)->eval(x), y2 = le(h, x)->eval(x);
return cmp(y1, y2) ? y1 : y2;
}
};
template <typename T, typename Cmp = decltype(less<T>())>
vector<int> nsv(const vector<T>& a, int L, int R, int d, Cmp&& cmp = less<T>()) {
static_assert(is_convertible<decltype(cmp), function<bool(T, T)>>::value);
vector<int> s, nearest_smaller_to(a.size());
for(int i = L; i != R; i += d) {
while(not s.empty() and not cmp(a[s.back()], a[i])) {
s.pop_back();
}
nearest_smaller_to[i] = s.empty() ? L - d : s.back();
s.push_back(i);
}
return nearest_smaller_to;
}
template <typename T, typename Cmp = decltype(less<T>())>
vector<int> lnsv(const vector<T>& a, Cmp&& cmp = less<T>()) {
return nsv(a, 0, a.size(), 1, cmp);
}
template <typename T, typename Cmp = decltype(less<T>())>
vector<int> rnsv(const vector<T>& a, Cmp&& cmp = less<T>()) {
return nsv(a, a.size() - 1, -1, -1, cmp);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<ll> t(n);
cin >> t;
auto lsv = lnsv(t);
auto rgv = rnsv(t, greater<ll>());
vector<ll> opt(n + 1);
cht hull;
vector<int> I;
for(int i = 0; i < n; i = rgv[i]) {
I.push_back(i);
}
I.push_back(n);
for(int ii = I.size() - 1; ii >= 0; ii--) {
int i = I[ii];
if(i == n) {
opt[i] = 0;
} else {
opt[i] = min(
t[i] * (n - i) + opt[rgv[i]],
hull.h.empty() ? lim<ll>::max() : hull.opt(n - i)
);
}
if(ii > 0) {
hull.add(t[I[ii - 1]], opt[i]);
}
}
cout << opt[0] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
148 ms |
32380 KB |
Output is correct |
17 |
Correct |
145 ms |
33508 KB |
Output is correct |
18 |
Correct |
113 ms |
31380 KB |
Output is correct |
19 |
Correct |
145 ms |
33304 KB |
Output is correct |
20 |
Correct |
143 ms |
33456 KB |
Output is correct |
21 |
Correct |
153 ms |
33396 KB |
Output is correct |
22 |
Correct |
166 ms |
32840 KB |
Output is correct |
23 |
Correct |
161 ms |
36704 KB |
Output is correct |
24 |
Correct |
153 ms |
35844 KB |
Output is correct |
25 |
Correct |
166 ms |
36236 KB |
Output is correct |
26 |
Correct |
183 ms |
37104 KB |
Output is correct |
27 |
Correct |
130 ms |
32380 KB |
Output is correct |
28 |
Correct |
139 ms |
33556 KB |
Output is correct |
29 |
Correct |
158 ms |
36092 KB |
Output is correct |
30 |
Correct |
168 ms |
36656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
33468 KB |
Output is correct |
2 |
Correct |
134 ms |
33484 KB |
Output is correct |
3 |
Correct |
111 ms |
33608 KB |
Output is correct |
4 |
Correct |
112 ms |
33480 KB |
Output is correct |
5 |
Correct |
114 ms |
33484 KB |
Output is correct |
6 |
Correct |
111 ms |
33480 KB |
Output is correct |
7 |
Correct |
109 ms |
33484 KB |
Output is correct |
8 |
Correct |
114 ms |
33492 KB |
Output is correct |
9 |
Correct |
124 ms |
33364 KB |
Output is correct |
10 |
Correct |
110 ms |
33480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
324 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
352 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
328 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
328 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
328 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
148 ms |
32380 KB |
Output is correct |
31 |
Correct |
145 ms |
33508 KB |
Output is correct |
32 |
Correct |
113 ms |
31380 KB |
Output is correct |
33 |
Correct |
145 ms |
33304 KB |
Output is correct |
34 |
Correct |
143 ms |
33456 KB |
Output is correct |
35 |
Correct |
153 ms |
33396 KB |
Output is correct |
36 |
Correct |
166 ms |
32840 KB |
Output is correct |
37 |
Correct |
161 ms |
36704 KB |
Output is correct |
38 |
Correct |
153 ms |
35844 KB |
Output is correct |
39 |
Correct |
166 ms |
36236 KB |
Output is correct |
40 |
Correct |
183 ms |
37104 KB |
Output is correct |
41 |
Correct |
130 ms |
32380 KB |
Output is correct |
42 |
Correct |
139 ms |
33556 KB |
Output is correct |
43 |
Correct |
158 ms |
36092 KB |
Output is correct |
44 |
Correct |
168 ms |
36656 KB |
Output is correct |
45 |
Correct |
106 ms |
33468 KB |
Output is correct |
46 |
Correct |
134 ms |
33484 KB |
Output is correct |
47 |
Correct |
111 ms |
33608 KB |
Output is correct |
48 |
Correct |
112 ms |
33480 KB |
Output is correct |
49 |
Correct |
114 ms |
33484 KB |
Output is correct |
50 |
Correct |
111 ms |
33480 KB |
Output is correct |
51 |
Correct |
109 ms |
33484 KB |
Output is correct |
52 |
Correct |
114 ms |
33492 KB |
Output is correct |
53 |
Correct |
124 ms |
33364 KB |
Output is correct |
54 |
Correct |
110 ms |
33480 KB |
Output is correct |
55 |
Correct |
1 ms |
324 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
340 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
352 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
1 ms |
340 KB |
Output is correct |
65 |
Correct |
1 ms |
340 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
1 ms |
328 KB |
Output is correct |
68 |
Correct |
1 ms |
340 KB |
Output is correct |
69 |
Correct |
1 ms |
340 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
340 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
1 ms |
328 KB |
Output is correct |
74 |
Correct |
1 ms |
340 KB |
Output is correct |
75 |
Correct |
1 ms |
324 KB |
Output is correct |
76 |
Correct |
101 ms |
29804 KB |
Output is correct |
77 |
Correct |
103 ms |
29772 KB |
Output is correct |
78 |
Correct |
100 ms |
29248 KB |
Output is correct |
79 |
Correct |
111 ms |
29468 KB |
Output is correct |
80 |
Correct |
138 ms |
30040 KB |
Output is correct |
81 |
Correct |
98 ms |
29256 KB |
Output is correct |
82 |
Correct |
100 ms |
29284 KB |
Output is correct |
83 |
Correct |
115 ms |
29768 KB |
Output is correct |
84 |
Correct |
114 ms |
29712 KB |
Output is correct |
85 |
Correct |
110 ms |
29768 KB |
Output is correct |
86 |
Correct |
97 ms |
29256 KB |
Output is correct |
87 |
Correct |
98 ms |
29132 KB |
Output is correct |
88 |
Correct |
101 ms |
29512 KB |
Output is correct |
89 |
Correct |
115 ms |
29372 KB |
Output is correct |
90 |
Correct |
131 ms |
29264 KB |
Output is correct |
91 |
Correct |
95 ms |
29000 KB |
Output is correct |
92 |
Correct |
161 ms |
34408 KB |
Output is correct |
93 |
Correct |
144 ms |
33428 KB |
Output is correct |
94 |
Correct |
99 ms |
28736 KB |
Output is correct |
95 |
Correct |
120 ms |
30148 KB |
Output is correct |
96 |
Correct |
99 ms |
28964 KB |
Output is correct |
97 |
Correct |
112 ms |
29392 KB |
Output is correct |
98 |
Correct |
110 ms |
29516 KB |
Output is correct |
99 |
Correct |
115 ms |
29208 KB |
Output is correct |
100 |
Correct |
102 ms |
29260 KB |
Output is correct |