# Continuous knapsack problem

Given items as `(value, weight)` we need to place them in a knapsack (container) of a capacity `k`. Note! We can break items to maximize value!

Example input:

```values[] = [1, 4, 5, 2, 10]
weights[] = [3, 2, 1, 2, 4]
k = 8```

Expected output:

`maximumValueOfItemsInK = 20;`

Algorithm:

```1) Sort values and weights by value/weight.
values[] = [5, 10, 4, 2, 1]
weights[] = [1, 4, 2, 2, 3]
2) currentWeight = 0; currentValue = 0;
3) FOR i = 0; currentWeight < k && i < values.length; i++ DO:
IF k - currentWeight < weights[i] DO
currentValue = currentValue + values[i];
currentWeight = currentWeight + weights[i];
ELSE
currentValue = currentValue + values[i]*(k - currentWeight)/weights[i]
currentWeight = currentWeight + weights[i]*(k - currentWeight)/weights[i]
END_IF
END_FOR
PRINT "maximumValueOfItemsInK = " + currentValue;```
{"ft_lang_label":"__label__en","ft_lang_prob":0.65936065,"math_prob":0.99994075,"size":926,"snap":"2019-51-2020-05","text_gpt3_token_len":280,"char_repetition_ratio":0.24186552,"word_repetition_ratio":0.0,"special_character_ratio":0.32181427,"punctuation_ratio":0.21264368,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9982353,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-29T11:48:21Z\",\"WARC-Record-ID\":\"<urn:uuid:8102db77-340c-42f7-a08d-300e3d2c4a52>\",\"Content-Length\":\"11752\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:35c79778-54c0-4004-a59d-20e403e61b34>\",\"WARC-Concurrent-To\":\"<urn:uuid:d8c27a5e-0acb-4a68-8489-675af248ef92>\",\"WARC-IP-Address\":\"104.248.60.43\",\"WARC-Target-URI\":\"https://www.programming-books.io/essential/algorithms/continuous-knapsack-problem-66f74ce19305441aa05d3181120a91ac\",\"WARC-Payload-Digest\":\"sha1:J7WEKUEJPIA42AHAFHSLIWG2LOR2O7ML\",\"WARC-Block-Digest\":\"sha1:QYU7J267QR5J3BUVL3ZNMMGYN5CWH4AD\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579251796127.92_warc_CC-MAIN-20200129102701-20200129132701-00033.warc.gz\"}"}