#include <iostream>
#include <vector>
static const int N = 6;
static const int M = 6;
typedef std::vector<int> vec;
typedef std::vector<std::vector<int>> vecvec;
static const int U[M][N] = {{1, 12, 10, 8, 9, 3},
{14, 16, 17, 6, 2, 15},
{12, 1, 13, 2, 4, 18},
{13, 9, 9, 10, 13, 12},
{0, 2, 17, 14, 7, 11},
{19, 3, 2, 19, 0, 1}};
vecvec assign(int n,int m, vec ne, vec po, vecvec ot, int j) {
for (int i = 0; i < m; i++) {
if(po[i] == 0) {
po[i] = 1;
ne[j] = i;
ne[n] += U[ne[j]][j];
if (j < n-1) ot = assign(n, m, ne, po, ot, j+1);
if(ne[n] >= ot[0][n]) {
if(ne[n] > ot[0][n]) ot.clear();
ot.push_back(ne);
}
else {
po[i] = 0;
ne[n] -= U[ne[j]][j];
}
}
}
return ot;
}
vecvec assign() {
return assign( N, M, vec(N+1, 0), vec(N+1, 0), vecvec(1,vec(N+1, 0)), 0);
}
int main() {
vecvec ot = assign();
for (auto v : ot) {
for(int i : v) std::cout << i << ' ';
std::cout << std::endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgogCnN0YXRpYyBjb25zdCBpbnQgTiA9IDY7CnN0YXRpYyBjb25zdCBpbnQgTSA9IDY7CiAKdHlwZWRlZiBzdGQ6OnZlY3RvcjxpbnQ+IHZlYzsKdHlwZWRlZiBzdGQ6OnZlY3RvcjxzdGQ6OnZlY3RvcjxpbnQ+PiB2ZWN2ZWM7CiAKc3RhdGljIGNvbnN0IGludCBVW01dW05dID0ge3sxLCAxMiwgMTAsIDgsIDksIDN9LAoJCQkJCQkJezE0LCAxNiwgMTcsIDYsIDIsIDE1fSwKCQkJCQkJCXsxMiwgMSwgMTMsIDIsIDQsIDE4fSwKCQkJCQkJCXsxMywgOSwgOSwgMTAsIDEzLCAxMn0sCgkJCQkJCQl7MCwgMiwgMTcsIDE0LCA3LCAxMX0sCgkJCQkJCQl7MTksIDMsIDIsIDE5LC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