CF366E.Dima and Magic Guitar

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Dima loves Inna very much. He decided to write a song for her. Dima has a magic guitar with nn strings and mm frets. Dima makes the guitar produce sounds like that: to play a note, he needs to hold one of the strings on one of the frets and then pull the string. When Dima pulls the ii -th string holding it on the jj -th fret the guitar produces a note, let's denote it as aija_{ij} . We know that Dima's guitar can produce kk distinct notes. It is possible that some notes can be produced in multiple ways. In other words, it is possible that aij=apqa_{ij}=a_{pq} at (i,j)(p,q)(i,j)≠(p,q) .

Dima has already written a song — a sequence of ss notes. In order to play the song, you need to consecutively produce the notes from the song on the guitar. You can produce each note in any available way. Dima understood that there are many ways to play a song and he wants to play it so as to make the song look as complicated as possible (try to act like Cobein).

We'll represent a way to play a song as a sequence of pairs (xi,yi)(x_{i},y_{i}) (1<=i<=s)(1<=i<=s) , such that the xix_{i} -th string on the yiy_{i} -th fret produces the ii -th note from the song. The complexity of moving between pairs (x1,y1)(x_{1},y_{1}) and (x2,y2)(x_{2},y_{2}) equals + . The complexity of a way to play a song is the maximum of complexities of moving between adjacent pairs.

Help Dima determine the maximum complexity of the way to play his song! The guy's gotta look cool!

输入格式

The first line of the input contains four integers nn , mm , kk and ss (1<=n,m<=2000,1<=k<=9,2<=s<=105)(1<=n,m<=2000,1<=k<=9,2<=s<=10^{5}) .

Then follow nn lines, each containing mm integers aija_{ij} (1<=aij<=k)(1<=a_{ij}<=k) . The number in the ii -th row and the jj -th column ( aija_{ij} ) means a note that the guitar produces on the ii -th string and the jj -th fret.

The last line of the input contains ss integers qiq_{i} (1<=qi<=k)(1<=q_{i}<=k) — the sequence of notes of the song.

输出格式

In a single line print a single number — the maximum possible complexity of the song.

输入输出样例

  • 输入#1

    4 6 5 7
    3 1 2 2 3 1
    3 2 2 2 5 5
    4 2 2 2 5 3
    3 2 2 1 4 3
    2 3 1 4 1 5 1
    

    输出#1

    8
    
  • 输入#2

    4 4 9 5
    4 7 9 5
    1 2 1 7
    8 3 4 9
    5 7 7 2
    7 1 9 2 5
    

    输出#2

    4
    
首页