int un,vn;//un左边点数,vn右边点数
int g[maxn][maxn];
int linker[maxn],lu[maxn],lv[maxn];
int slack[maxn];
bool visu[maxn],visv[maxn];
bool dfs( int u )
{
visu[u] = true;
for ( int v=0 ; v<vn ; v++ )
{
if ( visv[v] ) continue;
int tmp = lu[u]+lv[v]-g[u][v];
if ( tmp==0 )
{
visv[v] = true;
if ( linker[v]==-1||dfs(linker[v]) )
{
linker[v] = u;
return true;
}
}
else if ( slack[v]>tmp )
slack[v] = tmp;
}
return false;
}
int km()
{
memset ( linker , -1 , sizeof(linker) );
memset ( lv , 0 , sizeof(lv) );
for ( int i=0 ; i<un ; i++ )
{
lu[i] = -inf;
for ( int j=0 ; j<vn ; j++ )
if ( g[i][j]>lu[i] ) lu[i] = g[i][j];
}
for ( int u=0 ; u<un ; u++ )
{
for ( int i=0 ; i<vn ; i++ )
slack[i] = inf;
while ( true )
{
memset ( visu , false , sizeof(visu) );
memset ( visv , false , sizeof(visv) );
if ( dfs(u) ) break;
int d = inf;
for ( int i=0 ; i<vn ; i++ )
if ( !visv[i]&&d>slack[i] )
d = slack[i];
for ( int i=0 ; i<un ; i++ )
if ( visu[i] ) lu[i] -= d;
for ( int i=0 ; i<vn ; i++ )
{
if ( visv[i] ) lv[i] += d;
else slack[i] -= d;
}
}
}
int res = 0;
for ( int i=0 ; i<vn ; i++ )
if ( linker[i]!=-1 ) res += g[linker[i]][i];
return res;
}