#include using ll = long long; using uint = unsigned int; using ull = unsigned long long; using ld = long double; template using max_heap = std::priority_queue; template using min_heap = std::priority_queue, std::greater>; constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; } constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; } constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); } constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; } constexpr ull ceil2(const ull v) { return 1ULL << clog(v); } constexpr ull floor2(const ull v) { return v ? (1ULL << (log2p1(v) - 1)) : 0ULL; } constexpr bool btest(const ull mask, const int ind) { return (mask >> ind) & 1ULL; } template bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); } template bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); } template constexpr T inf_v = std::numeric_limits::max() / 4; template constexpr Real pi_v = Real{3.141592653589793238462643383279502884}; template constexpr T TEN(const int n) { return n == 0 ? T{1} : TEN(n - 1) * T{10}; } template struct fix : F { fix(F&& f) : F{std::forward(f)} {} template auto operator()(Args&&... args) const { return F::operator()(*this, std::forward(args)...); } }; template auto nd_array(int const (&szs)[n], const T x = T{}) { if constexpr (i == n) { return x; } else { return std::vector(szs[i], nd_array(szs, x)); } } class printer { public: printer(std::ostream& os_ = std::cout) : m_os{os_} { m_os << std::fixed << std::setprecision(15); } template int ln(const Args&... args) { return dump(args...), m_os << '\n', 0; } template int el(const Args&... args) { return dump(args...), m_os << std::endl, 0; } private: template void dump(const T& v) { m_os << v; } template void dump(const std::vector& vs) { for (int i = 0; i < (int)vs.size(); i++) { m_os << (i ? " " : ""), dump(vs[i]); } } template void dump(const std::vector>& vss) { for (int i = 0; i < (int)vss.size(); i++) { m_os << (0 <= i or i + 1 < (int)vss.size() ? "\n" : ""), dump(vss[i]); } } template int dump(const T& v, const Args&... args) { return dump(v), m_os << ' ', dump(args...), 0; } std::ostream& m_os; }; printer out; class range { private: struct itr { itr(const int start = 0, const int step = 1) : m_cnt{start}, m_step{step} {} bool operator!=(const itr& it) const { return m_cnt != it.m_cnt; } int& operator*() { return m_cnt; } itr& operator++() { return m_cnt += m_step, *this; } int m_cnt, m_step; }; int m_start, m_end, m_step; public: range(const int start, const int end, const int step = 1) : m_start{start}, m_end{end}, m_step{step} { assert(m_step != 0); if (m_step > 0) { m_end = m_start + std::max(m_step - 1, m_end - m_start + m_step - 1) / m_step * m_step; } if (m_step < 0) { m_end = m_start - std::max(-m_step - 1, m_start - m_end - m_step - 1) / (-m_step) * (-m_step); } } itr begin() const { return itr{m_start, m_step}; } itr end() const { return itr{m_end, m_step}; } }; range rep(const int end, const int step = 1) { return range(0, end, step); } range per(const int rend, const int step = -1) { return range(rend - 1, -1, step); } class scanner { public: scanner(std::istream& is_ = std::cin) : m_is{is_} { m_is.tie(nullptr), std::ios::sync_with_stdio(false); } template T val() { T v; return m_is >> v, v; } template T val(const T offset) { return val() - offset; } template std::vector vec(const int n) { return make_v(n, [this]() { return val(); }); } template std::vector vec(const int n, const T offset) { return make_v(n, [this, offset]() { return val(offset); }); } template std::vector> vvec(const int n0, const int n1) { return make_v>(n0, [this, n1]() { return vec(n1); }); } template std::vector> vvec(const int n0, const int n1, const T offset) { return make_v>(n0, [this, n1, offset]() { return vec(n1, offset); }); } template auto tup() { return std::tuple...>{val()...}; } template auto tup(const Args&... offsets) { return std::tuple...>{val(offsets)...}; } private: template std::vector make_v(const int n, F f) { std::vector ans; for (int i = 0; i < n; i++) { ans.push_back(f()); } return ans; } std::istream& m_is; }; scanner in; int main() { const auto [N, K] = in.tup(); auto as = in.vec(N); auto bs = in.vec(N); auto pss = in.vvec(N, N); using pii = std::pair; using pip = std::pair; max_heap Q; for (const int i : range(0, N)) { for (const int j : range(0, N)) { if (as[i] == 0 or bs[j] == 0) { continue; } Q.push({pss[i][j], {i, j}}); } } for ([[maybe_unused]] const int _ : range(0, K)) { int i, j; while (true) { const auto [v, p] = Q.top(); Q.pop(); const auto [i_, j_] = p; if (as[i_] != 0 and bs[j_] != 0) { i = i_, j = j_; break; } } as[i]--, bs[j]--; pss[i][j]--; Q.push({pss[i][j], {i, j}}); } int ans = 0; for (const int i : range(0, N)) { for (const int j : range(0, N)) { ans += pss[i][j] * pss[i][j]; } } out.ln(ans); return 0; }