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
#include <cstdio>
#include <bits/stdc++.h>

#define MAX_TEST_SIZE 100000
#define ULL unsigned long long

using namespace std;

struct bajton
{
    int vol;
    int temp;
    int tempTarget;
};

struct cup
{
    int vol;
    int temp;
};



bajton test[MAX_TEST_SIZE];
cup startCups[MAX_TEST_SIZE];
cup targetCups[MAX_TEST_SIZE];

bool compareCupTemp(cup c1, cup c2)
{
    return c1.temp < c2.temp;
}

bool checkTest(int size, bajton test[])
{
    ULL energyStart = 0, energyTarget = 0;

    for(int i=0; i<size; i++)
    {
        energyStart += test[i].vol * test[i].temp;
        energyTarget += test[i].vol * test[i].tempTarget;

        startCups[i].vol = targetCups[i].vol = test[i].vol;
        startCups[i].temp = test[i].temp;
        targetCups[i].temp = test[i].tempTarget;
    }

    if(energyStart!=energyTarget) return false;

    sort(startCups, startCups+size, compareCupTemp);
    sort(targetCups, targetCups+size, compareCupTemp);

    int iStart = 0, iTarget = 0;
    energyStart = energyTarget = 0;

    while(iTarget<size)
    {
        //get i-th target cup
        int vol = targetCups[iTarget].vol;
        energyTarget += targetCups[iTarget].vol * targetCups[iTarget].temp;
        iTarget++;

        //get the same volume from start cups and count energy
        while(vol>0)
        {
            if(iStart>=size) return false;
            if(startCups[iStart].vol<=vol)
            {
                vol -= startCups[iStart].vol;
                energyStart += startCups[iStart].vol * startCups[iStart].temp;
                iStart++;
            }
            else
            {
                startCups[iStart].vol -= vol;
                energyStart += vol * startCups[iStart].temp;
                vol = 0;
            }
        }

        //compare energies
        if(energyStart>energyTarget) return false;
    }

    return true;
}


int main()
{
    int t;

    scanf("%d", &t);
    for(int i=0; i<t; i++)
    {
        int n;
        scanf("%d", &n);
        for(int j=0; j<n; j++)
        {
            scanf("%d %d %d", &test[j].vol, &test[j].temp, &test[j].tempTarget);
        }

        printf("%s\n", checkTest(n, test)? "TAK" : "NIE");
    }

    return 0;
}