#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{}; 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() {
// your code goes here
vecvec ot = assign();
for (auto v : ot) {
for(int i : v) std::cout << i << ' ';
std::cout << std::endl;
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKc3RhdGljIGNvbnN0IGludCBOID0gNjsKc3RhdGljIGNvbnN0IGludCBNID0gNjsKCnR5cGVkZWYgc3RkOjp2ZWN0b3I8aW50PiB2ZWM7CnR5cGVkZWYgc3RkOjp2ZWN0b3I8c3RkOjp2ZWN0b3I8aW50Pj4gdmVjdmVjOwoKc3RhdGljIGNvbnN0IGludCBVW01dW05dID0ge3sxLCAxMiwgMTAsIDgsIDksIDN9LAoJCQkJCQkJezE0LCAxNiwgMTcsIDYsIDIsIDE1fSwKCQkJCQkJCXsxMiwgMSwgMTMsIDIsIDQsIDE4fSwKCQkJCQkJCXsxMywgOSwgOSwgMTAsIDEzLCAxMn0sCgkJCQkJCQl7MCwgMiwgMTcsIDE0LCA3LCAxMX0sCgkJCQkJCQl7MTksIDMsIDIsIDE5LCAwLCAxf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