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
#include <iostream>
#include <algorithm>

using namespace std;

struct Walka
{
    int a;
    int g;
    int r;
    int id;
};

Walka w[100000];

bool cmp(Walka a, Walka b)
{
    return (a.a > b.a);
}

int main()
{
    ios::sync_with_stdio(0);

    int n,z, atak, gratis, suma;
    cin >> n >> z;
    suma = z;

    for(int i = 0;i < n;i++)
    {
        cin >> w[i].a >> w[i].g;

        w[i].r = w[i].g - w[i].a;
        w[i].id = i + 1;

        suma += w[i].r;
    }

    sort(w, w+n, cmp);

    if(suma > 0)
    {
        cout << "TAK" << endl;
        for(int i = 0;i < n;i++)
        {
            if(w[i].a == 500000)
                continue;
            if(z - w[i].a > 0)
            {
                cout << w[i].id << " ";
                z -= w[i].a;
                z += w[i].g;
            }
            else
            {
                for(int j = i + 1;i < n;j++)
                {
                    if(z - w[j].a > 0)
                    {
                        cout << w[j].id << " ";
                        z -= w[j].a;
                        z += w[j].g;
                        w[j].a = 500000;
                        break;
                    }
                }
                i--;
            }
        }
        cout << endl;
    }
    else
        cout << "NIE" << endl;

    return 0;
}