MDOSTAVA - Pizza Delivery

 

Little Ivica recently got a job delivering pizzas for the most popular pizzeria in town.

At the start of his work day, he receives a list with the locations to which he needs to deliver pizzas, in order in which the locations are given.

The city is divided into R×C cells. The rows are numbered 1 through R, columns 1 through C.

From every cell, it is possible to move to neighbouring cells to the left and right. Moving up or down is only allowed in the first and last columns (columns 1 and C).

The pizzeria is in the top left corner (1, 1) and this is the location Ivica starts from. Ivica takes with him all the pizzas he will deliver that day so he does not have to return to the pizzeria between deliveries or after the last delivery.

For each location in the city, Ivica knows how much time he will spend every time he is in it (trying to get through the intersection, for example). Write a program that calculates the smallest amount of time for Ivica to deliver all the pizzas.

Input

The first line contains the integers R and C (1 ≤ R ≤ 2000, 1 ≤ C ≤ 200), the dimensions of the city.

Each of the following R lines contains C integers. These are the times Ivica spends every time he enters a location. The times will be integers between 0 and 5000, inclusive.

The next line contains an integer D (1 ≤ D ≤ 200 000), the number of pizza deliveries that day. (No, it's not unrealistically large at all.) Each of the following D lines contains two integers A and B (1 ≤ A ≤ R, 1 ≤ B ≤ C), the location to which a pizza must be delivered. The pizzas are given in the order in which they must be delivered. No location will be given twice in a row.

Output

Output the smallest amount of time for Ivica to deliver all the pizzas.

Sample

input
3 3
1 8 2
2 3 2
1 0 1
3
1 3
3 3
2 2

output
17
input
2 5
0 0 0 0 0
1 4 2 3 2
4
1 5
2 2
2 5
2 1

output
9

In the first example, the shortest path goes through the following locations: (1, 1), (2, 1), (3, 1), (3, 2), (3, 3), (2, 3), (1, 3), (2, 3), (3, 3), (2, 3) and (2, 2). The locations in bold show where Mirko made deliveries. The total time for the deliveries is 1+2+1+0+1+2+2+2+1+2+3=17.


Được gửi lên bởi:psetter
Ngày:2009-03-08
Thời gian chạy:1s
Giới hạn mã nguồn:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Ngôn ngữ cho phép:Tất cả ngoại trừ: ERL GOSU JS-RHINO NODEJS PERL6 PYPY RUST SED VB.NET
Nguồn bài:COCI 6th 09

hide comments
2013-06-13 10:57:23 Cottontail Tee
Chật vật lên xuống cả buổi chiều vì quên xét TH 2 địa điểm kế tiếp nhau nằm cùng hàng T_T
2010-10-14 05:13:58 Siêu Nhân Trong Suốt
Time chặt quá :(( . AC bên spoj và phải submit tận 7 lần ở VOJ để mong 1 lần thần may mắn xuất hiện :D

Last edit: 2010-10-14 06:19:05
2009-10-11 13:17:28 TNO
Em thấy khó hiểu test 1 quá
tại sao fải qua ô (3,3) 2 lần khi có thể đi 1 lần qua (3,3) rồi đến ô (2,2)

Last edit: 2009-10-11 13:18:24
2009-05-19 02:43:12 dhkhtn
cai nay phai hoi BTC, nhung ma chac la duoc , xem cai vi du ay.
2009-05-19 02:42:08 Tue Le
Những ô đã đc cung cấp pizza rồi có đc đi vào lại ko ạ?
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.