#P7685. [CEOI 2005] Mobile Service

[CEOI 2005] Mobile Service

Description

A company provides services to its partners located in different towns. The company currently has 33 mobile service workers. If a service request occurs at some location, a worker must move from their current location to the request location (if no worker is already there) to fulfill the request. At any time, only one worker may move. They can move only when requested, and multiple workers are not allowed to be in the same location. Moving a worker from location pp to location qq incurs a certain cost C(p,q)C(p,q). The cost is not necessarily symmetric, but staying still costs 00, i.e., C(p,p)=0C(p,p)=0. The company must handle the received requests strictly in first-come-first-served order.
Please write a program that decides which worker should move for each request, so that the total cost of serving the given list of requests is as small as possible.

Input Format

The first line contains two integers LL and NN. LL is the number of locations, and NN is the number of requests. Locations are represented by integers from 11 to LL. Each of the next LL lines contains LL non-negative integers. The jj-th number in line i+1i+1 is the cost C(i,j)C(i,j). The last line contains NN integers, the list of requests. Each request is identified by the location where it occurs. Initially, the three service workers are at locations 1,2,1, 2, and 33, and these are also used as the identifiers of the three workers.

Output Format

The first line contains an integer MM, the minimum total cost to serve the list of requests. The second line contains exactly NN integers. The ii-th number is the identifier of the worker (1,21, 2 or 33) who will serve the ii-th request.

5 9
0 1 1 1 1
1 0 2 3 2
1 1 0 4 1
2 1 5 0 1
4 2 3 4 0
4 2 4 1 5 4 3 2 1
5
1 2 1 2 2 1 3 1 3 

Hint

Constraints

For 100%100\% of the testdata, 3L2003 \leq L \leq 200, 1N10001 \leq N \leq 1000, C(i,j)<2000C(i,j) < 2000.

Notes

From Mobile Service, CENTRAL-EUROPEAN OLYMPIAD IN INFORMATICS 2005.
Translated and organized by

/user/234011

Translated by ChatGPT 5