This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 5e5 + 10;
const int MOD = 1e9 + 7;
#define int long long
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
const int is_query = -(1ll<<62);
struct Line {
int m, b;
mutable function<const Line*()> succ;
bool operator<(const Line& rhs) const {
if (rhs.b != is_query) return m < rhs.m;
const Line* s = succ();
if (!s) return 0;
int x = rhs.m;
return b - s->b < (s->m - m) * x;
}
};
struct dynamic_hull : public multiset<Line> { // wiint maintain upper hull for maximum
bool bad(iterator y) {
auto z = next(y);
if (y == begin()) {
if (z == end()) return 0;
return y->m == z->m && y->b <= z->b;
}
auto x = prev(y);
if (z == end()) return y->m == x->m && y->b <= x->b;
return (x->b - y->b)*(z->m - y->m) >= (y->b - z->b)*(y->m - x->m);
}
void insert_line(int m, int b) {
auto y = insert({ m, b });
y->succ = [=] { return next(y) == end() ? 0 : &*next(y); };
if (bad(y)) { erase(y); return; }
while (next(y) != end() && bad(next(y))) erase(next(y));
while (y != begin() && bad(prev(y))) erase(prev(y));
}
int eval(int x) {
auto l = *lower_bound((Line) { x, is_query });
return l.m * x + l.b;
}
};
void solve(int tc) {
int n;
cin >> n;
int h[n+1];
int ps[n+1];
ps[0] = 0;
for(int i=1; i<=n; i++) {
cin >> h[i];
}
for(int i=1; i<=n; i++) {
cin >> ps[i];
ps[i] += ps[i-1];
}
int dp[n+1];
dp[1] = 0;
dynamic_hull cht;
cht.insert_line(-h[1], -h[1] * h[1] + ps[1]);
for(int i=2; i<=n; i++) {
dp[i] = -cht.eval(-2 * h[i]) + h[i] * h[i] + ps[i-1];
cht.insert_line(-h[i], -h[i] * h[i] + ps[i] - dp[i]);
}
cout << dp[n] << "\n";
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0);
int t = 1;// cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
/*
6
3 8 7 1 6 6
0 -1 9 1 2 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... |