#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = 1e18;
ll s(int x) {
return (ll)x * x;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> h(n), w(n);
for (int &i : h) cin >> i;
for (int &i : w) cin >> i;
if (n <= 1000) {
vector<ll> dp(n, inf);
dp[0] = -w[0];
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
dp[i] = min(dp[i], dp[j] + s(h[i] - h[j]) - w[i]);
}
}
for (int i : w) dp[n - 1] += i;
cout << dp[n - 1];
} else {
ll ans = inf;
vector<ll> mx(41, -inf);
for (int i = 1; i < n - 1; i++) {
for (int j = 0; j <= 40; j++) {
ans = min(ans, s(j - 20 - h[0]) + s(h[i] - h[n - 1]) + s(j - 20 - h[i]) - mx[j] - w[i]);
}
mx[h[i] + 20] = max(mx[h[i] + 20], (ll)w[i]);
}
for (int i = 1; i < n - 1; i++) {
ans += w[i];
}
cout << ans;
}
return 6/22;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
12 ms |
2140 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Runtime error |
12 ms |
2140 KB |
Execution killed with signal 11 |
7 |
Halted |
0 ms |
0 KB |
- |