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
#include <cstdio>
#include <utility>
#include <vector>

using namespace std;

vector<vector<pair<int, int> > > positives; // number, profit
vector<vector<pair<int, int> > > negatives; // number, profit
int order[100000];

int main() {
    positives.resize(100001);
    negatives.resize(100001);
    int n, live, a, b, c=0;
    scanf("%d%d", &n, &live);
    for(int i=1; i<=n; i++) {
        scanf("%d%d", &a, &b);
        if (b - a >= 0) {
            positives[a].push_back(make_pair(i, b));
        } else {
            negatives[b].push_back(make_pair(i, a));
        }
    }
    for(int i=0; i<=100000 && live > 0; i++) {
        for(int j=0; j<positives[i].size(); j++) {
            live -= i;
            if(live <= 0) break;
            live += positives[i][j].second;
            order[c++] = positives[i][j].first;
        }
    }
    for(int i=100000; i>=0 && live > 0; i--) {
        for(int j=0; j<negatives[i].size(); j++) {
            live -= negatives[i][j].second;
            if(live <= 0) break;
            live += i;
            order[c++] = negatives[i][j].first;
        }
    }
    if (live > 0) {
        printf("TAK\n");
        for (int i=0; i<c; i++)
            printf("%d ", order[i]);
    } else {
        printf("NIE\n");
    }
    return 0;
}