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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

typedef pair<int, int> pii;

int main()
{
    ios_base::sync_with_stdio(0);
    int n, z;
    cin>>n>>z;
    vector<pair<int, pii> > plusy;
    vector<pair<int, pii> > minusy;
    int s = 0;
    for(int i=0; i<n; i++){
        int a, b;
        cin>>a>>b;
        if(a-b>=0) {
            minusy.push_back(make_pair(b, make_pair(a-b, i)));
            s+=a-b;
        }
        else plusy.push_back(make_pair(a, make_pair(b-a, i)));
    }
    sort(plusy.begin(), plusy.end());
    sort(minusy.begin(), minusy.end());
    bool ans = true;
    for(int i=0; i<plusy.size() && ans; i++){
        pair<int, pii> para = plusy[i];
        if(para.first >= z) ans = false;
        else z += para.second.first;
       // cout<<z<<endl;
    }
    z-=s;
    for(int i=0; i<minusy.size() && ans; i++){
    //    cout<<z<<endl;
        if(z<0) ans = false;
        pair<int, pii> para = minusy[i];
        if(para.first >= z) ans = false;
        else z += para.second.first;
     //   cout<<z<<endl;
    }
    if(!ans) cout<<"NIE"<<endl;
    else{
        cout<<"TAK"<<endl;
        for(int i=0; i<plusy.size(); i++) cout<<plusy[i].second.second+1<<" ";
        for(int i=0; i<minusy.size(); i++) cout<<minusy[minusy.size()-1-i].second.second+1<<" ";
        cout<<endl;
    }
    return 0;
}