Marathon Ice Hockey

Time limit1sMemory limit64 MB

Summary
Assign each player a number of minutes by a fixed greedy order, then convert the resulting cyclic blocks into an explicit substitution list.
Level

Medium7 of 10

Topics
Greedy, Sorting, Simulation, Implementation
Solved
No attempts yet

Problem

A marathon ice hockey game lasts MM minutes. During every minute of the game, exactly six players of Ante's team are on the ice.

Ante brought NN players to the tournament. Player ii has quality KiK_i and endurance IiI_i. The endurance is the total number of minutes that player ii may spend on the ice during the whole game, and those minutes do not have to be consecutive. If a player is on the ice for XX minutes, then rests on the bench, then plays YY more minutes, he has used X+YX + Y minutes of his endurance. No player may ever be on the ice for more minutes than his endurance.

A substitution happens between two consecutive minutes, never inside a minute, and several players may be swapped at the same moment. A player who enters the ice at some moment cannot leave at that same moment, and a player who leaves cannot come back at that same moment.

The quality of the team during one minute is the sum of the qualities of the six players who are on the ice in that minute. ZZ is the sum of those MM values. For example, if the game lasts 3 minutes and the quality of the team is 15 in the first minute, 12 in the second and 14 in the third, then Z=15+12+14=41Z = 15 + 12 + 14 = 41.

The input always allows at least one schedule that keeps six players on the ice in every minute, that is, I1+I2+⋯+IN≥6MI_1 + I_2 + \dots + I_N \ge 6M.

Print the largest ZZ that Ante can reach, together with a schedule that reaches it. Many schedules reach the largest ZZ, so the output section pins down exactly one of them.

Marathon ice hockey has no goalie.

Input

The first line contains the integers MM and NN (1≤M≤500 0001 \le M \le 500\,000, 6≤N≤500 0006 \le N \le 500\,000), the length of the game in minutes and the number of players Ante brought.

Each of the next NN lines contains the integers KiK_i and IiI_i (1≤Ki≤100 0001 \le K_i \le 100\,000, 1≤Ii≤M1 \le I_i \le M), the quality and the endurance of one player. The players are numbered from 1 to NN in the order they are given in the input.

Output

The schedule is fixed by the rule below, so exactly one output is accepted.

Sort the players by quality in decreasing order, and players of equal quality by increasing number. Hand out playing time in that order: the next player in the order gets the smaller of his endurance and the number of minutes still unassigned, and 6M6M player-minutes are handed out in total. Once 6M6M minutes are assigned, every remaining player gets 0 minutes. This assignment reaches the largest ZZ, and ZZ is the sum of KiK_i multiplied by the playing time of player ii.

Lay the schedule out in a row of 6M6M cells numbered 1 to 6M6M. Take the players whose playing time is at least 1 minute, in the same sorted order, and give each of them a block of consecutive cells as long as his playing time, filling the row from cell 1 without gaps. Cell cc belongs to minute ((c−1) mod M)+1((c - 1) \bmod M) + 1. Every playing time is at most MM, so the six cells of one minute hold six different players, and those are the players on the ice in that minute.

The first line of output contains ZZ.

The second line contains the numbers of the six players who are on the ice in minute 1, in increasing order.

The third line contains BB, the number of substitution lines that follow.

Each of the next BB lines contains three integers XX, AA and CC, meaning that after XX minutes of play, player AA leaves the ice and player CC enters it.

Build the substitution lines this way. For each XX from 1 to M−1M - 1, let LL be the players who are on the ice in minute XX but not in minute X+1X + 1, sorted in increasing order, and let EE be the players who are on the ice in minute X+1X + 1 but not in minute XX, also sorted in increasing order. The two lists have the same length. Pair them by position and print one line per pair, with the groups printed in increasing order of XX.

Examples3

  1. Example 1

    Input
    200 6
    3 200
    4 200
    5 200
    6 200
    7 200
    8 200
    
    Expected output
    6600
    1 2 3 4 5 6
    0
    
  2. Example 2

    Input
    9 9
    10 3
    9 3
    13 9
    5 3
    15 9
    100 9
    3 6
    2 6
    1 6
    
    Expected output
    1260
    1 3 5 6 7 8
    4
    3 1 2
    3 8 9
    6 2 4
    6 7 8
    
  3. Example 3

    Input
    3 9
    100 3
    100 3
    100 3
    100 3
    100 2
    100 1
    50 1
    30 2
    1 1
    
    Expected output
    1610
    1 2 3 4 5 7
    2
    1 7 8
    2 5 6