fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 30;
  4. int n;
  5. int c[N][N];
  6. int choose[N];
  7. int ans[N];
  8. bool check[N];
  9. int res = 0;
  10. int maxrow[N];
  11. void prepare(){
  12. for (int i = 0; i < n; i++ ){
  13. int maxval = -1 ;
  14. for ( int j = 0 ; j < n ;j++ ){
  15. if ( c[i][j] > maxval ){
  16. maxval = c[i][j];
  17. }
  18. }
  19. maxrow[i] = maxval;
  20. }
  21. for (int i = n - 2; i >= 0; i-- ){
  22. maxrow[i] += maxrow[i + 1];
  23. }
  24. }
  25. void dequy ( int id , int curc ){
  26. if ( id < n && curc + maxrow[id] <= res ) return;
  27. if ( id == n ){
  28. if ( curc > res) {
  29. res = curc ;
  30. for ( int i = 0 ; i < n ; i++ ){
  31. ans[i] = choose[i];
  32. }
  33. }
  34. return;
  35. }
  36. for (int i = 0; i < n ; i++ ){
  37. if ( !check[i] ){
  38. check[i] = true;
  39. choose[id] = i + 1 ;
  40. dequy(id + 1 , curc + c[id][i]);
  41. check[i] = false;
  42. }
  43. }
  44. }
  45. int main(){
  46. cin >> n;
  47. for ( int i = 0 ; i < n ; i++ ){
  48. for ( int j = 0 ; j < n ; j++ ){
  49. cin >> c[i][j];
  50. }
  51. }
  52. prepare();
  53. dequy(0, 0);
  54. cout << res << endl;
  55. for ( int i = 0 ; i < n ; i++ ){
  56. cout << ans[i] << " ";
  57. }
  58. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
0