Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- int k[100002];
- int a[100002], b[100002];
- signed main() {
- ios::sync_with_stdio(0);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n;
- cin >> n;
- int cc = 0;
- for (int i = 0; i < n; i++) {
- cin >> k[i];
- cc += k[i];
- }
- for (int i = 0; i < n; i++)
- cin >> a[i];
- for (int i = 0; i < n; i++)
- cin >> b[i];
- int x;
- cin >> x;
- int l = 0, r = 100000000;
- while (l < r) {
- int m = (l + r) / 2;
- int cur = 0;
- for (int i = 0; i < n; i++) {
- cur += min(a[i], (m + 1) / 2);
- if (x <= m)
- cur += b[i];
- }
- if (2 * cur > cc) {
- r = m;
- } else {
- l = m + 1;
- }
- }
- int cur = 0;
- ll ans = 0;
- for (int i = 0; i < n; i++) {
- int y = min(a[i], l / 2);
- cur += y;
- ans += 1ll * y * y;
- if (x <= l - 1) {
- cur += b[i];
- ans += 1ll * x * b[i];
- }
- }
- for (int i = 0; i < n; i++) {
- if (2 * cur > cc)
- break;
- if ((l & 1) && 2 * a[i] - 1 >= l) {
- cur++;
- ans += l;
- }
- if (x == l && 2 * cur <= cc) {
- ans += 1ll * min(cc / 2 - cur + 1, b[i]) * x;
- cur += min(cc / 2 - cur + 1, b[i]);
- }
- }
- if (2 * cur <= cc) {
- cout << -1;
- } else {
- cout << ans;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement