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
#include <iostream>
#include <vector>
#include <map>

using namespace std;

typedef struct {
    int n; // numer procesu
    int c;
    int p;
    int k;
    int s; // wolne "sloty"
} Process;

Process proc[101];
map<int, vector<int> > times;

int main(void) {
    ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; i++) {
        cin >> proc[i].p;
        cin >> proc[i].k;
        cin >> proc[i].c;

        proc[i].n = i + 1;
        proc[i].s = proc[i].k - proc[i].p;


        for (int j = proc[i].p; j < proc[i].k; j++)
            times[j].push_back(proc[i].n);
    }

    map<int, vector<int> >::iterator it;
    for (it = times.begin(); it != times.end(); it++) {
        vector<int> vec = it->second;
        int actualSize = 0;
        
        for (int i = 0; i < vec.size(); i++) {
            int index = vec[i] - 1;
            if (proc[index].c != 0)
                actualSize++;
        }
        
        if (actualSize <= m) {
            for (int i = 0; i < vec.size(); i++) {
                int index = vec[i] - 1;
                proc[index].s--;
                proc[index].c--;
            }
            
            times.erase(it->first);
        }
    }

    for (it = times.begin(); it != times.end(); it++) {
        vector<int> vec = it->second;
        for (int i = 0; i < m; i++) {
            int choosen = -1;
            int vecIndex = -1;

            for (int j = 0; j < vec.size(); j++) {
                int index = vec[j] - 1;
                Process p = proc[index];
                if (p.c == p.s && p.c > 0) {
                    choosen = index;
                    vecIndex = j;
                    break;
                }
            }

            if (choosen == -1) {
                double min = 99999;
                for (int j = 0; j < vec.size(); j++) {
                    int index = vec[j] - 1;
                    Process p = proc[index];
                    if (p.c <= 0)
                        continue;

                    double w = p.s / ((double) p.c);

                    if (w < min) {
                        min = w;
                        choosen = index;
                        vecIndex = j;
                    }
                }
            }

            if (choosen != -1) {
                vec.erase(vec.begin() + vecIndex);
                proc[choosen].s--;
                proc[choosen].c--;
            }
        }
    }


    bool result = true;
    for (int i = 0; i < n; i++) {
        if (proc[i].c > 0) {
            result = false;
            break;
        }
    }

    cout << (result ? "TAK" : "NIE") << endl;

    return 0;
}