#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define lb lower_bound
#define ub upper_bound
#define sz(v) int((v).size())
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
const int N = 1e6 + 7;
struct line {
ll m, b;
line() {}
line(ll _m, ll _b) {
m = _m; b = _b;
}
ll operator()(ll x) {
return m * x + b;
}
} tree[N * 4];
void update(int v, int l, int r, line seg) {
if (l == r - 1) {
if (seg(l) < tree[v](l))
tree[v] = seg;
return;
}
int mid = (l + r) >> 1;
if (seg.m > tree[v].m)
swap(seg, tree[v]);
if (tree[v](mid) > seg(mid)) {
swap(seg, tree[v]);
update(v * 2, l, mid, seg);
}
else {
update(v * 2 + 1, mid, r, seg);
}
}
ll get(int v, int l, int r, ll x) {
if (l == r - 1) {
return tree[v](x);
}
int mid = (l + r) >> 1;
if (x < mid) return min(tree[v](x), get(v * 2, l, mid, x));
else return min(tree[v](x), get(v * 2 + 1, mid, r, x));
}
void solve() {
int n;
cin >> n;
vector <ll> h(n), w(n), pref(n);
for (auto &to : h) cin >> to;
for (auto &to : w) cin >> to;
for (int i = 0; i < n; i++) {
pref[i] = w[i];
if (i) pref[i] += pref[i - 1];
}
vector <ll> dp(n, 2e18);
dp[0] = 0;
update(1, 0, N, line(-2 * h[0], dp[0] - pref[0] + h[0] * h[0]));
for (int i = 1; i < n; i++) {
dp[i] = get(1, 0, N, h[i]) + pref[i - 1] + h[i] * h[i];
update(1, 0, N, line(-2 * h[i], dp[i] - pref[i] + h[i] * h[i]));
}
cout << dp[n - 1];
}
int main() {
do_not_disturb
int t = 1;
//~ cin >> t;
while (t--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
3844 KB |
Output is correct |
2 |
Correct |
40 ms |
4648 KB |
Output is correct |
3 |
Correct |
39 ms |
4540 KB |
Output is correct |
4 |
Correct |
48 ms |
4412 KB |
Output is correct |
5 |
Incorrect |
35 ms |
5932 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |