//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;
ll 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
building.cpp:72:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
72 | main()
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
9892 KB |
Output is correct |
2 |
Correct |
36 ms |
9812 KB |
Output is correct |
3 |
Correct |
35 ms |
9904 KB |
Output is correct |
4 |
Correct |
36 ms |
9672 KB |
Output is correct |
5 |
Correct |
25 ms |
12380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
34 ms |
9892 KB |
Output is correct |
7 |
Correct |
36 ms |
9812 KB |
Output is correct |
8 |
Correct |
35 ms |
9904 KB |
Output is correct |
9 |
Correct |
36 ms |
9672 KB |
Output is correct |
10 |
Correct |
25 ms |
12380 KB |
Output is correct |
11 |
Correct |
35 ms |
10076 KB |
Output is correct |
12 |
Correct |
36 ms |
10480 KB |
Output is correct |
13 |
Correct |
27 ms |
9400 KB |
Output is correct |
14 |
Correct |
37 ms |
10588 KB |
Output is correct |
15 |
Correct |
26 ms |
13876 KB |
Output is correct |
16 |
Correct |
25 ms |
12372 KB |
Output is correct |
17 |
Correct |
18 ms |
9308 KB |
Output is correct |
18 |
Correct |
18 ms |
9308 KB |
Output is correct |