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
#include <iostream>
#include<algorithm>
using namespace std;
int n;
long long int z;
pair<int,int> dod[100008];
pair<int,int> uj[1000008];
int waru[100008];
int ward[100008];
int a;
int b;
int lic;
int uic;
int wic;
int tab[100008];
bool wynik;

int main()
{
    cin >> n;
    cin >> z;
    for( int i=0; i<n; i++ )
    {
        cin >> a;
        cin >> b;
        if ( b-a>=0 )
        {
            dod[lic].first=a;
            ward[i]=b-a;
            dod[lic].second=i;
            lic++;
        }
        else
        {
            uj[uic].first=a;
            waru[i]=b-a;
            uj[uic].second=i;
            uic++;
        }
    }
    sort(&dod[0],&dod[lic]);
    wynik=true;
    for ( int i=0; i<lic; i++ )
    {
        if ( z-dod[i].first > 0 )
        {
            z+=ward[dod[i].second];
            tab[wic]=dod[i].second;
            wic++;

        }
        else
        {
            wynik=false;
            break;
        }
    }
    if ( wynik == true )
    {
        sort(&uj[0],&uj[uic]);
        for ( int i=uic-1; i>=0; i-- )
        {
            if(z>uj[i].first)
            {
                z+=waru[uj[i].second];
                tab[wic]=uj[i].second;
                wic++;
            }
            else
            {
                wynik=false;
                break;
            }
        }
    }
    if( wynik == true )
    {
        cout << "TAK" << endl;
        for ( int i=0; i<wic; i++ )
        {
            cout << tab[i]+1 << ' ';
        }
    }
    else
    {
        cout << "NIE";
    }
    return 0;
}