Description

A fish salesman has determined there are two lucrative spots P and Q where he can set up his stand. He has (perfectly) predicted the profits to be had during a period of n days on each spot, call them p i and q i for 1 i n . The salesman obviously wants to maximize his profit, but he cannot be in both spots on one day, so he will have to decide where he is going to be on each day. Breaking up his stand and setting it up again in the other spot is a difficult job, however, which takes a whole day, on which there will be no profits.

As an example consider the following instance:

P  Q
80 90
30 60
30 60
70 50
80 20

We expect 300 as output here, representing that we set up shop at location Q on days 1 and 2 and on location P on days 4 and 5 (with day 3 being the switch day).

Give an iterative dynamic programming solution to find the maximum profit the salesman can earn.

Test Data is Available

You can access the following files:
example.out Download
example.in Download