Mobile Service
Time limit3sMemory limit128 MB
Given a cost matrix and a request sequence, find the minimum total cost of moving three servers to serve each request in order using optimal dynamic programming.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Math
- Solved
- No attempts yet
Problem
A company provides service to customers located in different towns. The company has three service-staff employees. When a request occurs at some location, one employee must move from their current location to the location of the request in order to satisfy it, unless an employee is already there. Only one employee can move at any moment; employees move only in response to a request, and no two employees may occupy the same location.
Moving an employee from location to location costs a given amount . This cost function is not necessarily symmetric, but the cost of not moving is , i.e. . The company must satisfy the received requests in strict first-come, first-served order.
Decide which employee serves each request so that the total cost of serving the whole sequence of requests is as small as possible, and report that minimum total cost.
Input
The first line contains two integers and . () is the number of locations and () is the number of requests. Locations are identified by the integers from to .
Each of the next lines contains non-negative integers. The -th number on line is the cost , which is less than .
The last line contains integers, the list of requests. Each request is given by the identifier of the location where it occurs. Initially, the three employees are located at locations , , and , respectively.
Output
Print a single integer : the minimal total cost of serving the entire sequence of requests.