#include <bits/stdc++.h>
using namespace std;
/*
Ben Watson
Handle codeforces : quangminh98
Mua Code nhu mua Florentino !!
*/
#define ll long long
const string name = "test";
void solve();
signed main()
{
if (fopen((name + ".inp").c_str(), "r"))
{
freopen((name + ".inp").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
return 0;
}
// main program
const ll oo = 0x3f3f3f3f3f3f3f3f;
struct LiChao
{
struct line
{
int a;
ll b;
line() : a(0), b(oo) {};
line(int a, ll b) : a(a), b(b) {};
ll calc(int x) { return 1ll * a * x + b; }
};
vector<line> st;
int n;
LiChao(int n) : n(n) { st.resize(4 * n + 1); }
void add(int head, int l, int r, line seg)
{
if (l == r)
{
if (seg.calc(l) < st[head].calc(l))
swap(seg, st[head]);
return;
}
int mid = l + r >> 1;
if (seg.a < st[head].a)
swap(seg, st[head]);
if (seg.calc(mid) < st[head].calc(mid))
{
swap(seg, st[head]);
add(head << 1 | 1, mid + 1, r, seg);
}
add(head << 1, l, mid, seg);
}
void add(int a, ll b) { add(1, 1, n, line(a, b)); }
ll query(int head, int l, int r, int pos)
{
if (l == r)
return st[head].calc(pos);
int mid = l + r >> 1;
if (pos <= mid)
return min(st[head].calc(pos), query(head << 1, l, mid, pos));
return min(st[head].calc(pos), query(head << 1 | 1, mid + 1, r, pos));
}
ll query(int pos) { return query(1, 1, n, pos); }
};
void solve()
{
int n; cin >> n;
int h[n + 1], w[n + 1];
vector<ll> prefix(n + 1, 0);
for (int i = 1; i <= n; i++)
cin >> h[i];
for (int i = 1; i <= n; i++)
{
cin >> w[i];
prefix[i] = prefix[i - 1] + w[i];
}
LiChao lichao(1e6);
vector<ll> dp(n + 1, 0);
lichao.add(-2 * h[1], -prefix[1] + 1ll * h[1] * h[1]);
for (int i = 2; i <= n; i++)
{
dp[i] = prefix[i - 1] + 1ll * h[i] * h[i] + lichao.query(h[i]);
lichao.add(-2 * h[i], -prefix[i] + 1ll * h[i] * h[i] + dp[i]);
}
cout << dp[n] << '\n';
}
Compilation message (stderr)
building.cpp: In function 'int main()':
building.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen((name + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
building.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |