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

class P
{
public:
    P(int d, int h, int n) : d(d), h(h), n(n) {}
    int d, h, n;
};

class N
{
public:
    N(int d, int h, int n) : d(d), h(h), n(n) {}
    int d, h, n;
};


//sortuje po damage, najpierw najmniejsze
inline bool cmp1(const P& a, const P& b)
{
    return a.d < b.d;
}
//sortuje po health, najpierw największe
inline bool cmp2(const N& a, const N& b)
{
    return a.h > b.h;
}
int main()
{
    int n;
    LL z;
    scanf("%d%lld", &n, &z);
    vector<P> pv;
    vector<N> nv;
    pv.reserve(n);
    nv.reserve(n);
    vector<int> res1, res2;
    res1.reserve(n);
    res2.reserve(n);
    for(int i = 0; i < n; ++i)
    {
        int d, h;
        scanf("%d%d", &d, &h);
        if(d <= h)
            pv.push_back(P(d, h, i));
        else
            nv.push_back(N(d, h, i));
    }
    sort(pv.begin(), pv.end(), cmp1);
    sort(nv.begin(), nv.end(), cmp2);
    bool flag = true;
    for(int i = 0; i < pv.size() && flag; ++i)
    {
        z -= pv[i].d;
        if(z <= 0)
            flag = false;
        z += pv[i].h;
        res1.push_back(pv[i].n);
    }
    for(int i = 0; i < nv.size() && flag; ++i)
    {
            z -= nv[i].d;
            if(z <= 0)
                flag = false;
            res2.push_back(nv[i].n);
            z += nv[i].h;
    }

    if(flag)
    {
        printf("TAK\n");
        for(int i = 0; i < res1.size(); ++i)
            printf("%d ", res1[i]+1);
        for(int i = 0; i < res2.size(); ++i)
            printf("%d ", res2[i]+1);
        printf("\n");
    }
    else
    {
        printf("NIE\n");
    }
    return 0;
}