이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |