fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. int n,k;
  5. int res=-1e18;
  6. bool check[25];
  7. int a[25][25];
  8. void backtrack( int i,int sum){
  9. if( i==k+1){
  10. res=max(res,sum);
  11. return;
  12. }
  13. for( int j=1;j<=n;++j){
  14. if(check[j]) continue;
  15. check[j]=true;
  16. backtrack(i+1,sum+a[i][j]);
  17. check[j]=false;
  18. }
  19. }
  20. signed main(){
  21. cin>>n;
  22. for( int i=1;i<=n;++i){
  23. for( int j=1;j<=n;++j){
  24. cin>>a[i][j];
  25. }
  26. }
  27. backtrack(1,0);
  28. cout<<res;
  29. }
Success #stdin #stdout 0.01s 5316KB
stdin
3
7 9 6
3 5 8
8 6 9
stdout
Standard output is empty