This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//https://www.spoj.com/problems/EC_P/
//https://www.spoj.com/problems/tag/scc
#include <bits/stdc++.h>
#define F first
#define S second
#define I insert
#define LL long long
#define PB push_back
#define MP make_pair
const LL N=100005, mod=998244353;
using namespace std;
LL n, m, i, j, k, l, r, t, h[N], a[N], dp[N], sum, ans;
void dpn2(){
for(i=1;i<n;i++){
sum=0;dp[i]=mod;
for(j=i-1;j>=0;j--){
dp[i]=min(dp[i],dp[j]+sum+(h[i]-h[j])*(h[i]-h[j]));
sum+=a[j];
}
}
cout<<dp[n-1]<<endl;
exit(0);
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
for(i=0;i<n;i++)cin>>h[i];
for(i=0;i<n;i++){cin>>a[i];sum+=a[i];}
if(n<=1000)dpn2();
ans=sum+(h[0]-h[n-1])*(h[0]-h[n-1]);
for(i=1;i<n-1;i++){
ans=min(ans,sum-a[i]+(h[0]-h[i])*(h[0]-h[i])+(h[n-1]-h[i])*(h[n-1]-h[i]));
}
for(i=1;i<n-1;i++){
for(j=i+1;j<n-1;j++){
ans=min(ans,sum-a[i]-a[j]+(h[0]-h[i])*(h[0]-h[i])+(h[j]-h[i])*(h[j]-h[i])+(h[j]-h[n-1])*(h[j]-h[n-1]));
}
}
cout<<ans<<endl;
return 0;
}
// IIIIIIIII OOOOO A NN N EEEEEEEEEE
// I O O A A N N N E
// I O O A A N N N E
// I O O A A N N N E
// I O O AAAAAAAAA N N N EEEEEEEE
// I O O A A N N N E
// I O O A A N N N E
// I O O A A N N N E
// IIIIIIIII OOOOO A A N NN EEEEEEEEEE ___ KAPANADZE
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |