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
// Nikodem "nixon" Kramarz
// zad. Bohater, PA 2014

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#define MAX_N 100009
using namespace std;

struct monster
{
    int cost, balance, ind;
};

bool cmp_act(monster a, monster b)
{

    if(a.cost != b.cost)
        return a.cost > b.cost;

    else if(a.balance != b.balance)
        return a.balance > b.balance;
    else
        return a.ind < b.ind;
}

bool cmp(monster a, monster b)
{
    if(a.cost != b.cost)
        return a.cost < b.cost;
    else if(a.balance != b.balance)
        return a.balance < b.balance;
     return a.ind < b.ind;
}

int n, z; 
monster monsters[MAX_N];
vector <int> res;
vector <monster> act, pending;

int main()
{
    ios_base::sync_with_stdio(0);
    cin >> n >> z;

    int a;
    for(int i=0; i<n; i++)
    {
        monster my_monster;
        cin >> my_monster.cost >> a;
        my_monster.balance = a - my_monster.cost;
        my_monster.ind = i;

        if(my_monster.balance > 0)
            pending.push_back(my_monster);
        else
            act.push_back(my_monster);
    }

    sort(pending.begin(), pending.end(), cmp);
    sort(act.begin(), act.end(), cmp_act);
    //for(int i=0; i<n; i++)
    //    cout << monsters[i].balance << " " << monsters[i].cost << " " << monsters[i].ind << "\n";

    for(int i=0; i<pending.size(); i++)
    {
        if(pending[i].cost >= z)
        {
            cout << "NIE\n";
            return 0;
        }
        z += pending[i].balance;
        res.push_back(pending[i].ind);
    }

    for(int i=0; i<act.size(); i++)
    {
        if(act[i].cost >= z)
        {
            cout << "NIE\n";
            return 0;
        }
        z += act[i].balance;
        res.push_back(act[i].ind);
    }

    cout << "TAK\n";
    for(int i=0; i<n; i++)
        cout << res[i] + 1<< " ";
    cout << "\n";
}