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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <time.h>

#ifdef TS_TEST
#define TS_TIMER_START(t) do { (t) = clock(); } while (0)
#define TS_TIMER_SHOW(t, title) do {\
        int msec = (clock() - (t)) * 1000 / CLOCKS_PER_SEC;\
        fprintf(stderr, "%s: %d.%03d\n", (title), msec/1000, msec%1000);\
    } while (0)
#else
#define TS_TIMER_START(t)
#define TS_TIMER_SHOW(t, title)
#endif

#define MIN(a, b) ((a) <= (b) ? (a) : (b))
#define MAX(a, b) ((a) >= (b) ? (a) : (b)) 

#define N_MIN 1
#define N_MAX 100000
#define Z_MIN 1
#define Z_MAX 100000
#define D_MIN 0
#define D_MAX 100000
#define A_MIN 0
#define A_MAX 100000

typedef long long i64;

int n;
int z;
int d[N_MAX + 1] = { 0, };
int a[N_MAX + 1] = { 0, };

int v[N_MAX + 1] = { 0, };  /* Visited */
int r;  /* Krok 1..n */
int s[N_MAX + 1] = { 0, };  /* s[i], i=1..r - Numer potwora dla kroku */
/*
    s_0 = 0
    for i, j in {1..r+1}, i != j
        s_i in {1..n}
        s_i <> s_j
*/
i64 h[N_MAX + 1] = { 0, };  /* h[i], i=1..r-1 - Zdrowie dla kroku */
/*
    h_0 = z
    h_r = h_{r-1} - d_{s_r} + a{s_r}
*/

/*
h_0 := z;
r := 1
s_1 := 1

loop:
    if s_r > n and r = 1:
        return NIE
    if s_r > n:
        r--
        v_{s_r} = 0
        s_r++;
        continue
    if v{s_r} or d{s_r} >= h{r-1}:
        s_r++
        continue
    if r = n:
        return TAK
    v_{s_r} = 1
    h_r := h_{r-1} - d{s_r} + a{s_r}
    r++
    s_r := 1
*/

void
load_data()
{
    int i;
    scanf("%d %d", &n, &z);
    assert(N_MIN <= n && n <= N_MAX);
    assert(Z_MIN <= z && z <= Z_MAX);
    for (i = 1;  i <= n;  i++) {
        scanf("%d %d", &d[i], &a[i]);
        assert(D_MIN <= d[i] && d[i] <= D_MAX);
        assert(A_MIN <= a[i] && a[i] <= A_MAX);
    }
}

int
compute()
{
    h[0] = z;
    r = 1;
    s[1] = 1;
    for (;;) {
        if (s[r] > n && r == 1) {
            return 0;
        }
        if (s[r] > n) {
            r--;
            v[s[r]] = 0;
            s[r]++;
            continue;
        }
        if (v[s[r]] || d[s[r]] >= h[r - 1]) {
            s[r]++;
            continue;
        }
        if (r == n) {
            return 1;
        }
        v[s[r]] = 1;
        h[r] = h[r - 1] - d[s[r]] + a[s[r]];
        r++;
        s[r] = 1;
    }
    return 0;
}

void
show_answer(int answer)
{
    int i;
    if (answer) {
        puts("TAK");
        for (i = 1;  i <= n;  i++) {
            printf(i == 1 ? "%d" : " %d", s[i]);
        }
        puts("");
    } else {
        puts("NIE");
    }
}

int
main()
{
    clock_t cl_total;
    clock_t cl_compute;
    TS_TIMER_START(cl_total);
    load_data();
    TS_TIMER_SHOW(cl_total, "Load time");
    TS_TIMER_START(cl_compute);
    int answer = compute();
    TS_TIMER_SHOW(cl_compute, "Compute time");
    show_answer(answer);
    TS_TIMER_SHOW(cl_total, "Total time");
    return 0;
}