#include <algorithm>
#include <fstream>
#include <string>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <sstream>
#include <iostream>
#include <cmath>
using namespace std;
typedef unsigned int uint;
typedef long long int64;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pI;
typedef pair<string, int> pSI;
typedef pair<int, string> pIS;
#define FOR(i,n) for(int i=0, upTo##i=n; i<upTo##i; ++i)
#define REVFOR(i,n) for(int i=(n)-1; i>=0; --i)
#define FOR2(i,b,n) for(int i=b; i<(n); ++i)
#define REVFOR2(i,b,n) for(int i=(n)-1; i>=b; --i)
#define SC(i) scanf("%d", i)
#define ALL(C) (C).begin(), (C).end()
#define RALL(C) (C).rbegin(), (C).rend()
#define MIN(C) *min_element(ALL(C))
#define MAX(C) *max_element(ALL(C))
#define A first
#define B second
class X {
public:
int x, y, pos;
X(int _x, int _y, int _pos) : x(_x), y(_y), pos(_pos) { }
};
vector<X> p;
vector<X> m;
bool compP(const X &p1, const X &p2) {
return p1.x < p2.x;
}
bool compM(const X &p1, const X &p2) {
return p1.y > p2.y;
}
void start() {
int n, z;
cin >> n >> z;
int a, b;
FOR(i, n) {
cin >> a >> b;
if (a <= b) {
p.push_back(X(a, b, i + 1));
}
else {
m.push_back(X(a, b, i + 1));
}
}
sort(ALL(p), compP);
sort(ALL(m), compM);
bool res = true;
FOR(i, p.size()) {
z -= p[i].x;
if (z <= 0)
{
res = false;
break;
}
z += p[i].y;
}
if (res){
FOR(i, m.size()) {
z -= m[i].x;
if (z <= 0)
{
res = false;
break;
}
z += m[i].y;
}
}
cout << (res ? "TAK" : "NIE") << endl;
if (res) {
FOR(i, p.size()) {
if (i != 0) cout << ' ';
cout << p[i].pos;
}
if (m.size() != 0 && p.size() != 0)
cout << ' ';
FOR(i, m.size()) {
if (i != 0) cout << ' ';
cout << m[i].pos;
}
}
}
int main(void) {
start();
return 0;
}
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | #include <algorithm> #include <fstream> #include <string> #include <queue> #include <set> #include <stack> #include <map> #include <sstream> #include <iostream> #include <cmath> using namespace std; typedef unsigned int uint; typedef long long int64; typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pI; typedef pair<string, int> pSI; typedef pair<int, string> pIS; #define FOR(i,n) for(int i=0, upTo##i=n; i<upTo##i; ++i) #define REVFOR(i,n) for(int i=(n)-1; i>=0; --i) #define FOR2(i,b,n) for(int i=b; i<(n); ++i) #define REVFOR2(i,b,n) for(int i=(n)-1; i>=b; --i) #define SC(i) scanf("%d", i) #define ALL(C) (C).begin(), (C).end() #define RALL(C) (C).rbegin(), (C).rend() #define MIN(C) *min_element(ALL(C)) #define MAX(C) *max_element(ALL(C)) #define A first #define B second class X { public: int x, y, pos; X(int _x, int _y, int _pos) : x(_x), y(_y), pos(_pos) { } }; vector<X> p; vector<X> m; bool compP(const X &p1, const X &p2) { return p1.x < p2.x; } bool compM(const X &p1, const X &p2) { return p1.y > p2.y; } void start() { int n, z; cin >> n >> z; int a, b; FOR(i, n) { cin >> a >> b; if (a <= b) { p.push_back(X(a, b, i + 1)); } else { m.push_back(X(a, b, i + 1)); } } sort(ALL(p), compP); sort(ALL(m), compM); bool res = true; FOR(i, p.size()) { z -= p[i].x; if (z <= 0) { res = false; break; } z += p[i].y; } if (res){ FOR(i, m.size()) { z -= m[i].x; if (z <= 0) { res = false; break; } z += m[i].y; } } cout << (res ? "TAK" : "NIE") << endl; if (res) { FOR(i, p.size()) { if (i != 0) cout << ' '; cout << p[i].pos; } if (m.size() != 0 && p.size() != 0) cout << ' '; FOR(i, m.size()) { if (i != 0) cout << ' '; cout << m[i].pos; } } } int main(void) { start(); return 0; } |
English