/* -*- coding: utf-8 -*- * * 1947.cc: No.1947 質より種類数 - yukicoder */ #include #include using namespace std; /* constant */ const int MAX_N = 5000; const int MAX_V = 5000; /* typedef */ typedef long long ll; /* global variables */ int vs[MAX_N], ws[MAX_N], vws[MAX_V + 1]; ll dp[2][MAX_V + 1]; /* subroutines */ template inline void setmax(T &a, T b) { if (a < b) a = b; } /* main */ int main() { int n, v, c; scanf("%d%d%d", &n, &v, &c); for (int i = 0; i < n; i++) scanf("%d%d", vs + i, ws + i); for (int i = 0; i < n; i++) if (vws[vs[i]] < ws[i]) for (int j = vs[i], w = ws[i]; j <= v; j += vs[i], w += ws[i]) setmax(vws[j], w); fill(dp[0], dp[0] + v + 1, -1); dp[0][0] = 0; int cur = 0, nxt = 1; for (int i = 0; i < n; i++) { int wi = c + ws[i]; for (int j = v - vs[i]; j >= 0; j--) if (dp[cur][j] >= 0) setmax(dp[cur][j + vs[i]], dp[cur][j] + wi); } copy(dp[cur], dp[cur] + v + 1, dp[nxt]); for (int j = 0; j < v; j++) if (dp[cur][j] > 0) for (int k = 1; j + k <= v; k++) setmax(dp[nxt][j + k], dp[cur][j] + vws[k]); swap(cur, nxt); ll maxd = 0; for (int j = 0; j <= v; j++) setmax(maxd, dp[cur][j]); printf("%lld\n", maxd); return 0; }