#include #include using namespace std; int main() { int N, W; cin >> N >> W; vector v(N), w(N); for (int i = 0; i < N; ++i) { cin >> v[i] >> w[i]; } vector> dp(N + 1, vector(W + 1, 0)); for (int i = 1; i <= N; ++i) { for (int weight = 1; weight <= W; ++weight) { if (w[i - 1] <= weight) { dp[i][weight] = max(dp[i - 1][weight], dp[i - 1][weight - w[i - 1]] + v[i - 1]); } else { dp[i][weight] = dp[i - 1][weight]; } } } int maxCapacity = dp[N][W]; if (maxCapacity == 0) { cout << "-1" << endl; // Knapsack not possible } else { cout << maxCapacity << endl; } return 0; }