#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
using ll = long long;
const ll mod=1e9+7;
const int nx=1e5+5;
struct dak
{
ll a, b;
dak() : a(0), b(0) {}
dak(ll a, ll b) : a(a), b(b) {}
ll cal(ll x)
{
return a*x+b;
}
};
struct lichao
{
vector<dak> nod;
lichao() {}
lichao(int sz) : nod(sz*4+5, dak(0, inf)) {}
void update(int id, int l, int r, dak f)
{
if(l==r)
{
nod[id]=(f.cal(l)<nod[id].cal(l)?f:nod[id]);
return;
}
int mid=(l+r)>>1;
if(f.cal(mid)<nod[id].cal(mid)) swap(nod[id], f);
if(f.a>nod[id].a)
update(id<<1, l, mid, f);
if(f.a<nod[id].a)
update(id<<1|1, mid+1, r, f);
}
ll get(int id, int l, int r, int p)
{
ll cur=nod[id].cal(p);
int mid=(l+r)>>1;
if(l==r) return cur;
if(p<=mid) return min(cur, get(id<<1, l, mid, p));
return min(cur, get(id<<1|1, mid+1, r, p));
}
};
int n;
ll h[nx], a[nx], dp[nx], mx=0;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n;
for(int i = 1; i <= n; i++)
cin>>h[i], mx=max(mx, h[i]);
for(int i = 1; i <= n; i++)
cin>>a[i], a[i]+=a[i-1];
memset(dp, -0x3f, sizeof(dp));
dp[1]=0;
lichao st(mx);
for(int i = 1; i <= n; i++)
{
if(i>1)
{
dp[i]=st.get(1, 0, mx, h[i])+a[i-1]+h[i]*h[i];
}
st.update(1, 0, mx, dak(-2ll*h[i], dp[i]-a[i]+h[i]*h[i]));
}
cout<<dp[n];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |