#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
ll n, a[1000009];
struct line{
ll a, b;
ll get(ll x){
return a*x+b;
}
};
ld inter(line x, line y){
return ld(y.b-x.b)/ld(x.a-y.a);
}
vector<line> vec;
void add(line x){
while(vec.size() >= 2){
if(inter(vec[vec.size()-2], x) <= inter(vec[vec.size()-2], vec.back()))
vec.pop_back();
else
break;
}
vec.pb(x);
}
ll get(ll lim, ll x){
ll l = 0, r = vec.size()-1;
while(l < r){
ll m = (l+r)/2;
if(-vec[m].a < lim || vec[m+1].get(x) < vec[m].get(x))
l = m+1;
else
r = m;
}
return vec[l].get(x);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n;
for(ll i = 1; i <= n; ++i)
cin >> a[i];
a[0] = -1;
add({0, 0});
vector<pll> bigs;
bigs.pb({0, 0});
for(ll i = 1; i <= n; ++i){
while(bigs.size() && a[bigs.back().ff] <= a[i]){
bigs.pop_back();
}
ll lim = 0;
ll dp = 1e18;
if(bigs.size()){
lim = bigs.back().ff;
dp = bigs.back().ss;
}
dp = min(get(lim, a[i])+a[i]*n, dp);
add({-i, dp});
bigs.pb({i, dp});
}
cout << bigs.back().ss << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
324 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 |
2 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 |
340 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 |
2 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 |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
153 ms |
13180 KB |
Output is correct |
17 |
Correct |
168 ms |
13984 KB |
Output is correct |
18 |
Correct |
144 ms |
12448 KB |
Output is correct |
19 |
Correct |
171 ms |
13840 KB |
Output is correct |
20 |
Correct |
159 ms |
13764 KB |
Output is correct |
21 |
Correct |
167 ms |
13884 KB |
Output is correct |
22 |
Correct |
157 ms |
13472 KB |
Output is correct |
23 |
Correct |
183 ms |
14500 KB |
Output is correct |
24 |
Correct |
174 ms |
14000 KB |
Output is correct |
25 |
Correct |
167 ms |
14384 KB |
Output is correct |
26 |
Correct |
182 ms |
14656 KB |
Output is correct |
27 |
Correct |
149 ms |
13172 KB |
Output is correct |
28 |
Correct |
174 ms |
13960 KB |
Output is correct |
29 |
Correct |
216 ms |
14176 KB |
Output is correct |
30 |
Correct |
185 ms |
14536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
176 ms |
34356 KB |
Output is correct |
2 |
Correct |
195 ms |
34344 KB |
Output is correct |
3 |
Correct |
215 ms |
34352 KB |
Output is correct |
4 |
Correct |
170 ms |
34304 KB |
Output is correct |
5 |
Correct |
169 ms |
34352 KB |
Output is correct |
6 |
Correct |
176 ms |
34332 KB |
Output is correct |
7 |
Correct |
189 ms |
34352 KB |
Output is correct |
8 |
Correct |
177 ms |
34332 KB |
Output is correct |
9 |
Correct |
181 ms |
34348 KB |
Output is correct |
10 |
Correct |
180 ms |
34308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
324 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 |
2 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 |
340 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 |
236 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
324 KB |
Output is correct |
39 |
Correct |
1 ms |
384 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
324 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 |
2 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 |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
153 ms |
13180 KB |
Output is correct |
31 |
Correct |
168 ms |
13984 KB |
Output is correct |
32 |
Correct |
144 ms |
12448 KB |
Output is correct |
33 |
Correct |
171 ms |
13840 KB |
Output is correct |
34 |
Correct |
159 ms |
13764 KB |
Output is correct |
35 |
Correct |
167 ms |
13884 KB |
Output is correct |
36 |
Correct |
157 ms |
13472 KB |
Output is correct |
37 |
Correct |
183 ms |
14500 KB |
Output is correct |
38 |
Correct |
174 ms |
14000 KB |
Output is correct |
39 |
Correct |
167 ms |
14384 KB |
Output is correct |
40 |
Correct |
182 ms |
14656 KB |
Output is correct |
41 |
Correct |
149 ms |
13172 KB |
Output is correct |
42 |
Correct |
174 ms |
13960 KB |
Output is correct |
43 |
Correct |
216 ms |
14176 KB |
Output is correct |
44 |
Correct |
185 ms |
14536 KB |
Output is correct |
45 |
Correct |
176 ms |
34356 KB |
Output is correct |
46 |
Correct |
195 ms |
34344 KB |
Output is correct |
47 |
Correct |
215 ms |
34352 KB |
Output is correct |
48 |
Correct |
170 ms |
34304 KB |
Output is correct |
49 |
Correct |
169 ms |
34352 KB |
Output is correct |
50 |
Correct |
176 ms |
34332 KB |
Output is correct |
51 |
Correct |
189 ms |
34352 KB |
Output is correct |
52 |
Correct |
177 ms |
34332 KB |
Output is correct |
53 |
Correct |
181 ms |
34348 KB |
Output is correct |
54 |
Correct |
180 ms |
34308 KB |
Output is correct |
55 |
Correct |
1 ms |
236 KB |
Output is correct |
56 |
Correct |
1 ms |
332 KB |
Output is correct |
57 |
Correct |
1 ms |
332 KB |
Output is correct |
58 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
62 |
Correct |
1 ms |
332 KB |
Output is correct |
63 |
Correct |
1 ms |
324 KB |
Output is correct |
64 |
Correct |
1 ms |
384 KB |
Output is correct |
65 |
Correct |
1 ms |
332 KB |
Output is correct |
66 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
72 |
Correct |
1 ms |
332 KB |
Output is correct |
73 |
Correct |
1 ms |
332 KB |
Output is correct |
74 |
Correct |
1 ms |
332 KB |
Output is correct |
75 |
Correct |
1 ms |
332 KB |
Output is correct |
76 |
Correct |
164 ms |
14128 KB |
Output is correct |
77 |
Correct |
164 ms |
14088 KB |
Output is correct |
78 |
Correct |
152 ms |
13552 KB |
Output is correct |
79 |
Correct |
151 ms |
13768 KB |
Output is correct |
80 |
Correct |
167 ms |
14384 KB |
Output is correct |
81 |
Correct |
147 ms |
13604 KB |
Output is correct |
82 |
Correct |
156 ms |
13548 KB |
Output is correct |
83 |
Correct |
171 ms |
13964 KB |
Output is correct |
84 |
Correct |
156 ms |
13920 KB |
Output is correct |
85 |
Correct |
165 ms |
14148 KB |
Output is correct |
86 |
Correct |
149 ms |
13512 KB |
Output is correct |
87 |
Correct |
148 ms |
13380 KB |
Output is correct |
88 |
Correct |
162 ms |
13844 KB |
Output is correct |
89 |
Correct |
162 ms |
13648 KB |
Output is correct |
90 |
Correct |
182 ms |
13580 KB |
Output is correct |
91 |
Correct |
153 ms |
13308 KB |
Output is correct |
92 |
Correct |
181 ms |
14648 KB |
Output is correct |
93 |
Correct |
174 ms |
13860 KB |
Output is correct |
94 |
Correct |
148 ms |
13048 KB |
Output is correct |
95 |
Correct |
167 ms |
14496 KB |
Output is correct |
96 |
Correct |
151 ms |
13372 KB |
Output is correct |
97 |
Correct |
183 ms |
13672 KB |
Output is correct |
98 |
Correct |
168 ms |
13800 KB |
Output is correct |
99 |
Correct |
149 ms |
13560 KB |
Output is correct |
100 |
Correct |
148 ms |
13544 KB |
Output is correct |