#include <bits/stdc++.h>
using namespace std;
#define MOD1 1000000007
#define MOD2 998244353
#define fir first
#define sec second
#define pushf push_front
#define pushb push_back
#define popf pop_front
#define popb pop_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define lbound(v, x) lower_bound(all(v), x) - v.begin()
#define ubound(v, x) upper_bound(all(v), x) - v.begin()
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define FOR1(a) for (int _ = 0; _ < (a); ++_)
#define FOR2(i, a) for (int i = 0; i < (a); ++i)
#define FOR3(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR1(a) for (int _ = (a)-1; _ >= 0; --_)
#define RFOR2(i, a) for (int i = (a)-1; i >= 0; --i)
#define RFOR3(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define overload3(a, b, c, d, ...) d
// Always choose the fourth argument to call. Hence, which function to call is determined by the number of given arguments.
#define REP(...) overload3(__VA_ARGS__, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define RREP(...) overload3(__VA_ARGS__, RFOR3, RFOR2, RFOR1)(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
typedef queue<int> qi;
typedef queue<ll> qll;
typedef queue<pii> qpii;
typedef queue<pll> qpll;
typedef deque<int> dqi;
typedef deque<ll> dqll;
typedef deque<pii> dqpii;
typedef deque<pll> dqpll;
typedef priority_queue<int> pqi;
typedef priority_queue<ll> pqll;
typedef priority_queue<pii> pqpii;
typedef priority_queue<pll> pqpll;
typedef priority_queue<int, vi, greater<int> > r_pqi;
typedef priority_queue<ll, vll, greater<ll> > r_pqll;
typedef priority_queue<pii, vpii, greater<pii> > r_pqpii;
typedef priority_queue<pll, vpll, greater<pll> > r_pqpll;
struct line {
ll m, c;
line() {
m = 0, c = 3e18;
}
line(ll _m, ll _c) : m(_m), c(_c) {}
ll operator ()(ll x) {
return m * x + c;
}
};
double intersect(line a, line b) {
return (double)(b.c - a.c) / (a.m - b.m);
}
struct node {
ll s, e, m;
line val;
node *l, *r;
node(ll _s, ll _e) {
s = _s;
e = _e;
m = (s+e)>>1;
l = nullptr, r = nullptr;
}
void update(line a) {
if(val.m < a.m) {
swap(val, a);
}
if(s == e) {
if(val(m) >= a(m)) {
val = a;
}
return;
}
if(val(m) >= a(m)) {
if(l == nullptr) {
l = new node(s, m);
}
swap(a, val);
l->update(a);
}
else{
if(r == nullptr) {
r = new node(m+1, e);
}
r->update(a);
}
}
ll query(ll p) {
if(s == e) {
return val(p);
}
if(l == nullptr) {
return val(p);
}
if(p <= m) {
if(l == nullptr) {
return val(p);
}
return min(val(p), l->query(p));
}
else {
if(r == nullptr) {
return val(p);
}
return min(val(p), r->query(p));
}
}
}*root;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
const int CT = 1e9+5;
root = new node(-CT, CT);
int n;
ll tot = 0;
cin >> n;
vi h(n), w(n), dp(n);
REP(i, n) cin >> h[i];
REP(i, n) {
cin >> w[i];
tot += w[i];
}
dp[0] = -w[0];
root->update(line(-2 * h[0], dp[0] + h[0] * h[0]));
REP(i, 1, n) {
dp[i] = root->query(h[i]) - w[i] + h[i] * h[i];
root->update(line(-2 * h[i], dp[i] + h[i] * h[i]));
}
cout << tot + dp[n-1];
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |