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
#include <iostream>
using namespace std;
int main() {
    int i, n, z, d, a, t2_it = 0;
    cin >> n >> z;
    int tab[n][2];
    int tab2[n];
    for(i = 0; i < n; i++) {
	cin >> d >> a;
	tab[i][0] = d;
	tab[i][1] = a;
    }
    while(true) {
	int max_dmg = 0, index, min_hp = 100001;
	bool fragged = false;
	for(i = 0; i < n; i++) {
	    bool alive = true;
	    for(int j = 0; j < t2_it; j++) {
		alive = alive && (tab2[j] != i);
	    }
	    if(!alive) continue;
	    if(z > tab[i][0]) {
		if(tab[i][0] > max_dmg) {
		    max_dmg = tab[i][0];
		    index = i;
		    fragged = true;
		} else if((tab[i][0] == max_dmg) && 
		(tab[i][1] < min_hp)) {
		    max_dmg = tab[i][0];
		    index = i;
		    fragged = true;
		    min_hp = tab[i][1];
		}
	    }
	}
	if(!fragged) break;
	tab2[t2_it] = index;
	t2_it++;
	z += tab[index][1] - tab[index][0];
	if(t2_it == n) break;
    }
    if(t2_it == n) {
	cout << "TAK" << endl;
	for(i = 0; i < n; i++) {
	    cout << tab2[i]+1 << " ";
	}
    } else {
	cout << "NIE" << endl;
    }
}