#include<bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define int long long
using namespace std;
const int N=2e6+5,mod=1e9+7;
int t,a[N],b[N],n;
pair<ll,ll>tree[4*N];
ll f(ll k,ll x,ll b){
return k*x+b;
}
void insert(int u,int l,int r,ll k,ll b) {
if(l==r) {
if(f(tree[u].f,l,tree[u].s) > f(k,l,b)) {
tree[u] = {k,b};
}
return;
}
int mid = (l+r)/2;
if(k < tree[u].f) {
swap(k,tree[u].f);
swap(b,tree[u].s);
}
if(f(k,mid,b) >= f(tree[u].f,mid,tree[u].s)) {
insert(2*u,l,mid,k,b);
}
else {
insert(2*u+1,mid+1,r,tree[u].f,tree[u].s);
tree[u] = {k,b};
}
}
ll getans(int u,int ind,int l,int r) {
if(l==r) {
return f(tree[u].f,ind,tree[u].s);
}
int mid = (l+r)/2;
if(ind <= mid) return min(f(tree[u].f,ind,tree[u].s),getans(2*u,ind,l,mid));
return min(f(tree[u].f,ind,tree[u].s),getans(2*u+1,ind,mid+1,r));
}
void build(int u,int l,int r) {
tree[u] = {1e10,1e17};
if(l==r) return;
int mid = (l+r)/2;
build(2*u,l,mid);
build(2*u+1,mid+1,r);
}
main(){
cin>>n;
int mx = 0;
for(int i=1;i<=n;i++){
cin>>a[i];
mx = max(mx,a[i]);
}
mx *= 2;
for(int i=1;i<=n;i++){
cin>>b[i];
b[i] += b[i-1];
}
build(1,1,mx);
insert(1,1,mx,-2*a[1],-b[1]+a[1]*a[1]);
for(int i=2;i<=n;i++) {
ll ans = getans(1,a[i],1,mx) + a[i]*a[i] + b[i-1];
insert(1,1,mx,-2*a[i],ans-b[i]+a[i]*a[i]);
if(i==n) cout<<ans;
}
}
Compilation message
building.cpp:48:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
48 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
10040 KB |
Output is correct |
2 |
Correct |
97 ms |
10052 KB |
Output is correct |
3 |
Correct |
98 ms |
10052 KB |
Output is correct |
4 |
Correct |
92 ms |
10048 KB |
Output is correct |
5 |
Correct |
93 ms |
18228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
96 ms |
10040 KB |
Output is correct |
7 |
Correct |
97 ms |
10052 KB |
Output is correct |
8 |
Correct |
98 ms |
10052 KB |
Output is correct |
9 |
Correct |
92 ms |
10048 KB |
Output is correct |
10 |
Correct |
93 ms |
18228 KB |
Output is correct |
11 |
Correct |
115 ms |
10052 KB |
Output is correct |
12 |
Correct |
110 ms |
10052 KB |
Output is correct |
13 |
Correct |
86 ms |
1988 KB |
Output is correct |
14 |
Correct |
134 ms |
10064 KB |
Output is correct |
15 |
Correct |
89 ms |
18256 KB |
Output is correct |
16 |
Correct |
88 ms |
18240 KB |
Output is correct |
17 |
Correct |
117 ms |
67488 KB |
Output is correct |
18 |
Correct |
60 ms |
1828 KB |
Output is correct |