#include <bits/stdc++.h>
using namespace std;
struct lineS
{
long long m=0, n=1e18;
lineS(){}
lineS(long long m, long long n)
{
this->m=m;
this->n=n;
}
};
vector<lineS> lichao;
long long n;
long long h[100005], val[100005];
long long dp[100005];
long long f(long long x, lineS lin)
{
return x * lin.m + lin.n;
}
void addLine(long long st, long long dr, long long p, lineS nou)
{
long long mij = (st+dr)/2;
if( f(mij, nou) < f(mij, lichao[p]) )
swap(nou, lichao[p]);
if(st == dr)
return;
if( f(st, nou) < f(st, lichao[p]) )
addLine(st, mij, 2*p, nou);
if( f(dr, nou) < f(dr, lichao[p]))
addLine(mij+1, dr, 2*p+1, nou);
}
long long query(long long x, long long st, long long dr, long long p)
{
//cout<<st<<' '<<dr<<' '<<x<<'\n';
if(st == dr)
return f(x, lichao[p]);
long long mij = (st+dr)/2;
if(x <= mij)
return min( f(x, lichao[p]), query(x, st, mij, 2*p) );
else
return min( f(x, lichao[p]), query(x, mij+1, dr, 2*p+1) );
}
int main()
{
long long maxH = 1e6;
lichao.resize(4 * maxH + 4);
cin>>n;
for(int i=1;i<=n;i++)
cin>>h[i];
for(int i=1;i<=n;i++)
cin>>val[i];
for(int i=1;i<=n;i++)
{
if(i == 1)
dp[i] = -val[i];
else
{
//cout<<"query "<<h[i]<<' '<<query(h[i], 0, maxH, 1)<<'\n';
dp[i] = query(h[i], 0, maxH, 1) + h[i]*h[i] - val[i];
}
//cout<<"add "<<-2 * h[i]<<' '<<dp[i] + h[i]*h[i]<<'\n';
addLine(0, maxH, 1, lineS(-2 * h[i], dp[i] + h[i]*h[i]));
}
long long ans = dp[n];
for(int i=1;i<=n;i++)
ans += val[i];
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
62940 KB |
Output is correct |
2 |
Correct |
25 ms |
62876 KB |
Output is correct |
3 |
Correct |
25 ms |
62952 KB |
Output is correct |
4 |
Correct |
28 ms |
62952 KB |
Output is correct |
5 |
Correct |
26 ms |
62932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
66184 KB |
Output is correct |
2 |
Correct |
120 ms |
66264 KB |
Output is correct |
3 |
Correct |
96 ms |
66276 KB |
Output is correct |
4 |
Correct |
94 ms |
66128 KB |
Output is correct |
5 |
Correct |
90 ms |
66124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
62940 KB |
Output is correct |
2 |
Correct |
25 ms |
62876 KB |
Output is correct |
3 |
Correct |
25 ms |
62952 KB |
Output is correct |
4 |
Correct |
28 ms |
62952 KB |
Output is correct |
5 |
Correct |
26 ms |
62932 KB |
Output is correct |
6 |
Correct |
120 ms |
66184 KB |
Output is correct |
7 |
Correct |
120 ms |
66264 KB |
Output is correct |
8 |
Correct |
96 ms |
66276 KB |
Output is correct |
9 |
Correct |
94 ms |
66128 KB |
Output is correct |
10 |
Correct |
90 ms |
66124 KB |
Output is correct |
11 |
Correct |
111 ms |
66388 KB |
Output is correct |
12 |
Correct |
115 ms |
66248 KB |
Output is correct |
13 |
Correct |
91 ms |
66380 KB |
Output is correct |
14 |
Correct |
122 ms |
66364 KB |
Output is correct |
15 |
Correct |
90 ms |
66048 KB |
Output is correct |
16 |
Correct |
85 ms |
66144 KB |
Output is correct |
17 |
Correct |
103 ms |
66256 KB |
Output is correct |
18 |
Correct |
81 ms |
66308 KB |
Output is correct |