#include #include #include #include #include #include #include #include #include #include #include #include #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REP(I, N) for (int I = 0; I < (N); ++I) #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define RI(X) scanf("%d", &(X)) #define RII(X, Y) scanf("%d%d", &(X), &(Y)) #define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z)) #define DRI(X) int (X); scanf("%d", &X) #define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y) #define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z) #define RS(X) scanf("%s", (X)) #define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0) #define MP make_pair #define PB push_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define PII pair #define VI vector #define VPII vector > #define PLL pair #define VPLL vector > #define F first #define S second typedef long long LL; using namespace std; const int MOD = 1e9+7; const int SIZE = 1e6+10; struct data{ int A,B,C; bool operator<(const data& b)const{ return C*B+b.B*(C+b.C) void maa(T& x,T y){ if(x void mii(T& x,T y){ if(x>y)x=y; } int main(){ CASET{ DRII(n,t); REP(i,n)RIII(a[i].A,a[i].B,a[i].C); sort(a,a+n); MS1(dp); dp[0]=0; REP(i,n){ for(int j=t-a[i].C;j>=0;j--){ if(dp[j]==-1)continue; maa(dp[j+a[i].C],dp[j]+a[i].A-a[i].B*(a[i].C+j)); } } int an=0; REP(i,t+1){ maa(an,dp[i]); } printf("%d\n",an); } return 0; }