#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<cassert>
#include<limits.h>
#include<cmath>
#include<set>
#include<numeric> //gcd(a,b)
#include<algorithm>
#include<bitset>
#include<stack>
using namespace std;
#define ll long long
#define f first
//#define endl "\n"
#define s second
#define pii pair<int,int>
#define pppiiii pair<pii,pii>
#define ppii pair<int,pii>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
//#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
#define int long long
const int mxn=1e6,inf=1e9,minf=-1e18,Mxn=2e6,lg=63;
int mod=1e9+7;
int root,n;
void setIO(string name){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
struct line{
int m,c;
int get(int x){return (x*m)+c;}
};
struct lichao{
line v[4*mxn+10];
void init(int n){
for(int i=1;i<=4*mxn;i++)v[i]=(line){0,inf};
}
void add(int l,int r,int pos,line x){
int mid=l+(r-l)/2;
bool m=x.get(mid)<v[pos].get(mid);
bool lef=x.get(l)<v[pos].get(l);
if(m)swap(v[pos],x);
if(l==r)return;
if(lef!=m)add(l,mid,pos*2,x);
else add(mid+1,r,pos*2+1,x);
}
int qry(int l,int r,int pos,int x){
int mid=l+(r-l)/2;
if(l==r)return v[pos].get(x);
if(x<=mid)return min(qry(l,mid,pos*2,x),v[pos].get(x));
return min(qry(mid+1,r,pos*2+1,x),v[pos].get(x));
}
}t;
int v1[mxn+10],v2[mxn+10],dp[mxn+10];
int32_t main(){
fastio
int n;cin>>n;
int mx=0;
for(int i=1;i<=n;i++)cin>>v1[i],mx=max(mx,v1[i]);
for(int i=1;i<=n;i++)cin>>v2[i],v2[i]+=v2[i-1];
t.init(mx);
t.add(1,mx,1,(line){-2*v1[1],-v2[1]+(v1[1]*v1[1])});
for(int i=2;i<=n;i++){
dp[i]=t.qry(1,mx,1,v1[i])+(v1[i]*v1[i])+v2[i-1];
t.add(1,mx,1,(line){-2*v1[i],dp[i]-v2[i]+(v1[i]*v1[i])});
}
cout<<dp[n];
}
/*
*/
Compilation message
building.cpp: In function 'void setIO(std::string)':
building.cpp:39:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
building.cpp:40:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
67668 KB |
Output is correct |
2 |
Correct |
10 ms |
67932 KB |
Output is correct |
3 |
Correct |
9 ms |
67676 KB |
Output is correct |
4 |
Correct |
11 ms |
67932 KB |
Output is correct |
5 |
Correct |
10 ms |
67932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
73172 KB |
Output is correct |
2 |
Correct |
40 ms |
73040 KB |
Output is correct |
3 |
Correct |
41 ms |
73052 KB |
Output is correct |
4 |
Correct |
38 ms |
72888 KB |
Output is correct |
5 |
Incorrect |
34 ms |
73040 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
67668 KB |
Output is correct |
2 |
Correct |
10 ms |
67932 KB |
Output is correct |
3 |
Correct |
9 ms |
67676 KB |
Output is correct |
4 |
Correct |
11 ms |
67932 KB |
Output is correct |
5 |
Correct |
10 ms |
67932 KB |
Output is correct |
6 |
Correct |
42 ms |
73172 KB |
Output is correct |
7 |
Correct |
40 ms |
73040 KB |
Output is correct |
8 |
Correct |
41 ms |
73052 KB |
Output is correct |
9 |
Correct |
38 ms |
72888 KB |
Output is correct |
10 |
Incorrect |
34 ms |
73040 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |