Задача: Рюкзак | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Задача: Рюкзак

Рюкзак Найдите максимальный вес золота, который можно унести в рюкзаке вместительностью s, если есть n золотых слитков с заданными весами. Входные данные Первая строка содержит одно число s (1 ≤ s ≤ 10000). Далее следует n (1 ≤ n ≤ 300) неотрицательных чисел, не превосходящих 100000 - веса слитков. Выходные данные Выведите искомый максимальный вес. ======================================= Входные данные #1 10 1 4 8 Выходные данные #1 9 ======================================= Входные данные #2 20 5 7 12 18 Выходные данные #2 19 Я ее сделал перебором, но мой код очень долго работает ... Вот мой код: https://code.sololearn.com/ctRX0XkcIRff/?ref=app

27th Jan 2020, 4:17 PM
Oleksandr
Oleksandr - avatar
22 Answers
+ 5
Alexander your code seems good, but it would take a lot of time to execute. Also, what is it you neede help with, does your code not work properly ?
27th Jan 2020, 4:39 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 5
Alexander whoops 😂😂 let me check. Do you have any other test cases ? EDIT: lmao my code is completly wrong. I thought you were only supposed to have two numbers together, not many at the same time. Sorry! I'll try in a bit.
27th Jan 2020, 5:28 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 4
You can get more help if you ask in english, you seem to know it as well. I said so because I noticed you posted it yesterday but didn't get any answer (I think).
27th Jan 2020, 4:23 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 4
Alexander Okay, I'll try. EDIT: Here is my code, I think it will be faster than the permutations: https://code.sololearn.com/cjWpEgsH1LRN/?ref=app (let me know if my code is slow or doesn't work)
27th Jan 2020, 4:58 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
Aymane Boukrouh Thanks for the advice. I understand. I'm just afraid if I translate the task into English by a Google translator, then the person may not understand the task correctly. 😶
27th Jan 2020, 4:30 PM
Oleksandr
Oleksandr - avatar
+ 2
Aymane Boukrouh Knapsack Find the maximum weight of gold that can be carried out in a knapsack of capacity s, if there are n gold ingots with specified weights. Input The first line contains one number s (1 ≤ s ≤ 10000). Then given n (1 ≤ n ≤ 300) non-negative integers, not exceeding 100000 - the weights of ingots. Output Print the desired maximum weight. =================================== Input example #1 10 1 4 8 Output example #1 9 =================================== Input example #2 20 5 7 12 18 Output example #2 19 ===================================
27th Jan 2020, 4:32 PM
Oleksandr
Oleksandr - avatar
+ 2
Aymane Boukrouh I need help writing faster code that will solve the "Knapsack" problem and learn about different methods for solving tasks.
27th Jan 2020, 4:47 PM
Oleksandr
Oleksandr - avatar
+ 2
Alexandr Your past code worked 50% from 100% and the new 93% from 100%. 7% is the time limit
27th Jan 2020, 5:43 PM
Oleksandr
Oleksandr - avatar
+ 2
Hi! From where you got this task? I need it also 😉
29th Jan 2020, 3:36 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
I checked this site! Its very good! Thanks! 😉😁👍
29th Jan 2020, 4:35 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
I will coming back soon...
29th Jan 2020, 4:39 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
in fact, everyone scolds Python for its slowness, but you made an attempt to squeeze the maximum possible out of it 😁 do you solve problems only for yourself or is this part of your mandatory coding in an educational institution?
29th Jan 2020, 5:15 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
I don't want to see the finished solution yet, as I will try to solve this problem on my own
29th Jan 2020, 5:16 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
Then... if you good in english you can try solve exercises on codewars.com there you become a karate fighter and try to pass the levels of tasks sorted by difficulty. you can also arrange fights with other users there
29th Jan 2020, 5:40 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Alexandr Your code works fine but with large numbers slow.
27th Jan 2020, 5:32 PM
Oleksandr
Oleksandr - avatar
+ 1
29th Jan 2020, 4:32 PM
Oleksandr
Oleksandr - avatar
0
Aymane Boukrouh The code works faster but gives incorrect answers.
27th Jan 2020, 5:27 PM
Oleksandr
Oleksandr - avatar
0
Aymane Boukrouh Huh it happens😁 .. sorry, but I have no more examples.
27th Jan 2020, 5:34 PM
Oleksandr
Oleksandr - avatar
29th Jan 2020, 4:38 PM
Oleksandr
Oleksandr - avatar
0
Ярослав Вернигора(Yaroslav Vernigora) This task has already been solved by Alexandr, here is its code that works great👌: from itertools import combinations n = int(input()) ar = [int(x) for x in input().split() if int(x) <= n] def maxsearch(m): if sum(m) <= n: return sum(m) maxx = 0 for i in range(1, len(m) + 1): res = filter(lambda x: maxx < x <= n, map(sum, combinations(m, i))) for y in res: if y > maxx: maxx = y if maxx == n: return maxx return maxx print(maxsearch(ar))
29th Jan 2020, 4:46 PM
Oleksandr
Oleksandr - avatar