An eccentric tourist has decided to visit some of the most beautiful Italian cities. In his luggage, together with an Italian/wherever-he-comes-from phrasebook, he has a number of large coins made with pure gold.
The tourist has already chosen a list of N cities to travel across, and intends to follow his plan until he runs out of money (so he might not reach the last cities in his list). He starts in the first city, and travels from one city to the next one, following the order he has established and paying one golden coin per route.
When he is in a city (included the first one), he can spend the night in a hotel in order to visit that city. However, he can only stay one night in each city at maximum. If he does so, he has to pay the hotel one golden coin, and his happiness increases by a value that depends on a city.
Clearly, the tourist is interested in maximising the happiness he gains by suitably using the coins during this holiday. Given an array containing each of the amount of happiness that can be gained by visiting each city, calculate the maximum amount of happiness he can gain with K golden coins.
4 5 4 5 2 7