// ----------------------- TEMPLATE NO.4 ---------------------------
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 100;
const int MX = 1e6 + 2;
const ll oo = 1e16;
// loops
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define ROF(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
// others :>
#define nl '\n'
//----------------------------------SOLUTION-----------------------------------//
int n;
ll w[N], h[N], dp[N], pre[N];
struct line{
ll a, b;
line() : a(0), b(oo) {}
line(ll a, ll b) : a(a), b(b) {}
ll calc(ll x) { return a * x + b; }
ll slope() { return a; }
} f[4 * MX];
#define mid ((lx + rx) / 2)
void update(int idx, int lx, int rx, line x){
if(lx == rx){
f[idx] = (x.calc(lx) < f[idx].calc(lx) ? x : f[idx]);
return;
}
if(f[idx].calc(mid) > x.calc(mid)) swap(x, f[idx]);
if(f[idx].slope() < x.slope())
update(idx<<1, lx, mid, x);
if(f[idx].slope() > x.slope())
update(idx<<1|1, mid+1, rx, x);
}
ll query(int idx, int lx, int rx, int p){
ll cur = f[idx].calc(p);
if(lx == rx) return cur;
if(p <= mid)
return min(cur, query(idx<<1, lx, mid, p));
else
return min(cur, query(idx<<1|1, mid+1, rx, p));
}
ll sqr(ll x){
return x * x;
}
void solve(){
cin >> n;
FOR(i, 1, n) cin >> h[i];
FOR(i, 1, n) cin >> w[i];
FOR(i, 1, n) pre[i] = pre[i-1] + w[i];
// dp[i] = min(dp[j] + h[i] ^ 2 - 2 * h[i] * h[j] + h[j] ^ 2 + pre[i-1] - pre[j])
// dp[i] = min(dp[j] + h[j] ^ 2 - pre[j] - 2 * h[i] * h[j]) + h[i] ^ 2 + pre[i-1]
update(1, 0, MX, line(-2 * h[1], sqr(h[1]) - pre[1]) );
FOR(i, 2, n){
dp[i] = query(1, 0, MX, h[i]) + sqr(h[i]) + pre[i-1];
update(1, 0, MX, line(-2 * h[i], dp[i] + sqr(h[i]) - pre[i]));
}
cout << dp[n];
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define Wayzz "name"
if (fopen (Wayzz".inp", "r")) {
freopen (Wayzz".inp", "r", stdin);
freopen (Wayzz".out", "w", stdout);
}
int T = 1;
while(T--) solve();
cerr <<"\nTime Elapse: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
Compilation message
building.cpp: In function 'int main()':
building.cpp:79:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen (Wayzz".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
building.cpp:80:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | freopen (Wayzz".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
64080 KB |
Output is correct |
2 |
Correct |
9 ms |
64080 KB |
Output is correct |
3 |
Correct |
9 ms |
64092 KB |
Output is correct |
4 |
Correct |
9 ms |
64248 KB |
Output is correct |
5 |
Correct |
9 ms |
64080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
66120 KB |
Output is correct |
2 |
Correct |
41 ms |
66120 KB |
Output is correct |
3 |
Correct |
43 ms |
66216 KB |
Output is correct |
4 |
Correct |
43 ms |
66028 KB |
Output is correct |
5 |
Correct |
38 ms |
66128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
64080 KB |
Output is correct |
2 |
Correct |
9 ms |
64080 KB |
Output is correct |
3 |
Correct |
9 ms |
64092 KB |
Output is correct |
4 |
Correct |
9 ms |
64248 KB |
Output is correct |
5 |
Correct |
9 ms |
64080 KB |
Output is correct |
6 |
Correct |
46 ms |
66120 KB |
Output is correct |
7 |
Correct |
41 ms |
66120 KB |
Output is correct |
8 |
Correct |
43 ms |
66216 KB |
Output is correct |
9 |
Correct |
43 ms |
66028 KB |
Output is correct |
10 |
Correct |
38 ms |
66128 KB |
Output is correct |
11 |
Correct |
49 ms |
67408 KB |
Output is correct |
12 |
Correct |
51 ms |
67072 KB |
Output is correct |
13 |
Correct |
45 ms |
67184 KB |
Output is correct |
14 |
Correct |
53 ms |
67144 KB |
Output is correct |
15 |
Correct |
38 ms |
66868 KB |
Output is correct |
16 |
Correct |
38 ms |
66888 KB |
Output is correct |
17 |
Correct |
27 ms |
67152 KB |
Output is correct |
18 |
Correct |
26 ms |
67164 KB |
Output is correct |