#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mid ((l + r) >> 1)
#define tl (node + 1)
#define tr (node + 2 * (mid - l + 1))
const int MAX = 1e6 + 25;
struct line {
int m = 1e10, b = 1e12;
int operator() (int x) { return m * x + b; }
};
vector <line> tree(MAX * 2);
void insert (int l, int r, line a, int node) {
if (l == r) {
if (tree[node](l) > a(l)) {
tree[node] = a;
}
return;
}
if (tree[node].m > a.m) swap(tree[node], a);
if (tree[node](mid) > a(mid)) {
swap(tree[node], a);
insert(mid + 1, r, a, tr);
} else {
insert(l, mid, a, tl);
}
}
int get (int l, int r, int a, int node) {
if (l == r) {
return tree[node](a);
}
if (a <= mid) return min(tree[node](a), get(l, mid, a, tl));
else return min(tree[node](a), get(mid + 1, r, a, tr));
}
int n;
const int MAXN = 1e5 + 25;
int a[MAXN], b[MAXN];
int dp[MAXN];
signed main () {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
cin >> b[i];
b[i] += b[i - 1];
}
insert(0, MAX, {-2 * a[1], dp[1] + a[1] * a[1] - b[1]}, 1);
for (int i = 2; i <= n; i++) {
int val = get(0, MAX, a[i], 1);
dp[i] += val + a[i] * a[i] + b[i - 1];
insert(0, MAX, {-2 * a[i], dp[i] + a[i] * a[i] - b[i]}, 1);
}
cout << dp[n] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
33624 KB |
Output is correct |
2 |
Correct |
7 ms |
33628 KB |
Output is correct |
3 |
Correct |
6 ms |
33624 KB |
Output is correct |
4 |
Correct |
7 ms |
33628 KB |
Output is correct |
5 |
Correct |
6 ms |
33628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
34948 KB |
Output is correct |
2 |
Correct |
66 ms |
35116 KB |
Output is correct |
3 |
Correct |
63 ms |
34900 KB |
Output is correct |
4 |
Correct |
60 ms |
34784 KB |
Output is correct |
5 |
Correct |
55 ms |
35156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
33624 KB |
Output is correct |
2 |
Correct |
7 ms |
33628 KB |
Output is correct |
3 |
Correct |
6 ms |
33624 KB |
Output is correct |
4 |
Correct |
7 ms |
33628 KB |
Output is correct |
5 |
Correct |
6 ms |
33628 KB |
Output is correct |
6 |
Correct |
63 ms |
34948 KB |
Output is correct |
7 |
Correct |
66 ms |
35116 KB |
Output is correct |
8 |
Correct |
63 ms |
34900 KB |
Output is correct |
9 |
Correct |
60 ms |
34784 KB |
Output is correct |
10 |
Correct |
55 ms |
35156 KB |
Output is correct |
11 |
Correct |
72 ms |
35152 KB |
Output is correct |
12 |
Correct |
68 ms |
35056 KB |
Output is correct |
13 |
Correct |
64 ms |
35160 KB |
Output is correct |
14 |
Correct |
79 ms |
35260 KB |
Output is correct |
15 |
Correct |
54 ms |
34944 KB |
Output is correct |
16 |
Correct |
55 ms |
34928 KB |
Output is correct |
17 |
Correct |
53 ms |
34932 KB |
Output is correct |
18 |
Correct |
52 ms |
35152 KB |
Output is correct |