using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, w, d) = (c[0], c[1], c[2]); var map = NArr(n); WriteLine(Choice(n, w, d, map)); } static long Choice(int n, int w, int d, int[][] map) { var dp0 = Enumerable.Repeat(long.MinValue / 2, w + 1).ToArray(); dp0[0] = 0; var dp1 = (long[]) dp0.Clone(); for (var i = 0; i < n; ++i) { if (map[i][0] == 0) { var ndp = (long[]) dp0.Clone(); for (var j = w; j >= 0; --j) if (j + map[i][1] <= w) ndp[j + map[i][1]] = Math.Max(ndp[j + map[i][1]], dp0[j] + map[i][2]); dp0 = ndp; } else { var ndp = (long[]) dp1.Clone(); for (var j = w; j >= 0; --j) if (j + map[i][1] <= w) ndp[j + map[i][1]] = Math.Max(ndp[j + map[i][1]], dp1[j] + map[i][2]); dp1 = ndp; } } var seg = new SegmentTree(w + 1, new SegOp()); for (var i = 0; i <= w; ++i) seg[i] = dp1[i]; var ans = 0L; for (var i = 0; i < dp0.Length; ++i) { var min = Math.Max(0, i - d); var max = Math.Min(w - i, i + d); if (min <= max) { ans = Math.Max(ans, dp0[i] + seg.Prod(min, max + 1)); } } return ans; } class SegOp : ISegmentTreeOperator { public long Identity => long.MinValue / 2; public long Operate(long x, long y) { return Math.Max(x, y); } } interface ISegmentTreeOperator { T Identity { get; } T Operate(T x, T y); } class SegmentTree { int size; int log; T[] d; ISegmentTreeOperator op; void Update(int k) { d[k] = op.Operate(d[2 * k], d[2 * k + 1]); } public SegmentTree(int n, ISegmentTreeOperator op) { this.op = op; size = 1; while (size < n) size <<= 1; log = CountRZero(size); d = new T[2 * size]; for (var i = 0; i < d.Length; ++i) d[i] = op.Identity; } public SegmentTree(T[] v, ISegmentTreeOperator op) { this.op = op; size = 1; while (size < v.Length) size <<= 1; log = CountRZero(size); d = new T[2 * size]; for (var i = 0; i < size; ++i) d[i] = op.Identity; for (var i = 0; i < v.Length; ++i) d[size + i] = v[i]; for (var i = size - 1; i >= 1; --i) Update(i); } int CountRZero(int n) { var ans = 0; while (n % 2 == 0) { ++ans; n >>= 1; } return ans; } public T this[int p] { get { return d[p + size]; } set { p += size; d[p] = value; for (var i = 1; i <= log; ++i) Update(p >> i); } } public T Prod(int l, int r) { var sml = op.Identity; var smr = op.Identity; l += size; r += size; while (l < r) { if ((l & 1) != 0) sml = op.Operate(sml, d[l++]); if ((r & 1) != 0) smr = op.Operate(d[--r], smr); l >>= 1; r >>= 1; } return op.Operate(sml, smr); } T AllProd() => d[1]; } }