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
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector < pair < int,int > > p;
vector < pair < int,int> > p2, p3, p4;
vector <int> result;
int n, z;
int main(){
    scanf("%d%d",&n,&z);
    for (int i = 0; i < n; i++){
        int d,a;
        scanf("%d%d",&d,&a);
        p.push_back( make_pair( d,a ));
    }
    for (int i = 0; i < n; i++){
        p2.push_back( make_pair( p[i].first - p[i].second, i ));
    }
    sort( p2.begin(), p2.end() );
    for (int i = 0; i < p2.size(); i++){
        if (p2[i].first <= 0){
            p3.push_back( make_pair( p[ p2[i].second ].first , p2[i].second ));
        }
        else {
            p4.push_back( make_pair( p[ p2[i].second ].second, p2[i].second));
        }
    }
    sort( p3.begin(), p3.end() );
    sort( p4.begin(), p4.end() );
    for (int i = 0; i < p3.size(); i++){
        if (z - p3[i].first > 0){
            z = z - p[ p3[i].second ].first + p[ p3[i].second ].second;
            result.push_back( p3[i].second );
        }
        else {
            printf("NIE\n");
            return 0;
        }
    }
    for (int i = p4.size()-1; i >= 0; i--){
        if (z - p[ p4[i].second ].first > 0){
            z = z - p[ p4[i].second ].first + p[ p4[i].second ].second;
            result.push_back( p4[i].second );
        }
        else {
            printf("NIE\n");
            return 0;
        }
    }
    printf("TAK\n");
    for (int i = 0; i < result.size(); i++){
        printf("%d ", result[i]+1);
    }
}