#include using namespace std; using ll = long long; struct SlopeTrick { const ll INF = 1e18; ll min_f; ll add_l, add_r; map L, R; ll total_count_R; SlopeTrick() : min_f(0), add_l(0), add_r(0), total_count_R(0) {} void add_x_minus_a(const ll& a, ll count) { if (count == 0) return; L[a - add_l] += count; ll rem = count; while (rem > 0 && !L.empty()) { auto it = L.rbegin(); ll l_val = it->first + add_l; ll exist = it->second; ll move = min(rem, exist); if (l_val > a) { min_f += move * (l_val - a); } if (move == exist) L.erase(next(it).base()); else it->second -= move; R[l_val - add_r] += move; total_count_R += move; rem -= move; } } void add_a_minus_x(const ll& a, ll count) { if (count == 0) return; R[a - add_r] += count; total_count_R += count; ll rem = count; while (rem > 0 && !R.empty()) { auto it = R.begin(); ll r_val = it->first + add_r; ll exist = it->second; ll move = min(rem, exist); if (a > r_val) { min_f += move * (a - r_val); } if (move == exist) R.erase(it); else it->second -= move; total_count_R -= move; L[r_val - add_l] += move; rem -= move; } } void clear_left() { L.clear(); add_l = 0; } void limit_slope_max(ll K) { while (total_count_R > K) { auto it = R.rbegin(); ll exist = it->second; ll remove_count = min(exist, total_count_R - K); if (remove_count == exist) { R.erase(next(it).base()); } else { it->second -= remove_count; } total_count_R -= remove_count; } } }; int main() { ll n,k; cin >> n >> k; vector a(n),b(n),c(n),d(n); for(int i = 0; i < n; i++) cin >> a[i]; for(int i = 0; i < n; i++) cin >> b[i]; for(int i = 0; i < n; i++) cin >> c[i]; for(int i = 0; i < n; i++) cin >> d[i]; SlopeTrick st; for (int i = 0; i < n; ++i) { st.add_x_minus_a(a[i], b[i]); st.add_a_minus_x(c[i], d[i]); st.clear_left(); st.limit_slope_max(k); } cout << st.min_f << endl; }