Saturday, May 6, 2017

Episode 19 - Knapsack

This week's episode will cover knapsack and subset sum. I'll be going over some of the basics of the pseudo-polynomial time algorithm and some of the variants. Gradually, I'll move into more complex variants and related techniques before ending with the solution to this problem:

I recommend attempting the above problem. Even though it is knapsack (which is generally considered simple), the problem is quite tough. I hope this episode has something interesting for everyone!

As I'm the author of this problem, I'll be running through how I came up with the problem and some of the tricks I learned from other problems that became relevant to the solution. I hope you will find this episode a very thorough treatment of knapsack and subset sum in general.

Here is another difficult problem related to subset sum you should be able to solve after this episode:

In other news, I participated in the TCO regional event in Austin last weekend. It was great to meet many new friends. Some even watch this show! Overall, it was a fun event and I recommend attending any TCO event in the future. For this contest, I employed a new strategy of listening to a waterfall during the contest to help me focus. Do you have a special technique for focusing during contests?

Tomorrow will be the regional event at ITMO in St. Petersburg. I'm sure many top competitors will be attending. Check it out if you live near there! It seems there will be a mirror of this contest online. :)

I'm also looking to make Algorithms Live! more useful. Some viewers have requested written versions of each episode. Unfortunately, I don't have enough time to convert each episode into a written version. If any viewers are up for the task, it would be extremely helpful. :) I'd be happy to send my notes and code written during the episode. Please contact me on Codeforces if this sounds interesting to you.

Another suggestion was to include time tags in the video description for specific topics in each episode. As I cover multiple topics, this will help viewers navigate to pieces of interest. I'll be starting on this process (slowly) for each episode. You can help by leaving a comment with time tags for your favorite old episode. Do you have any other suggests for improving this show? Let me know in the comments!

See you live! (stream, time)


  1. Hi, Matt! Do you have a sample solution or test cases for "jewel thief"? I am struggling with TLE at kattis TT


    2. AC! so the secret was to cache "time overtakes" value instead of doing binary search all over again when adding new partial solution in "the bag". That only affects constant in O() but my timing was 6.8 out of 8 second so it was a deal breaker.
      I see that there are solutions (also in c++) AC around one second time and wonder: is there another approach to this problem?

    3. My Java solution is much faster than that (or should be). Perhaps your implementation is doing operations that are slow in C++? Cleaning up your implementation can also improve in practice runtime.

      If you post the code on pastebin, I can take a look.

    4. Here it is:
      (sorry for macro usage)

  2. Btw, I have tried to listen to "waterfall" during last atcoder contest - it was OK, I have not experienced any loss of concentration thought performance was at about the same level. Usually I listen to BT-binary universe album and do it exclusively during contests to make some kind of mental habit. Do you still listening to "white noise" and found it productive?

    1. I enjoy BT as well. His +/- album is great for background music. I still listen to white noise when I compete. My advantage is purely based on my subjective experience. It helps me focus without distracting me and prevents my mind from wandering too much to things that aren't the contest.