#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_N=1e6+10;
const int MAX_P=MAX_N*4;
const int MAX=1e9;
ll val[MAX_N];
ll n;
ll mdp[MAX_N];
ll coste(ll i,ll x){
ll tx=n+1; tx*=x;
ll b=mdp[i-1]-i*x;
return tx+b;
}
int time(int i,int j){
int l=0,r=1e9+10;
while(r-l>1){
int mid=(l+r)/2;
if(coste(i,mid)>=coste(j,mid)) r=mid;
else l=mid;
}
return r;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
for(int i=0;i<n;i++){
int a; cin>>a;
val[i+1]=a;
}
mdp[0]=0;
deque<int> dq;
for(int i=1;i<=n;i++){
int t=dq.size();
while(t>=3 && time(dq[t-2],dq[t-1])>=time(dq[t-2],i)){
dq.pop_back(); t--;
}
dq.push_back(i);
while(dq.size()>=2 && coste(dq[0],val[i])>=coste(dq[1],val[i])){
dq.pop_front();
}
mdp[i]=coste(dq.front(),val[i]);
}
cout<<mdp[n]<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
393 ms |
15944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
- |