Hi everyone
I need help with this knapsack variant
Thanks

| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3603 |
| 4 | jiangly | 3583 |
| 5 | turmax | 3559 |
| 6 | tourist | 3541 |
| 7 | strapple | 3515 |
| 8 | ksun48 | 3461 |
| 9 | dXqwq | 3436 |
| 10 | Otomachi_Una | 3413 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 157 |
| 2 | adamant | 153 |
| 3 | Um_nik | 147 |
| 4 | Proof_by_QED | 146 |
| 5 | Dominater069 | 145 |
| 6 | errorgorn | 141 |
| 7 | cry | 139 |
| 8 | YuukiS | 135 |
| 9 | TheScrasse | 134 |
| 10 | chromate00 | 133 |
I need help with this knapsack variant
Thanks

| Название |
|---|



I cant figure out anything from hint but can share my approach. Just make multiple copies of the coin.
Say W={5,4} and K={2,3} then modify W as W1={5,5,4,4,4} and V as V1={100,100,70,70,70}. Forget K now.
Build dp table as dp[i][j]=minimum cost to get sum of j using first i coins. Complexity: O(S*sum(Ki)). You can do it in O(S) space.
For each object X, split its K_i copies into different objects with weight (2^a)*W_i and value (2^a)*V_i, where all the of 2^a of a particular X sum to K_i.
This probably seems a bit vague, so let me show some examples:
Object With 5 Copies: Split it into 3 objects with the weight of 1 copy, 2 copies, and 2 copies, allowing someone to take anywhere between 0 and 5 copies (we can easily see why 2 objects with the weight of 1 copy and 4 copies will not work).
Object With 12 Copies: Split it into 5 objects with the weight of 1 copy, 1 copy, 2 copies, 4 copies, and 4 copies.
We initially start with freq[1] = K_i, and loop for powers of 2 until we encounter freq[i] = 0 to split up the object's copies.
We now have a complexity of S*sqrt(sum of K_i).
EDIT: BTW @AmericanPsycho what book is this?
This book is Competitive Programming 3.
How is the square root coming?
Because 0 <= freq[i] <= 2 and the number of different freqs >= 1 will be O(sqrt(k)) since the sum of the first k natural numbers is O(k^2).