This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Ai_2007
//Make the impossible possible
#include<bits/stdc++.h>
#define ll long long
#define fo(i,m,n) for(int i=m; i<=n; i++)
#define fod(i,m,n) for(int i=m; i>=n; i--)
#define fi first
#define se second
#define Nmax 1000001
#define pb push_back
#define pii pair<int,int>
using namespace std;
const int maxn=1e6+5;
const ll INFL=1e18;
int n,h[maxn],w[maxn],pre[maxn],dp[maxn];
struct line {
ll m, b;
line() { m = 0, b = INFL; }
line(ll m, ll b) : m(m), b(b) { }
ll get(ll x) { return m * x + b; }
};
struct LiChao {
struct node {
node *l, *r;
line x;
node(line x) : x(x) { l = r = 0; }
};
node *t = new node(line());
void upd(line x, node *&v, int l = 0, int r = maxn) {
if (!v) { v = new node(x); return; }
int m = (l + r) / 2;
bool lf = x.get(l) < v->x.get(l);
bool md = x.get(m) < v->x.get(m);
if (md) swap(x, v->x);
if (l == r) return;
if (lf != md) upd(x, v->l, l, m);
else upd(x, v->r, m+1, r);
}
ll get(int x, node *&v, int l = 0, int r = maxn) {
if (!v) return INFL;
int m = (l + r) / 2;
if (l == r) return v->x.get(x);
if (x < m) return min(v->x.get(x), get(x, v->l, l, m));
else return min(v->x.get(x), get(x, v->r, m+1, r));
}
void add(line x) { return upd(x, t); }
ll get(int x) { return get(x, t); }
}cht;
void solve()
{
cin >> n;
fo(i, 1, n) cin >> h[i];
fo(i, 1, n)
{
cin >> w[i];
pre[i] = pre[i - 1] + w[i];
}
cht.add({-2 * h[1], h[1] * h[1] - w[1]});
dp[1] = 0;
fo(i, 2, n)
{
dp[i] = cht.get(h[i]) + pre[i - 1] + h[i] * h[i];
cht.add({-2 * h[i],h[i] * h[i] - pre[i] + dp[i]});
}
cout << dp[n];
}
main()
{
// freopen("bridgepol.inp","r",stdin);
// freopen("bridgepol.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
Compilation message (stderr)
building.cpp:72:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
72 | main()
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |