提交时间:2022-07-13 11:51:56

运行 ID: 51545

#include<bits/stdc++.h> using namespace std; template<typename T>void in(T &a) { T ans=0; bool f=0; char c=getchar(); for(;c<'0'||c>'9';c=getchar())if(c=='-')f=1; for(;c>='0'&&c<='9';c=getchar())ans=ans*10+c-'0'; a=(f?-ans:ans); } template<typename T,typename... Args>void in(T &a,Args&...args) { in(a),in(args...); } int n,w[505][505],k; namespace solve{ void solve1(){ cout<<n*(n-1)/2<<'\n'; } int maxn=-1; bool check(string now){ for(int i=1;i<now.size();i++)if(now[i]!=now[0])return 0; return 1; } int sum(string now){ int ans=0; for(int i=0;i<now.size();i++)for(int j=0;j<i;j++)ans+=((now[i]==now[j])?(w[i+1][j+1]):(k-w[i+1][j+1])); return ans; } void dfs(int d,string now){ if(d>n){ if(check(now))return; maxn=max(maxn,sum(now)); return; } dfs(d+1,now+'0'),dfs(d+1,now+'1'); } void solve2(){ dfs(1,""),cout<<maxn<<'\n'; } } int main(){ in(n,k); for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)in(w[i][j]); if(k==1)solve::solve1(); else solve::solve2(); return 0; }