// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
template<class T> using V = vector<T>;
using vi = V<int>;
using pi = pair<int, int>;
using ll = long long;
using vl = V<ll>;
const ll INFL = ll(1e16) + 1008;
const int nax = 1e6 + 6;
const int SQ = 1e3 + 1;
vi oc[nax];
int main() {
cin.tie(0)->sync_with_stdio(0);
int N; cin >> N;
vi A(N); for(auto& x : A) cin >> x;
vi W(N); for(auto& x : W) cin >> x;
vl P = {0}; for(auto& x : W) P.pb(P.back() + x);
auto query = [&](int l, int r) {
if (l > r) return 0LL;
return P[r + 1] - P[l];
};
auto sq = [&](int x) { return x * 1LL * x; };
vl dp(N, INFL); dp[0] = 0;
oc[A[0]].pb(0);
for(int i = 1; i < N; i++) {
int x = A[i];
for(int d = max(-SQ, -x); d <= min(SQ, nax-1-x); d++) {
int y = x + d;
if (sz(oc[y]) == 0) continue;
int j = oc[y].back(); ll cost = sq(x - y) + query(j + 1, i - 1);
// cout << i << " " << j << " => " << cost << endl;
dp[i] = min(dp[i], dp[j] + cost);
}
oc[x].pb(i);
}
cout << dp.back() << nl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23748 KB |
Output is correct |
3 |
Correct |
12 ms |
23700 KB |
Output is correct |
4 |
Correct |
14 ms |
23820 KB |
Output is correct |
5 |
Incorrect |
15 ms |
23764 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
965 ms |
27888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
12 ms |
23748 KB |
Output is correct |
3 |
Correct |
12 ms |
23700 KB |
Output is correct |
4 |
Correct |
14 ms |
23820 KB |
Output is correct |
5 |
Incorrect |
15 ms |
23764 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |