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
#include <iostream>
using namespace std;

int n,z,d,a;
bool ft=true,cv=false;
void perm(int *Value, int N, int k,int **tab);

int main() {
	cin >> n >> z;
	int **tab = new int *[n];
	for(int i=0;i<n;i++){
		tab[i] = new int [3];}
	int Value[n];
	for(int i=0;i<n;i++){
		Value[i]=0;
		tab[i][0]=i+1;
		cin >> tab[i][1];
		cin >> tab[i][2];
	}
	perm(Value, n, 0, tab);
	if(ft)cout << "NIE";
	return 0;
}

void perm(int *Value, int N, int k, int **tab){
  static int level = -1;
  level = level+1; Value[k] = level;
  if (level == N){
  	d=z;
  	cv=true;
    for(int i=0;i<N;i++){
    	if(cv==true){
    		d=d-tab[Value[i]-1][1];
    		if(d<=0)cv=false;
    		d=d+tab[Value[i]-1][2];}}
    if(cv&&ft){
    	cout << "TAK" << "\n";
    	for(int i=0;i<N-1;i++)cout << tab[Value[i]-1][0] << " ";
    	cout << tab[Value[N-1]-1][0];
    	ft=false;}}
  else
    for (int i=0;i<N;i++)
      if (Value[i] == 0)
         perm(Value, N, i, tab);
  level = level-1; Value[k] = 0;
}