fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define LSOne(S) ((S) & -(S))
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef vector<ii> vii;
  8. typedef vector<int> vi;
  9.  
  10. const int mod = 1000000007;
  11. const int MAX = 20;
  12. int c[MAX][MAX];
  13.  
  14. int nivel(const vi& ssy){ // devuelve la cantidad de compatibilidad que tiene un subconjunto de sayayines
  15. int res = 0;
  16. for(int i = 0; i<ssy.size(); ++i){
  17. for(int j = i + 1; j < ssy.size(); ++j){
  18. res += c[ssy[i]][ssy[j]];
  19. }
  20. }
  21. return res;
  22. }
  23.  
  24. vector<vi> subsets(vi& k, int m, int n){ // devuelve un vector con los subconjuntos de tamaño n
  25. vector<vi> res;
  26.  
  27. for(int i = 0; i < (1 << m); ++i){
  28. vi act;
  29. for(int j = 0; j < m; ++j){
  30. if((i & (1 << j))){
  31. act.push_back(k[j]);
  32. }
  33. }
  34. if(act.size() == n) res.push_back(act);
  35. }
  36. return res;
  37. }
  38.  
  39. void solve(){
  40. int n, m;
  41. cin >> n >> m;
  42. map<int, string> names; // nombres de los sayayines por la posicion en que esten
  43. vi k;
  44. for(int i = 0; i<m; ++i){
  45. cin >> names[i];
  46. k.push_back(i);
  47. for(int j = 0; j<m; ++j){
  48. cin >> c[i][j];
  49. }
  50. }
  51. vector<vi> sub = subsets(k, m, n);
  52. vi resInt;
  53. vector<string> resString;
  54. int maxNiv = INT_MIN;
  55. for(const auto& i : sub){
  56. int niv = nivel(i);
  57. if(niv > maxNiv){
  58. maxNiv = niv;
  59. resInt = i;
  60. }
  61. }
  62. for(const auto& i: resInt) resString.push_back(names[i]);
  63. sort(resString.begin(), resString.end());
  64. for(const auto& i: resString) cout << i << " ";
  65. cout << "\n";
  66.  
  67. //int res = INT_MIN;
  68. //vi
  69.  
  70. //cout << nivel(p);
  71.  
  72. }
  73.  
  74. int main(){
  75. ios_base::sync_with_stdio(false);
  76. cin.tie(nullptr);
  77.  
  78. int TC = 1;
  79. //cin >> TC;
  80. while(TC--) solve();
  81. }
Success #stdin #stdout 0.25s 17148KB
stdin
10 20
GOKU 0 1 -1 3 2 0 1 -2 3 1 2 3 -1 0 3 -2 1 3 0 2
VEGETA 1 0 2 -1 3 -1 2 1 -3 2 1 0 2 -2 3 0 1 2 -1 3
GOHAN 2 -1 3 1 0 3 -2 1 2 -1 0 3 1 2 3 -1 0 2 -2 1
TRUNKS 3 2 -1 0 1 2 -3 1 2 0 1 2 3 -1 0 3 1 2 -2 3
PICCOLO 2 1 0 1 -2 3 0 2 -1 3 2 1 0 3 -1 2 3 -1 0 1
KRILIN 1 -2 3 -3 1 2 -1 2 0 1 2 -1 2 3 -1 0 2 1 -2 3
BULMA -2 1 -3 2 3 -2 1 0 1 -2 3 -1 2 -1 0 1 -3 2 -2 3
CHI_CHI 3 0 1 -1 2 3 -2 1 0 2 1 2 -1 0 3 1 -2 3 -1 2
YAMCHA 1 2 -1 2 -1 2 3 -3 2 0 1 2 -1 0 2 3 1 2 -1 3
TIEN 2 1 0 1 -2 3 -1 2 -2 1 0 3 -1 2 3 1 -1 0 2 -1
stdout
    CHI_CHI GOHAN GOKU KRILIN TRUNKS YAMCHA