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
//Michal Wos MIM UW
#include <climits>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <string>
#include <string.h>
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define f first
#define s second
#define x first
#define y second
#define Size(x) ((int)(x).size())
#define FOR(z,b,e) for(__typeof(b) z = b;  z<=e; z++ )
#define debon 1
#define deb(burak) if(debon) {cout<<"DEB-> "<<#burak<<": "<<burak<<endl;}
#define debv(burak) if(debon) {cout<<"DEB-> "<<#burak<<": \t"; for(unsigned int zyx=0;zyx<burak.size();zyx++) cout<<burak[zyx]<<" "; cout<<endl;}
#define debt(burak,SIzE) if(debon) {cout<<"DEB-> "<<#burak<<": \t"; for(unsigned int zyx=0;zyx<SIzE;zyx++) cout<<burak[zyx]<<" "; cout<<endl;}
#define debend if(debon) {cout<<"_____________________"<<endl;}
#define memcheck if(debon) {FILE *fp = fopen("/proc/self/status","r");while( !feof(fp) ) putchar(fgetc(fp));}
using namespace std;
typedef unsigned long long ULL;
typedef long long LL;
void readLL(LL *n) {register char c=0; while (c < 33) c=getc_unlocked(stdin); (*n)=0; while (c>32) {(*n)=(*n)*10LL + (c-'0'); c=getc_unlocked(stdin);}}
const int inf=1073741824,mod=1e9+7;
const int s=1e5+10;

int n;
LL z,h;
vector <int> kolejnosc;

struct Mons {
    int el, ob, id;
    Mons(int el1, int ob1, int id1) : el(el1), ob(ob1), id(id1) {
    }
    int diff() {
        return el - ob;
    }
};

bool byObrazenia (const Mons& a, const Mons& b) {
    if (a.ob == b.ob) {
        return (a.el > b.el);
    }
    return (a.ob < b.ob);
}

bool byEliksir (const Mons& a, const Mons& b) {
    if ( a.el == b.el ) {
        return (a.ob > b.ob);
    }
    return (a.el > b.el);
}

bool check( const vector<Mons>& potw ) {
    for (int i = 0; i < Size(potw); i++ ) {
        z -= potw[i].ob;
        if ( z <= 0 ) {
            printf("NIE\n");
            return false;
        }
        z += potw[i].el;
        kolejnosc.pb( potw[i].id );
    }
    return true;
}

int main()
{
    scanf("%d%lld",&n, &z);
    h = z;
    vector <Mons> dodatnie, ujemne;

    int d,el;
    for ( int i = 1; i <= n; i++ ) {
        scanf("%d%d",&d,&el);
        h += el - d;
        Mons m(el, d, i);
        if ( el - d >= 0 ) {
            dodatnie.pb(m);
        } else {
            ujemne.pb(m);
        }
    }
    // a little heuristic never kill nobody
    if ( h <= 0 ) {
        printf("NIE\n");
        return 0;
    }

    sort(dodatnie.begin(), dodatnie.end(), byObrazenia);

    if ( !check(dodatnie) ) {
        return 0;
    }

    sort(ujemne.begin(), ujemne.end(), byEliksir);

    if ( !check(ujemne) ) {
        return 0;
    }

    printf("TAK\n");

    for ( int i = 0; i < Size(kolejnosc); i++ ) {
        printf("%d ", kolejnosc[i]);
    }
    /* */

    return 0;
}