This page is still under construction.

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

Moving the Light Stone

Interview

Time limit1sMemory limit256 MB

Summary
Pick drag or carry for each of N segments, paying a switch cost K whenever the choice changes, to minimize the total.
Level

Medium5 of 10

Topics
Dynamic programming, Greedy, Array, Implementation
Solved
No attempts yet

Problem

At the center of the kingdom of Polymath stands the Light Stone, which supplies light to the people. The Light Stone can supply light to places up to a distance LL away. Because the Light Stone is growing weaker, the people want to move it so that every house can be supplied with light.

The destination of the Light Stone is already decided. The problem is that moving the Light Stone costs a lot. To minimize the cost, they must decide for each part of the way whether to drag the Light Stone or carry it.

Divide the route over which the Light Stone is moved into NN segments. In segment ii, dragging the Light Stone costs AiA_i, and carrying it costs BiB_i. Also, each time the way of moving the Light Stone changes, an extra cost KK is incurred. (There is no extra cost KK at the very beginning or the very end.)

For example, let N=3N=3, K=2K=2, A=[1,7,3]A=[1, 7, 3], B=[9,3,4]B=[9, 3, 4]. Dragging the stone over the entire route costs 1+7+3=111+7+3=11. On the other hand, dragging it in the first segment and carrying it in the second and third segments costs 1+2+3+4=101+2+3+4=10, so the stone can be moved for less.

Input

The first line gives the number of segments NN and the cost KK of changing the way of moving.

The second line gives the costs A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N of dragging the Light Stone.

The third line gives the costs B1,B2,⋯ ,BNB_1, B_2, \cdots, B_N of carrying the Light Stone.

Output

Print the minimum cost of moving the Light Stone.

Constraints

  • 1≤N≤2×1051 \le N \le 2 \times 10^5
  • 0≤K≤1090 \le K \le 10^9
  • 1≤Ai,Bi≤1091 \le A_i, B_i \le 10^9

Examples1

  1. Example 1

    Input
    3 2
    1 7 3
    9 3 4
    
    Expected output
    10