提交时间:2022-07-13 12:22:23
运行 ID: 51644
#include <iostream> #define ll long long using namespace std; int w[510][510],s[13],n,K; ll ans = 0; void solve() { int i,j; ll res = 0; for(i = 1;i <= n;i++) for(j = i + 1;j <= n;j++) { if(s[i] == s[j]) res+=w[i][j]; else res+=(K - w[i][j]); } ans = max(ans,res); } void DFS(int k) { if(k == n + 1) { bool a1 = 0,a2 = 0; for(int i = 1;i <= n;i++) { if(s[i] == 0) a1 = 1; else a2 = 1; } if(a1&&a2) solve(); return; } s[k] = 0; DFS(k + 1); s[k] = 1; DFS(k + 1); return; } int main() { int i,j,k; cin>>n>>K; if(K == 1) { ans = n * n - 3 * n + 2; cout<<ans / 2<<endl; return 0; } for(i = 1;i <= n;i++) for(j = 1;j <= n;j++) cin>>w[i][j]; if(n <= 20) DFS(1); else { for(i = 1;i <= n;i++) { ll res = 0; for(j = 1;j <= n;j++) { for(k = j + 1;k <= n;k++) { if(j == i||k == i) res+=(K - w[j][k]); else res+=w[j][k]; } } ans = max(ans,res); } } cout<<ans; return 0; }