#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include <cstdint>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
#define int long long
#define double long double
const int mod=1e9+7,mxn=1e6+5,lg=30,inf=1e18,minf=-1e18;
int mode=1;
struct line{
int m,c;
mutable int p;
int get(int x) const{return (m*x)+c;}
bool operator<(line a)const{
if(mode)return m>a.m;
else return p<a.p;
}
};
struct cht{
multiset<line>v;
int div(int a,int b){return (a/b)-(!(a%b==0||(a*b)>=0));}
int intersect(multiset<line>::iterator a,multiset<line>::iterator b){return div((a->c-b->c),(b->m-a->m));}
void cal(multiset<line>::iterator a){a->p=((a==v.end())?inf:intersect(a,next(a)));}
bool bad(multiset<line>::iterator a,multiset<line>::iterator b){
if(b==v.end())return a->p=inf,0;
if(a->m==b->m)return a->c<b->c;
a->p=intersect(a,b);
return a->p>b->p;
}
bool isect(multiset<line>::iterator x,multiset<line>::iterator y)
{
if (y==v.end()) return x->p=inf, 0;
if (x->m==y->m) x->p=(x->c<=y->c)?inf:-inf;
else x->p=div(y->c-x->c, x->m-y->m);
return x->p>=y->p;
}
void add(line nw)
{
auto x=v.insert(nw), y=next(x);
while (isect(x, y)) y=v.erase(y);
if ((y=x)!=v.begin()&&isect(--x, y)) isect(x, v.erase(y));
while ((y=x)!=v.begin()&&(--x)->p>=y->p) isect(x, v.erase(y));
}
int qry(int x){
if(v.empty())return inf;
mode=0;
auto it=v.lb((line){0,0,x})->get(x);
mode=1;
return it;
}
}t;
int v1[mxn+10],v2[mxn+10],dp[mxn+10];
int32_t main(){
fastio
int n;cin>>n;
for(int i=1;i<=n;i++)cin>>v1[i];
for(int i=1;i<=n;i++)cin>>v2[i],v2[i]+=v2[i-1];
t.add((line){-2*v1[1],-v2[1]+(v1[1]*v1[1]),0});
for(int i=2;i<=n;i++){
dp[i]=t.qry(v1[i])+(v1[i]*v1[i])+v2[i-1];
t.add((line){-2*v1[i],dp[i]-v2[i]+(v1[i]*v1[i]),0});
}
cout<<dp[n];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
9432 KB |
Output is correct |
2 |
Correct |
36 ms |
9552 KB |
Output is correct |
3 |
Correct |
35 ms |
9820 KB |
Output is correct |
4 |
Correct |
31 ms |
9564 KB |
Output is correct |
5 |
Correct |
35 ms |
10888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
35 ms |
9432 KB |
Output is correct |
7 |
Correct |
36 ms |
9552 KB |
Output is correct |
8 |
Correct |
35 ms |
9820 KB |
Output is correct |
9 |
Correct |
31 ms |
9564 KB |
Output is correct |
10 |
Correct |
35 ms |
10888 KB |
Output is correct |
11 |
Correct |
32 ms |
9808 KB |
Output is correct |
12 |
Correct |
34 ms |
9912 KB |
Output is correct |
13 |
Correct |
28 ms |
9808 KB |
Output is correct |
14 |
Correct |
33 ms |
10076 KB |
Output is correct |
15 |
Correct |
45 ms |
15952 KB |
Output is correct |
16 |
Correct |
35 ms |
10744 KB |
Output is correct |
17 |
Correct |
19 ms |
10056 KB |
Output is correct |
18 |
Correct |
19 ms |
9820 KB |
Output is correct |