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

int main()
{
    int tests;
    std::cin >> tests;
    for (int t = 0; t < tests; t++) {
        int kids;
        std::cin >> kids;
        int vols[kids];
        int currs[kids];
        int targets[kids];
        unsigned long long totalVol = 0;
        unsigned long long totalCurr = 0;
        unsigned long long totalTarget = 0;
        int maxCurr = 0, minCurr = 1000001, maxTarget = 0, minTarget = 1000001;
        for (int k = 0; k < kids; k++) {
            int vol, curr, target;
            std::cin >> vol;
            totalVol += vol;
            vols[k] = vol;
            std::cin >> curr;
            totalCurr = totalCurr + (vol * curr);
            currs[k] = curr;
            if (curr < minCurr) minCurr = curr;
            if (curr > maxCurr) maxCurr = curr;
            std::cin >> target;
            totalTarget = totalTarget + (vol * target);
            targets[k] = target;
            if (target < minTarget) minTarget = target; 
            if (target > maxTarget) maxTarget = target;
        }
        long long mean = totalCurr / totalVol;
        unsigned long long currUpDev = 0;
        unsigned long long currDownDev = 0;
        unsigned long long targetUpDev = 0;
        unsigned long long targetDownDev = 0;
        for (int k = 0; k < kids; k++) {
            if (currs[k] - mean >= 0) {
                currUpDev = currUpDev + vols[k] * (currs[k] - mean);
            } else {
                currDownDev = currDownDev + vols[k] * (mean - currs[k]);
            }
            if (targets[k] - mean >= 0) {
                targetUpDev = targetUpDev + vols[k] * (targets[k] - mean);
            } else {
                targetDownDev = targetDownDev + vols[k] * (mean - targets[k]);
            }
        }
        if (totalCurr == totalTarget && minCurr <= minTarget && maxCurr >= maxTarget && currDownDev >= targetDownDev && currUpDev >= targetUpDev) {
            std::cout << "TAK" << std::endl;
        } else {
            std::cout << "NIE" << std::endl;
        }
    }
    return 0;
}