Exotic Foods
InterviewTime limit1sMemory limit128 MB
Given a sequence of food values, choose a subset with no two chosen positions adjacent so the total value is as large as possible.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Array, Greedy, Implementation
- Solved
- No attempts yet
Problem
Traveling through time is exhausting and quickly works up an appetite. Fortunately, every era is full of exotic foods such as dinosaur meat, dodo-bird eggs, and mammoth milk. Unfortunately, the time machine has no room for a refrigerator, so if Tim wants to eat an exotic food he must eat it on the spot. But once he eats, he becomes so full that he cannot eat anything at all during the very next era he visits. Each food has a value that Tim assigns to it, and his goal is to maximize the total value of the foods he actually eats. The order in which he visits the eras is already fixed and cannot be changed. By choosing which foods to eat, what is the maximum total value he can achieve?
Input
The first line contains the number of data sets. Then follow data sets, each in the following form. The first line of each data set contains an integer , the number of foods Tim will encounter, with . The next line contains integers giving the values of the foods, with . The order of the is the order in which Tim encounters them.
Output
For each data set, first print Data Set x: on its own line, where is the data set's number starting from 1. On the next line, print the maximum total value Tim can achieve. Separate consecutive data sets with a single blank line.