Here is the challenge. In principle, the ideas were its solutions: this is dynamics or recursion. But to no avail. Please help in the development of the algorithm, because I simply cannot stand recursion and rarely understand it, so I just need to draw up a task solution, understandable and clear, so that there is no great difficulty in translating into Pascal.

P.S. You can write the program itself, with explanations ...

task text

    3 answers 3

    This is a backpack task with some additional conditions. First, we need to assemble a backpack of exactly the size (FE) , and secondly, we need to solve the same problem, but not maximizing the final weight, but minimizing it.


    For your particular case, the solution is known with the help of dynamic programming that works in pseudopolynomial time.

    You just need to slightly modify the dynamics in order to find a solution with a weight exactly equal to (FE) , and not less than or equal to it and run it 2 times - to maximize and minimize.

      Standard task on DP. Here is the solution:

      • let f [i] be the maximum amount we can collect with some coins, so their total weight is i.
      • Obviously, f [0] = 0.
      • Fill in the values ​​of all the cells in the array f minus infinity (-maxlongint).
      • Let us run over the indices of the array f starting from 0. Let the current index in the array f be equal to i. If f [i]! = Maxlongint (! = This is not equal), then we were somehow able to gain weight i. Then we go through all the coins (index j) and try to "shove" it into our DP. Those. if f [i + w [j]] <f [i] + p [j] (if we add our coin (the total weight is then equal to i + w [j]), then if the amount that was collected before (equal to f [i + w [j]]), less than the one we can get by adding to the current amount of the new coin (f [i] + p [j])), then we update our answer for this weight: f [i + w [j]] = f [i] + p [j];

      Now, if in f [WITH_W - WITHOUT_W] == -maxlongint, then we could not collect the required amount, therefore This is impossible. (WITH_W, WITHOUT_W - weight of the piggy bank With and without coins)

      Similarly, to find the minimum amount.

        Here is an example solution in javascript. Can be checked in the browser

         function calc( sum, cost, weight ){ var min = [], //Массив ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹Ρ… стоимостСй max = [], //Массив ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹Ρ… стоимостСй c_cost, c_weight, //ВСс/Π‘Ρ‚ΠΎΠΈΠΌΠΎΡΡ‚ΡŒ Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ ΠΌΠΎΠ½Π΅Ρ‚ c_min, c_max, //Мин/Макс стоимости для Π΄Π°Π½Π½ΠΎΠ³ΠΎ вСса t_min, t_max, //Мин/Макс вСс ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒΡΡ ΠΏΡ€ΠΈ //Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠΈ этой ΠΌΠΎΠ½Π΅Ρ‚Ρ‹ w, j, //Π‘Ρ‡Ρ‘Ρ‚Ρ‡ΠΈΠΊ ΠΏΠΎ вСсу ΠΈ ΠΏΠΎ ΠΌΠΎΠ½Π΅Ρ‚Π°ΠΌ mj = cost.length; //ΠžΠ³Ρ€Π°Π½ΠΈΡ‡Π΅Π½ΠΈΠ΅ Π½Π° ΠΊΠΎΠ»-Π²ΠΎ ΠΌΠΎΠ½Π΅Ρ‚ min[0] = max[0] = 0; //Для вСса 0 ΠΈ 0 //ΠŸΡ€ΠΎΡ…ΠΎΠ΄ΠΈΠΌ ΠΏΠΎ всСм вСсам for ( w = 1; w <= sum; w++ ){ c_min = 0; //Π˜Π·Π½Π°Ρ‡Π°Π»ΡŒΠ½ΠΎ, Ρ‚Π°ΠΊΠΎΠ³ΠΎ вСса нСльзя ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ c_max = 0; //ΠΈ ΠΏΠΎΡ‚ΠΎΠΌΡƒ Π΅Π³ΠΎ ΠΌΠΈΠ½ ΠΈ макс стоимости - 0 //ΠŸΡ€ΠΎΡ…ΠΎΠ΄ΠΈΠΌ ΠΏΠΎ Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠ΅ ΠΌΠΎΠ½Π΅Ρ‚ for ( j = 0; j < mj; j++ ){ c_cost = cost[j]; //БохраняСм Π΄Π°Π½Π½Ρ‹Π΅ c_weight = weight[j]; //Π’Π΅ΠΊΡƒΡ‰Π΅ΠΉ ΠΌΠΎΠ½Π΅Ρ‚Ρ‹ //Если вСс Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ ΠΌΠΎΠ½Π΅Ρ‚Ρ‹ Ρ€Π°Π²Π΅Π½ Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΌΡƒ вСсу, //Ρ‚ΠΎ этот вСс ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ if ( w == c_weight ){ //Если вСс Π½Π΅ достигался Ρ€Π°Π½Π΅Π΅, ΠΈΠ»ΠΈ Π΅Π³ΠΎ ΠΌΠΈΠ½ ΡΡ‚ΠΎΠΈΠΌΠΎΡΡ‚ΡŒ большС ΠΌΠΎΠ΅ΠΉ if ( ( c_min == 0 ) || ( c_min > c_cost ) ) c_min = c_cost; //Если макс ΡΡ‚ΠΎΠΈΠΌΠΎΡΡ‚ΡŒ мСньшС ΠΌΠΎΠ΅ΠΉ if ( c_max < c_cost ) c_max = c_cost; } //Если вСс Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ ΠΌΠΎΠ½Π΅Ρ‚Ρ‹ большС Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΌΡƒ вСсу else if ( w > c_weight ){ //ВычисляСм ΠΊΠ°ΠΊΠΈΠΌΠΈ станут ΠΌΠΈΠ½ ΠΈ макс стоимости //ΠΏΡ€ΠΈ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠΈ этой ΠΌΠΎΠ½Π΅Ρ‚Ρ‹ t_min = min[ w - c_weight ] + c_cost; t_max = max[ w - c_weight ] + c_cost; //Если вСс ( w - c_weight ) - достигаСтся, //Ρ‚ΠΎ Ссли вСс Π½Π΅ достигался Ρ€Π°Π½Π΅Π΅, ΠΈΠ»ΠΈ Π΅Π³ΠΎ ΠΌΠΈΠ½ ΡΡ‚ΠΎΠΈΠΌΠΎΡΡ‚ΡŒ большС ΠΌΠΎΠ΅ΠΉ if ( ( t_min != c_cost ) && ( ( c_min == 0 ) || ( c_min > t_min ) ) ) c_min = t_min; //Если вСс Π½Π΅ достигался Ρ€Π°Π½Π΅Π΅, ΠΈΠ»ΠΈ Π΅Π³ΠΎ макс ΡΡ‚ΠΎΠΈΠΌΠΎΡΡ‚ΡŒ мСньшС ΠΌΠΎΠ΅ΠΉ if ( ( t_max != c_cost ) && ( c_max < t_max ) ) c_max = t_max; } } //ЗаписываСм Π΄Π°Π½Π½Ρ‹Π΅ Π² ΠΏΠ°ΠΌΡΡ‚ΡŒ min[ w ] = c_min; max[ w ] = c_max; } return [ min[sum], max[sum], min, max ]; } cost = [ 1, 2, 3 ]; weight = [ 2, 4, 5 ]; sum = 36; res = calc( sum, cost, weight ); if ( res[0] && res[1] ) console.log( res[0], ' ', res[1] ); else console.log('This is impossible.');