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
//Dawid Dworak, AGH, zad BOH Potyczki Algorytmiczne
#include <iostream>
#include <map>
#include <vector>
#define lu long unsigned int

using namespace std;

int main(){
    multimap<lu,pair<lu,lu> >plusy;
    multimap<lu,pair<lu,lu> >::iterator it;
    multimap<lu,pair<lu,lu>,std::greater<lu> >minusy;
    vector<lu>order;
    vector<lu>::iterator it2;
    pair<lu,lu>p;
    lu n,z,p1,p2;
    ios_base::sync_with_stdio(0);
    cin >> n >> z;
    for(lu i=1; i<=n; i++){
        cin >> p1 >> p2;
        if(p2>=p1){
                p=make_pair(i,p2);
                plusy.insert(pair<lu,pair<lu,lu> >(p1,p));
        }
        else {
                p=make_pair(i,p1);
                minusy.insert(pair<lu,pair<lu,lu> >(p2,p));
        }
    } //posortowane
    for(it=plusy.begin();it!=plusy.end();++it){ //odejmowane - it->first, index it->second.first, dodawane it->second.second
        if(it->first >= z){
            cout << "NIE";
            return 0;
        }
        z=z-it->first+it->second.second;
        order.push_back(it->second.first);
    }
    for(it=minusy.begin();it!=minusy.end();++it){ //odejmowane - it->second.second, index it->second.first, dodawane it->first
        if(it->second.second >= z){
            cout << "NIE";
            return 0;
        }
        z=z+it->first-it->second.second;
        order.push_back(it->second.first);
    }
    cout << "TAK\n";
    for(it2=order.begin(); it2!=order.end();it2++){
        cout << *it2 << " ";
    }
    return 0;
}