This page is still under construction.

Parts of this page are still being built. What you see may change.

Building Heights

Time limit2sMemory limit512 MB

Summary
With building 1 at height 0, adjacent heights differing by at most K, and M caps, find the maximum achievable height of any building.
Level

Medium6 of 10

Topics
Greedy, Implementation, Math, Prefix sum
Solved
No attempts yet

Problem

You put up NN new buildings in a row. Number them 1 to NN from the left.

The heights obey these limits.

  • Every building height is a non-negative integer.
  • Building 1 has height 0.
  • Two neighboring buildings differ in height by at most KK.
  • Building XiX_i has height at most TiT_i.

Write a program that finds the height of the tallest building you can put up while every limit holds.

Input

The first line contains NN and KK. (1≤N,K≤1091 \le N, K \le 10^9)

The second line contains MM, the number of buildings that carry a height cap. (0≤M≤min⁡(N,500)0 \le M \le \min(N, 500))

If MM is at least 1, the third line contains X1,X2,…,XMX_1, X_2, \dots, X_M and the fourth line contains T1,T2,…,TMT_1, T_2, \dots, T_M, separated by spaces. (1≤Xi≤N1 \le X_i \le N, 1≤Ti≤1091 \le T_i \le 10^9, Xi<Xi+1X_i < X_{i+1}) If MM is 0, the third and fourth lines are not given.

Output

Print the height of the tallest building you can put up while every limit holds.

Examples3

  1. Example 1

    Input
    10 1
    2
    3 8
    1 1
    
    Expected output
    3
    
  2. Example 2

    Input
    1000000000 1000000000
    0
    
    Expected output
    999999999000000000
    
  3. Example 3

    Input
    20 3
    5
    4 7 13 15 18
    8 22 1 55 42
    
    Expected output
    22