Letter Delivery
Time limit3sMemory limit1024 MB
Assign letter requests to couriers on a line so the total round-trip distance is minimized, then print each courier's delivery order.
Problem
UCPC Middle School has a trend of sending letters to friends in other classes. The school has classes, numbered through , and each classroom lies along a long corridor in class number order. The position of the classroom for class is the integer , 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 to and lists a pair 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 between neighbors, and the requests have origin and destination pairs and , in that order. If the class 1 courier gets request 2 and the class 3 courier gets request 1, the class 1 courier travels and the class 3 courier travels . Donggyu pays 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 . Donggyu pays only , 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.
Input
The first line contains two integers and , separated by a space. (; )
The second line contains distinct integers in increasing order, separated by spaces. The -th integer is the position of the classroom of class . ()
Each of the next lines contains two integers and , separated by a space. (; ) The -th of these lines describes request , where is the origin class number and is the destination class number.
Output
On the first line, print the minimum total pay Donggyu must give to the couriers.
Then print lines. On the -th line, print the number of requests assigned to the courier of class , followed by the request numbers in the order they are delivered. If more than one assignment is possible, print only one of them.

