#define pb push_back
#define mp make_pair
#define _(a,b) memset((a),(b),sizeof(a))
#define sz(a) (int)(a).size()
const int maxn = 60100;
const int INF = 1000000000;
struct Edge
{
int a, b, id, cap, flow, cost;
};
vector<int> g[maxn];
vector<Edge> e;
int S, T;
int d[maxn], pot[maxn], last[maxn];
set<pair<int, int> > q;
void adde(int a, int b, int cap, int cost)
{
Edge t;
t.a = a;
t.b = b;
t.id = sz(e);
t.cap = cap;
t.flow = 0;
t.cost = cost;
e.pb(t);
g[t.a].pb(t.id);
t.id++;
swap(t.a, t.b);
t.cap = 0;
t.cost = -cost;
e.pb(t);
g[t.a].pb(t.id);
}
void update(int id, int nd, int lst)
{
if (d[id] > nd)
{
q.erase(mp(d[id], id));
d[id] = nd;
last[id] = lst;
q.insert(mp(nd, id));
}
}
bool dij()
{
_(d, 127);
update(S, 0, -1);
while (!q.empty())
{
int id = q.begin()->second;
q.erase(q.begin());
for (int i = 0; i < sz(g[id]); i++)
{
Edge &t = e[g[id][i]];
if (t.flow < t.cap)
update(t.b, d[t.a] + pot[t.a] - pot[t.b] + t.cost, t.id);
}
}
for (int i = 0; i < maxn; i++)
{
if (d[i] < INF)
pot[i] += d[i];
}
return d[T] < INF;
}
void augm()
{
int id = T, mn = INF;
while (id != S)
{
Edge &t = e[last[id]];
mn = min(mn, t.cap - t.flow);
id = t.a;
}
id = T;
while (id != S)
{
Edge &t = e[last[id]];
t.flow += mn;
e[t.id ^ 1].flow -= mn;
id = t.a;
}
}
int flow()
{
while (dij())
augm();
int ret = 0;
for (int i = 0; i < sz(e); i += 2)
ret += e[i].flow * e[i].cost;
return ret;
}
I2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBtcCBtYWtlX3BhaXIKI2RlZmluZSBfKGEsYikgbWVtc2V0KChhKSwoYiksc2l6ZW9mKGEpKQojZGVmaW5lIHN6KGEpIChpbnQpKGEpLnNpemUoKQoKY29uc3QgaW50IG1heG4gPSA2MDEwMDsKY29uc3QgaW50IElORiA9IDE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