#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define all(c) c.begin(), c.end()
#define pii pair<int, int>
#define pll pair<long long, long long>
using namespace std;
const int N = 1e6 + 10;
struct line
{
ll a, b;
line() {};
line(ll a, ll b) : a(a), b(b) {};
};
int n;
ll a[N], dp[N];
bool bad(line A, line B, line C)
{
return (ld) (C.b - A.b) / (A.a - C.a) < (ld) (B.b - A.b) / (A.a - B.a);
}
void addLine(vector<line> &memo, line cur)
{
int k = memo.size();
while(k >= 2 && bad(memo[k - 2], memo[k - 1], cur))
{
memo.pop_back();
k--;
}
memo.pb(cur);
}
ll Fn(line A, ll x)
{
return A.a * x + A.b;
}
ll Query(vector<line> &memo, ll x)
{
int l = 0, r = memo.size() - 1;
if(r == - 1) return 0ll;
while(l < r)
{
int mid = l + r >> 1;
if(Fn(memo[mid], x) < Fn(memo[mid + 1], x)) r = mid;
else l = mid + 1;
}
return Fn(memo[l], x);
}
vector<line> memo;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("DISCHARG.inp", "r", stdin);
//freopen("DISCHARG.out", "w", stdout);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
reverse(a + 1, a + 1 + n);
vector<int> pos;
for(int i = 1; i <= n; i++)
{
while(!pos.empty() && a[pos.back()] <= a[i]) pos.pop_back();
pos.pb(i);
}
for(int i = 1; i <= pos.size(); i++)
{
addLine(memo, line(a[pos[i - 1]], dp[i - 1]));
dp[i] = Query(memo, pos[i - 1]);
// cout << dp[i] << '\n';
}
cout << dp[pos.size()];
return 0;
}
Compilation message
Discharging.cpp: In function 'long long int Query(std::vector<line>&, long long int)':
Discharging.cpp:53:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | int mid = l + r >> 1;
| ~~^~~
Discharging.cpp: In function 'int main()':
Discharging.cpp:78:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i = 1; i <= pos.size(); i++)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 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 |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 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 |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
95 ms |
17984 KB |
Output is correct |
17 |
Correct |
101 ms |
19316 KB |
Output is correct |
18 |
Correct |
79 ms |
15944 KB |
Output is correct |
19 |
Correct |
107 ms |
19244 KB |
Output is correct |
20 |
Correct |
105 ms |
19628 KB |
Output is correct |
21 |
Correct |
100 ms |
19276 KB |
Output is correct |
22 |
Correct |
96 ms |
18432 KB |
Output is correct |
23 |
Correct |
134 ms |
21684 KB |
Output is correct |
24 |
Correct |
102 ms |
20388 KB |
Output is correct |
25 |
Correct |
112 ms |
20700 KB |
Output is correct |
26 |
Correct |
117 ms |
22464 KB |
Output is correct |
27 |
Correct |
87 ms |
17328 KB |
Output is correct |
28 |
Correct |
98 ms |
19704 KB |
Output is correct |
29 |
Correct |
106 ms |
20704 KB |
Output is correct |
30 |
Correct |
114 ms |
21872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
17964 KB |
Output is correct |
2 |
Correct |
98 ms |
17784 KB |
Output is correct |
3 |
Correct |
97 ms |
17720 KB |
Output is correct |
4 |
Correct |
111 ms |
17792 KB |
Output is correct |
5 |
Correct |
95 ms |
17740 KB |
Output is correct |
6 |
Correct |
99 ms |
17732 KB |
Output is correct |
7 |
Correct |
109 ms |
17792 KB |
Output is correct |
8 |
Correct |
99 ms |
17784 KB |
Output is correct |
9 |
Correct |
101 ms |
17772 KB |
Output is correct |
10 |
Correct |
116 ms |
17708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 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 |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 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 |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
324 KB |
Output is correct |
31 |
Correct |
1 ms |
324 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
372 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
324 KB |
Output is correct |
37 |
Correct |
1 ms |
316 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
320 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
324 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 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 |
316 KB |
Output is correct |
47 |
Correct |
1 ms |
324 KB |
Output is correct |
48 |
Correct |
1 ms |
328 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 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 |
328 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 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 |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
95 ms |
17984 KB |
Output is correct |
31 |
Correct |
101 ms |
19316 KB |
Output is correct |
32 |
Correct |
79 ms |
15944 KB |
Output is correct |
33 |
Correct |
107 ms |
19244 KB |
Output is correct |
34 |
Correct |
105 ms |
19628 KB |
Output is correct |
35 |
Correct |
100 ms |
19276 KB |
Output is correct |
36 |
Correct |
96 ms |
18432 KB |
Output is correct |
37 |
Correct |
134 ms |
21684 KB |
Output is correct |
38 |
Correct |
102 ms |
20388 KB |
Output is correct |
39 |
Correct |
112 ms |
20700 KB |
Output is correct |
40 |
Correct |
117 ms |
22464 KB |
Output is correct |
41 |
Correct |
87 ms |
17328 KB |
Output is correct |
42 |
Correct |
98 ms |
19704 KB |
Output is correct |
43 |
Correct |
106 ms |
20704 KB |
Output is correct |
44 |
Correct |
114 ms |
21872 KB |
Output is correct |
45 |
Correct |
107 ms |
17964 KB |
Output is correct |
46 |
Correct |
98 ms |
17784 KB |
Output is correct |
47 |
Correct |
97 ms |
17720 KB |
Output is correct |
48 |
Correct |
111 ms |
17792 KB |
Output is correct |
49 |
Correct |
95 ms |
17740 KB |
Output is correct |
50 |
Correct |
99 ms |
17732 KB |
Output is correct |
51 |
Correct |
109 ms |
17792 KB |
Output is correct |
52 |
Correct |
99 ms |
17784 KB |
Output is correct |
53 |
Correct |
101 ms |
17772 KB |
Output is correct |
54 |
Correct |
116 ms |
17708 KB |
Output is correct |
55 |
Correct |
1 ms |
324 KB |
Output is correct |
56 |
Correct |
1 ms |
324 KB |
Output is correct |
57 |
Correct |
1 ms |
332 KB |
Output is correct |
58 |
Correct |
1 ms |
372 KB |
Output is correct |
59 |
Correct |
1 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
332 KB |
Output is correct |
61 |
Correct |
1 ms |
324 KB |
Output is correct |
62 |
Correct |
1 ms |
316 KB |
Output is correct |
63 |
Correct |
1 ms |
332 KB |
Output is correct |
64 |
Correct |
1 ms |
320 KB |
Output is correct |
65 |
Correct |
1 ms |
332 KB |
Output is correct |
66 |
Correct |
1 ms |
324 KB |
Output is correct |
67 |
Correct |
1 ms |
332 KB |
Output is correct |
68 |
Correct |
1 ms |
332 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 |
316 KB |
Output is correct |
72 |
Correct |
1 ms |
324 KB |
Output is correct |
73 |
Correct |
1 ms |
328 KB |
Output is correct |
74 |
Correct |
1 ms |
332 KB |
Output is correct |
75 |
Correct |
0 ms |
332 KB |
Output is correct |
76 |
Correct |
91 ms |
14152 KB |
Output is correct |
77 |
Correct |
99 ms |
14068 KB |
Output is correct |
78 |
Correct |
87 ms |
13580 KB |
Output is correct |
79 |
Correct |
105 ms |
13708 KB |
Output is correct |
80 |
Correct |
123 ms |
14472 KB |
Output is correct |
81 |
Correct |
112 ms |
13596 KB |
Output is correct |
82 |
Correct |
99 ms |
13636 KB |
Output is correct |
83 |
Correct |
121 ms |
14024 KB |
Output is correct |
84 |
Correct |
86 ms |
13848 KB |
Output is correct |
85 |
Correct |
105 ms |
14216 KB |
Output is correct |
86 |
Correct |
110 ms |
13508 KB |
Output is correct |
87 |
Correct |
91 ms |
13424 KB |
Output is correct |
88 |
Correct |
89 ms |
13836 KB |
Output is correct |
89 |
Correct |
87 ms |
13576 KB |
Output is correct |
90 |
Correct |
81 ms |
13544 KB |
Output is correct |
91 |
Correct |
89 ms |
13280 KB |
Output is correct |
92 |
Correct |
120 ms |
20872 KB |
Output is correct |
93 |
Correct |
106 ms |
19384 KB |
Output is correct |
94 |
Correct |
90 ms |
13004 KB |
Output is correct |
95 |
Correct |
96 ms |
14396 KB |
Output is correct |
96 |
Correct |
103 ms |
13392 KB |
Output is correct |
97 |
Correct |
100 ms |
13696 KB |
Output is correct |
98 |
Correct |
123 ms |
13872 KB |
Output is correct |
99 |
Correct |
106 ms |
13568 KB |
Output is correct |
100 |
Correct |
89 ms |
13596 KB |
Output is correct |