This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
using namespace std;
#define int long long
const int N = 1e6 + 500;
int dp[N];
int h[N];
int pre[N];
bool seen[N];
int val[N];
int dist(int i,int j){
return (h[i] - h[j]) * (h[i] - h[j]);
}
int point(int i){
return pre[i] - pre[i-1];
}
void sub1(int n){
dp[1] = pre[n];
for (int i=2;i<=n;i++){
dp[i] = 1e17;
for (int j=1;j<i;j++)
dp[i] = min(dp[i],dp[j] + dist(i,j) - point(i));
}
cout<<dp[n]<<endl;
exit(0);
}
void sub2(int n){
dp[1] = pre[n];
int ans = dist(1,n) + pre[n];
for (int i=2;i<n;i++)
ans = min(ans,pre[n] + dist(1,i) + dist(i,n) - point(i));
for (int i=2;i<=n;i++)
dp[i] = pre[n] - point(i) + dist(i,1);
for (int i=2;i<n;i++){
int v = -point(i) + dist(i,n);
for (int j=max(h[i]-1000,0ll);j<=h[i] + 1000;j++)
if (seen[j])
ans = min(ans,v + val[j] + (h[i] - j) * (h[i] - j));
if (!seen[h[i]])
val[h[i]] = dp[i];
seen[h[i]] = true;
val[h[i]] = min(val[h[i]],dp[i]);
}
cout<<ans<<endl;
exit(0);
}
signed main(){
int n;
cin>>n;
for (int i=1;i<=n;i++)
cin>>h[i];
for (int i=1;i<=n;i++){
cin>>pre[i];
pre[i] *= (i != 1 and i != n);
pre[i] += pre[i-1];
}
if (n <= 1000)
sub1(n);
sub2(n);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |