#include <bits/extc++.h>
#define endl '\n'
typedef long long ll;
#define int ll
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
void Main() {
int n, w, res = 0;
cin >> n >> w;
vector<int> dp(w + 1);
for (int i = 1; i <= n; ++i) {
int v, c, m;
cin >> v >> c >> m;// w v c
if (!c) {
res += v * m;
continue;
}
int k = w / c;
m = min(m, k);
for (int d = 0; d < c; ++d) {
deque<int> q1, q2;
k = (w - d) / c;
for (int j = 0; j <= k; ++j) {
while (!q2.empty() && dp[d + j * c] - j * v >= q2.back()) {
q2.pop_back();
q1.pop_back();
}
q1.push_back(j);
q2.push_back(dp[d + j * c] - j * v);
while (!q1.empty() && q1[0] < j - m) {
q1.pop_front();
q2.pop_front();
}
dp[d + j * c] = max(dp[d + j * c], q2[0] + j * v);
}
}
}
cout << res + dp[w] << endl;
}
// #define __CP_MULTI_TEST_CASES
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int t = 1;
#ifdef __CP_MULTI_TEST_CASES
cin >> t;
#endif
while (t--) {
Main();
}
return 0;
}