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>
#define mp make_pair
#define X first
#define Y second
using namespace std;
typedef long long ll;
typedef pair <int, int> ii;
const int N = 1e5 + 1;
const ll INF = 1e18;
struct line{
ll a, b;
line(ll _a = 0, ll _b = 0){
a = _a;
b = _b;
}
ll y(int x){
return a * x + b;
}
};
struct segmentTree{
line d[N * 4];
int x[N];
void build(int id, int l, int r, int _x[]){
d[id] = line(0, INF);
if (l == r){
x[l] = _x[l];
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid, _x);
build(id << 1 | 1, mid + 1, r, _x);
}
void addLine(int id, int l, int r, line val){
if (val.y(x[l]) >= d[id].y(x[l]) && val.y(x[r]) >= d[id].y(x[r]))
return;
if (val.y(x[l]) <= d[id].y(x[l]) && val.y(x[r]) <= d[id].y(x[r])){
d[id] = val;
return;
}
int mid = (l + r) >> 1;
addLine(id << 1, l, mid, val);
addLine(id << 1 | 1, mid + 1, r, val);
}
ll getMin(int id, int l, int r, int pos){
if (pos < l || r < pos)
return INF;
ll res = d[id].y(x[pos]);
if (l == r)
return res;
int mid = (l + r) >> 1;
res = min(res, getMin(id << 1, l, mid, pos));
res = min(res, getMin(id << 1 | 1, mid + 1, r, pos));
return res;
}
} ST;
int n, h[N], w[N];
int x[N], nX;
ll s[N];
void readInput(){
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &h[i]);
for(int i = 1; i <= n; i++)
scanf("%d", &w[i]);
}
void solve(){
for(int i = 1; i <= n; i++){
s[i] = s[i - 1] + w[i];
x[i] = h[i];
}
sort(x + 1, x + 1 + n);
nX = unique(x + 1, x + 1 + n) - (x + 1);
ll res = 0;
ST.build(1, 1, nX, x);
ST.addLine(1, 1, nX, line(-2 * h[1], (ll) h[1] * h[1] - s[1]));
for(int i = 2; i <= n; i++){
int pos = lower_bound(x + 1, x + 1 + nX, h[i]) - x;
res = ST.getMin(1, 1, nX, pos) + (ll) h[i] * h[i] + s[i - 1];
ST.addLine(1, 1, nX, line(-2 * h[i], res + (ll) h[i] * h[i] - s[i]));
}
printf("%lld", res);
}
int main(){
readInput();
solve();
return 0;
}
Compilation message (stderr)
building.cpp: In function 'void readInput()':
building.cpp:68:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
68 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
building.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
70 | scanf("%d", &h[i]);
| ~~~~~^~~~~~~~~~~~~
building.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
72 | scanf("%d", &w[i]);
| ~~~~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |