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
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

struct Monster
{
    int damage, cure, number;
    
    Monster (int a = 0, int b = 0, int c = 0)
    {
        damage = a;
        cure = b;
        number = c;
    }
    
    int fightResult()	const
    {
        return cure - damage;
    }
};

vector <Monster> withLoss, withoutLoss, withLossCopy;

int res[100000];
bool defeated[100000];

bool comp1(const Monster &A, const Monster &B)  //  withoutLoss
{
    return A.damage < B.damage;
}

bool comp2(const Monster &A, const Monster &B)  //  withLoss
{
    if (A.fightResult() == B.fightResult())
		return A.damage > B.damage;

	return A.fightResult() > B.fightResult();
}

bool comp3(const Monster &A, const Monster &B)  //  withLossCopy
{
    if (A.damage == B.damage)
        return A.cure > B.cure;
        
    return A.damage > B.damage;
}

int main()
{
    int n;
    long long hp;
    
    scanf ("%d%lld", &n, &hp);
    
    for (int i=0; i<n; i++)
    {
        int damage, cure;
        
        scanf ("%d%d", &damage, &cure);
        
        damage > cure ? withLoss.push_back(Monster(damage, cure, i)) : withoutLoss.push_back(Monster(damage, cure, i));
    }
    
    sort(withoutLoss.begin(), withoutLoss.end(), comp1);
    
    int resCtr = 0;
    
    for (int i=0; i<withoutLoss.size(); i++)
    {//printf("%d\n", withoutLoss[i].number + 1);
        if (withoutLoss[i].damage >= hp)
        {
            printf("NIE\n");
            return 0;
        }
        
        hp += withoutLoss[i].fightResult();
        
        res[resCtr++] = withoutLoss[i].number;
    }
    
    withLossCopy = withLoss;
    
    sort(withLoss.begin(), withLoss.end(), comp2);
    sort(withLossCopy.begin(), withLossCopy.end(), comp3);
    
    int i=0, j=0;
    
    while (1)
    {
        while (i < withLoss.size() and defeated[withLoss[i].number])
            i++;
            
        if (i == withLoss.size())
            break;
            
        while (j < withLossCopy.size() and defeated[withLossCopy[j].number])
            j++;
            
        if (j < withLossCopy.size() and withLossCopy[j].damage >= hp + withLoss[i].fightResult())
        {//printf("%d\n", withLossCopy[j].number + 1);
            if (withLossCopy[j].damage >= hp)
            {
                printf("NIE\n");//printf("NIEhp:%lld ,damage:%d\n", hp, withLossCopy[j].damage);
                return 0;
            }
            
            hp += withLossCopy[j].fightResult();
            res[resCtr++] = withLossCopy[j].number;
            defeated[withLossCopy[j].number] = 1;
            
            j++;
        }
        
        else
        {//printf("%d\n", withLoss[i].number + 1);
            if (withLoss[i].damage >= hp)
            {
                printf("NIE\n");
                return 0;
            }
            
            hp += withLoss[i].fightResult();
            res[resCtr++] = withLoss[i].number;
            defeated[withLoss[i].number] = 1;
            
            i++;
        }
    }
    
    printf("TAK\n");
    
    for (int i=0; i<n; i++)
        printf("%d ", res[i] + 1);
        
    return 0;
}

/*
3 5
3 1
4 8
8 3
*/