#include using namespace std; using ll = long long; int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); ll N, C; cin >> N >> C; vector dp(C+1, vector(51, vector(51, -1e9))); auto check = [&](int a, int b, int c){ if (b == 0 && c == 0) return 1; if (c == 0){ if (b == a) return 0; return 1; } if (a == b || b == c || c == a) return 0; if (a > b && b < c) return 1; if (a < b && b > c) return 1; return 0; }; dp[0][0][0] = 0; vector L(N), D(N); for (int i=0; i> L[i]; for (int i=0; i> D[i]; for (int i=0; i<=C; i++){ for (int j=0; j<=50; j++){ for (int k=0; k<=50; k++){ for (int l=0; l C) continue; if (check(L[l], k, j)){ dp[i+D[l]][L[l]][k] = max(dp[i+D[l]][L[l]][k], dp[i][k][j]+L[l]); } } } } } int ans = 0; for (int i=0; i<=C; i++){ for (int j=1; j<=50; j++){ for (int k=1; k<=50; k++){ ans = max(ans, dp[i][j][k]); } } } cout << ans << endl; return 0; }