fork download
#include <bits/stdc++.h>
using namespace std;

#define LSOne(S) ((S) & -(S))
typedef long long ll;
typedef pair<int, int> ii; 
typedef vector<ii> vii;
typedef vector<int> vi;

const int mod = 1000000007;
const int MAX = 20;
int c[MAX][MAX];

int nivel(const vi& ssy){ // devuelve la cantidad de compatibilidad que tiene un subconjunto de sayayines
    int res = 0;
    for(int i = 0; i<ssy.size(); ++i){
        for(int j = i + 1; j < ssy.size(); ++j){
            res += c[ssy[i]][ssy[j]];
        }
    }
    return res;
}

vector<vi> subsets(vi& k, int m, int n){ // devuelve un vector con los subconjuntos de tamaño n
    vector<vi> res;

    for(int i = 0; i < (1 << m); ++i){
        vi act;
        for(int j = 0; j < m; ++j){
            if((i & (1 << j))){
                act.push_back(k[j]);
            }
        }
        if(act.size() == n) res.push_back(act);
    }
    return res;
}

void solve(){
    int n, m;
    cin >> n >> m;
    map<int, string> names; // nombres de los sayayines por la posicion en que esten
    vi k;
    for(int i = 0; i<m; ++i){
        cin >> names[i];
        k.push_back(i);
        for(int j = 0; j<m; ++j){
            cin >> c[i][j];
        }
    }
    vector<vi> sub = subsets(k, m, n);
    vi resInt;
    vector<string> resString;
    int maxNiv = INT_MIN;
    for(const auto& i : sub){
        int niv = nivel(i);
        if(niv > maxNiv){
            maxNiv = niv;
            resInt = i;
        }
    }
    for(const auto& i: resInt) resString.push_back(names[i]);
    sort(resString.begin(), resString.end());
    for(const auto& i: resString) cout << i << " ";
    cout << "\n";

    //int res = INT_MIN;
    //vi 

    //cout << nivel(p);

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int TC = 1;
    //cin >> TC;
    while(TC--) solve();
}
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