#include typedef struct { int key, id; } data; void merge_sort(int n, data x[]) { static data y[5001] = {}; if (n <= 1) return; merge_sort(n / 2, &(x[0])); merge_sort((n + 1) / 2, &(x[n/2])); int i, p, q; for (i = 0, p = 0, q = n / 2; i < n; i++) { if (p >= n / 2) y[i] = x[q++]; else if (q >= n) y[i] = x[p++]; else y[i] = (x[p].key < x[q].key)? x[p++]: x[q++]; } for (i = 0; i < n; i++) x[i] = y[i]; } void chmax(long long* a, long long b) { if (*a < b) *a = b; } int main() { int i, N, M, V[5001], W[5001]; data d[5001]; scanf("%d %d", &N, &M); for (i = 1; i <= N; i++) { scanf("%d %d", &(V[i]), &(W[i])); d[i-1].key = V[i]; d[i-1].id = i; } merge_sort(N, d); int j, k; long long ans = 0, dp[5001] = {}; for (i = N - 1; i >= 0; i--) { k = d[i].id; for (j = M - W[k]; j >= 0; j--) { chmax(&ans, V[k] * (dp[j] + V[k])); chmax(&(dp[j+W[k]]), dp[j] + V[k]); } } printf("%lld\n", ans); fflush(stdout); return 0; }