CF1201D Treasure Hunting

题目描述

有一个$n*m$的矩阵,行的标号从$1$到$n$,列的标号从$1$到$m$,矩阵中共有$k$个宝藏,第$i$个宝藏的位置为$(r_i,c_i)$。有$q$个安全的列,第$i$个安全的列的编号是$b_i$。 最初你站在矩阵的左下角(也就是$(1,1)$的位置),并且可以向左走(从$(r,c)$到$(r,c-1)$)和向右走(从$(r,c)$到$(r,c+1)$)。同时,你也可是向上走(从$(r,c)$到$(r+1,c)$),但是你必须在安全的列上。由于某些玄学原因,你不可以向下走。 你的任务是收集所有的宝藏,但是你的时间已经不多了,所以你必须走最快的路径。现在你要使用你的人脑大法来计算出最快的路径需要多少时间(经过有宝藏的格子时收集宝藏不消耗时间,只有移动消耗时间)。

输入格式

输出格式

说明/提示

In the first example you should use the second column to go up, collecting in each row treasures from the first column. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1201D/69be72eb65e8a117f939589fc74d8d456faf3fe4.png)In the second example, it is optimal to use the first column to go up. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1201D/127fc8a2ee6c2fe0e6e82a0905c4b0be74ded6e4.png)In the third example, it is optimal to collect the treasure at cell $ (1;6) $ , go up to row $ 2 $ at column $ 6 $ , then collect the treasure at cell $ (2;2) $ , go up to the top row at column $ 1 $ and collect the last treasure at cell $ (3;4) $ . That's a total of $ 15 $ moves. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1201D/63c6a47b6fa91504b3daf0a0a974fd73bb988950.png)