#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 1000000;
const ll INF = 1000000000000000000LL;
ll a[N+5];
ll dp[N+5];
struct line{
ll k, n;
ll eval(int x){ return k*x + n; }
} seg[4*N+5];
ll query(int node, int l, int r, int i){
ll g = seg[node].eval(i);
if(l == r) return g;
int mid = (l+r)/2;
if(i <= mid) return min(g, query(node*2, l, mid, i));
return min(g, query(node*2+1, mid+1, r, i));
}
void upd(int node, int l, int r, line g){
int mid = (l+r)/2;
if(g.eval(mid) < seg[node].eval(mid)){
swap(g, seg[node]);
}
if(l == r) return;
if(seg[node].eval(l) > g.eval(l)) upd(node*2, l, mid, g);
else upd(node*2+1, mid+1, r, g);
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n;
cin >> n;
for(int i=1; i<=n; i++){
cin >> a[i];
}
reverse(a+1, a+1+n);
for(int i=1; i<=4*n; i++) seg[i].n = INF;
vector <pair <int, int>> vec;
for(int i=n; i>=1; i--){
if(vec.empty() || vec.back().second < a[i]){
vec.push_back({i, a[i]});
}
}
reverse(vec.begin(), vec.end());
for(int i=0; i<vec.size(); i++){
int id, h;
tie(id, h) = vec[i];
line g;
g.k = a[id];
g.n = 0;
if(i) g.n = dp[i-1];
upd(1, 1, n, g);
dp[i] = query(1, 1, n, id);
}
cout << dp[vec.size() - 1] << "\n";
return 0;
}
Compilation message
Discharging.cpp: In function 'int main()':
Discharging.cpp:55:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i=0; i<vec.size(); i++){
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 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 |
328 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 |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
190 ms |
82060 KB |
Output is correct |
17 |
Correct |
191 ms |
83780 KB |
Output is correct |
18 |
Correct |
139 ms |
79680 KB |
Output is correct |
19 |
Correct |
187 ms |
83608 KB |
Output is correct |
20 |
Correct |
188 ms |
84168 KB |
Output is correct |
21 |
Correct |
184 ms |
83624 KB |
Output is correct |
22 |
Correct |
179 ms |
82604 KB |
Output is correct |
23 |
Correct |
265 ms |
86692 KB |
Output is correct |
24 |
Correct |
195 ms |
85028 KB |
Output is correct |
25 |
Correct |
206 ms |
85472 KB |
Output is correct |
26 |
Correct |
221 ms |
87540 KB |
Output is correct |
27 |
Correct |
160 ms |
81336 KB |
Output is correct |
28 |
Correct |
196 ms |
84008 KB |
Output is correct |
29 |
Correct |
206 ms |
85500 KB |
Output is correct |
30 |
Correct |
216 ms |
87008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
152 ms |
80564 KB |
Output is correct |
2 |
Correct |
117 ms |
80412 KB |
Output is correct |
3 |
Correct |
119 ms |
80372 KB |
Output is correct |
4 |
Correct |
117 ms |
80312 KB |
Output is correct |
5 |
Correct |
123 ms |
80332 KB |
Output is correct |
6 |
Correct |
119 ms |
80368 KB |
Output is correct |
7 |
Correct |
126 ms |
80328 KB |
Output is correct |
8 |
Correct |
125 ms |
80320 KB |
Output is correct |
9 |
Correct |
123 ms |
80356 KB |
Output is correct |
10 |
Correct |
124 ms |
80336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 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 |
328 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 |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
324 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 |
340 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 |
460 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
336 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
336 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
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 |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
324 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 |
328 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 |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
190 ms |
82060 KB |
Output is correct |
31 |
Correct |
191 ms |
83780 KB |
Output is correct |
32 |
Correct |
139 ms |
79680 KB |
Output is correct |
33 |
Correct |
187 ms |
83608 KB |
Output is correct |
34 |
Correct |
188 ms |
84168 KB |
Output is correct |
35 |
Correct |
184 ms |
83624 KB |
Output is correct |
36 |
Correct |
179 ms |
82604 KB |
Output is correct |
37 |
Correct |
265 ms |
86692 KB |
Output is correct |
38 |
Correct |
195 ms |
85028 KB |
Output is correct |
39 |
Correct |
206 ms |
85472 KB |
Output is correct |
40 |
Correct |
221 ms |
87540 KB |
Output is correct |
41 |
Correct |
160 ms |
81336 KB |
Output is correct |
42 |
Correct |
196 ms |
84008 KB |
Output is correct |
43 |
Correct |
206 ms |
85500 KB |
Output is correct |
44 |
Correct |
216 ms |
87008 KB |
Output is correct |
45 |
Correct |
152 ms |
80564 KB |
Output is correct |
46 |
Correct |
117 ms |
80412 KB |
Output is correct |
47 |
Correct |
119 ms |
80372 KB |
Output is correct |
48 |
Correct |
117 ms |
80312 KB |
Output is correct |
49 |
Correct |
123 ms |
80332 KB |
Output is correct |
50 |
Correct |
119 ms |
80368 KB |
Output is correct |
51 |
Correct |
126 ms |
80328 KB |
Output is correct |
52 |
Correct |
125 ms |
80320 KB |
Output is correct |
53 |
Correct |
123 ms |
80356 KB |
Output is correct |
54 |
Correct |
124 ms |
80336 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
324 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 |
340 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 |
460 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
1 ms |
468 KB |
Output is correct |
69 |
Correct |
1 ms |
336 KB |
Output is correct |
70 |
Correct |
1 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
336 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
1 ms |
340 KB |
Output is correct |
74 |
Correct |
1 ms |
340 KB |
Output is correct |
75 |
Correct |
1 ms |
340 KB |
Output is correct |
76 |
Correct |
105 ms |
76788 KB |
Output is correct |
77 |
Correct |
105 ms |
76796 KB |
Output is correct |
78 |
Correct |
98 ms |
76136 KB |
Output is correct |
79 |
Correct |
97 ms |
76364 KB |
Output is correct |
80 |
Correct |
101 ms |
77020 KB |
Output is correct |
81 |
Correct |
103 ms |
76372 KB |
Output is correct |
82 |
Correct |
100 ms |
76236 KB |
Output is correct |
83 |
Correct |
100 ms |
76628 KB |
Output is correct |
84 |
Correct |
103 ms |
76556 KB |
Output is correct |
85 |
Correct |
99 ms |
76748 KB |
Output is correct |
86 |
Correct |
95 ms |
76108 KB |
Output is correct |
87 |
Correct |
102 ms |
76004 KB |
Output is correct |
88 |
Correct |
97 ms |
76368 KB |
Output is correct |
89 |
Correct |
98 ms |
76272 KB |
Output is correct |
90 |
Correct |
100 ms |
76108 KB |
Output is correct |
91 |
Correct |
94 ms |
75920 KB |
Output is correct |
92 |
Correct |
202 ms |
85452 KB |
Output is correct |
93 |
Correct |
189 ms |
83780 KB |
Output is correct |
94 |
Correct |
99 ms |
75600 KB |
Output is correct |
95 |
Correct |
100 ms |
77000 KB |
Output is correct |
96 |
Correct |
109 ms |
75996 KB |
Output is correct |
97 |
Correct |
98 ms |
76248 KB |
Output is correct |
98 |
Correct |
107 ms |
76500 KB |
Output is correct |
99 |
Correct |
93 ms |
76108 KB |
Output is correct |
100 |
Correct |
95 ms |
76236 KB |
Output is correct |