#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("%I64d", res);
}
int main(){
readInput();
solve();
return 0;
}
Compilation message
building.cpp: In function 'void solve()':
building.cpp:91:17: warning: format '%d' expects argument of type 'int', but argument 2 has type 'll' {aka 'long long int'} [-Wformat=]
91 | printf("%I64d", res);
| ~~~~^ ~~~
| | |
| int ll {aka long long int}
| %I64lld
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 |
1 |
Correct |
5 ms |
6636 KB |
Output is correct |
2 |
Correct |
4 ms |
6636 KB |
Output is correct |
3 |
Correct |
4 ms |
6636 KB |
Output is correct |
4 |
Correct |
5 ms |
6636 KB |
Output is correct |
5 |
Correct |
5 ms |
6656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
9668 KB |
Output is correct |
2 |
Correct |
103 ms |
9700 KB |
Output is correct |
3 |
Correct |
94 ms |
9572 KB |
Output is correct |
4 |
Correct |
81 ms |
9444 KB |
Output is correct |
5 |
Incorrect |
85 ms |
9828 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
6636 KB |
Output is correct |
2 |
Correct |
4 ms |
6636 KB |
Output is correct |
3 |
Correct |
4 ms |
6636 KB |
Output is correct |
4 |
Correct |
5 ms |
6636 KB |
Output is correct |
5 |
Correct |
5 ms |
6656 KB |
Output is correct |
6 |
Correct |
92 ms |
9668 KB |
Output is correct |
7 |
Correct |
103 ms |
9700 KB |
Output is correct |
8 |
Correct |
94 ms |
9572 KB |
Output is correct |
9 |
Correct |
81 ms |
9444 KB |
Output is correct |
10 |
Incorrect |
85 ms |
9828 KB |
Output isn't correct |