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 #include #include #include #define S 1000007 using namespace std; typedef long long ll; vector < pair < ll , ll > > V; vector < pair < ll , ll > > V2; ll dp[S]; ll dp2[S]; int main(void){ int t,n,m,c,d; ll l,a,b,x; bool ta; scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i = 1; i <= n;i++){ scanf("%lld %lld %lld",&l,&a,&b); V.push_back({a,l}); V2.push_back({b,l}); } sort(V.begin(),V.end()); sort(V2.begin(),V2.end()); ll x1 = 0,x2 = 0; x = 0; int l1 = n-1, l2 = n-1; bool ta = 0; while(l1 > -1 || l2 > -1){ if(V[l1].second < V2[l2].second){ x += V[l1].second * (-V2[l2].first + V[l1].first); V2[l2].second -= V[l1].second; l1--; }else if(V[l1].second > V2[l2].second){ x += V2[l2].second * (V[l1].first - V2[l2].first); V[l1].second -= V2[l2].second; l2--; }else{ x += V2[l2].second * (V[l1].first - V2[l2].first); l1--; l2--; } if(x < 0){ printf("NIE\n"); ta = 1; break; } } if(!ta && x != 0){ printf("NIE\n"); }else if(!ta){ printf("TAK\n"); } V.clear(); V2.clear(); } return 0; }