/*
* @Author: hungeazy
* @Date: 2025-03-16 23:37:37
* @Last Modified by: hungeazy
* @Last Modified time: 2025-03-16 23:52:05
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
using namespace std;
using namespace __gnu_pbds;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define int long long
#define ll long long
#define ull unsigned long long
#define sz(x) x.size()
#define sqr(x) (1LL * (x) * (x))
#define all(x) x.begin(), x.end()
#define fill(f,x) memset(f,x,sizeof(f))
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define FOD(i,r,l) for(int i=r;i>=l;i--)
#define debug(x) cout << #x << " = " << x << '\n'
#define ii pair<int,int>
#define iii pair<int,ii>
#define di pair<ii,ii>
#define vi vector<int>
#define vii vector<ii>
#define mii map<int,int>
#define fi first
#define se second
#define pb push_back
#define MOD 1000000007
#define __lcm(a,b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define MASK(i) (1LL << (i))
#define c_bit(i) __builtin_popcountll(i)
#define BIT(x,i) ((x) & MASK(i))
#define SET_ON(x,i) ((x) | MASK(i))
#define SET_OFF(x,i) ((x) & ~MASK(i))
#define oo 1e18
#define name ""
#define endl '\n'
#define time() cerr << endl << "-------------Time:" << 1000.0 * clock() / CLOCKS_PER_SEC << "ms.";
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
template <class T> using ordered_set = tree <T, null_type, less_equal <T>, rb_tree_tag,tree_order_statistics_node_update>;
const int N = (int)1e6+10;
int n,h[N],w[N];
/**
dp[i] = dp[j]+sqr(h[i]-h[j])+pre[i-1]-pre[j]
dp[i] = -2*h[j]*h[i]+dp[j]+sqr(h[j])-pre[j]+sqr(h[i])+pre[i-1];
**/
namespace hungeazy {
int pre[N];
struct Line {
int a,b;
Line() {a = 0, b = oo;}
int compute(int x) {return a*x+b;}
};
struct LCT_MIN {
Line st[N<<2];
void update(int id, int l, int r, Line L)
{
if (l == r)
{
if (st[id].compute(l) > L.compute(l)) swap(st[id],L);
return;
}
int mid = (l+r)>>1;
if (st[id].a > L.a) swap(st[id],L);
if (st[id].compute(mid) > L.compute(mid))
{
swap(st[id],L);
update(id<<1|1,mid+1,r,L);
}
else update(id<<1,l,mid,L);
}
int get(int id, int l, int r, int x)
{
if (l == r) return st[id].compute(x);
int mid = (l+r)>>1;
if (x <= mid) return min(get(id<<1,l,mid,x),st[id].compute(x));
else return min(get(id<<1|1,mid+1,r,x),st[id].compute(x));
}
} LiChaoTree;
void solve(void)
{
FOR(i,1,n) pre[i] = pre[i-1]+w[i];
Line l;
l.a = -2*h[1];
l.b = sqr(h[1])-w[1];
LiChaoTree.update(1,1,N-10,l);
int ans = 0;
FOR(i,2,n)
{
ans = LiChaoTree.get(1,1,N-10,h[i])+sqr(h[i])+pre[i-1];
l.a = -2*h[i];
l.b = ans+sqr(h[i])-pre[i];
LiChaoTree.update(1,1,N-10,l);
}
cout << ans;
}
}
signed main()
{
fast;
if (fopen(name".inp","r"))
{
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
cin >> n;
FOR(i,1,n) cin >> h[i];
FOR(i,1,n) cin >> w[i];
hungeazy::solve();
time();
return 0;
}
// ██░ ██ █ ██ ███▄ █ ▄████
//▓██░ ██▒ ██ ▓██▒ ██ ▀█ █ ██▒ ▀█▒
//▒██▀▀██░▓██ ▒██░▓██ ▀█ ██▒▒██░▄▄▄░
//░▓█ ░██ ▓▓█ ░██░▓██▒ ▐▌██▒░▓█ ██▓
//░▓█▒░██▓▒▒█████▓ ▒██░ ▓██░░▒▓███▀▒
// ▒ ░░▒░▒░▒▓▒ ▒ ▒ ░ ▒░ ▒ ▒ ░▒ ▒
// ▒ ░▒░ ░░░▒░ ░ ░ ░ ░░ ░ ▒░ ░ ░
// ░ ░░ ░ ░░░ ░ ░ ░ ░ ░ ░ ░ ░
// ░ ░ ░ ░ ░ ░
컴파일 시 표준 에러 (stderr) 메시지
building.cpp: In function 'int main()':
building.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
124 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
building.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
125 | freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |