#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define rep(i, n) for(int i = 1; i <= n; ++i)
#define forn(i, l, r) for(int i = l; i <= r; ++i)
#define ford(i, r, l) for(int i = r; i >= l; --i)
#define FOR(i, n) for(int i = 0; i < n; ++i)
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define pb push_back
#define task "test"
#define endl "\n"
#define sz(a) int(a.size())
#define bit(i, mask) (mask >> i & 1)
#define all(a) (a).begin(), (a).end()
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
const int N = 1e6 + 2;
const int S = N * 81 + 100;
const int M = 101;
const int K = 1e2 + 1;
const int MOD = 1e9 + 9;
const ll MOD2 = 1e9 + 9;
const int INF = 1e13 + 9999;
const ll LINF = 1e18 + 9999;
const int offset = N;
const int LIM = 1e6 + 2;
const int AL = 26;
const ll P = 1e15;
const int LOG = 16;
int n;
int dp[N], w[N], h[N], sum[N];
bool bad(pii x, pii y, pii z)
{
return (double)(z.se - x.se) / (x.fi - z.fi) > (double)(y.se - x.se) / (x.fi - y.fi) ;
}
int val(pii x, int y)
{
return x.fi * y + x.se;
}
int top = 0;
pii st[N];
void add(pii line)
{
int l = 1, r = top - 1, res = top;
while(l <= r)
{
int mid = l + r >> 1;
if(bad(st[mid - 1], st[mid], line))
res = mid, r = mid - 1;
else l = mid + 1;
}
top = res + 1;
st[res] = line;
}
void solve()
{
cin >> n;
rep(i, n) cin >> h[i];
rep(i, n) cin >> w[i], sum[i] = sum[i - 1] + w[i];
rep(i, n)
{
if(i == 1) dp[i] = 0;
else
{
int l = 0, r = top - 2, res = val(st[top - 1], h[i]);
while(l <= r)
{
int mid = l + r >> 1;
if(val(st[mid], h[i]) >= val(st[mid + 1], h[i]))
l = mid + 1;
else r = mid - 1;
minimize(res, val(st[mid], h[i]));
minimize(res, val(st[mid + 1], h[i]));
}
dp[i] = res + sum[i - 1] + h[i] * h[i];
}
pii new_line = {-2 * h[i], h[i] * h[i] + dp[i] - sum[i]};
add(new_line);
}
cout << dp[n];
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen("building.inp", "r", stdin);
//freopen("building.out", "w", stdout);
int TC = 1;
while(TC--)
{
solve();
cout << endl;
//reset
//reset
//reset
}
return 0;
}
Compilation message
building.cpp: In function 'void add(std::pair<long long int, long long int>)':
building.cpp:64:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
64 | int mid = l + r >> 1;
| ~~^~~
building.cpp: In function 'void solve()':
building.cpp:87:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
87 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
3444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |