Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main() {
- int n, max_weight;
- cin >> n >> max_weight;
- int w[n];
- int c[n];
- for (int i = 0; i < n; i++) {
- cin >> w[i];
- cin >> c[i];
- }
- /*cout << "n is " << n << endl;
- cout << "max_weight is: " << max_weight << endl;
- for (int i = 0; i < n; i++) {
- cout << "weight[" << i << "] = " << w[i] << " and cost[" << i << "] = " << c[i] << endl;
- }
- */
- //если n = 3:
- //000 - 0
- //001 - 1
- //010 - 2
- //011 - 3
- //100
- //101
- //110
- //111
- //
- //
- //110
- //010
- int bestCost = 0;
- int bestMask = 0;
- for (int mask = 0; mask < (1<<n); mask++) {
- int totalWeight = 0;
- int totalCost = 0;
- for (int j = 0; j < n; j++) {
- if ((mask&(1<<j)) > 0) {
- totalWeight += w[j];
- totalCost += c[j];
- }
- }
- if (totalWeight <= max_weight && totalCost > bestCost) {
- bestCost = totalCost;
- bestMask = mask;
- }
- }
- cout << bestCost << endl;
- cout << bestMask << endl;
- for (int i = 0; i < n; i++) {
- if ((bestMask & (1<<i)) > 0)
- cout << i << " ";
- }
- cout << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment