#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll INF = 1e18;
const int maxV = 1e6 + 1;
int n;
struct line {
ll A,B;
};
ll getVal(line X, ll y) {
return X.A*y + X.B;
}
line t[4*maxV];
void update(int v, int tl, int tr, line X) {
if (tl == tr) {
if (getVal(X,tl) < getVal(t[v],tl)) t[v] = X;
return;
}
int tm = (tl + tr)/2;
if (t[v].A < X.A) swap(t[v],X);
if (getVal(t[v],tm) > getVal(X,tm)) {
swap(t[v],X);
update(2*v,tl,tm,X);
} else {
update(2*v+1,tm+1,tr,X);
}
}
ll getMin(int v, int tl, int tr, ll pos) {
if (tl == tr) return getVal(t[v],pos);
int tm = (tl + tr)/2;
return min(getVal(t[v],pos),getMin(2*v,tl,tm,pos));
}
void build(int v, int tl, int tr) {
if (tl == tr) {
t[v] = {0,INF};
return;
}
int tm = (tl + tr)/2;
build(2*v, tl, tm);
build(2*v+1,tm+1,tr);
t[v] = {0,INF};
}
const int maxn = 1e5 + 7;
ll dp[maxn];
ll H[maxn], C[maxn];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
ll maxH = 0;
for (int i = 1; i <= n; i++) {
cin >> H[i];
maxH = max(maxH,H[i]);
}
for (int i = 1; i <= n; i++) {
cin >> C[i];
C[i] += C[i-1];
}
build(1,0,maxH);
line X;
X.A = H[1];
X.B = -C[1] + H[1]*H[1];
update(1,0,maxH,X);
for (int i = 2; i <= n; i++) {
dp[i] = getMin(1,0,maxH,-2*H[i]) + H[i]*H[i] + C[i-1];
X.A = H[i];
X.B = dp[i] - C[i] + H[i]*H[i];
update(1,0,maxH,X);
}
cout << dp[n];
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
7716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |