#include <bits/stdc++.h>
#define int long long
using namespace std;
const int nmax = 1e5;
const int hmax = 1e6;
const int oo = LLONG_MAX;
int n;
int h[nmax + 5], w[nmax + 5];
int sp[nmax + 5];
int dp[nmax + 5];
pair<int,int> lc[4 * hmax + 5];
int get_val(pair<int,int> f, int x)
{
if(f.first==oo)
{
return oo;
}
return f.first * x + f.second;
}
bool is_smaller(pair<int,int> f, pair<int,int> g, int x)
{
return get_val(f, x) < get_val(g, x);
}
int query(int poz, int nod, int st, int dr)
{
if(st==dr)
{
return get_val(lc[nod], poz);
}
int mij = (st + dr) >> 1;
if(poz <= mij)
{
return min(get_val(lc[nod],poz), query(poz,nod*2,st,mij));
}
return min(get_val(lc[nod],poz), query(poz,nod*2+1,mij+1,dr));
}
void update(pair<int,int> f, int nod, int st, int dr)
{
int mij = (st + dr) >> 1;
bool okst = is_smaller(f, lc[nod], st);
bool okmij = is_smaller(f, lc[nod], mij);
if(okmij)
{
swap(f,lc[nod]);
}
if(st==dr)
{
return;
}
if(okst!=okmij)
{
update(f,nod*2,st,mij);
}
else
{
update(f,nod*2+1,mij+1,dr);
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef home
freopen("nr.in","r",stdin);
freopen("nr.out","w",stdout);
#endif // home
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>h[i];
}
for(int i=1;i<=n;i++)
{
cin>>w[i];
sp[i] = sp[i - 1] + w[i];
}
for(int i=1;i<=4*hmax;i++)
{
lc[i] = {oo,oo};
}
dp[1] = 0;
update({-2 * h[1],dp[1] + 1LL * h[1] * h[1] - sp[1]},1,1,hmax);
for(int i=2;i<=n;i++)
{
dp[i] = query(h[i],1,1,hmax) + 1LL * h[i] * h[i] + sp[i - 1];
update({-2 * h[i],dp[i] + 1LL * h[i] * h[i] - sp[i]},1,1,hmax);
}
cout<<dp[n]<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
63068 KB |
Output is correct |
2 |
Correct |
9 ms |
62928 KB |
Output is correct |
3 |
Correct |
10 ms |
63064 KB |
Output is correct |
4 |
Correct |
9 ms |
63068 KB |
Output is correct |
5 |
Correct |
9 ms |
63320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
66044 KB |
Output is correct |
2 |
Correct |
53 ms |
66012 KB |
Output is correct |
3 |
Correct |
49 ms |
66132 KB |
Output is correct |
4 |
Correct |
40 ms |
66132 KB |
Output is correct |
5 |
Correct |
38 ms |
66128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
63068 KB |
Output is correct |
2 |
Correct |
9 ms |
62928 KB |
Output is correct |
3 |
Correct |
10 ms |
63064 KB |
Output is correct |
4 |
Correct |
9 ms |
63068 KB |
Output is correct |
5 |
Correct |
9 ms |
63320 KB |
Output is correct |
6 |
Correct |
46 ms |
66044 KB |
Output is correct |
7 |
Correct |
53 ms |
66012 KB |
Output is correct |
8 |
Correct |
49 ms |
66132 KB |
Output is correct |
9 |
Correct |
40 ms |
66132 KB |
Output is correct |
10 |
Correct |
38 ms |
66128 KB |
Output is correct |
11 |
Correct |
49 ms |
67320 KB |
Output is correct |
12 |
Correct |
48 ms |
67152 KB |
Output is correct |
13 |
Correct |
39 ms |
67196 KB |
Output is correct |
14 |
Correct |
49 ms |
67152 KB |
Output is correct |
15 |
Correct |
38 ms |
66856 KB |
Output is correct |
16 |
Correct |
47 ms |
66984 KB |
Output is correct |
17 |
Correct |
35 ms |
67152 KB |
Output is correct |
18 |
Correct |
37 ms |
67152 KB |
Output is correct |