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
110
111
112
113
114
#include<stdio.h>
void sr(int t[],int p[],int q[],int n)
{
 int i,j,k,m,x,y,z;
 for(i=2;i<=n;i++)
 {
  j=i;
  k=j/2;
  x=t[i];
  y=p[i];
  z=q[i];
  while((t[k]<x)&&(k>0))
  {
   t[j]=t[k];
   p[j]=p[k];
   q[j]=q[k];
   j=k;
   k=j/2;
  }
  t[j]=x;
  p[j]=y;
  q[j]=z;
 }
 for(i=n;i>1;i--)
 {
  x=t[1];
  y=p[1];
  z=q[1];
  t[1]=t[i];
  p[1]=p[i];
  q[1]=q[i];
  t[i]=x;
  p[i]=y;
  q[i]=z;
  j=1;
  k=2;
  while(k<i)
  {
   if((t[k+1]>t[k])&&(k+1<i))
    m=k+1;
   else
    m=k;
   if(t[m]<=t[j])
    break;
   x=t[j];
   y=p[j];
   z=q[j];
   t[j]=t[m];
   p[j]=p[m];
   q[j]=q[m];
   t[m]=x;
   p[m]=y;
   q[m]=z;
   j=m;
   k=j+j;
  }
 }
 return;
}

main()
{
 int n,z,x,y,d=0,u=0,i;
 long long int zz;
 scanf("%d %d",&n,&z);
 zz=z;
 int pd[n],pu[n],zd[n],zu[n],ku[n],kd[n];
 for(i=0;i<n;i++)
 {
  scanf("%d %d",&x,&y);
  if(x>y)
  {
   pu[u]=x;
   zu[u]=y;
   ku[u]=i;
   u++;
  }
  else
  {
   pd[d]=x;
   zd[d]=y;
   kd[d]=i;
   d++;
  }
 }
  sr(pd-1,zd-1,kd-1,d);
  sr(zu-1,pu-1,ku-1,u);
  for(i=0;i<d;i++)
  {
   zz-=pd[i];
   if(zz<=0)
   {
    printf("NIE");
    return 0;
   }
   zz+=zd[i];
  }
  for(i=u-1;i>=0;i--)
  {
   zz-=pu[i];
   if(zz<=0)
   {
    printf("NIE");
    return 0;
   }
   zz+=zu[i];
  }
  printf("TAK\n");
  for(i=0;i<d;i++)
   printf("%d ",kd[i]+1);
  for(i=u-1;i>=0;i--)
   printf("%d ",ku[i]+1);
 return 0;
}