#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
struct Monster{
int d, a, j;
Monster() {}
Monster(int x, int y, int z)
: d(x), a(y), j(z) {}
} t1[100000], t2[100000];
inline bool cmp1(const Monster& a, const Monster& b){
return (a.d<b.d);
}
inline bool cmp2(const Monster& a, const Monster& b){
return (a.a==b.a?a.d>b.d:a.a>b.a);
}
int Q[100000];
bool b[100002];
int main()
{
ll h;
int n, d, a, k=0, m=0, x=0;
scanf("%d%lld", &n, &h);
for(int i=1;i<=n;++i){
scanf("%d%d", &d, &a);
if(a>=d) t1[k++]=Monster(d, a, i);
else t2[m++]=Monster(d, a, i);
}
sort(t1, t1+k, cmp1);
sort(t2, t2+m, cmp2);
bool f=true;
for(int i=0;i<k && f;++i)
if((h-=t1[i].d)>0){
h+=t1[i].a;
Q[x++]=t1[i].j;
}else f=false;
for(int i=0;i<m && f;++i)
if(h>t2[i].d){
h+=(t2[i].a-t2[i].d);
Q[x++]=t2[i].j;
}else f=false;
if(f){
printf("TAK\n");
for(int i=0;i<x;++i)
printf("%d ", Q[i]);
}else printf("NIE");
}
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 | #include <cstdio> #include <algorithm> using namespace std; typedef long long ll; struct Monster{ int d, a, j; Monster() {} Monster(int x, int y, int z) : d(x), a(y), j(z) {} } t1[100000], t2[100000]; inline bool cmp1(const Monster& a, const Monster& b){ return (a.d<b.d); } inline bool cmp2(const Monster& a, const Monster& b){ return (a.a==b.a?a.d>b.d:a.a>b.a); } int Q[100000]; bool b[100002]; int main() { ll h; int n, d, a, k=0, m=0, x=0; scanf("%d%lld", &n, &h); for(int i=1;i<=n;++i){ scanf("%d%d", &d, &a); if(a>=d) t1[k++]=Monster(d, a, i); else t2[m++]=Monster(d, a, i); } sort(t1, t1+k, cmp1); sort(t2, t2+m, cmp2); bool f=true; for(int i=0;i<k && f;++i) if((h-=t1[i].d)>0){ h+=t1[i].a; Q[x++]=t1[i].j; }else f=false; for(int i=0;i<m && f;++i) if(h>t2[i].d){ h+=(t2[i].a-t2[i].d); Q[x++]=t2[i].j; }else f=false; if(f){ printf("TAK\n"); for(int i=0;i<x;++i) printf("%d ", Q[i]); }else printf("NIE"); } |
English