1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <chrono>
#include <functional>
#include <cstdlib>
#include <cassert>
using namespace std;

std::vector<int> computeK2(std::vector<int>& numbers)
{
  std::vector<int> results;
  std::vector<int> min(numbers.size());
  min[0] = numbers[0];
  for (int i = 1; i < numbers.size(); ++i)
  {
    min[i] = std::min(min[i - 1], numbers[i]);
  }
  std::vector<int> max(numbers.size());
  max[numbers.size() - 1] = numbers[numbers.size() - 1];
  for (int i = numbers.size() - 2; i >= 0; --i)
  {
    max[i] = std::max(max[i + 1], numbers[i]);
  }
  for (int end = 0; end < numbers.size() - 1; ++end)
  {
    if (min[end] >= max[end + 1])
    {
      results.push_back(end + 1);
      return results;
    }
  }
  return results;
}

std::vector<int> computeK3(std::vector<int>& numbers)
{
  std::vector<int> results;
  int min = numbers[0];
  int max = numbers[0];
  for (int i = 0; i < numbers.size(); ++i)
  {
    min = std::min(min, numbers[i]);
    max = std::max(max, numbers[i]);
  }
  for (int i = 0; i < numbers.size(); ++i)
  {
    if ((numbers[i] == min && i>0) || (numbers[i] == max && i < numbers.size()-1))
    {
      if (i == 0)
      {
        results.push_back(i+1);
        results.push_back(i + 2);
      }
      else
      {
        results.push_back(i);
        results.push_back(i+1);
      }
      return results;
    }
  }
  return results;
}

std::vector<int> computeKMany(std::vector<int>& numbers, int k)
{
  std::vector<int> results;
  int firstNotSorted = -1;
  for (int i = 0; i < numbers.size()-1; ++i)
  {
    if (numbers[i] >= numbers[i+1])
    {
      firstNotSorted = i;
      break;
    }
  }
  if (firstNotSorted == -1)
    return results;
  if (firstNotSorted == 0)
  {
    for (int i = 0; i <k-1; ++i)
      results.push_back(i+1);
    return results;
  }
  int end = std::min(firstNotSorted - 1, k - 4);
  if ((firstNotSorted + 2) == numbers.size())
    end = std::min(firstNotSorted - 1, k - 3);
  for (int i = 0; i < end; ++i)
    results.push_back(i + 1);
  results.push_back(firstNotSorted);
  results.push_back(firstNotSorted+1);
  if (firstNotSorted + 2 < numbers.size())
    results.push_back(firstNotSorted+2);
  for (int i=firstNotSorted + 2; results.size() < k-1; ++i)
    results.push_back(i+1);
  return results;
}


std::vector<int> getIntervals(std::vector<int> & numbers, int k)
{
  if (k == 2)
  {
    return computeK2(numbers);
  }
  if (k == 3)
  {
    return computeK3(numbers);
  }
  return computeKMany(numbers, k);
}


bool checkSolution(std::vector<int>& numbers, int k, std::vector<int>& results)
{
  int generator = 0;
  bool found = false;
  for (int generator = 0; generator < (1 << numbers.size()); ++generator)
  {
    std::vector<int> intervals;
    for (int i = 0; i < numbers.size()-1; ++i)
    {
      if (generator & (1<<i))
        intervals.push_back(i+1);
    }
    if (intervals.size() != (k-1))
      continue;
    std::vector<int> newIntervals = intervals;
    newIntervals.push_back(numbers.size());
    int previousEnd=0;
    int previousMin = -1;
    int success = false;
    for (int i = 0; i<newIntervals.size(); ++i)
    {
      int currentEnd = newIntervals[i];
      int currenMin = -1;
      for (int pos = previousEnd; pos < currentEnd; ++pos)
      {
        int value = numbers[pos];
        if (value > previousMin)
        {
          if (currenMin == -1)
            currenMin = value;
          else
            currenMin = std::min(currenMin, value);
        }
      }
      if (currenMin == -1)
      {
        success = true;
        if (intervals == results)
          return true;
        break;
      }
      previousMin = currenMin;
      previousEnd = currentEnd;
    }
    if (success)
    {
      found = true;
    }
  }
  if (found)
  {
    return false;
  }
  else
  {
    if (results.empty())
      return true;
    else
      return false;
  }
}

void test(std::vector<int> & initialNumbers, int restNumber)
{
  if (restNumber == 0)
  {
    for (int k = 2; k <= initialNumbers.size(); ++k)
    {
      std::vector<int> intervals = getIntervals(initialNumbers, k);
      bool success = checkSolution(initialNumbers, k, intervals);
      if (!success)
      {
        std::cout << "ERROR\n";
      }
    }
  }
  else
  {
    for (int i = 1; i < initialNumbers.size() + restNumber; ++i)
    {
      initialNumbers.push_back(i);
      test(initialNumbers, restNumber-1);
      initialNumbers.pop_back();
    }
  }
}

void testAll()
{
  std::vector<int> initialNumbers;
  test(initialNumbers, 2);
  test(initialNumbers, 3);
  test(initialNumbers, 4);
  test(initialNumbers, 5);
  test(initialNumbers, 6);
  //test(initialNumbers, 7);
}

void testSingle()
{
  std::vector<int> initialNumbers{1,2,3,1, 1};
  std::vector<int> intervals = getIntervals(initialNumbers, 4);
  checkSolution(initialNumbers, 4, intervals);
}

int main()
{
  //testAll();
  //testSingle();
  //return 0;
  const clock_t begin_time = clock();
  ios::sync_with_stdio(false);
  int n;
  std::cin >> n;
  int k;
  std::cin >> k;
  std::vector<int> numbers;
  for (int i = 0; i < n; ++i)
  {
    int number;
    std::cin >> number;
    numbers.push_back(number);
  }
  std::vector<int> intervals = getIntervals(numbers, k);
  if (intervals.size() == 0)
    std::cout << "NIE\n";
  else
  {
    std::cout << "TAK\n";
    for (int end: intervals)
      std::cout << end << " ";
  }


  //std::cout << float(clock() - begin_time) / CLOCKS_PER_SEC << "\n";
  return 0;
};