Submission #444022

#TimeUsernameProblemLanguageResultExecution timeMemory
444022penguinhackerBuilding Bridges (CEOI17_building)C++14
0 / 100
65 ms2276 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

const int mxN=1e5;
const ll INF=1e18;
int n, h[mxN], w[mxN];

struct Line {
	ll m, b;
	mutable ll p;
	bool operator<(const Line& o) const { return m>o.m; }
	bool operator<(ll x) const { return p<x; }
};

set<Line, less<>> s;

ll ix(set<Line>::iterator l1, set<Line>::iterator l2) {
	assert(l1->m>l2->m);
	ll x=l2->b-l1->b, y=l1->m-l2->m;
	return x>=0?(x+y-1)/y:x/y;
}

bool bad(set<Line>::iterator it) {
	return it!=s.end()&&it!=s.begin()&&next(it)!=s.end()&&ix(it, next(it))<=ix(prev(it), it);
}

void ins(ll m, ll b) {
	Line cur={m, b};
	auto it=s.lower_bound(cur);
	if (it!=s.end()&&it->m==m) {
		if (it->b<=b)
			return;
		it=s.erase(it);
	}
	it=s.insert(it, cur);
	while(it!=s.begin()&&bad(prev(it))) s.erase(prev(it));
	while(next(it)!=s.end()&&bad(next(it))) s.erase(next(it));
	it->p=it==s.begin()?-INF:ix(prev(it), it);
	if (next(it)!=s.end())
		next(it)->p=ix(it, next(it));
}

ll qry(ll x) {
	auto it=s.lower_bound(x);
	if (it==s.end()||it->p>x) {
		assert(it!=s.begin());
		--it;
	}
	return it->m*x+it->b;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for (int i=0; i<n; ++i)
		cin >> h[i];
	for (int i=0; i<n; ++i)
		cin >> w[i];
	ins(-2*h[0], (ll)h[0]*h[0]);
	for (int i=1; i<n-1; ++i) {
		ll dp=qry(h[i])-w[i]+(ll)h[i]*h[i];
		ins(-2*h[i], dp+(ll)h[i]*h[i]);
	}
	ll ans=qry(h[n-1])+(ll)h[n-1]*h[n-1];
	cout << ans+accumulate(w+1, w+n-1, 0ll);
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...