#include <bits/stdc++.h>
using namespace std;
long long seg[4000005], dp[100005], h[100005], w[100005];
vector<pair<long long, long long> > v;
void update(long long i, long long l, long long r, long long x)
{
if (l==r)
{
if (v[x].first*l+v[x].second<v[seg[i]].first*l+v[seg[i]].second)
seg[i]=x;
return;
}
int m=(l+r)/2;
if (v[x].first*m+v[x].second<v[seg[i]].first*m+v[seg[i]].second)
swap(x, seg[i]);
if (v[x].first<v[seg[i]].first)
update(i*2+1, m+1, r, x);
else
update(i*2, l, m, x);
}
long long query(long long i, long long l, long long r, long long x)
{
if (l==r)
return v[seg[i]].first*x+v[seg[i]].second;
long long m=(l+r)/2;
if (x<=m)
return min(v[seg[i]].first*x+v[seg[i]].second, query(i*2, l, m, x));
else
return min(v[seg[i]].first*x+v[seg[i]].second, query(i*2+1, m+1, r, x));
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, sum=0;
cin >> n;
for (long long i=1; i<=n; i++)
cin >> h[i];
for (long long i=1; i<=n; i++)
{
cin >> w[i];
sum+=w[i];
}
dp[1]=h[1]*h[1]-w[1];
v.push_back({1, 1e16});
v.push_back({-2*h[1], dp[1]});
update(1, 0, 1e6, 1);
for (long long i=2; i<=n; i++)
{
dp[i]=query(1, 0, 1e6, h[i])+2*h[i]*h[i]-w[i];
v.push_back({-2*h[i], dp[i]});
update(1, 0, 1e6, i);
}
cout << dp[n]-h[n]*h[n]+sum;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
4772 KB |
Output is correct |
2 |
Correct |
42 ms |
5880 KB |
Output is correct |
3 |
Correct |
42 ms |
5820 KB |
Output is correct |
4 |
Correct |
38 ms |
5724 KB |
Output is correct |
5 |
Correct |
34 ms |
7132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
41 ms |
4772 KB |
Output is correct |
7 |
Correct |
42 ms |
5880 KB |
Output is correct |
8 |
Correct |
42 ms |
5820 KB |
Output is correct |
9 |
Correct |
38 ms |
5724 KB |
Output is correct |
10 |
Correct |
34 ms |
7132 KB |
Output is correct |
11 |
Correct |
51 ms |
7624 KB |
Output is correct |
12 |
Correct |
55 ms |
7368 KB |
Output is correct |
13 |
Correct |
42 ms |
6216 KB |
Output is correct |
14 |
Correct |
51 ms |
7440 KB |
Output is correct |
15 |
Correct |
34 ms |
6820 KB |
Output is correct |
16 |
Correct |
34 ms |
7224 KB |
Output is correct |
17 |
Correct |
27 ms |
5728 KB |
Output is correct |
18 |
Correct |
27 ms |
5708 KB |
Output is correct |