#include <bits/stdc++.h>
using namespace std;
struct Line {
mutable long long int k, m, p;
bool operator<(const Line& o) const { return k > o.k; }
bool operator<(long long int x) const { return p < x; }
};
struct LineContainer : multiset<Line, less< > > {
static const long long int inf = LLONG_MAX;
long long int div(long long int a, long long int b) {
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(long long int k, long long int 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));
}
long long int get(long long int x) {
assert(!empty());
auto l = *lower_bound(x);
return l.k * x + l.m;
}
} st;
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
long long int n;
cin >> n;
vector<pair<long long int, int>> v;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
if (v.empty()) {
v.push_back({x, 1});
}
else {
if (v.back().first < x) v.push_back({x, 1});
else {
v[v.size() - 1].second++;
}
}
}
long long int c = 0, d = 0;
st.add(0, 0);
for (auto w : v) {
c += w.second;
d = 1ll * w.first * n + st.get(w.first);
st.add(-c, d);
}
cout << d;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
452 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
420 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
420 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
103 ms |
13092 KB |
Output is correct |
17 |
Correct |
98 ms |
13512 KB |
Output is correct |
18 |
Correct |
74 ms |
14348 KB |
Output is correct |
19 |
Correct |
90 ms |
13720 KB |
Output is correct |
20 |
Correct |
93 ms |
13832 KB |
Output is correct |
21 |
Correct |
92 ms |
14540 KB |
Output is correct |
22 |
Correct |
85 ms |
14012 KB |
Output is correct |
23 |
Correct |
113 ms |
24036 KB |
Output is correct |
24 |
Correct |
99 ms |
23048 KB |
Output is correct |
25 |
Correct |
100 ms |
24248 KB |
Output is correct |
26 |
Correct |
114 ms |
22964 KB |
Output is correct |
27 |
Correct |
79 ms |
13508 KB |
Output is correct |
28 |
Correct |
93 ms |
13640 KB |
Output is correct |
29 |
Correct |
114 ms |
24292 KB |
Output is correct |
30 |
Correct |
110 ms |
24248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
10036 KB |
Output is correct |
2 |
Correct |
83 ms |
10072 KB |
Output is correct |
3 |
Correct |
75 ms |
10064 KB |
Output is correct |
4 |
Correct |
88 ms |
10032 KB |
Output is correct |
5 |
Correct |
84 ms |
10112 KB |
Output is correct |
6 |
Correct |
74 ms |
9900 KB |
Output is correct |
7 |
Correct |
74 ms |
10068 KB |
Output is correct |
8 |
Correct |
84 ms |
9900 KB |
Output is correct |
9 |
Correct |
79 ms |
9900 KB |
Output is correct |
10 |
Correct |
74 ms |
10028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
452 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
420 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
0 ms |
460 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
600 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
464 KB |
Output is correct |
44 |
Correct |
0 ms |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
344 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
452 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
420 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
103 ms |
13092 KB |
Output is correct |
31 |
Correct |
98 ms |
13512 KB |
Output is correct |
32 |
Correct |
74 ms |
14348 KB |
Output is correct |
33 |
Correct |
90 ms |
13720 KB |
Output is correct |
34 |
Correct |
93 ms |
13832 KB |
Output is correct |
35 |
Correct |
92 ms |
14540 KB |
Output is correct |
36 |
Correct |
85 ms |
14012 KB |
Output is correct |
37 |
Correct |
113 ms |
24036 KB |
Output is correct |
38 |
Correct |
99 ms |
23048 KB |
Output is correct |
39 |
Correct |
100 ms |
24248 KB |
Output is correct |
40 |
Correct |
114 ms |
22964 KB |
Output is correct |
41 |
Correct |
79 ms |
13508 KB |
Output is correct |
42 |
Correct |
93 ms |
13640 KB |
Output is correct |
43 |
Correct |
114 ms |
24292 KB |
Output is correct |
44 |
Correct |
110 ms |
24248 KB |
Output is correct |
45 |
Correct |
79 ms |
10036 KB |
Output is correct |
46 |
Correct |
83 ms |
10072 KB |
Output is correct |
47 |
Correct |
75 ms |
10064 KB |
Output is correct |
48 |
Correct |
88 ms |
10032 KB |
Output is correct |
49 |
Correct |
84 ms |
10112 KB |
Output is correct |
50 |
Correct |
74 ms |
9900 KB |
Output is correct |
51 |
Correct |
74 ms |
10068 KB |
Output is correct |
52 |
Correct |
84 ms |
9900 KB |
Output is correct |
53 |
Correct |
79 ms |
9900 KB |
Output is correct |
54 |
Correct |
74 ms |
10028 KB |
Output is correct |
55 |
Correct |
0 ms |
344 KB |
Output is correct |
56 |
Correct |
0 ms |
460 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
600 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
0 ms |
348 KB |
Output is correct |
68 |
Correct |
1 ms |
464 KB |
Output is correct |
69 |
Correct |
0 ms |
344 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
1 ms |
344 KB |
Output is correct |
74 |
Correct |
1 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
61 ms |
6440 KB |
Output is correct |
77 |
Correct |
62 ms |
6320 KB |
Output is correct |
78 |
Correct |
58 ms |
5800 KB |
Output is correct |
79 |
Correct |
58 ms |
6064 KB |
Output is correct |
80 |
Correct |
71 ms |
6700 KB |
Output is correct |
81 |
Correct |
58 ms |
5816 KB |
Output is correct |
82 |
Correct |
57 ms |
5928 KB |
Output is correct |
83 |
Correct |
70 ms |
6308 KB |
Output is correct |
84 |
Correct |
59 ms |
6200 KB |
Output is correct |
85 |
Correct |
59 ms |
6488 KB |
Output is correct |
86 |
Correct |
58 ms |
5680 KB |
Output is correct |
87 |
Correct |
56 ms |
5712 KB |
Output is correct |
88 |
Correct |
61 ms |
5972 KB |
Output is correct |
89 |
Correct |
57 ms |
5804 KB |
Output is correct |
90 |
Correct |
57 ms |
5800 KB |
Output is correct |
91 |
Correct |
55 ms |
5424 KB |
Output is correct |
92 |
Correct |
100 ms |
15032 KB |
Output is correct |
93 |
Correct |
101 ms |
14544 KB |
Output is correct |
94 |
Correct |
54 ms |
5204 KB |
Output is correct |
95 |
Correct |
66 ms |
6736 KB |
Output is correct |
96 |
Correct |
55 ms |
5460 KB |
Output is correct |
97 |
Correct |
58 ms |
5796 KB |
Output is correct |
98 |
Correct |
57 ms |
5968 KB |
Output is correct |
99 |
Correct |
70 ms |
5804 KB |
Output is correct |
100 |
Correct |
58 ms |
5928 KB |
Output is correct |