#include <cstdio> #include <climits> #include <vector> #include <queue> #include <algorithm> using namespace std; typedef long long ll; int read() { int n; scanf("%d", &n); return n; } vector<int> a, b, v; inline bool positive(int i) { return a[i] <= b[i]; } inline bool negative(int i) { return a[i] > b[i]; } bool cmp(int i, int j) { if (positive(i) && negative(j)) return true; if (positive(j) && negative(i)) return false; if (positive(i)) { if (a[i] < a[j]) return true; if (a[i] > a[j]) return false; return i < j; } if (b[i] < b[j]) return false; if (b[i] > b[j]) return true; return i < j; } bool check(int n, ll z) { for (int i=0; i<n; ++i) { z -= a[v[i]]; if (z <= 0) return false; z += b[v[i]]; } return true; } int main() { int n = read(); ll z = read(); a.resize(n); b.resize(n); v.resize(n); for (int i=0; i<n; ++i) { a[i] = read(); b[i] = read(); v[i] = i; } sort(v.begin(), v.end(), cmp); if (check(n, z)) { printf("TAK\n"); for (int i=0; i<n; ++i) printf("%d%s", v[i] + 1, i+1 == n ? "\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 | #include <cstdio> #include <climits> #include <vector> #include <queue> #include <algorithm> using namespace std; typedef long long ll; int read() { int n; scanf("%d", &n); return n; } vector<int> a, b, v; inline bool positive(int i) { return a[i] <= b[i]; } inline bool negative(int i) { return a[i] > b[i]; } bool cmp(int i, int j) { if (positive(i) && negative(j)) return true; if (positive(j) && negative(i)) return false; if (positive(i)) { if (a[i] < a[j]) return true; if (a[i] > a[j]) return false; return i < j; } if (b[i] < b[j]) return false; if (b[i] > b[j]) return true; return i < j; } bool check(int n, ll z) { for (int i=0; i<n; ++i) { z -= a[v[i]]; if (z <= 0) return false; z += b[v[i]]; } return true; } int main() { int n = read(); ll z = read(); a.resize(n); b.resize(n); v.resize(n); for (int i=0; i<n; ++i) { a[i] = read(); b[i] = read(); v[i] = i; } sort(v.begin(), v.end(), cmp); if (check(n, z)) { printf("TAK\n"); for (int i=0; i<n; ++i) printf("%d%s", v[i] + 1, i+1 == n ? "\n" : " "); } else { printf("NIE\n"); } return 0; } |