#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_N=1e6+10;
struct line{
ll m,b;
line(ll mm,ll bb){
m=mm; b=bb;
}
ll eval(ll x){
return m*x+b;
}
long double intersect(line l){
return (long double)(b-l.b)/(l.m-m);
}
};
ll val[MAX_N];
int main(){
int n; cin>>n;
for(int i=1;i<=n;i++){
cin>>val[i];
}
deque<line> dq;
dq.push_front({0,0});
ll ans=0;
for(int i=1;i<=n;i++){
int t=dq.size();
line nl={-i,ans};
while(t>=2 && nl.intersect(dq[t-2])<=dq[t-1].intersect(dq[t-2])){
dq.pop_back();
t--;
}
dq.push_back(nl);
while(dq.size()>=2 && dq[0].eval(val[i])>=dq[1].eval(val[i]))
dq.pop_front();
ans=dq.front().eval(val[i])+val[i]*(n+1);
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
304 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 |
332 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 |
204 KB |
Output is correct |
15 |
Correct |
1 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 |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
304 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 |
332 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 |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
251 ms |
13104 KB |
Output is correct |
17 |
Correct |
262 ms |
13976 KB |
Output is correct |
18 |
Correct |
227 ms |
12740 KB |
Output is correct |
19 |
Correct |
259 ms |
13860 KB |
Output is correct |
20 |
Correct |
263 ms |
13824 KB |
Output is correct |
21 |
Correct |
263 ms |
13824 KB |
Output is correct |
22 |
Correct |
248 ms |
13584 KB |
Output is correct |
23 |
Correct |
276 ms |
14528 KB |
Output is correct |
24 |
Correct |
261 ms |
13892 KB |
Output is correct |
25 |
Correct |
290 ms |
14352 KB |
Output is correct |
26 |
Correct |
295 ms |
14676 KB |
Output is correct |
27 |
Correct |
254 ms |
13216 KB |
Output is correct |
28 |
Correct |
271 ms |
14020 KB |
Output is correct |
29 |
Correct |
277 ms |
14220 KB |
Output is correct |
30 |
Correct |
280 ms |
14408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
372 ms |
12524 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |