提交时间:2022-07-13 11:51:56
运行 ID: 51546
#include<bits/stdc++.h> using namespace std; typedef long long ll; inline int read() { int x=0,c=getchar(),f=1; for(; c<=47||c>=58; c=getchar())f=f&&(c^45); for(; c>=48&&c<=57; c=getchar())x=(x<<3)+(x<<1)+(c&15); return f?x:-x; } const int MAXN=505; int w[MAXN][MAXN],k,n; ll ans,sum[MAXN]; int a[MAXN],b[MAXN]; void Sub1() { int len=(1<<n)-1; ll res=0; for(int x=1; x<len; ++x) { int A=0,B=0; ll tmp=0; for(int i=0; i<n; ++i) if((x>>i)&1) a[++A]=i+1; else b[++B]=i+1; for(int i=1; i<=A; ++i) for(int j=1; j<=B; ++j) tmp+=2*w[a[i]][b[j]]-k; res=max(res,ans-tmp); } printf("%lld\n",res); } int main() { scanf("%d%d",&n,&k); for(int i=1; i<=n; ++i) for(int j=1; j<=n; ++j) w[i][j]=read(),ans+=w[i][j],sum[i]+=w[i][j]; ans>>=1; if(k==1)return printf("%lld\n",ans-(n-1)),0; if(n<=20) return Sub1(),0; ll x=sum[1]; for(int i=2; i<=n; ++i)x=min(x,sum[i]); printf("%lld\n",ans-(x-((n-1ll)*k-x))); return 0; }