The first thing to understand is that as far as the numbers go, there is no difference between the lost soldiers and the remaining soldiers. Thus, we can reduce the properties of the castle to soldiers lost and wanted.
The second thing you need to understand is that if you go down a tree branch, you must fill out the entire branch to return. This allows us to reduce the entire branch to a single "mega-castle" with the required and lost total soldiers.
, , , : , . , , , . . , . ! , .
, , / , . 0 , , , . : , , . , (un) ( , ). , ( , ). : (un) , , . ( ) .
, :
O (n * c ^ 2 * lg (c)), n - , c - . , nc '', node clg (c) , . [ ]
, , , .