#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vii> vvii;
#define task "test"
#define fastIO ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define forw(i, l, r) for( ll i = (l) ; i < (r) ; i++ )
#define forb(i, r, l) for( ll i = (r) ; i >= (l) ; i-- )
#define sz(x) (int)x.size()
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define numBit(x) __builtin_popcount(x)
#define lb lower_bound
#define ub upper_bound
#define ar array
#define endl '\n'
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
const int N = 2e5 + 7;
const ll inf = 1e18;
int n;
ll h[N], w[N], dp[N], totW = 0;
ll sqr(ll x) {
return x * x;
}
int main() {
// fastIO;
scanf("%d", &n);
forw(i, 1, n + 1) scanf("%lld", &h[i]);
forw(i, 1, n + 1) scanf("%lld", &w[i]), totW += w[i];
memset(dp, 0x3f, sizeof dp);
if (n <= 1000) {
dp[1] = -w[1];
forw(i, 1, n + 1) forb(j, i - 1, 1)
dp[i] = min(dp[i], dp[j] + sqr(h[i] - h[j]) - w[i]);
printf("%lld", dp[n] + totW);
}
else {
ll ans = sqr(h[1] - h[n]) - w[1] - w[n] + totW;
forw(W, -20, 21) {
set<ll> s;
forw(i, 2, n) {
ll mid = (h[1] + h[i]) / 2;
ans = min(ans, sqr(h[1] - h[i]) + sqr(h[n] - h[i]) - w[1] - w[i] - w[n] + totW);
auto it = s.upper_bound(mid);
if(it != s.end()) {
ans = min(ans, sqr(h[i] - h[n]) + sqr(*it - h[1]) + sqr(h[i] - *it) - w[1] - w[i] - w[n] - W + totW);
}
it = s.upper_bound(mid);
if(it != s.begin()) {
--it;
ans = min(ans, sqr(h[i] - h[n]) + sqr(*it - h[1]) + sqr(h[i] - *it) - w[1] - w[i] - w[n] - W + totW);
}
if (w[i] == W) s.insert(h[i]);
}
}
printf("%lld", ans);
}
}
/*
Subtask 1: dp[i] = max(dp[j] + (hi - hj) ^ 2 + sum(w[i + 1]...w[j])
Subtask 2: Chọn 2 cây cầu sao cho h[1] + sum(2, i - 1) + h[i] + sum(i + 1, j - 1) + h[j] + sum(j + 1, n - 1) + h[n]
*/
Compilation message
building.cpp: In function 'int main()':
building.cpp:46:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
building.cpp:47:26: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | forw(i, 1, n + 1) scanf("%lld", &h[i]);
| ~~~~~^~~~~~~~~~~~~~~
building.cpp:48:26: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | forw(i, 1, n + 1) scanf("%lld", &w[i]), totW += w[i];
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1868 KB |
Output is correct |
3 |
Correct |
1 ms |
1868 KB |
Output is correct |
4 |
Correct |
2 ms |
1868 KB |
Output is correct |
5 |
Correct |
2 ms |
1868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
312 ms |
3764 KB |
Output is correct |
2 |
Correct |
305 ms |
3892 KB |
Output is correct |
3 |
Correct |
299 ms |
3916 KB |
Output is correct |
4 |
Correct |
82 ms |
3752 KB |
Output is correct |
5 |
Correct |
225 ms |
3780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1868 KB |
Output is correct |
3 |
Correct |
1 ms |
1868 KB |
Output is correct |
4 |
Correct |
2 ms |
1868 KB |
Output is correct |
5 |
Correct |
2 ms |
1868 KB |
Output is correct |
6 |
Correct |
312 ms |
3764 KB |
Output is correct |
7 |
Correct |
305 ms |
3892 KB |
Output is correct |
8 |
Correct |
299 ms |
3916 KB |
Output is correct |
9 |
Correct |
82 ms |
3752 KB |
Output is correct |
10 |
Correct |
225 ms |
3780 KB |
Output is correct |
11 |
Incorrect |
49 ms |
4628 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |