#include <algorithm> #include <cstdio> #include <vector> using namespace std; typedef vector<pair<pair<int, int>, int> > Vector; bool Build(long long z, const Vector &moves, vector<int> *ret) { for (int i = 0; i < moves.size(); ++i) { z -= moves[i].first.first; if (z <= 0) return false; z += moves[i].first.second; ret->push_back(moves[i].second); } return true; } int main() { int n; long long z; scanf("%d%lld", &n, &z); Vector adi, dai; long long final = z; for (int i = 1; i <= n; ++i) { int a, d; scanf("%d%d", &d, &a); if (d > a) adi.push_back(make_pair(make_pair(a, d), i)); else dai.push_back(make_pair(make_pair(d, a), i)); final -= d; final += a; } sort(adi.begin(), adi.end()); sort(dai.begin(), dai.end()); vector<int> ad, da; if (Build(z, dai, &da) && Build(final, adi, &ad)) { printf("TAK"); bool first = true; for (int i = 0; i < da.size(); ++i) { if (first) { printf("\n"); first = false; } else { printf(" "); } printf("%d", da[i]); } for (int i = ad.size() - 1; i >= 0; --i) { if (first) { printf("\n"); first = false; } else { printf(" "); } printf("%d", ad[i]); } printf("\n"); } else { printf("NIE\n"); } 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 | #include <algorithm> #include <cstdio> #include <vector> using namespace std; typedef vector<pair<pair<int, int>, int> > Vector; bool Build(long long z, const Vector &moves, vector<int> *ret) { for (int i = 0; i < moves.size(); ++i) { z -= moves[i].first.first; if (z <= 0) return false; z += moves[i].first.second; ret->push_back(moves[i].second); } return true; } int main() { int n; long long z; scanf("%d%lld", &n, &z); Vector adi, dai; long long final = z; for (int i = 1; i <= n; ++i) { int a, d; scanf("%d%d", &d, &a); if (d > a) adi.push_back(make_pair(make_pair(a, d), i)); else dai.push_back(make_pair(make_pair(d, a), i)); final -= d; final += a; } sort(adi.begin(), adi.end()); sort(dai.begin(), dai.end()); vector<int> ad, da; if (Build(z, dai, &da) && Build(final, adi, &ad)) { printf("TAK"); bool first = true; for (int i = 0; i < da.size(); ++i) { if (first) { printf("\n"); first = false; } else { printf(" "); } printf("%d", da[i]); } for (int i = ad.size() - 1; i >= 0; --i) { if (first) { printf("\n"); first = false; } else { printf(" "); } printf("%d", ad[i]); } printf("\n"); } else { printf("NIE\n"); } return 0; } |