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
import java.util.*;
import java.util.stream.Collectors;

public class sze {

    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int processCount = sc.nextInt();
        int processorsCount = sc.nextInt();
        int minTime= Integer.MAX_VALUE;
        int maxTime= Integer.MIN_VALUE;
        int [][] processesArray = new int[processCount][4];
        for(int i = 0; i < processCount; i++){
            int start = sc.nextInt();
            int end = sc.nextInt();
            int todo = sc.nextInt();
            processesArray[i][0]=start;
            processesArray[i][1]=end;
            processesArray[i][2]=todo;
            processesArray[i][3]=end-start-todo;
            if(start < minTime){
                minTime = start;
            }
            if(end > maxTime){
                maxTime = end;
            }
        }
        for(int i = 0; i < processCount; i++) {
            if (processesArray[i][3] < 0) {
                System.out.println("NIE");
                return;
            }
        }
        for(int i = minTime; i<maxTime; i++){
            int zeroMarginCounter = 0;
            for(int j = 0; j < processesArray.length; j++){
                if(processesArray[j][3] == 0 && processesArray[j][2] != 0){
                    zeroMarginCounter++;
                    if(zeroMarginCounter > processorsCount){
                        System.out.println("NIE");
                        return;
                    }
                }
            }
            Arrays.sort(processesArray, new Comparator<int[]>() {
                @Override
                public int compare(int[] o1, int[] o2) {
                    if(o1[2] == 0){
                        return Integer.MAX_VALUE;
                    } else if(o2[2] == 0){
                        return Integer.MIN_VALUE;
                    } else {
                        int c1 = Integer.compare(o1[0], o2[0]);
                        if (c1 == 0) {
                            return Integer.compare(o1[3], o2[3]);
                        } else {
                            return c1;
                        }
                    }
                }
            });
            for(int j = 0; j<processesArray.length; j++){
                if(processesArray[j][0]==i) {
                    if(j<processorsCount) {
                        processesArray[j][2] -= 1;
                    } else {
                        processesArray[j][3]-=1;
                    }
                    processesArray[j][0]+=1;
                }
            }
        }
        for(int j =0; j<processesArray.length; j++){
            if(processesArray[j][2]>0){
                System.out.println("NIE");
                return;
            }
        }
        System.out.println("TAK");
    }

}