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
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;

void sortVector(vector<pair<int, int>> &vectorToSort)
{
    sort(vectorToSort.begin(), vectorToSort.end(),
         [](const pair<int, int> & a, const pair<int, int> & b) -> bool
         {
             return a.second > b.second;
         });
}

void oneTest()
{
    int numberOfBajtoniatek, capacity, inputTemp, outputTemp;
    long long int currentTempInput = 0, currentTempOutput = 0;
    long long int currentCapacityInput = 0, currentCapacityOutput = 0;
    vector<pair<int, int>> inputData;
    vector<pair<int, int>> outputData;
    cin>> numberOfBajtoniatek;

    for(int i = 0; i < numberOfBajtoniatek; i++)
    {
        cin>>capacity>>inputTemp>>outputTemp;
        inputData.emplace_back(capacity, inputTemp);
        outputData.emplace_back(capacity, outputTemp);
    }
    sortVector(inputData);
    sortVector(outputData);

    int indexInInput = 0;
    for(int i = 0; i < outputData.size(); i++)
    {
        while(currentCapacityInput + inputData[indexInInput].first < currentCapacityOutput)
        {
            currentTempInput = (currentTempInput + inputData[indexInInput].first*inputData[indexInInput].second);
            currentCapacityInput += inputData[indexInInput].first;
            indexInInput++;
        }
        if(currentCapacityInput < currentCapacityOutput)
        {
            long long int howMuchToSubtract = currentCapacityOutput - currentCapacityInput;
            inputData[indexInInput].first -= howMuchToSubtract;
            currentTempInput = (currentTempInput + howMuchToSubtract*inputData[indexInInput].second);
            currentCapacityInput += howMuchToSubtract;
        }
//        cout<<"current temp input "<< currentTempInput << " currentTempOutput"<<currentTempOutput << " currentCapacityOutput: "<< currentCapacityOutput << " "<< currentCapacityInput<<endl;
        if(currentTempInput < currentTempOutput)
        {
            cout<<"NIE"<<endl; // <<currentTempInput - currentTempOutput<<endl;
            return;
        }
        currentTempOutput = currentTempOutput + outputData[i].first*outputData[i].second;
        currentCapacityOutput = currentCapacityOutput + outputData[i].first;
    }
    while(currentCapacityInput < currentCapacityOutput)
    {
        currentTempInput = currentTempInput + inputData[indexInInput].first*inputData[indexInInput].second;
        currentCapacityInput += inputData[indexInInput].first;
        indexInInput++;
    }
    if(currentTempInput == currentTempOutput)
    {
        cout<<"TAK"<<endl;
        return;
    }
    cout<<"NIE"<<endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    int numberOfTests;

    cin>>numberOfTests;

    for(int i = 0; i < numberOfTests; i++)
    {
        oneTest();
    }
    return 0;
}