Knight's game X39759


Statement
 

pdf   zip

Consider a chess board with nn rows (indexed 00, 11, \ldots, n1n-1 from top to bottom) and mm columns (indexed 00, 11, \ldots, m1m-1 from left to right). Each position of the board is determined by a pair (r,c)(r, c), where rr is the index of the row and cc is the index of the column.

Let us define the following game. We begin with a knight (see the figure to recall how it moves) at the upper left corner. Given a sequence of goal positions of the board p1p_{1}, p2p_{2}, ..., pkp_{k}, we have to move the knight to goal position p1p_{1} doing knight jumps; from there we have to get to goal position p2p_{2}, doing knight jumps; and so on until getting to the last goal position pkp_{k}. For each goal we reach, we get WW points. But for each knight jump we do, we lose LL points. The match is over when the next goal position cannot be reached, or we decide to stop moving. What is the maximum score that we can get if we play optimally?

image

Input

Input contains different cases, only with integer numbers. Each case starts with nn and mm, followed by WW and LL. Finally, we have kk and the kk goal positions represented by pairs of integers rir_{i} cic_{i} separated by blank spaces, where 0ri<n0 \leq r_{i} < n and 0ci<m0 \leq c_{i} < m. It holds that 2n,m50002 \leq n, m \leq 5000, that nm104n \cdot m \leq 10^{4}, that 1W,L1001 \leq W, L \leq 100 and that 1kmin(nm,1000)1 \leq k \leq \min(n \cdot m, 1000).

Output

For each case, write in a line the maximum score that can be achieved if we play optimally.

Public test cases
  • Input

    3 3
    10 1
    1
    2 1
    
    3 3
    10 1
    2
    0 2   1 0
    
    3 3
    10 1
    4
    0 0   2 1   0 2   0 2
    
    3 3
    10 1
    1
    1 1
    
    3 3
    10 1
    2
    2 1   1 1
    
    2 13
    7 3
    3
    0 4   1 10   0 12
    
    8 8
    25 3
    4
    5 6   7 6   3 4   2 0
    

    Output

    9
    17
    38
    0
    9
    3
    64
    
  • Information
    Author
    Enric Rodríguez
    Language
    English
    Translator
    Enric Rodríguez
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++