I tried many critical search and pruning, and finally found still like the pressure of better quality
#include <bits/stdc++.h> using namespace std; // #define IO #define fi first #define se second #define pb push_back #define mk make_pair #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define rep(i,s,t) for(int i=s;i<t;i++) #define REP(i,s,t) for(int i=s;i<=t;i++) #define DOW(i,s,t) for(int i=s;i>=t;i--) #define dow(i,s,t) for(int i=s;i>t;i--) #define clr(a,b) memset(a,b,sizeof(a)) #define debug(x) cout<<#x<<' '<<x<<endl typedef long long ll; typedef pair<int,int>pii; inline int lowbit(int x){ return x&(-x); } template<typename T> inline void read(T&x){ x=0;int f=1;char ch=getchar(); while(ch<'0' ||ch>'9'){ if(ch=='-')f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar(); } x*=f; } const int N = 110; ll g[N][N]; ll n,ans; void dfs(int mask,int num,int pre,ll preans){ if(num<<1==n){ ans=max(ans,preans);return; } IF (N- . 1 -pre NUM + <n-/ 2 ) return ; // represents all subsequent even number are selected, nor meet the conditions (set can not be selected from n / 2 elements), direct return ( optimal pruning) for ( int I = pre + . 1 ; I <n-; I ++ ) { LL nowans = preans; REP (J, 0 , n-) { IF (& mask ( . 1 << J)) nowans- = G [ I] [J]; the else nowans + = G [I] [J]; } DFS (mask | ( . 1 << I), NUM + . 1 , I, nowans); } } int main () { CIN >> n-; = n-<< . 1 ; REP (I, 0 , n-) REP (J, 0 , n-) Read (G [I] [J]); LL nowans = 0 ; // will now first into another set point REP (I, 0 , n-) nowans + = G [ 0 ] [I]; DFS (( . 1 << 0 ), . 1 , 0 , nowans); the printf ( " % LLD \ n- " , ANS); return 0 ; }
And then there's a different idea of the search, beginning two sets are empty, and then to fill in two sets of elements, it will be a lot less redundant search
#include<bits/stdc++.h> using namespace std; #define ll long long ll n,vis[30],mp[30][30],ans; int s1[30],s2[30],top1,top2; void dfs(int pos,ll now){ if(pos>2*n){ ans=max(ans,now); return; } if(top1<n){ s1[++top1]=pos; ll nxt=now; for(int i=1;i<=top2;i++) nxt+=mp[pos][s2[i]]; dfs(pos+1,nxt); top1--; } if(top2<n){ s2[++top2]=pos; ll nxt=now; for(int i=1;i<=top1;i++) nxt+=mp[pos][s1[i]]; dfs(pos+1,nxt); top2--; } } int main(){ cin>>n; for(int i=1;i<=2*n;i++) for(int j=1;j<=2*n;j++) scanf("%d",&mp[i][j]); ans=0; dfs(1,0); cout<<ans<<endl; } /* (28,14)= */