#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
const int INF = 1e18;
void solve(){
int n;
cin >> n;
int h[n],w[n];
for(int i = 0;i < n;i++){
cin >> h[i];
}
for(int i = 0;i < n;i++){
cin >> w[i];
}
int pref[n + 1];
pref[0] = 0;
for(int i = 0;i < n;i++){
pref[i + 1] = pref[i] + w[i];
}
if(n <= 1000){
int dp[n + 1];
dp[0] = 0;
for(int i = 1;i < n;i++){
for(int j = 0;j < i;j++){
if(j == 0){
dp[i] = (h[i] - h[j]) * (h[i] - h[j]) + pref[i] - pref[j + 1];
} else{
dp[i] = min(dp[i],(h[i] - h[j]) * (h[i] - h[j]) + pref[i] - pref[j + 1] + dp[j]);
}
}
}
cout << dp[n - 1] << "\n";
return;
}
int ans = INF;
for(int i = 1;i < n - 1;i++){
int check = (h[i] - h[0]) * (h[i] - h[0]) + pref[i] - pref[1];
check += (h[i] - h[n - 1]) * (h[i] - h[n - 1]) + pref[n - 1] - pref[i + 1];
ans = min(ans,check);
}
int check = (h[n - 1] - h[0]) * (h[n - 1] - h[0]) + pref[n - 1] - pref[1];
ans = min(ans,check);
cout << ans << "\n";
}
//(h[i] - h[j]) * (h[i] - h[j]) + pref[i] - pref[j + 1]
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int t = 1;
//~ cin >> t;
while(t--){
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
2652 KB |
Output is correct |
2 |
Correct |
12 ms |
2648 KB |
Output is correct |
3 |
Incorrect |
12 ms |
2652 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
13 ms |
2652 KB |
Output is correct |
7 |
Correct |
12 ms |
2648 KB |
Output is correct |
8 |
Incorrect |
12 ms |
2652 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |