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
#include<bits/stdc++.h>
using namespace std;
#define show if(0)
unsigned long long t,n,l,a,b,Sa,Sb,SA,SB,p,k,SI;   //p - poczatek, k - koniec
long long x;
bool err;
vector<unsigned long long> pot[4],ilosc[4],obec,oczek;
int main(){
  cin>>t;
  while(t--){
    obec.clear();
    oczek.clear();
    err=0;
    Sa=0;
    Sb=0;
    SI=0;
    show
      cout<<t<<endl;
    cin>>n;
    for(int i=0;i<n;i++){
      cin>>l>>a>>b;
      for(int k=0;k<l;k++){
        obec.push_back(a);
        oczek.push_back(b);
      }
      Sa+=(a*l);
      Sb+=(b*l);
      SI+=l;
    }
    sort(obec.begin(),obec.end());
    sort(oczek.begin(),oczek.end());
    show{
      for(int i=0;i<SI;i++){
        cout<<obec[i]<<" ";
      }
      cout<<endl;
      for(int i=0;i<SI;i++){
        cout<<oczek[i]<<" ";
      }
      cout<<endl;
    }
    if(obec[0]>oczek[0]||obec[SI-1]<oczek[SI-1]||Sa!=Sb)
      err=1;
    else{
      x=obec[SI-1]-oczek[SI-1];
      obec[SI-1]-=x;
      show
        cout<<x<<endl;
      for(int i=SI-2;i>=0;i--){
        if(obec[i]>oczek[i])
          x+=obec[i]-oczek[i];
        if(obec[i]<oczek[i]){
          x-=(oczek[i]-obec[i]);
        }
        show
          cout<<x<<endl;
        if(x<0){
          err=1;
          break;
        }
      }
    }
    if(err)
      cout<<"NIE\n";
    else
      cout<<"TAK\n";
  }
  return 0;
}