This page is still under construction.

Parts of this page are still being built. What you see may change.

Letter Delivery

Time limit3sMemory limit1024 MB

Summary
Assign letter requests to couriers on a line so the total round-trip distance is minimized, then print each courier's delivery order.
Level

Hard8 of 10

Topics
Greedy, Sorting, Intervals
Solved
No attempts yet

Problem

UCPC Middle School has a trend of sending letters to friends in other classes. The school has NN classes, numbered 11 through NN, and each classroom lies along a long corridor in class number order. The position of the classroom for class ii is the integer xix_i, the distance from the start of the corridor.

Donggyu saw the letter trend as a good business opportunity and planned a letter delivery service. Students submit delivery requests through an app, and the letters are delivered in bulk during break time. Each request is numbered from 11 to MM and lists a pair (si,ei)(s_i, e_i) of class numbers for its origin and destination.

For smooth delivery, Donggyu hired one courier from each class. He assigns the requests to the couriers, and each courier delivers the assigned letters and is paid by Donggyu. The rules are as follows.

  • Each courier must deliver letters in the order Donggyu sets.
  • If a courier carries two or more letters, the letters may get mixed up, so a courier delivers only one letter at a time.
  • After finishing all deliveries, a courier must return to their own classroom to attend class.
  • Each courier starts at their own classroom, delivers all assigned letters, and returns to their own classroom. The courier is paid the minimum travel distance needed for this.
  • A courier who is not assigned any request is not paid.

For example, suppose four classrooms sit in a row with a gap of 11 between neighbors, and the requests have origin and destination pairs (4,2)(4,2) and (1,3)(1,3), in that order. If the class 1 courier gets request 2 and the class 3 courier gets request 1, the class 1 courier travels 2+2=42+2=4 and the class 3 courier travels 1+2+1=41+2+1=4. Donggyu pays 4+4=84+4=8 in total to the two couriers. (Figure I.1)

If no request goes to the couriers of classes 2, 3, and 4, and the class 1 courier delivers request 2 and then request 1, the class 1 courier travels 2+1+2+1=62+1+2+1=6. Donggyu pays only 66, to the class 1 courier. (Figure I.2)

In this example, giving requests 2 and 1 in order to the class 1 courier alone gives the lowest total pay. Determine how Donggyu should assign the requests to the couriers.

Figure I.1: An assignment where Donggyu pays 88Figure I.2: An assignment where Donggyu pays 66

Input

The first line contains two integers NN and MM, separated by a space. (2≤N≤300 0002 \leq N \leq 300\,000; 1≤M≤300 0001 \leq M \leq 300\,000)

The second line contains NN distinct integers xix_i in increasing order, separated by spaces. The ii-th integer is the position of the classroom of class ii. (0≤xi≤1090 \leq x_i \leq 10^9)

Each of the next MM lines contains two integers sis_i and eie_i, separated by a space. (1≤si,ei≤N1 \leq s_i, e_i \leq N; si≠eis_i \neq e_i) The ii-th of these lines describes request ii, where sis_i is the origin class number and eie_i is the destination class number.

Output

On the first line, print the minimum total pay Donggyu must give to the couriers.

Then print NN lines. On the ii-th line, print the number of requests assigned to the courier of class ii, followed by the request numbers in the order they are delivered. If more than one assignment is possible, print only one of them.

Examples1

  1. Example 1

    Input
    4 2
    1 2 3 4
    4 2
    1 3
    
    Expected output
    6
    2 2 1
    0
    0
    0