## 題解

s->xi,[ai-mid,ai+mid]
yj->t,[aj-mid,aj+mid]
xi->yj,[L,R]

## 代碼

#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<queue>using namespace std;#define N 410#define E 100005#define inf 2000000000int n,m,s,t,ss,tt,L,R,in,out,maxflow,ans;int a[N][N],line[N],lie[N];int tot,point[N],nxt[E],v[E],remain[E];int d[N],deep[N],last[N],num[N],cur[N];queue <int> q;void addedge(int x,int y,int cap){    ++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=cap;    ++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;}void bfs(int t){    for (int i=1;i<=t;++i) deep[i]=t;    deep[t]=0;    for (int i=1;i<=t;++i) cur[i]=point[i];    while (!q.empty()) q.pop();    q.push(t);    while (!q.empty())    {        int now=q.front();q.pop();        for (int i=point[now];i!=-1;i=nxt[i])            if (deep[v[i]]==t&&remain[i^1])            {                deep[v[i]]=deep[now]+1;                q.push(v[i]);            }    }}int addflow(int s,int t){    int ans=inf,now=t;    while (now!=s)    {        ans=min(ans,remain[last[now]]);        now=v[last[now]^1];    }    now=t;    while (now!=s)    {        remain[last[now]]-=ans;        remain[last[now]^1]+=ans;        now=v[last[now]^1];    }    return ans;}void isap(int s,int t){    bfs(t);    for (int i=1;i<=t;++i) ++num[deep[i]];    int now=s;    while (deep[s]<t)    {        if (now==t)        {            maxflow+=addflow(s,t);            now=s;        }        bool has_find=false;        for (int i=cur[now];i!=-1;i=nxt[i])            if (deep[v[i]]+1==deep[now]&&remain[i])            {                has_find=true;                cur[now]=i;                last[v[i]]=i;                now=v[i];                break;            }        if (!has_find)        {            int minn=t-1;            for (int i=point[now];i!=-1;i=nxt[i])                if (remain[i]) minn=min(minn,deep[v[i]]);            if (!(--num[deep[now]])) break;            ++num[deep[now]=minn+1];            cur[now]=point[now];            if (now!=s) now=v[last[now]^1];        }    }}bool check(int mid){    tot=-1;memset(point,-1,sizeof(point));    memset(num,0,sizeof(num));    memset(d,0,sizeof(d));    in=out=0;maxflow=0;    for (int i=1;i<=n;++i)    {        addedge(s,i,mid+mid);        d[s]-=line[i]-mid,d[i]+=line[i]-mid;    }    for (int i=1;i<=m;++i)    {        addedge(n+i,t,mid+mid);        d[n+i]-=lie[i]-mid,d[t]+=lie[i]-mid;    }    for (int i=1;i<=n;++i)        for (int j=1;j<=m;++j)        {            addedge(i,n+j,R-L);            d[i]-=L,d[n+j]+=L;        }    addedge(t,s,inf);    for (int i=1;i<=t;++i)    {        if (d[i]>0) addedge(ss,i,d[i]),in+=d[i];        if (d[i]<0) addedge(i,tt,-d[i]),out-=d[i];    }    if (in!=out) return 0;    isap(ss,tt);    return maxflow==in;}int find(){    int l=0,r=2000000,mid,ans=2000000;    while (l<=r)    {        mid=(l+r)>>1;        if (check(mid)) ans=mid,r=mid-1;        else l=mid+1;    }    return ans;}int main(){    scanf("%d%d",&n,&m);    s=n+m+1,t=s+1,ss=t+1,tt=ss+1;    for (int i=1;i<=n;++i)        for (int j=1;j<=m;++j)        {            scanf("%d",&a[i][j]);            line[i]+=a[i][j];            lie[j]+=a[i][j];        }    scanf("%d%d",&L,&R);    ans=find();    printf("%d\n",ans);}