Editorial for Trip to Amsterdam
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
pair<int, int> knapsack[n];
for (int i = 0; i < n; i++)
cin >> knapsack[i].first >> knapsack[i].second;
long long dp[n + 1][k + 1];
dp[0][0] = 0;
for (int i = 1; i < n + 1; i++)
dp[i][0] = 0;
for (int i = 1; i < k + 1; i++)
dp[0][i] = 0;
for (int i = 1; i < n + 1; i++)
for (int j = 1; j < k + 1; j++)
if (j - knapsack[i - 1].first >= 0)
dp[i][j] = max(dp[i - 1][j - knapsack[i - 1].first] + knapsack[i - 1].second, dp[i - 1][j]);
else
dp[i][j] = dp[i - 1][j];
stack<int> result;
for (int i = n, j = k; i > 0; i--)
if (dp[i][j] != dp[i - 1][j])
{
result.emplace(i);
j -= knapsack[i - 1].first;
}
while (!result.empty())
{
cout << result.top() << ' ';
result.pop();
}
return 0;
}