#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=255;
const int inf=0x3f3f3f3f;
int N,M,K,b[maxn],need[maxn][maxn],have[maxn][maxn],price[maxn][maxn][maxn];
struct Edge
{
int value,flow,to,rev;
Edge() {}
Edge(int a,int b,int c,int d):to(a),value(b),flow(c),rev(d) {}
};
vector<Edge> E[maxn];
void add(int form,int to,int value,int flow)
{
E[form].push_back(Edge(to,value,flow,E[to].size()));
E[to].push_back(Edge(form,-value,0,E[form].size()-1));
}
bool book[maxn];
int cost[maxn],pre[maxn],pree[maxn];
bool spfa(int from,int to)
{
memset(book,false,sizeof book);
memset(cost,inf,sizeof cost);
book[from]=true,cost[from]=0;
queue<int> Q;
Q.push(from);
while(!Q.empty())
{
int t=Q.front();
Q.pop();
book[t]=false;
for(int i=0; i<E[t].size(); i++)
{
Edge&e=E[t][i];
if(e.flow>0&&cost[e.to]>cost[t]+e.value)
{
cost[e.to]=cost[t]+e.value;
pre[e.to]=t;
pree[e.to]=i;
if(book[e.to]==false)
{
Q.push(e.to);
book[e.to]=true;
}
}
}
}
return cost[to]!=inf;
}
int w(int from,int to)
{
int sum=0;
while(spfa(from,to))
{
int maxflow=inf,flag=to;
while(flag!=from)
{
maxflow=min(maxflow,E[pre[flag]][pree[flag]].flow);
flag=pre[flag];
}
flag=to;
while(flag!=from)
{
sum+=E[pre[flag]][pree[flag]].value * maxflow;
E[pre[flag]][pree[flag]].flow -= maxflow;
E[flag][E[pre[flag]][pree[flag]].rev].flow += maxflow;
flag=pre[flag];
}
}
return sum;
}