#line 1 "a.cpp" #include #include #include using namespace std; #line 1 "/home/kotatsugame/library/graph/MF_Dinic.cpp" //Dinic O(EV^2) #line 3 "/home/kotatsugame/library/graph/MF_Dinic.cpp" #include #line 5 "/home/kotatsugame/library/graph/MF_Dinic.cpp" #include #include template struct MF{ struct edge{ int to,rev; T cap; }; vector >G; vectorlevel,iter; MF(int n_=0):G(n_),level(n_),iter(n_){} void add_edge(int from,int to,T cap) { G[from].push_back({to,(int)G[to].size(),cap}); G[to].push_back({from,(int)G[from].size()-1,0}); } T dfs(int u,int t,T f) { if(u==t)return f; for(;iter[u]0&&level[u]0) { e.cap-=d; G[e.to][e.rev].cap+=d; return d; } } } return 0; } T max_flow(int s,int t) { T ret=0; for(;;) { fill(level.begin(),level.end(),-1); queueP; level[s]=0; P.push(s); while(!P.empty()) { int u=P.front();P.pop(); for(edge&e:G[u]) { if(e.cap>0&&level[e.to]<0) { level[e.to]=level[u]+1; P.push(e.to); } } } if(level[t]<0)return ret; fill(iter.begin(),iter.end(),0); for(T f;(f=dfs(s,t,numeric_limits::max()))>0;)ret+=f; } } }; #line 6 "a.cpp" int N,M; main() { cin>>N>>M; vector > >A(N*M); for(int i=0;i>A[i*M+j].first; A[i*M+j].second=make_pair(i,j); } sort(A.begin(),A.end()); int ans=0; for(int i=0;iP(N+M+2); while(j