#include <bits/stdc++.h>
// #include <ext/rope>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
using namespace std;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define bit(mask, i) ((mask >> i) & 1)
#define el '\n'
#define F first
#define S second
template <class X, class Y> bool maximize(X &x, const Y &y) { return (x < y ? x = y, 1 : 0); }
template <class X, class Y> bool minimize(X &x, const Y &y) { return (x > y ? x = y, 1 : 0); }
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;
const int MULTI = 0;
const ld eps = 1e-9;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; // R D L U
const int ddx[4] = {-1, 1, 1, -1}, ddy[4] = {1, 1, -1, -1}; // UR DR DL UL
const char cx[4] = {'R', 'D', 'L', 'U'};
const ll base = 31;
const int nMOD = 2;
const ll mods[] = {(ll)1e9 + 10777, (ll)1e9 + 19777, (ll)1e9 + 3, (ll)1e9 + 3777};
const int N = 1e5 + 5;
const int M = 1e6;
int n, h[N], w[N];
ll p[N], dp[N];
struct line {
ll a, b;
line(ll a = 0, ll b = 0) : a(a), b(b) {}
ll calc(ll x) {
return a * x + b;
}
};
struct LichaoTree {
struct node {
line tr;
int lpt, rpt, l, r;
node(int l = 0, int r = 0) : tr(0, LLONG_MIN), lpt(-1), rpt(-1), l(l), r(r) {}
};
vector<node> tree;
int timer;
LichaoTree(int l = 0, int r = 0) {
timer = 0;
tree.push_back(node(l, r));
}
void update(int id, line f) {
if (tree[id].l == tree[id].r) {
tree[id].tr = (f.calc(tree[id].l) > tree[id].tr.calc(tree[id].l) ? f : tree[id].tr);
return;
}
int mid = (tree[id].l + tree[id].r) >> 1;
if (f.calc(mid) > tree[id].tr.calc(mid)) swap(tree[id].tr, f);
if (f.a < tree[id].tr.a) {
if (tree[id].lpt == -1) tree[id].lpt = ++timer, tree.push_back(node(tree[id].l, mid));
update(tree[id].lpt, f);
}
if (f.a > tree[id].tr.a) {
if (tree[id].rpt == -1) tree[id].rpt = ++timer, tree.push_back(node(mid + 1, tree[id].r));
update(tree[id].rpt, f);
}
}
ll query(int id, int pos) {
ll cur = tree[id].tr.calc(pos);
int mid = (tree[id].l + tree[id].r) >> 1;
if (tree[id].l == tree[id].r) return cur;
if (pos <= mid)
return max(cur, tree[id].lpt == -1 ? LLONG_MIN : query(tree[id].lpt, pos));
else
return max(cur, tree[id].rpt == -1 ? LLONG_MIN : query(tree[id].rpt, pos));
}
};
void solve() {
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> h[i];
for (int i = 1; i <= n; ++i)
cin >> w[i], p[i] = p[i - 1] + w[i];
LichaoTree LCT(0, M);
LCT.update(0, line(2 * h[1], p[1] - 1LL * h[1] * h[1]));
for (int i = 2; i <= n; ++i) {
dp[i] = 1LL * h[i] * h[i] + p[i - 1] - LCT.query(0, h[i]);
LCT.update(0, line(2 * h[i], p[i] - dp[i] - 1LL * h[i] * h[i]));
}
cout << dp[n];
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (!MULTI) solve();
else {
int test; cin >> test;
while (test--) 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... |