Guest User

Untitled

a guest
Aug 16th, 2025
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #ifdef HORI
  4. #include "../lib/hori.h"
  5. #else
  6. #define debug(...)
  7. #endif
  8. #define int int64_t
  9. using vi = vector<int>;
  10. #define FOR(i, a, b) for (int i = (a); i < (b); i++)
  11. #define all(x) x.begin(), x.end()
  12. #define nl '\n'
  13. auto vec(auto&& v, int n) {return vector(n, v);}
  14. auto vec(auto&& v, int n, auto... m) {return vector(n, vec(v, m...));}
  15. template <class T> concept C = !is_same<T, string>::value && ranges::range<T>;
  16. template <C T> ostream& operator<<(ostream &os, const T &v) {for (auto x : v) os << x << ' '; return os << nl;}
  17. template <C T> istream &operator>>(istream &in, T &v) {for (auto& x : v) in >> x; return in;}
  18. #define L(x, ...) ([&] (auto&& x) -> decltype(auto) {return __VA_ARGS__;})
  19. template <class... T> void re(T &...a) {(cin >> ... >> a);}
  20. template <class... T> void pr(T... a) {((cout << ' ' << a), ...);}
  21. #define def(t, a...) t a; re(a);
  22.  
  23. const int oo = 1e18, MX = 2e5;
  24.  
  25. void solve() {
  26.   def(int, n);
  27.   vi b(n), w(n); re(b, w);
  28.   int c = 0;
  29.   FOR(i, 0, n) {
  30.     int x = min(w[i], b[i]);
  31.     b[i] -= x, w[i] -= x;
  32.     c += x;
  33.   }
  34.   int ans = oo;
  35.   FOR(_, 0, 2) {
  36.     int cc = c, cnt = n, ind = 0;
  37.     vi sb = b; sort(all(sb));
  38.     int sum_w = accumulate(all(w), 0ll), sum_b = accumulate(all(b), 0ll);
  39.     while (true) {
  40.       int x = (ind < n ? sb[ind] : oo) - (ind == 0 ? 0 : sb[ind - 1]);
  41.       auto eval = [] (int n, int cc, int sum_b, int sum_w) {
  42.         bool g = (sum_b + sum_w) or (cc % (n - 2));
  43.         int s = cc / (n - 2); if (!g and s > 0) s -= 1;
  44.         return sum_b + sum_w + (cc - s * (n - 2)) * 2;
  45.       };
  46.       FOR(i, 0, min(x, n / max<int>(cnt, 1) * 2)) ans = min(ans, eval(n, cc + (cnt - 1) * i, sum_b - cnt * i, sum_w + (n - cnt) * i));
  47.       if (ind < n) FOR(i, 0, min(x, n / max<int>(cnt, 1) * 2)) ans = min(ans, eval(n, cc + (cnt - 1) * (x - i), sum_b - cnt * (x - i), sum_w + (n - cnt) * (x - i)));
  48.       if (cnt == 0) break;
  49.       int pind = ind;
  50.       sum_b -= cnt * x; sum_w += (n - cnt) * x; cc += (cnt - 1) * x;
  51.       while (ind < n and sb[ind] == sb[pind]) cnt--, ind++;
  52.     }
  53.     swap(b, w);
  54.   }
  55.   pr(ans, nl);
  56. }
  57.  
  58. signed main() {
  59.   ios::sync_with_stdio(0); cin.tie(0);
  60.   def(int, t); while (t--)
  61.     solve();
  62. }
Advertisement
Add Comment
Please, Sign In to add comment