fork download
  1. #include <bits/stdc++.h>
  2. #define oo (long long)(1e18)
  3. using namespace std;
  4. long long res=-oo, dd[27], x[27], n, a[27][27];
  5. void ghinghiem()
  6. {
  7. long long sum=0;
  8. for(long long i=1; i<=n; i++)sum = sum + a[i][x[i]];
  9. res = max(res , sum );
  10. }
  11. void backtrack(long long i)
  12. {
  13. for(long long j=1; j<=n; j++)
  14. if(dd[j] == 0)
  15. {
  16. x[i] = j;
  17. dd[j] = 1;
  18. if(i == n) ghinghiem();
  19. else backtrack(i + 1);
  20. dd[j] = 0;
  21. }
  22. }
  23. int main()
  24. {
  25. ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  26. cin >> n;
  27. for(long long i=1; i<=n; i++)
  28. for(long long j=1; j<=n; j++) cin >> a[i][j];
  29. backtrack(1);
  30. cout << res;
  31. return 0;
  32. }
  33. /*
  34. Solve by: Truong Tuan Kiet - Informatics K36. Solve in 10h00 - 25/6/2025
  35. */
  36.  
  37.  
  38.  
  39.  
  40.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
-1000000000000000000