//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 |
1 |
Correct |
3 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
424 KB |
Output is correct |
4 |
Correct |
4 ms |
424 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3067 ms |
2036 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
424 KB |
Output is correct |
4 |
Correct |
4 ms |
424 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Execution timed out |
3067 ms |
2036 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |