Under construction

Still being built. Things may change

Cafeteria Serving

Time limit1sMemory limit1024 MB

Summary
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 pj,1,pj,2,⋯ ,pj,ljp_{j,1}, p_{j,2}, \cdots, p_{j,l_j}. Each student can receive the same food at most once.
  • If student j receives food pj,kp_{j,k}, their happiness increases by vj,kv_{j,k}.
  • Food received by student j cannot be received by student j+1. (1≤j≤M−11 \leq j \leq M-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. (1≤N,M≤1051 \leq N, M \leq 10^5)

The next M lines describe the foods each student can eat. On line j, the first integer is ljl_j, the number of foods student j can eat. It is followed by 2lj2l_j integers pj,1,vj,1,pj,2,vj,2,⋯ ,pj,lj,vj,ljp_{j,1}, v_{j,1}, p_{j,2}, v_{j,2}, \cdots, p_{j,l_j}, v_{j,l_j}, separated by spaces. The values pj,kp_{j,k} are given in increasing order. (1≤j≤M1 \leq j \leq M; 1≤lj≤N1 \leq l_j \leq N; 1≤pj,1<pj,2<⋯<pj,lj≤N1 \leq p_{j,1} < p_{j,2} < \cdots < p_{j,l_j} \leq N; 1≤vj,k≤1091 \leq v_{j,k} \leq 10^9)

The sum of all ljl_j does not exceed 10510^5.

Output

Print the maximum possible total happiness of all students.

Examples1

  1. Example 1

    Input
    5 5
    2 1 5 2 6
    1 2 5
    1 3 5
    1 4 5
    1 5 5
    
    Expected output
    26