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
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstdlib>
using namespace std;
const int N = 1e5;
int d[N], a[N];
vector<int> pos, neg;
bool cmp(int x, int y) { return a[x] > a[y]; }
bool cmp2(int x, int y) { return d[x] < d[y]; }
void fail() { printf("NIE\n"); exit(0); }
int main() {
    int n;
    long long z;
    scanf("%d%lld", &n,&z);
    for (int i=0; i<n; i++) {
        scanf("%d%d", &d[i],&a[i]);
        if (d[i] <= a[i]) pos.push_back(i); else neg.push_back(i);
    }
    sort(pos.begin(),pos.end(),cmp2);
    sort(neg.begin(),neg.end(),cmp);
    for (int x: pos)
        if (z <= d[x]) fail(); else z += a[x]-d[x];
    for (int x: neg)
        if (z <= d[x]) fail(); else z += a[x]-d[x];
    printf("TAK\n");
    for (int x: pos) printf("%d ", x+1);
    for (int x: neg) printf("%d ", x+1);
    printf("\n");
    return 0;
}