Submission #411127

#TimeUsernameProblemLanguageResultExecution timeMemory
411127BlagojceBuilding Bridges (CEOI17_building)C++11
100 / 100
164 ms13716 KiB
#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)


using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int i_inf = 1e9;
const ll inf = 1e18;
const ll mod = 1e9+7;
const ld eps = 1e-13;
const ld pi  = 3.14159265359;

mt19937 _rand(time(NULL));
clock_t z;

const int mxn = 2e5+5;

int n;
ll h[mxn];
ll w[mxn];

ll dp[mxn];

ll pref[mxn];

ll p(ll a){
	return a*a;
}




const ll is_query = -(1LL<<62);
struct Line {
    ll 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;
        ll x = rhs.m;
        return b - s->b < (s->m - m) * x;
    }
};
struct HullDynamic : public multiset<Line> { 
    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 1.0 * (x->b - y->b)*(z->m - y->m) >= 1.0 * (y->b - z->b)*(y->m - x->m);
    }
    void insert_line(ll m, ll 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));
    }
    ll eval(ll x) {
        auto l = *lower_bound((Line) { x, is_query });
        return l.m * x + l.b;
    }
}hull;

void solve(){
	cin >> n;
	fr(i, 0, n){
		cin >> h[i];
	}
	fr(i, 0, n){
		cin >> w[i];
	}
	pref[0] = w[0];
	fr(i, 1, n){
		pref[i] = pref[i-1]+w[i];
	}
	
	dp[0] = 0;
	hull.insert_line(2*h[0], -(dp[0] + p(h[0])-pref[0]));
	
	
	fr(i, 1, n){
		dp[i] = -hull.eval(h[i]) + p(h[i]) + pref[i] - w[i];
		
		
		hull.insert_line(2*h[i], -(dp[i] + p(h[i]) - pref[i]));
		
	}
	cout<<dp[n-1]<<endl;
	
	
	
	
	
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	solve();

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...