#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; }
ld intersect(line h){
return (ld)(n - h.n)/(h.k - k);
}
};
deque <line> dq;
void dodaj(line g){
while(dq.size() >= 2){
line b = dq.back();
dq.pop_back();
line a = dq.back();
dq.pop_back();
if(a.intersect(g) > a.intersect(b)){
dq.push_back(a);
dq.push_back(b);
break;
}
dq.push_back(a);
}
dq.push_back(g);
}
ll query(int x){
while(dq.size() >= 2 && dq[0].eval(x) >= dq[1].eval(x)) dq.pop_front();
return dq.front().eval(x);
}
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);
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];
dodaj(g);
dp[i] = query(id);
}
cout << dp[vec.size() - 1] << "\n";
return 0;
}
Compilation message
Discharging.cpp: In function 'int main()':
Discharging.cpp:62: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]
62 | for(int i=0; i<vec.size(); i++){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
344 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 |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
344 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 |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
93 ms |
15240 KB |
Output is correct |
17 |
Correct |
96 ms |
16188 KB |
Output is correct |
18 |
Correct |
82 ms |
13612 KB |
Output is correct |
19 |
Correct |
94 ms |
16132 KB |
Output is correct |
20 |
Correct |
101 ms |
16832 KB |
Output is correct |
21 |
Correct |
98 ms |
16136 KB |
Output is correct |
22 |
Correct |
93 ms |
15432 KB |
Output is correct |
23 |
Correct |
109 ms |
18528 KB |
Output is correct |
24 |
Correct |
104 ms |
17448 KB |
Output is correct |
25 |
Correct |
113 ms |
17484 KB |
Output is correct |
26 |
Correct |
114 ms |
19244 KB |
Output is correct |
27 |
Correct |
86 ms |
14592 KB |
Output is correct |
28 |
Correct |
96 ms |
16328 KB |
Output is correct |
29 |
Correct |
107 ms |
17716 KB |
Output is correct |
30 |
Correct |
111 ms |
18896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
8012 KB |
Output is correct |
2 |
Correct |
88 ms |
8024 KB |
Output is correct |
3 |
Correct |
92 ms |
8056 KB |
Output is correct |
4 |
Correct |
96 ms |
8116 KB |
Output is correct |
5 |
Correct |
87 ms |
8036 KB |
Output is correct |
6 |
Correct |
87 ms |
8136 KB |
Output is correct |
7 |
Correct |
87 ms |
8140 KB |
Output is correct |
8 |
Correct |
86 ms |
8012 KB |
Output is correct |
9 |
Correct |
87 ms |
8056 KB |
Output is correct |
10 |
Correct |
91 ms |
8108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
344 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 |
340 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 |
340 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 |
328 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
328 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 |
340 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 |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
344 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 |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
93 ms |
15240 KB |
Output is correct |
31 |
Correct |
96 ms |
16188 KB |
Output is correct |
32 |
Correct |
82 ms |
13612 KB |
Output is correct |
33 |
Correct |
94 ms |
16132 KB |
Output is correct |
34 |
Correct |
101 ms |
16832 KB |
Output is correct |
35 |
Correct |
98 ms |
16136 KB |
Output is correct |
36 |
Correct |
93 ms |
15432 KB |
Output is correct |
37 |
Correct |
109 ms |
18528 KB |
Output is correct |
38 |
Correct |
104 ms |
17448 KB |
Output is correct |
39 |
Correct |
113 ms |
17484 KB |
Output is correct |
40 |
Correct |
114 ms |
19244 KB |
Output is correct |
41 |
Correct |
86 ms |
14592 KB |
Output is correct |
42 |
Correct |
96 ms |
16328 KB |
Output is correct |
43 |
Correct |
107 ms |
17716 KB |
Output is correct |
44 |
Correct |
111 ms |
18896 KB |
Output is correct |
45 |
Correct |
90 ms |
8012 KB |
Output is correct |
46 |
Correct |
88 ms |
8024 KB |
Output is correct |
47 |
Correct |
92 ms |
8056 KB |
Output is correct |
48 |
Correct |
96 ms |
8116 KB |
Output is correct |
49 |
Correct |
87 ms |
8036 KB |
Output is correct |
50 |
Correct |
87 ms |
8136 KB |
Output is correct |
51 |
Correct |
87 ms |
8140 KB |
Output is correct |
52 |
Correct |
86 ms |
8012 KB |
Output is correct |
53 |
Correct |
87 ms |
8056 KB |
Output is correct |
54 |
Correct |
91 ms |
8108 KB |
Output is correct |
55 |
Correct |
1 ms |
340 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 |
328 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
1 ms |
328 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 |
340 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 |
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 |
74 ms |
8888 KB |
Output is correct |
77 |
Correct |
76 ms |
8808 KB |
Output is correct |
78 |
Correct |
69 ms |
8816 KB |
Output is correct |
79 |
Correct |
77 ms |
8804 KB |
Output is correct |
80 |
Correct |
84 ms |
8780 KB |
Output is correct |
81 |
Correct |
75 ms |
8756 KB |
Output is correct |
82 |
Correct |
70 ms |
8652 KB |
Output is correct |
83 |
Correct |
73 ms |
8704 KB |
Output is correct |
84 |
Correct |
78 ms |
8784 KB |
Output is correct |
85 |
Correct |
71 ms |
8744 KB |
Output is correct |
86 |
Correct |
72 ms |
8660 KB |
Output is correct |
87 |
Correct |
69 ms |
8732 KB |
Output is correct |
88 |
Correct |
71 ms |
8676 KB |
Output is correct |
89 |
Correct |
71 ms |
8900 KB |
Output is correct |
90 |
Correct |
69 ms |
8784 KB |
Output is correct |
91 |
Correct |
68 ms |
8780 KB |
Output is correct |
92 |
Correct |
101 ms |
16880 KB |
Output is correct |
93 |
Correct |
95 ms |
15856 KB |
Output is correct |
94 |
Correct |
65 ms |
8560 KB |
Output is correct |
95 |
Correct |
72 ms |
8624 KB |
Output is correct |
96 |
Correct |
67 ms |
8540 KB |
Output is correct |
97 |
Correct |
70 ms |
8652 KB |
Output is correct |
98 |
Correct |
71 ms |
8548 KB |
Output is correct |
99 |
Correct |
71 ms |
8624 KB |
Output is correct |
100 |
Correct |
75 ms |
8660 KB |
Output is correct |