Advertisement
achulkov2

Untitled

Sep 30th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int n;
  8. int m[20][20];
  9. int ans = 0;
  10. vector<int> best;
  11.  
  12. void work(vector<int>& p) {
  13.     int curr = 0;
  14.     for (int i = 0; i < n; ++i) {
  15.         for (int j = 0; j < i; ++j) {
  16.             if (p[i] != p[j]) curr += m[i][j];
  17.         }
  18.     }
  19.     if (curr > ans) {
  20.         ans = curr;
  21.         best = p;
  22.     }
  23. }
  24.  
  25. void go(vector<int>& p) {
  26.     if (p.size() == n) {
  27.         work(p);
  28.     } else {
  29.         for (int i = 0; i < 2; ++i) {
  30.             p.push_back(i);
  31.             go(p);
  32.             p.pop_back();
  33.         }
  34.     }
  35. }
  36.  
  37. int main() {
  38.     freopen("input.txt", "r", stdin);
  39.     freopen("output.txt", "w", stdout);
  40.     cin >> n;
  41.     for (int i = 0; i < n; ++i) {
  42.         for (int j = 0; j < n; ++j) {
  43.             cin >> m[i][j];
  44.         }
  45.     }
  46.     vector<int> p;
  47.     go(p);
  48.     cout << ans << "\n";
  49.     for (int i = 0; i < n; ++i) {
  50.         cout << best[i] + 1 << " ";
  51.     }
  52.     cout << "\n";
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement