#include using namespace std; #define REP(i,n) for(ll i=0;i<(ll)n;i++) #define dump(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << (x) << "\n"; #define spa << " " << #define fi first #define se second #define ALL(a) (a).begin(),(a).end() #define ALLR(a) (a).rbegin(),(a).rend() using ld = long double; using ll = long long; using ull = unsigned long long; using pii = pair; using pll = pair; using pdd = pair; template using V = vector; template using P = pair; template vector make_vec(size_t n, T a) { return vector(n, a); } template auto make_vec(size_t n, Ts... ts) { return vector(n, make_vec(ts...)); } template ostream& operator << (ostream& os, const pair v){os << "(" << v.first << ", " << v.second << ")"; return os;} template ostream& operator<<(ostream &os, const vector &v) { for (auto &e : v) os << e << ' '; return os; } template ostream& operator<<(ostream& os, const vector> &v){ for(auto &e : v){os << e << "\n";} return os;} struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template void UNIQUE(vector &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());} template bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } void fail() { cout << -1 << '\n'; exit(0); } inline int popcount(const int x) { return __builtin_popcount(x); } inline int popcount(const ll x) { return __builtin_popcountll(x); } template void debug(vector>&v,ll h,ll w){for(ll i=0;i void debug(vector&v,ll n){if(n!=0)cerr< using namespace atcoder; template < class Flow, class Cost > struct cs_graph{ struct edge{ int from, to; Flow cap, flow; Cost cost, cost_m; }; struct residual_edge{ int to, rev, id; bool is_rev; Flow cap; Cost cost; }; int n; vector E; vector dual; vector b; cs_graph() {} cs_graph(int n) : n(n){ dual.resize(n, 0); b.resize(n, 0); } int add_edge(int from, int to, Flow cap, Flow flow, Cost cost){ int m = int(E.size()); E.push_back(edge{from, to, cap, flow, cost, -1}); return m; } void set_b(const vector &b_input){ b = b_input; return; } Cost calc_objective(){ Cost obj = 0; for(const auto &e: E){ obj += e.cost * e.flow; } return obj; } void print_E(){ for(const auto &e: E){ cerr << "from: " << e.from << ", to: " << e.to << ", cap: " << e.cap << ", flow: " << e.flow << ", cost: " << e.cost << ", cost_m: " << e.cost_m << "\n" ; } return; } bool check_feasibility(){ mf_graph mf_G(n+2); int s = n; int t = s + 1; Flow plus_flow_sum = 0, minus_flow_sum = 0; for(int i=0;i 0){ plus_flow_sum += b[i]; mf_G.add_edge(s, i, b[i]); } if(b[i] < 0){ minus_flow_sum += (-b[i]); mf_G.add_edge(i, t, -b[i]); } } assert(plus_flow_sum == minus_flow_sum); for(const auto &e: E){ mf_G.add_edge(e.from, e.to, e.cap); } Flow mf = mf_G.flow(s, t); return (mf == plus_flow_sum); } Cost cost_scaling(){ Cost cost_max = 0; for(const auto &e: E){ if(e.cost > cost_max) cost_max = e.cost; } Cost scaling_factor = 2; Cost init_eps = 1; while(true){ init_eps *= scaling_factor; if(init_eps >= n * cost_max) break; } for(auto &e: E){ e.cost_m = e.cost * init_eps; } Cost eps = init_eps; while(true){ // improve approximation(eps, x, pi) vector> g(n); // for debug // auto print_residual_graph = [&](){ // for(int i=0;i 0) cerr << "from: " << i << ", to: " << e.to << ", cap: " << e.cap << ", r_cost: " << reduced_cost << "\n"; // } // } // }; // add edges to the residual graph for(int i=0;i<(int)E.size();i++){ auto &e = E[i]; Cost reduced_cost = e.cost_m - dual[e.from] + dual[e.to]; if(reduced_cost > 0){ e.flow = 0; g[e.from].push_back(residual_edge{e.to, (int)g[e.to].size(), i, false, e.cap, e.cost_m}); g[e.to].push_back(residual_edge{e.from, (int)g[e.from].size() - 1, i, true, 0, -e.cost_m}); }else{ e.flow = e.cap; g[e.from].push_back(residual_edge{e.to, (int)g[e.to].size(), i, false, 0, e.cost_m}); g[e.to].push_back(residual_edge{e.from, (int)g[e.from].size() - 1, i, true, e.cap, -e.cost_m}); } } // calculate node balance vector balance = b; for(const auto &e: E){ balance[e.from] -= e.flow; balance[e.to] += e.flow; } // define active node queue queue active; for(int i=0;i 0){ active.push(i); } } // print_residual_graph(); vector current_arc(n, 0); vector is_positive_cap(n, false); while(!active.empty()){ int v = active.front(); while(true){ if(current_arc[v] == (int)g[v].size()){ if(!is_positive_cap[v]){ return -1; } dual[v] += eps / scaling_factor; current_arc[v] = 0; is_positive_cap[v] = false; }else{ auto &e = g[v][current_arc[v]]; Cost reduced_cost = e.cost - dual[v] + dual[e.to]; if(e.cap > 0){ is_positive_cap[v] = true; } if(e.cap > 0 and reduced_cost < 0){ Flow push_flow = min(balance[v], e.cap); Flow pre_balance = balance[e.to]; // augment e.cap -= push_flow; g[e.to][e.rev].cap += push_flow; balance[v] -= push_flow; balance[e.to] += push_flow; // change flow of E if(!e.is_rev){ E[e.id].flow += push_flow; }else{ E[e.id].flow -= push_flow; } // add node e.to to active if(pre_balance <= 0 and balance[e.to] > 0){ active.push(e.to); } if(balance[v] == 0){ active.pop(); break; } } current_arc[v]++; } } } // print_E(); // dump(balance) // dump(dual) // print_residual_graph(); if(eps == 2) break; eps /= scaling_factor; } return calc_objective(); } }; int main(){ int N, K; cin >> N >> K; vector A(N), B(N); for(int i=0;i> A[i]; for(int i=0;i> B[i]; vector> P(N, vector(N, 0)); for(int i=0;i> P[i][j]; } } cs_graph G(2*N+2); int s = 2*N, t = s+1; for(int i=0;i b(2*N+2, 0); b[s] = K; b[t] = -K; G.set_b(b); cout << G.cost_scaling() + S << endl; return 0; }