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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <stdlib.h>

typedef unsigned long long ull;

typedef struct
{
    int attack;
    int heal;
    int order;
} ENEMY;

typedef struct
{
    int i;
    ull z;
} STACK;

#define MAX 100000

ENEMY enemy[MAX];
char used[MAX];
STACK stack[MAX];

int calc(int n, ull z)
{
    int lvl = 0;
    int i = 0;
#ifdef DEBUG
    int j;
#endif
    
    while(lvl < n)
    {
        if(i < n)
        {
            if(used[i] == '.')
            {
                if(z > enemy[i].attack)
                {
                    stack[lvl].i = i;
                    stack[lvl].z = z;
                    z += enemy[i].heal - enemy[i].attack;
                    used[i] = 'O';
                    lvl++;
                    i = 0;
#ifdef DEBUG
                    printf("[%d] [%s] i=%d, z=%llu\n", lvl, used, i, z);
                    for(j = 0; j < n; j++)
                    {
                        if(used[j] == 'O')
                        {
                            used[j] = 'x';
                        }
                    }
#endif
                }
                else
                {
                    i = n;
                }
            }
            else
            {
                /* next enemy */
                i++;
            }
        }
        else
        {
            /* all enemies checked */
            if(lvl == 0) break;
            lvl--;
            i = stack[lvl].i;
            z = stack[lvl].z;
            used[i] = '.';
            i++;
        }
    }

    return lvl == n;
}

int comp(const void *a, const void *b)
{
    return ((ENEMY*)a)->attack - ((ENEMY*)b)->attack;
}

int main(int argc, char **argv)
{
    int i;
    int n;
    ull z;
    FILE *f;

#ifdef LOCAL
    f = fopen(argv[1], "r");
#else
    f = stdin;
#endif

    fscanf(f, "%d %llu", &n, &z);
    
    for(i = 0; i < n; i++)
    {
        fscanf(f, "%d %d", &enemy[i].attack, &enemy[i].heal);
        enemy[i].order = i+1;
        used[i] = '.';
    }
    used[i] = 0;

#ifdef LOCAL
    fclose(f);
#endif

    qsort(enemy, n, sizeof(ENEMY), comp);
    
    if(calc(n, z) == 0)
    {
        printf("NIE\n");
    }
    else
    {
        printf("TAK\n");
        for(i = 0; i < n; i++)
        {
            printf("%d ", enemy[stack[i].i].order);
        }
        printf("\n");
    }
    
    return 0;
}