#line 2 "cpplib/util/template.hpp" #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx2") #include using namespace std; struct __INIT__{__INIT__(){cin.tie(0);ios::sync_with_stdio(false);cout< vec; typedef vector> mat; typedef vector>> mat3; typedef vector svec; typedef vector> smat; templateusing V=vector; templateusing VV=V>; templateinline void output(T t){bool f=0;for(auto i:t){cout<<(f?" ":"")<inline void output2(T t){for(auto i:t)output(i);} templateinline void debug(T t){bool f=0;for(auto i:t){cerr<<(f?" ":"")<inline void debug2(T t){for(auto i:t)debug(i);} #define loop(n) for(long long _=0;_<(long long)(n);++_) #define _overload4(_1,_2,_3,_4,name,...) name #define __rep(i,a) repi(i,0,a,1) #define _rep(i,a,b) repi(i,a,b,1) #define repi(i,a,b,c) for(long long i=(long long)(a);i<(long long)(b);i+=c) #define rep(...) _overload4(__VA_ARGS__,repi,_rep,__rep)(__VA_ARGS__) #define _overload3_rev(_1,_2,_3,name,...) name #define _rep_rev(i,a) repi_rev(i,0,a) #define repi_rev(i,a,b) for(long long i=(long long)(b)-1;i>=(long long)(a);--i) #define rrep(...) _overload3_rev(__VA_ARGS__,repi_rev,_rep_rev)(__VA_ARGS__) // #define rep(i,...) for(auto i:range(__VA_ARGS__)) // #define rrep(i,...) for(auto i:reversed(range(__VA_ARGS__))) // #define repi(i,a,b) for(lint i=lint(a);i<(lint)(b);++i) // #define rrepi(i,a,b) for(lint i=lint(b)-1;i>=lint(a);--i) // #define irep(i) for(lint i=0;;++i) // inline vector range(long long n){if(n<=0)return vector();vectorv(n);iota(v.begin(),v.end(),0LL);return v;} // inline vector range(long long a,long long b){if(b<=a)return vector();vectorv(b-a);iota(v.begin(),v.end(),a);return v;} // inline vector range(long long a,long long b,long long c){if((b-a+c-1)/c<=0)return vector();vectorv((b-a+c-1)/c);for(int i=0;i<(int)v.size();++i)v[i]=i?v[i-1]+c:a;return v;} // templateinline T reversed(T v){reverse(v.begin(),v.end());return v;} #define all(n) begin(n),end(n) templatebool chmin(T& s,const E& t){bool res=s>t;s=min(s,t);return res;} templatebool chmax(T& s,const E& t){bool res=s(s,t);return res;} const vector dx={1,0,-1,0,1,1,-1,-1}; const vector dy={0,1,0,-1,1,-1,1,-1}; #define SUM(v) accumulate(all(v),0LL) templateauto make_vector(T x,int arg,Args ...args){if constexpr(sizeof...(args)==0)return vector(arg,x);else return vector(arg,make_vector(x,args...));} #define extrep(v,...) for(auto v:__MAKE_MAT__({__VA_ARGS__})) #define bit(n,a) ((n>>a)&1) vector> __MAKE_MAT__(vector v){if(v.empty())return vector>(1,vector());long long n=v.back();v.pop_back();vector> ret;vector> tmp=__MAKE_MAT__(v);for(auto e:tmp)for(long long i=0;i>; templateusing graph_w=vector>>; templateostream& operator<<(ostream& out,pairv){out<<"("< struct dinic { struct edge { int to; typename std::list::iterator rev; T cap,flow; edge(int to,T cap):to(to),cap(cap),flow(T()){} }; int n,src,dst; T ret=T(); std::vector> g; std::vector::iterator>itr; std::vectorlevel,seen; std::map,bool>exist; std::map,typename std::list::iterator>m; dinic(){} dinic(int n,int s,int t):n(n),src(s),dst(t){g.assign(n,std::list());itr.resize(n);} void add_edge(int from, int to, T cap) { g[from].push_back(edge(to,cap)); g[to].push_back(edge(from,0)); m[std::make_pair(from,to)]=prev(g[from].end()); m[std::make_pair(to,from)]=prev(g[to].end()); g[from].back().rev=prev(g[to].end()); g[to].back().rev=prev(g[from].end()); exist[std::make_pair(from,to)]=1; exist[std::make_pair(to,from)]=1; } bool update_edge(int from, int to, T cap){ if(cap>0){ if(exist[std::make_pair(from,to)]){ auto e=m[std::make_pair(from,to)]; e->cap+=cap; }else{ add_edge(from,to,cap); } return 1; }else{ cap*=-1; if(exist[std::make_pair(from,to)]){ auto e=m[std::make_pair(from,to)]; if(e->cap-e->flow>=cap){ e->cap-=cap; }else{ e->cap-=cap; T req=e->flow-e->cap; e->flow-=req; e->rev->flow+=req; ret-=req; assert(cancel(dst,to,req)); assert(cancel(from,src,req)); if(e->cap==0&&e->rev->cap==0){ g[from].erase(e); g[to].erase(e->rev); exist[std::make_pair(from,to)]=0; exist[std::make_pair(to,from)]=0; } } return 1; }else{ return 0; } } } void bfs(int s) { level.assign(n,-1); std::queue q; level[s] = 0; q.push(s); while (!q.empty()) { int v = q.front(); q.pop(); for(edge e: g[v]){ if (e.cap-e.flow > 0 and level[e.to] < 0) { level[e.to] = level[v] + 1; q.push(e.to); } } } } T dfs(int v, int t,T f) { if (v == t) return f; for(auto &e=itr[v];e!=g[v].end();++e){ if (e->cap-e->flow > 0 and level[v] < level[e->to]) { T d = dfs(e->to, t, std::min(f, e->cap-e->flow)); if (d > 0) { e->flow+=d; e->rev->flow -= d; return d; } } } return 0; } T __cancel(int v,int t,T f){ if (v == t) return f; seen[v]=1; for (edge& e: g[v]){ if (e.rev->flow > 0&&!seen[e.to]) { T d = __cancel(e.to, t, std::min(f,e.rev->flow)); if (d > 0) { e.flow+=d; e.rev->flow-=d; return d; } } } return 0; } T run() { T f; while (bfs(src), level[dst] >= 0) { for(int i=0;i::max())) > 0) { ret += f; } } return ret; } T cancel(int s,int t,T mx){ T f; while(seen.assign(n,0),seen[s]=1,(f=__cancel(s, t, mx))>0)mx-=f; return mx==0; } T cap(int s,int t){ if(exist[std::make_pair(s,t)]){ return m[std::make_pair(s,t)]->cap; }else{ return 0; } } T flow(int s,int t){ if(exist[std::make_pair(s,t)]){ return m[std::make_pair(s,t)]->flow; }else{ return 0; } } void debug(){ for(int i=0;i0)std::cerr<<"("<>h>>w; mat v(h,vec(w,0)); vectorb; rep(i,h)rep(j,w){ cin>>v[i][j]; b.emplace_back(v[i][j]); } sort(all(b)); b.erase(unique(all(b)),b.end()); rep(i,h)rep(j,w)v[i][j]=lower_bound(all(b),v[i][j])-b.begin(); lint n=b.size(); vector>>tmp(n); rep(i,h)rep(j,w){ tmp[v[i][j]].emplace_back(i,j); } lint ans=0; rep(i,n){ if(!b[i])continue; dinicd(h+w+2,h+w,h+w+1); rep(j,h){ d.add_edge(h+w,j,1); } rep(j,w){ d.add_edge(j+h,h+w+1,1); } for(auto [s,t]:tmp[i]){ d.add_edge(s,t+h,INF); } lint k=d.run(); ans+=k; } cout<