Submission #230553

#TimeUsernameProblemLanguageResultExecution timeMemory
230553islingrBuilding Bridges (CEOI17_building)C++14
100 / 100
72 ms9868 KiB
#include <iostream>
#include <set>
#include <vector>
using namespace std;

#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define trav(x, v) for (auto &x : v)

using ll = long long;

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 LineContainer : multiset<Line, less<>> {
	const ll inf = 1e18;
	ll div(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
	bool isect(iterator x, iterator y) {
		if (y == end()) { x->p = inf; return false; }
		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 query(ll x) {
		auto l = *lower_bound(x);
		return l.k * x + l.m;
	}
};

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n; cin >> n;
	vector<ll> f(n), h(n), w(n);
	LineContainer q;
	trav(x, h) cin >> x;
	trav(x, w) cin >> x;
	rep(i, 1, n) w[i] += w[i - 1];
	q.add(2 * h[0], - f[0] - h[0] * h[0] + w[0]);
	rep(i, 1, n) {
		f[i] = h[i] * h[i] + w[i - 1] - q.query(h[i]);
		q.add(2 * h[i], - f[i] - h[i] * h[i] + w[i]);
	}
	cout << f[n - 1];
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...