#pragma GCC target ("avx2") // #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // #include using namespace std; using ll=long long; template using V = vector; template using P = pair; using vll = V; using vii = V; using vvll = V; using vvii = V< V >; using PII = P; using PLL = P; #define RevREP(i,n,a) for(ll i=n;i>a;i--) // (a,n] #define REP(i,a,n) for(ll i=a;i inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; } template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; } template ostream& operator <<(ostream& out, const P &p) { return out << '(' << p.first << ", " << p.second << ')'; } template ostream& operator <<(ostream& out, const V &v) { out << '['; for (int i=0;i class MinimumCostFlow { typedef std::pair P; struct edge { int to, rev; Cap cap; Cost cost; }; public: int V; std::vector< std::vector > G; std::vector h, dist; // ポテンシャル、最短距離 std::vector prevv, preve; // 直前の頂点と辺 MinimumCostFlow(int n): V(n), G(n), h(n), dist(n), prevv(n), preve(n) {} // from から to へ向かう容量 cap、コスト cost の辺をグラフに追加する void add_edge(int from, int to, Cap cap, Cost cost) { G[from].push_back({to, int(G[to].size()), cap, cost}); G[to].push_back({from, int(G[from].size()) - 1, 0, -cost}); } // s から t への流量 f の最小費用流を求める // 流せない場合は -1 Cost min_cost_flow(int s, int t, Cap f) { Cost res = 0; std::fill(h.begin(), h.end(), 0); while (f > 0) { // Dijkstra 法による h の更新 std::priority_queue< P, std::vector< P >, std::greater< P > > que; std::fill(dist.begin(), dist.end(), std::numeric_limits::max()); dist[s] = 0; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (dist[v] < p.first) continue; for (int i = 0; i < (int)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) { dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; que.push(P(dist[e.to], e.to)); } } } // これ以上流せない時 if (dist[t] == std::numeric_limits::max()) { return -1; } for (int v = 0; v < V; v++) h[v] += dist[v]; // s-t 間最短路に沿って目一杯流す Cap d = f; for (int v = t; v != s; v = prevv[v]) { d = std::min(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d * h[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } }; auto p2 = [](ll p) {return p * p;}; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, k; cin >> n >> k; vii a(n), b(n); vvii p(n, vii(n)); ll ans = 0; rep(i, n) cin >> a[i]; rep(i, n) cin >> b[i]; rep(i, n) rep(j, n) { cin >> p[i][j]; ans += p2(p[i][j]); } int s = 2 * n, t = 2 * n + 1; MinimumCostFlow mc(2 * n + 2); rep(i, n) { mc.add_edge(s, i, a[i], 0); mc.add_edge(i + n, t, b[i], 0); rep(j, n) { REP(ai, 1, a[i] + 1) mc.add_edge(i, j + n, 1, p2(ai - p[i][j]) - p2(ai - 1 - p[i][j]) + INF); } } ll cost = mc.min_cost_flow(s, t, k); cout << ans - ll(k) * INF + cost << '\n'; return 0; }