#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define el '\n'
const int N = 1e6 + 24;
const ll INF = 4e18;
int n; ll h[N], w[N];
ll pre[N], dp[N];
struct Line {
mutable ll k, m, p;
bool operator < (const Line& o) const { return k < o.k; }
bool operator < (ll x) const { return p < x; }
};
struct CHT : multiset<Line, less<>> {
static const ll inf = LLONG_MAX;
ll div(ll a, ll b) {
return a / b - ((a ^ b) < 0 && a % b);
}
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = inf, 0;
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(ll k, ll m) {
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
ll get(ll x) {
assert(!empty());
auto l = *lower_bound(x);
return l.k * x + l.m;
}
} cht;
// struct Line {
// ll a, b;
// ll calc(ll x) { return a * x + b; }
// };
// struct Lichao {
// int n; vector<Line> t;
// Lichao(int sz) : t(4 * sz + 24, {0, LLONG_MAX}), n(sz) {}
// void update(int id, int l, int r, Line f) {
// if (l == r) {
// if (f.calc(l) < t[id].calc(l)) t[id] = f;
// return;
// }
// int mid = (l + r) >> 1;
// if (f.calc(mid) < t[id].calc(mid)) swap(f, t[id]);
// if (f.calc(l) < t[id].calc(l)) update(2 * id, l, mid, f);
// else update(2 * id + 1, mid + 1, r, f);
// }
// ll get(int id, int l, int r, int i) {
// int mid = (l + r) >> 1;
// ll cur = t[id].calc(l);
// if (i == mid) return cur;
// else if (i < mid) return min(get(2 * id, l, mid, i), cur);
// else return min(get(2 * id + 1, mid + 1, r, i), cur);
// }
// };
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> h[i];
for (int i = 1; i <= n; i++) cin >> w[i];
for (int i = 1; i <= n; i++) {
pre[i] = pre[i - 1] + w[i];
}
dp[1] = 0;
for (int i = 1; i <= n; i++) {
if (i > 1) dp[i] = h[i] * h[i] + pre[i - 1] - cht.get(h[i]);
cht.add(2 * h[i], -(dp[i] + h[i] * h[i] - pre[i]));
// dp[i] = INF;
// for (int j = 1; j < i; j++) {
// dp[i] = min(dp[i], h[i] * h[i] + pre[i - 1] - 2 * h[i] * h[j] + dp[j] + h[j] * h[j] - pre[j]);
// }
}
cout << dp[n] << el;
}
int main() {
cin.tie(NULL) -> sync_with_stdio(false);
#define task "file"
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int T = 1; // cin >> T;
while (T--) solve();
}
Compilation message (stderr)
building.cpp: In function 'int main()':
building.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
building.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen(task".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... |