This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define ull unsigned long long
#define pll pair<ll, ll>
#define mp make_pair
#define ll long long
#define pb push_back
#define deb(x) cout << #x << " = " << x << endl
#define all(x) x.begin(), x.end()
#define ld long double
const ll mod = (ll)1e9 + 7;
const ll BASE = 47;
const ll inf = (ll)1e18;
const long double e = 2.718281828459;
const long double pi = 3.141592653;
const ld EPS = 1e-9;
using namespace std;
template <class T>
istream& operator>>(istream &in, vector<T> &arr) {
for (T &cnt : arr) {
in >> cnt;
}
return in;
};
struct line {
ll k, b;
ll val(ll x) {
return x * k + b;
}
};
struct lichao {
vector<line> t;
lichao(ll n) {
t.resize(4 * n, {0, inf});
}
void add(ll v, ll l, ll r, line cnt) {
ll mid = (l + r) / 2;
bool le = cnt.val(l) < t[v].val(l);
bool m = cnt.val(mid) < t[v].val(mid);
if (m) {
swap(t[v], cnt);
}
if (l == r) {
return;
}
if (le != m) {
add(v * 2, l, mid, cnt);
} else {
add(v * 2 + 1, mid + 1, r, cnt);
}
}
ll get_min(ll v, ll l, ll r, ll x) {
if (l == r) {
return t[v].val(x);
}
ll mid = (l + r) / 2;
if (x <= mid) {
return min(t[v].val(x), get_min(v * 2, l, mid, x));
} else {
return min(t[v].val(x), get_min(v * 2 + 1, mid + 1, r, x));
}
}
};
void solve() {
ll n;
cin >> n;
vector<ll> h(n), w(n);
cin >> h >> w;
ll MAXH = 1000000;
lichao kek(MAXH + 1);
vector<ll> dp(n);
dp[0] = 0;
kek.add(1, 0, MAXH, {-2 * h[0], h[0] * h[0] - w[0]});
for (int i = 1; i < n; i++) {
dp[i] = kek.get_min(1, 0, MAXH, h[i]) + h[i] * h[i] - w[i];
kek.add(1, 0, MAXH, {-2 * h[i], dp[i] + h[i] * h[i]});
}
cout << dp[n - 1] + accumulate(all(w), 0LL);
}
int main() {
#ifndef LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#else
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cout.precision(30);
ll seed = time(0);
//cerr << "Seed: " << seed << "\n";
srand(seed);
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... |