#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 = 0x3f3f3f3f;
int n;
ll h[N], w[N], dp[N], totW = 0;
ll sqr(int x) {
return 1LL * 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 = inf;
forw(W, -20, 21) {
set<int> s;
forw(i, 2, n) {
int mid = h[1] + h[i] >> 1;
ll tmp = sqr(h[i] - h[n]) - w[i] - w[n];
auto it = s.upper_bound(mid);
if(it != s.end()) {
ans = min(ans, tmp + sqr(*it - h[1]) + sqr(h[i] - *it) - w[1] - W + totW);
}
it = s.upper_bound(mid);
if(it != s.begin()) {
--it;
ans = min(ans, tmp + sqr(*it - h[1]) + sqr(h[i] - *it) - w[1] - 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:62:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
62 | int mid = h[1] + h[i] >> 1;
| ~~~~~^~~~~~
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];
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
3 ms |
1868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
313 ms |
3800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
3 ms |
1868 KB |
Output is correct |
6 |
Incorrect |
313 ms |
3800 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |