Cafeteria Serving
Time limit1sMemory limit1024 MB
Each student picks some foods from their list, and the next student cannot receive any food the previous one took. Find the maximum total happiness.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Segment tree, Hash map
- Solved
- No attempts yet
Problem
There are N serving counters numbered 1 to N. Counter i serves food i. M students line up to receive food.
Each student can receive certain foods, and their happiness increases by the value of the food received. The rules are as follows.
- The food numbers that student j can receive are fixed as . Each student can receive the same food at most once.
- If student j receives food , their happiness increases by .
- Food received by student j cannot be received by student j+1. ()
Initially, every student has happiness 0. Serve the food so that the total happiness of all students is maximized.
Input
The first line contains two integers N and M, separated by a space. ()
The next M lines describe the foods each student can eat. On line j, the first integer is , the number of foods student j can eat. It is followed by integers , separated by spaces. The values are given in increasing order. (; ; ; )
The sum of all does not exceed .
Output
Print the maximum possible total happiness of all students.