/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;
const int mxN = 3e5 + 5;
const int mod = 1e9 + 7;
const i64 oo = 1e18;
struct Line {
mutable i64 k, m, p;
bool operator < (const Line &rhs) const { return k < rhs.k; }
bool operator < (i64 x) const { return p < x; }
};
struct CDuonggsLineContainer : multiset<Line, less<>> {
const static i64 inf = LLONG_MAX;
i64 floor_div(i64 a, i64 b) {
return a / b - ((a ^ b) < 0 && a % b);
}
bool isect(iterator x, iterator y) {
if (y == end()) {
x->p = inf;
return false;
}
if (x->k == y->k) x->p = (x->m > y->m ? inf : -inf);
else x->p = floor_div(x->m - y->m, y->k - x->k);
return x->p >= y->p;
}
void add(i64 k, i64 m) {
k = -k, m = -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));
}
i64 query(i64 x) {
assert(!empty());
auto l = *lower_bound(x);
return -(l.k * x + l.m);
}
};
int n;
vector<pair<int, int>> a;
void solve() {
cin >> n;
for (int i = 1; i <= n; ++i) {
int val; cin >> val;
if (a.empty() || a.back().ff < val) a.emplace_back(val, 1);
else ++a.back().ss;
}
CDuonggsLineContainer cht;
int cur = 0;
i64 pre = 0;
for (auto [val, len] : a) {
cht.add(-cur, pre);
pre = 1ll * val * n + cht.query(val);
cur += len;
}
cout << pre << endl;
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
cout << "Check array size pls sir" << endl;
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
452 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
500 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
472 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
500 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
472 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
86 ms |
10132 KB |
Output is correct |
17 |
Correct |
88 ms |
10440 KB |
Output is correct |
18 |
Correct |
69 ms |
8572 KB |
Output is correct |
19 |
Correct |
89 ms |
9488 KB |
Output is correct |
20 |
Correct |
90 ms |
9056 KB |
Output is correct |
21 |
Correct |
87 ms |
10384 KB |
Output is correct |
22 |
Correct |
84 ms |
10356 KB |
Output is correct |
23 |
Correct |
101 ms |
14272 KB |
Output is correct |
24 |
Correct |
95 ms |
13764 KB |
Output is correct |
25 |
Correct |
97 ms |
15300 KB |
Output is correct |
26 |
Correct |
105 ms |
15076 KB |
Output is correct |
27 |
Correct |
76 ms |
9428 KB |
Output is correct |
28 |
Correct |
94 ms |
9024 KB |
Output is correct |
29 |
Correct |
97 ms |
15560 KB |
Output is correct |
30 |
Correct |
103 ms |
15004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
8068 KB |
Output is correct |
2 |
Correct |
72 ms |
7880 KB |
Output is correct |
3 |
Correct |
73 ms |
8016 KB |
Output is correct |
4 |
Correct |
72 ms |
8020 KB |
Output is correct |
5 |
Correct |
73 ms |
8016 KB |
Output is correct |
6 |
Correct |
77 ms |
8108 KB |
Output is correct |
7 |
Correct |
77 ms |
7992 KB |
Output is correct |
8 |
Correct |
73 ms |
8056 KB |
Output is correct |
9 |
Correct |
72 ms |
8020 KB |
Output is correct |
10 |
Correct |
72 ms |
8020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
452 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
500 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
472 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
460 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
452 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
500 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
472 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
86 ms |
10132 KB |
Output is correct |
31 |
Correct |
88 ms |
10440 KB |
Output is correct |
32 |
Correct |
69 ms |
8572 KB |
Output is correct |
33 |
Correct |
89 ms |
9488 KB |
Output is correct |
34 |
Correct |
90 ms |
9056 KB |
Output is correct |
35 |
Correct |
87 ms |
10384 KB |
Output is correct |
36 |
Correct |
84 ms |
10356 KB |
Output is correct |
37 |
Correct |
101 ms |
14272 KB |
Output is correct |
38 |
Correct |
95 ms |
13764 KB |
Output is correct |
39 |
Correct |
97 ms |
15300 KB |
Output is correct |
40 |
Correct |
105 ms |
15076 KB |
Output is correct |
41 |
Correct |
76 ms |
9428 KB |
Output is correct |
42 |
Correct |
94 ms |
9024 KB |
Output is correct |
43 |
Correct |
97 ms |
15560 KB |
Output is correct |
44 |
Correct |
103 ms |
15004 KB |
Output is correct |
45 |
Correct |
73 ms |
8068 KB |
Output is correct |
46 |
Correct |
72 ms |
7880 KB |
Output is correct |
47 |
Correct |
73 ms |
8016 KB |
Output is correct |
48 |
Correct |
72 ms |
8020 KB |
Output is correct |
49 |
Correct |
73 ms |
8016 KB |
Output is correct |
50 |
Correct |
77 ms |
8108 KB |
Output is correct |
51 |
Correct |
77 ms |
7992 KB |
Output is correct |
52 |
Correct |
73 ms |
8056 KB |
Output is correct |
53 |
Correct |
72 ms |
8020 KB |
Output is correct |
54 |
Correct |
72 ms |
8020 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
464 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
460 KB |
Output is correct |
65 |
Correct |
0 ms |
348 KB |
Output is correct |
66 |
Correct |
0 ms |
468 KB |
Output is correct |
67 |
Correct |
1 ms |
348 KB |
Output is correct |
68 |
Correct |
1 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
1 ms |
348 KB |
Output is correct |
76 |
Correct |
58 ms |
5460 KB |
Output is correct |
77 |
Correct |
59 ms |
5204 KB |
Output is correct |
78 |
Correct |
56 ms |
4948 KB |
Output is correct |
79 |
Correct |
56 ms |
4804 KB |
Output is correct |
80 |
Correct |
63 ms |
5456 KB |
Output is correct |
81 |
Correct |
64 ms |
4944 KB |
Output is correct |
82 |
Correct |
56 ms |
5016 KB |
Output is correct |
83 |
Correct |
76 ms |
5204 KB |
Output is correct |
84 |
Correct |
63 ms |
4936 KB |
Output is correct |
85 |
Correct |
58 ms |
5204 KB |
Output is correct |
86 |
Correct |
62 ms |
5200 KB |
Output is correct |
87 |
Correct |
57 ms |
4692 KB |
Output is correct |
88 |
Correct |
57 ms |
5040 KB |
Output is correct |
89 |
Correct |
57 ms |
4944 KB |
Output is correct |
90 |
Correct |
57 ms |
4920 KB |
Output is correct |
91 |
Correct |
56 ms |
4784 KB |
Output is correct |
92 |
Correct |
96 ms |
10448 KB |
Output is correct |
93 |
Correct |
89 ms |
8996 KB |
Output is correct |
94 |
Correct |
53 ms |
4436 KB |
Output is correct |
95 |
Correct |
60 ms |
5456 KB |
Output is correct |
96 |
Correct |
55 ms |
4788 KB |
Output is correct |
97 |
Correct |
57 ms |
4944 KB |
Output is correct |
98 |
Correct |
57 ms |
4948 KB |
Output is correct |
99 |
Correct |
58 ms |
5036 KB |
Output is correct |
100 |
Correct |
62 ms |
5204 KB |
Output is correct |