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
#include <stdio.h>

#define FOR(ii, ll, uu)  for(int ii##lim = (uu), ii = (ll); ii < ii##lim; ++ii)
#define REP(ii, nn) FOR(ii, 0, nn)

// from stackoverflow
#define getcx getchar_unlocked

inline int giu()
{
	int n = 0;
	int ch=getcx();
	while (ch < '0' || ch > '9')
	{
		ch = getcx();
	}
	while (ch >= '0' && ch <= '9')
		n = (n<<3)+(n<<1) + ch-'0', ch = getcx();
	return n;
}

#define GI (giu())

#include <vector>
#include <algorithm>

using namespace std;

typedef pair<int, int> PII;
#define ST first
#define ND second

int main()
{
	vector<PII> monsters;
	vector<PII> gains;
	vector<PII> losses;
	vector<int> order;
	int n = GI;
	long long z = GI;
	monsters.reserve(n);
	gains.reserve(n);
	losses.reserve(n);
	order.reserve(n);
	long long total_losses = 0;

	REP(i, n)
	{
		int di = GI, ai = GI;
		monsters.push_back(PII(di, ai));
		if (di <= ai)
		{
			gains.push_back(PII(di, i));
		}
		else
		{
			losses.push_back(PII(-ai, i));
			total_losses += di - ai;
		}
	}

	sort(gains.begin(), gains.end());
	REP(i, gains.size())
	{
		PII& m = monsters[gains[i].ND];
		if (m.ST >= z)
		{
			puts("NIE");
			return 0;
		}
		z += m.ND - m.ST;
		order.push_back(gains[i].ND+1);
	}

	if (total_losses >= z)
	{
		puts("NIE");
		return 0;
	}

	sort(losses.begin(), losses.end());
	REP(i, losses.size())
	{
		PII& m = monsters[losses[i].ND];
		if (m.ST >= z)
		{
			puts("NIE");
			return 0;
		}
		z += m.ND - m.ST;
		order.push_back(losses[i].ND+1);
	}

	puts("TAK");
	printf("%d", order[0]);
	REP(i, n-1)
	{
		printf(" %d", order[i+1]);
	}
	puts("");
	return 0;
}