#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define ll long long
#define ull unsigned long long
#define ld long double
#define f first
#define s second
#define endl '\n'
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define read_file freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// template<class T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
// __builtin_popcount() 1s cnt
// __builtin_clz() 0s left
// __builtin_ctz(x) 0s right
struct node{
int a, b;
node *left, *right;
node(int a, int b){
this->a=a; this->b=b;
this->left=nullptr; this->right=nullptr;
}
node(node* oth){
this->a=oth->a; this->b=oth->b;
this->left=oth->left; this->right=oth->right;
}
};
int eval(int a, int b, int x){
return a*x+b;
}
int eval(node* curr, int x){
return eval(curr->a, curr->b, x);
}
node* add(node* curr, int l, int r, int a, int b){
if(curr==nullptr) return new node(a, b);
int m=(l+r)/2;
if(eval(curr, m)>eval(a, b, m)){swap(curr->a, a); swap(curr->b, b);}
if(eval(a, b, l)<eval(curr, l)){
curr->left=add(curr->left, l, m, a, b);
}
else if(eval(a, b, r)<eval(curr, r)){
curr->right=add(curr->right, m+1, r, a, b);
}
return curr;
}
int get(node* curr, int l, int r, int x){
if(curr==nullptr) return 1e18;
int m=(l+r)/2;
int ans=eval(curr, x);
if(x<=m) ans=min(ans, get(curr->left, l, m, x));
else ans=min(ans, get(curr->right, m+1, r, x));
return ans;
}
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 sum=0; for(int i=0; i<n; i++) sum+=w[i];
node* root = new node(-2*h[0], h[0]*h[0]-w[0]);
for(int i=1; i<n-1; i++){
int ansi=get(root, 0, 1e6, h[i]);
root=add(root, 0, 1e6, -2*h[i], h[i]*h[i]-w[i]+(h[i]*h[i]+ansi));
}
cout<<sum+h[n-1]*h[n-1]-w[n-1]+get(root, 0, 1e6, h[n-1]);
}
int32_t main(){
fast;
int t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |