#include<bits/stdc++.h>
using namespace std;
struct functie{
long long a , b;
long long operator () (long long x) {
return 1LL * a * x + b;
}
functie(){
a = 0;
b = LLONG_MAX;
}
functie(long long _a ,long long _b){
a = _a;
b = _b;
}
};
const long long N = 1E5 + 5;
const int VMAX = 1e6 + 5;
functie lichao[4 * VMAX];
void ins(long long nod, long long l, long long r, functie f){
long long mid = (l + r)/2;
if(lichao[nod](mid) > f(mid))
swap(lichao[nod], f);
if(l == r)
return;
if(f(l) < lichao[nod](l))
ins(2*nod, l, mid, f);
if(f(r) < lichao[nod](r))
ins(2*nod + 1, mid + 1, r, f);
}
long long querymin(long long nod, long long l, long long r, long long p){
if(p < l || r < p)
return LLONG_MAX;
long long mid = (l + r)/2;
long long cans = lichao[nod](p);
if(l == r)
return cans;
if(p <= mid)
return min(cans, querymin(2*nod, l, mid, p));
else
return min(cans, querymin(2*nod + 1, mid + 1, r, p));
}
long long h[N], dp[N], s[N];
int main()
{
//freopen(".in","r",stdin);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
long long n;
cin>>n;
for(long long i = 1; i<=n; i++)
cin>>h[i];
for(long long i = 1; i<=n; i++){
cin>>s[i];
s[i] += s[i - 1];
}
dp[1] = 0;
ins(1, 1, VMAX, {-2 * h[1], 1LL * h[1] * h[1] + dp[1] - s[1]});
for(long long i = 2; i<=n; i++){
dp[i] = h[i] * h[i] + s[i - 1] + querymin(1, 1, N, h[i]);
ins(1, 1, VMAX, {- 2 * h[i], h[i] * h[i] + dp[i] - s[i]});
}
cout<<dp[n];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
62936 KB |
Output is correct |
2 |
Incorrect |
33 ms |
62924 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
65 ms |
65216 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
62936 KB |
Output is correct |
2 |
Incorrect |
33 ms |
62924 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |