#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(dr - st == 1)
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, 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(dr - st == 1)
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, dr, 2*p+1) );
}
int main()
{
long long maxH = 1e6 + 1;
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 |
24 ms |
62948 KB |
Output is correct |
2 |
Correct |
25 ms |
62924 KB |
Output is correct |
3 |
Correct |
27 ms |
62840 KB |
Output is correct |
4 |
Correct |
25 ms |
62884 KB |
Output is correct |
5 |
Correct |
25 ms |
62904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
66260 KB |
Output is correct |
2 |
Correct |
101 ms |
66252 KB |
Output is correct |
3 |
Correct |
108 ms |
66276 KB |
Output is correct |
4 |
Correct |
90 ms |
66180 KB |
Output is correct |
5 |
Correct |
86 ms |
66128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
62948 KB |
Output is correct |
2 |
Correct |
25 ms |
62924 KB |
Output is correct |
3 |
Correct |
27 ms |
62840 KB |
Output is correct |
4 |
Correct |
25 ms |
62884 KB |
Output is correct |
5 |
Correct |
25 ms |
62904 KB |
Output is correct |
6 |
Correct |
97 ms |
66260 KB |
Output is correct |
7 |
Correct |
101 ms |
66252 KB |
Output is correct |
8 |
Correct |
108 ms |
66276 KB |
Output is correct |
9 |
Correct |
90 ms |
66180 KB |
Output is correct |
10 |
Correct |
86 ms |
66128 KB |
Output is correct |
11 |
Correct |
110 ms |
66512 KB |
Output is correct |
12 |
Correct |
100 ms |
66260 KB |
Output is correct |
13 |
Correct |
103 ms |
66384 KB |
Output is correct |
14 |
Correct |
111 ms |
66420 KB |
Output is correct |
15 |
Correct |
88 ms |
66100 KB |
Output is correct |
16 |
Correct |
88 ms |
66124 KB |
Output is correct |
17 |
Correct |
85 ms |
66312 KB |
Output is correct |
18 |
Correct |
85 ms |
66272 KB |
Output is correct |