Problem: Zombies seem to have become much more intelligent lately – a few have somehow wandered into the base through the automatic gate. Heidi has had to beef up security, and a new gate has been installed. Unfortunately, now the questions being asked are more complicated, and even humans have trouble answering them. Can you still program the robot army to do this reliably?

The new questions are of the following form: a grayscale photograph has been divided into several horizontal pieces, which have been arbitrarily rearranged. The task is to assemble the original image back from these pieces (somewhat like in a jigsaw puzzle). To further delay the zombies, significant Gaussian-distributed noise has been added to the image.

Input

The input format is the same as in the previous version, except that the first line of every question now contains three space-separated numbers h, w and k (1 ≤ h, w ≤ 600, 2 ≤ k ≤ 16) – the height (number of rows) and width (number of columns) of the photograph and the number of pieces, respectively. The number of pieces evenly divides the height, and each piece is of the same height h / k.

Again, there is only one input file to be processed, and the same resources are provided to you as in the previous version (except that now you are given all input images in .bmp format, rather than the first 50).

Output

Your program should print q lines. The i-th line should contain your answer for the i-th question: a space-separated sequence of k numbers π1, π2, ..., πk such that: 

  * π is a permutation of {1, 2, ..., k}, that is, each number from 1 to k appears exactly once in π, 
  * for each j = 1, ..., k, πj is the position (index), in the original image, of the piece which is at position j in the input image. (See the illustration below for clarity.) 

<image>

The second image from the test set. If the three pieces in the original image are numbered 1, 2, 3 from top to bottom, then the numbering in the image on the right should be 2, 3, 1. The correct answer for this image is thus 2 3 1.

Again, your answers will be accepted if they conform to this format and if at least 75% of them are correct.

Again, you may process the input locally and submit just your precomputed answers (i.e., a program which just prints your output for the input file all.in).

Note

The link to download all the necessary materials is http://assets.codeforces.com/files/690/medium_contestant_package.zip

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
struct Task {
  static const int MK = 16, MX = 1 << MK, MH = 1000;
  std::size_t k, h, w;
  std::array<std::array<long long int, MX>, MK> DP, Parent;
  std::array<std::array<long long int, MH>, MH> img;
  std::array<std::array<long long int, MK>, MK> Cost;
  void clear() {
    for (std::size_t i = 0; i < MH; ++i)
      for (std::size_t j = 0; j < MH; ++j) img[i][j] = 0;
    for (std::size_t i = 0; i < MK; ++i)
      for (std::size_t j = 0; j < MX; ++j) DP[i][j] = -1;
    for (int i = 0; i < (int)k; ++i) {
      DP[i][1 << i] = 0;
      Parent[i][1 << i] = -1;
    }
  }
  long long int dist(const std::size_t x, const std::size_t y) {
    ;
    long long int res = 0;
    for (std::size_t i = 0; i < w / 2; ++i) {
      long long int first = 0, second = 0;
      for (std::size_t j = i * 2; j < (i + 1) * 2; ++j) {
        ;
        first += img[x][j];
        second += img[y][j];
      }
      long long int diff = std::max(0LL, std::abs(first - second) - 40);
      res += diff * diff * diff;
    };
    return res;
  }
  void compute() {
    for (std::size_t i = 0; i < k; ++i) {
      for (std::size_t j = 0; j < k; ++j) {
        Cost[i][j] = dist((i + 1) * (h / k) - 1, j * (h / k));
      }
    }
  }
  void read() {
    scanf("%lu%lu%lu", &h, &w, &k);
    for (std::size_t i = 0; i < h; ++i) {
      for (std::size_t j = 0; j < w; ++j) {
        scanf("%lld", &img[i][j]);
        ;
      };
    }
  }
  std::vector<int> TSP() {
    for (int i = 0; i < (int)k; ++i) {
      for (int mask = 0; mask < (1 << k); ++mask) {
        Parent[i][mask] = -1;
        DP[i][mask] = -1;
      }
    }
    for (int i = 0; i < (int)k; ++i) {
      DP[i][1 << i] = 0;
    }
    for (int mask = 0; mask < (1 << k); ++mask) {
      for (int i = 0; i < (int)k; ++i) {
        if (DP[i][mask] == -1) continue;
        for (int j = 0; j < (int)k; ++j) {
          int tmp = (1 << j);
          if ((tmp & mask) != 0) continue;
          tmp = mask ^ tmp;
          if (DP[j][tmp] == -1 || DP[j][tmp] > DP[i][mask] + Cost[i][j]) {
            DP[j][tmp] = DP[i][mask] + Cost[i][j];
            Parent[j][tmp] = i;
          }
        }
      }
    }
    int mask = (1 << k) - 1;
    int best = 0;
    for (int i = 0; i < (int)k; ++i) {
      ;
      if (DP[i][mask] < DP[best][mask] && DP[i][mask] != -1) {
        best = i;
      }
    };
    std::vector<int> result;
    while (Parent[best][mask] != -1) {
      ;
      fflush(stderr);
      result.push_back(best);
      int tmp = mask ^ (1 << best);
      best = Parent[best][mask];
      mask = tmp;
    }
    result.push_back(best);
    std::reverse(result.begin(), result.end());
    std::vector<int> wyniczek(result.size());
    for (int i = 0; i < (int)result.size(); ++i)
      wyniczek.at(result.at(i)) = i + 1;
    return wyniczek;
  }
  friend std::istream &operator>>(std::istream &stream, Task &in);
};
std::istream &operator>>(std::istream &stream, Task &in) {
  stream >> in.h >> in.w >> in.k;
  for (std::size_t i = 0; i < in.h; ++i)
    for (std::size_t j = 0; j < in.w; ++j) stream >> in.img[i][j];
  return stream;
}
Task a;
std::string T[1000];
int main() {
  T[1] = "2 1 ";
  T[2] = "2 3 1 ";
  T[3] = "4 3 2 1 ";
  T[4] = "1 3 4 2 5 ";
  T[5] = "1 6 11 3 10 9 15 12 7 13 2 5 4 14 8 ";
  T[6] = "5 1 3 11 10 7 6 9 13 15 12 4 14 2 8 ";
  T[7] = "9 7 2 5 1 10 8 3 4 6 ";
  T[8] = "2 1 4 3 ";
  T[9] = "2 10 6 15 7 12 3 5 14 4 8 11 13 1 9 ";
  T[10] = "11 5 6 8 10 12 7 2 1 4 9 3 13 14 15 ";
  T[11] = "11 7 8 4 5 15 13 14 3 9 12 2 1 10 6 ";
  T[12] = "12 7 11 4 10 2 5 14 13 1 6 3 9 8 15 16 ";
  T[13] = "3 2 1 ";
  T[14] = "4 2 1 3 5 ";
  T[15] = "1 8 11 15 3 2 7 16 13 4 6 10 9 12 5 14 ";
  T[16] = "9 8 6 13 11 10 2 7 14 12 5 4 15 3 1 ";
  T[17] = "12 8 9 3 1 14 2 11 4 16 10 7 5 13 15 6 ";
  T[18] = "15 5 2 14 3 13 1 7 12 8 4 10 6 11 9 ";
  T[19] = "9 7 3 14 2 12 13 5 1 15 11 10 8 4 6 ";
  T[20] = "9 7 13 10 15 16 1 5 6 2 4 11 8 3 14 12 ";
  T[21] = "6 13 2 11 5 10 3 14 9 1 12 8 16 4 15 7 ";
  T[22] = "2 6 15 13 12 7 4 9 16 11 10 1 5 8 3 14 ";
  T[23] = "3 2 6 14 7 12 10 9 5 4 8 15 11 13 1 ";
  T[24] = "3 11 4 5 14 10 16 9 8 6 7 13 12 1 15 2 ";
  T[25] = "4 3 11 9 8 16 6 15 2 13 7 14 10 1 12 5 ";
  T[26] = "1 12 9 7 6 5 2 13 14 10 15 8 11 4 3 ";
  T[27] = "2 1 3 ";
  T[28] = "2 1 ";
  T[29] = "1 3 2 6 4 8 7 5 ";
  T[30] = "14 7 8 4 2 9 13 6 1 5 11 15 16 10 3 12 ";
  T[31] = "1 7 4 2 6 5 3 8 ";
  T[32] = "13 7 6 14 12 15 3 5 1 9 8 10 4 11 2 ";
  T[33] = "11 2 15 5 14 3 9 10 7 1 12 13 8 6 4 ";
  T[34] = "4 2 3 1 ";
  T[35] = "16 13 11 14 9 2 15 3 1 5 6 7 12 8 4 10 ";
  T[36] = "3 1 4 9 16 15 7 10 6 13 5 11 2 14 12 8 ";
  T[37] = "2 4 16 8 13 14 1 12 11 15 7 10 3 9 6 5 ";
  T[38] = "3 1 6 5 2 4 ";
  T[39] = "14 8 2 10 6 16 9 7 15 4 1 3 11 13 5 12 ";
  T[40] = "15 8 10 9 11 12 7 13 5 14 1 4 3 2 6 ";
  T[41] = "5 7 3 10 6 12 8 4 11 1 2 9 ";
  T[42] = "2 1 ";
  T[43] = "13 14 8 6 4 7 5 10 3 11 2 9 15 12 1 ";
  T[44] = "2 15 11 13 12 5 3 4 9 8 14 6 10 1 7 ";
  T[45] = "3 1 4 2 ";
  T[46] = "2 12 16 1 3 6 15 11 4 10 9 14 5 7 13 8 ";
  T[47] = "3 1 2 ";
  T[48] = "16 4 13 11 7 9 5 2 10 3 6 15 12 14 8 1 ";
  T[49] = "2 5 1 9 15 7 3 11 13 4 8 12 6 14 10 ";
  T[50] = "6 3 13 11 14 10 7 2 5 16 4 12 8 1 9 15 ";
  T[51] = "5 7 11 2 10 15 1 9 3 8 14 13 16 12 4 6 ";
  T[52] = "16 1 2 3 7 15 6 12 8 11 10 14 13 4 9 5 ";
  T[53] = "6 5 2 10 12 8 4 13 9 11 1 15 3 14 7 ";
  T[54] = "12 13 9 1 3 11 4 8 16 14 10 7 15 5 6 2 ";
  T[55] = "4 2 3 1 ";
  T[56] = "8 5 15 2 12 1 11 6 16 4 13 10 14 7 9 3 ";
  T[57] = "6 3 1 5 2 4 ";
  T[58] = "14 11 3 7 2 15 4 5 6 10 1 13 8 9 12 ";
  T[59] = "10 8 2 11 15 5 1 13 16 12 14 9 6 4 3 7 ";
  T[60] = "8 2 16 11 9 12 15 6 13 7 4 5 14 1 10 3 ";
  T[61] = "3 9 10 13 6 16 5 4 8 7 12 11 1 15 14 2 ";
  T[62] = "1 5 6 12 3 14 4 2 9 13 8 7 15 11 10 ";
  T[63] = "6 7 3 2 5 8 1 9 10 4 ";
  T[64] = "16 11 15 13 3 14 1 10 2 8 4 5 6 12 7 9 ";
  T[65] = "8 1 13 15 7 10 5 9 3 2 6 4 12 11 14 ";
  T[66] = "12 5 11 14 3 2 1 8 10 16 15 7 6 9 4 13 ";
  T[67] = "3 2 1 ";
  T[68] = "4 7 8 2 1 6 5 3 ";
  T[69] = "2 1 ";
  T[70] = "10 3 7 6 12 4 1 5 9 2 8 13 16 15 14 11 ";
  T[71] = "15 9 2 8 1 4 14 13 5 3 12 6 7 11 10 ";
  T[72] = "4 1 5 2 3 ";
  T[73] = "3 8 6 11 9 5 1 7 2 4 10 12 ";
  T[74] = "16 10 1 4 7 12 8 3 9 14 11 5 2 15 6 13 ";
  T[75] = "16 15 3 8 14 2 4 5 11 6 7 13 1 12 10 9 ";
  T[76] = "11 16 14 1 6 12 2 3 4 8 7 13 10 9 15 5 ";
  T[77] = "15 14 4 6 9 5 3 2 13 12 10 11 7 1 8 ";
  T[78] = "13 10 15 11 4 16 2 3 14 9 5 6 8 7 12 1 ";
  T[79] = "4 3 5 14 6 8 16 10 9 12 2 11 13 15 7 1 ";
  T[80] = "1 4 5 10 9 6 8 3 2 7 ";
  T[81] = "7 6 15 5 12 13 2 4 3 14 11 1 10 8 9 ";
  T[82] = "2 14 9 3 8 7 6 15 10 11 16 5 12 13 1 4 ";
  T[83] = "2 5 9 1 11 4 16 6 8 7 12 3 13 10 15 14 ";
  T[84] = "3 5 7 14 1 9 6 4 10 8 11 15 2 16 12 13 ";
  T[85] = "15 14 10 13 1 5 2 12 4 11 8 9 6 7 3 16 ";
  T[86] = "6 1 4 16 2 9 8 5 12 11 10 13 3 7 14 15 ";
  T[87] = "16 14 9 8 4 1 7 2 12 10 3 5 11 6 15 13 ";
  T[88] = "6 1 5 2 4 3 ";
  T[89] = "3 10 4 5 9 6 1 2 7 8 ";
  T[90] = "8 2 15 10 12 5 14 11 4 1 3 13 7 9 6 ";
  T[91] = "5 13 12 7 9 1 10 4 15 8 3 2 14 6 11 ";
  T[92] = "2 3 6 1 4 5 ";
  T[93] = "4 15 13 6 7 11 12 3 14 2 8 9 1 10 5 ";
  T[94] = "14 7 8 6 12 13 16 15 3 10 11 9 1 4 5 2 ";
  T[95] = "7 2 4 13 9 1 15 8 12 11 6 3 5 14 10 ";
  T[96] = "3 4 5 6 15 8 9 10 14 12 11 13 7 1 2 ";
  T[97] = "4 11 5 12 8 14 10 7 3 9 16 13 15 1 6 2 ";
  T[98] = "2 6 3 1 4 5 ";
  T[99] = "6 5 7 9 2 8 3 1 4 10 ";
  T[100] = "14 7 15 11 1 4 3 13 5 10 6 9 8 2 12 ";
  T[101] = "15 14 5 3 7 4 1 9 11 6 10 2 12 13 8 ";
  T[102] = "16 14 3 7 13 2 6 1 10 12 9 4 5 8 11 15 ";
  T[103] = "3 8 5 10 12 11 4 6 7 9 2 1 ";
  T[104] = "11 3 5 4 12 8 1 2 6 7 9 10 ";
  T[105] = "3 11 6 16 13 15 5 2 12 7 14 8 10 9 4 1 ";
  T[106] = "6 5 1 4 3 2 ";
  T[107] = "1 4 2 10 12 11 9 5 6 13 3 14 15 8 7 ";
  T[108] = "7 13 10 5 2 12 6 3 8 4 15 11 1 9 14 ";
  T[109] = "12 5 7 8 1 9 10 15 6 4 14 13 3 2 11 ";
  T[110] = "2 5 3 9 13 4 7 12 6 14 10 11 15 1 8 ";
  T[111] = "5 4 2 6 1 3 ";
  T[112] = "4 8 9 1 5 13 11 7 3 12 2 6 14 15 10 ";
  T[113] = "2 1 ";
  T[114] = "11 5 7 9 15 2 8 14 3 13 10 12 1 6 4 ";
  T[115] = "5 14 15 4 13 6 8 10 7 12 2 11 16 3 9 1 ";
  T[116] = "12 8 7 2 3 9 15 5 11 6 4 14 13 1 10 ";
  T[117] = "14 7 11 13 2 3 12 1 10 9 5 8 4 15 6 ";
  T[118] = "6 4 3 5 1 2 7 8 ";
  T[119] = "8 16 4 11 13 5 2 14 9 6 3 15 12 1 10 7 ";
  T[120] = "3 5 1 8 2 9 7 12 4 11 10 6 ";
  T[121] = "2 5 4 9 11 12 13 6 3 1 15 10 8 7 14 ";
  T[122] = "13 6 8 11 12 15 1 2 10 9 7 14 3 5 4 ";
  T[123] = "2 11 15 12 5 8 9 1 14 10 4 3 6 7 13 ";
  T[124] = "5 8 16 3 10 6 14 7 1 15 12 9 13 2 11 4 ";
  T[125] = "10 12 1 16 11 4 2 15 6 7 13 14 5 3 8 9 ";
  T[126] = "1 2 5 3 4 ";
  T[127] = "6 5 1 4 2 3 ";
  T[128] = "8 4 7 5 6 1 2 3 ";
  T[129] = "4 2 3 1 ";
  T[130] = "4 5 6 8 3 1 2 9 10 7 ";
  T[131] = "1 8 10 12 14 13 16 11 9 4 3 2 7 5 15 6 ";
  T[132] = "2 1 ";
  T[133] = "13 7 15 14 10 1 8 6 2 11 9 3 4 12 5 ";
  T[134] = "15 13 8 2 7 11 6 5 14 9 10 12 3 4 1 ";
  T[135] = "2 1 ";
  T[136] = "8 4 5 10 6 1 14 13 12 9 2 3 11 15 7 ";
  T[137] = "10 3 6 8 5 11 14 16 7 1 9 2 13 4 15 12 ";
  T[138] = "7 5 6 2 4 1 8 3 ";
  T[139] = "10 2 7 9 11 6 1 8 3 5 12 4 ";
  T[140] = "4 2 3 1 ";
  T[141] = "3 13 9 7 11 15 14 10 5 2 16 12 4 8 1 6 ";
  T[142] = "1 6 10 4 15 5 3 13 7 12 9 2 8 11 14 ";
  T[143] = "6 11 2 3 1 12 7 4 16 15 5 8 10 9 13 14 ";
  T[144] = "4 3 1 2 5 ";
  T[145] = "3 4 11 7 15 1 13 10 9 14 16 12 5 8 2 6 ";
  T[146] = "4 9 14 7 12 3 10 5 13 1 2 11 8 6 15 ";
  T[147] = "10 9 16 13 7 3 12 4 15 8 5 1 11 2 6 14 ";
  T[148] = "2 1 ";
  T[149] = "14 4 2 1 13 5 3 6 12 10 7 9 15 8 11 ";
  T[150] = "3 4 1 6 2 5 ";
  T[151] = "5 10 4 6 2 8 11 9 1 7 3 13 15 14 12 ";
  T[152] = "2 4 1 3 ";
  T[153] = "16 14 12 11 7 3 2 6 1 9 4 10 15 13 5 8 ";
  T[154] = "14 12 1 4 16 15 2 11 5 8 10 9 6 13 7 3 ";
  T[155] = "1 2 3 4 ";
  T[156] = "8 11 2 3 1 10 14 6 13 7 15 5 12 4 9 ";
  for (int i = 1; i <= 156; ++i) std::cout << T[i] << "\n";
  return 0;
  int t;
  std::cin >> t;
  int i = 1;
  while (t--) {
    std::cin >> a;
    a.compute();
    std::cout << "T[" << i << "] = \"";
    for (auto it : a.TSP()) std::cout << it << " ";
    std::cout << "\";\n";
    ++i;
  }
  return 0;
}