This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0)
typedef long long ll;
#define f first
#define s second
#define LOGN 21
const ll MOD = 1e9 + 7;
const ll MAXN = 1e6 + 100;
#define int long long
struct Line {
int m, c;
int eval(int x) {
return m * x + c;
}
Line() : m(0), c(1000000000000LL) { }
Line(int _m, int _c) : m(_m), c(_c) { }
};
vector<int> h, w;
Line sg[4*MAXN];
void add_line(int k, int l, int r, Line new_line) {
if (sg[k].eval(l) <= new_line.eval(l) && sg[k].eval(r) <= new_line.eval(r))
return ;
if (sg[k].eval(l) > new_line.eval(l) && sg[k].eval(r) > new_line.eval(r)) {
swap(sg[k], new_line);
return ;
}
int mid = (l + r) / 2;
if (sg[k].eval(l) < new_line.eval(l))
swap(sg[k], new_line);
if (sg[k].eval(mid) > new_line.eval(mid))
add_line(2*k+1, mid+1, r, new_line);
else {
swap(sg[k], new_line);
add_line(2*k, l, mid, new_line);
}
}
int get(int k, int l, int r, int x) {
if (l == r)
return sg[k].eval(x);
int ans = sg[k].eval(x);
if (x <= (l + r) / 2)
ans = min(ans, get(2*k, l, (l+r)/2, x));
else
ans = min(ans, get(2*k+1, (l+r)/2+1, r, x));
return ans;
}
signed main() {
fast;
int N;
cin >> N;
h = vector<int>(N+1, 0);
w = vector<int>(N+1, 0);
for (int i = 0; i < 4*MAXN; i++)
sg[i] = Line();
for (int i = 1; i <= N; i++)
cin >> h[i];
for (int i = 1; i <= N; i++) {
cin >> w[i];
w[i] += w[i-1];
}
ll ans = 0;
add_line(1, 1, 1000000, Line(-2 * h[1], h[1] * h[1] - w[1]));
for (int i = 2; i <= N; i++) {
ans = h[i] * h[i] + w[i-1] + get(1, 1, 1000000, h[i]);
add_line(1, 1, 1000000, Line(-2 * h[i], ans + h[i] * h[i] - w[i]));
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |