Yaroslav is playing a game called "Time". The game has a timer showing the lifespan he's got left. As soon as the timer shows 0, Yaroslav's character dies and the game ends. Also, the game has n clock stations, station number i is at point (x_{i}, y_{i}) of the plane. As the player visits station number i, he increases the current time on his timer by a_{i}. The stations are for one-time use only, so if the player visits some station another time, the time on his timer won't grow.

A player spends d·dist time units to move between stations, where dist is the distance the player has covered and d is some constant. The distance between stations i and j is determined as |x_{i} - x_{j}| + |y_{i} - y_{j}|.

Initially, the player is at station number 1, and the player has strictly more than zero and strictly less than one units of time. At station number 1 one unit of money can increase the time on the timer by one time unit (you can buy only integer number of time units).

Now Yaroslav is wondering, how much money he needs to get to station n. Help Yaroslav. Consider the time to buy and to increase the timer value negligibly small.


-----Input-----

The first line contains integers n and d (3 ≤ n ≤ 100, 10^3 ≤ d ≤ 10^5) — the number of stations and the constant from the statement.

The second line contains n - 2 integers: a_2, a_3, ..., a_{n} - 1 (1 ≤ a_{i} ≤ 10^3). The next n lines contain the coordinates of the stations. The i-th of them contains two integers x_{i}, y_{i} (-100 ≤ x_{i}, y_{i} ≤ 100).

It is guaranteed that no two stations are located at the same point.


-----Output-----

In a single line print an integer — the answer to the problem.


-----Examples-----
Input
3 1000
1000
0 0
0 1
0 3

Output
2000

Input
3 1000
1000
1 0
1 1
1 2

Output
1000

Difficulty: HARD
Tags: Sorting

Graph algorithms

Shortest paths
Skill types: Sorting

Starter code:

Solutions:
from sys import stdin
from math import inf

def readline():
	return list(map(int, stdin.readline().strip().split()))

def dijkstra():
	(n, d) = readline()
	a = [0] + list(readline()) + [0]
	x = [0] * n
	y = [0] * n
	for i in range(n):
		(x[i], y[i]) = readline()
	lower_cost = [inf] * n
	lower_cost[0] = 0
	visited = [False] * n
	for i in range(n - 1):
		lower_value = inf
		position = 0
		for j in range(n):
			if not visited[j] and lower_value > lower_cost[j]:
				lower_value = lower_cost[j]
				position = j
		visited[position] = True
		for k in range(n):
			if not visited[k]:
				diff = lower_cost[position] + d * (abs(x[k] - x[position]) + abs(y[k] - y[position])) - a[position]
				if lower_cost[k] > diff:
					lower_cost[k] = diff
	return lower_cost[-1]

def __starting_point():
	print(dijkstra())
__starting_point()


f = lambda : list(map(int, input().split()))
(n, d) = f()
a = [0] + f() + [0]
p = [f() for i in range(n)]
r = list(range(n))
s = [[d * (abs(p[i][0] - p[j][0]) + abs(p[i][1] - p[j][1])) - a[j] * (i != j) for j in r] for i in r]
for k in r:
	s = [[min(s[i][j], s[i][k] + s[k][j]) for i in r] for j in r]
print(s[-1][0])


from sys import stdin, stdout
from math import inf

def main():
	(n, d) = readline()
	a = [0] + list(readline()) + [0]
	x = [0] * n
	y = [0] * n
	for i in range(n):
		(x[i], y[i]) = readline()
	return dijkstra(n, d, a, x, y)

def readline():
	return list(map(int, stdin.readline().strip().split()))

def dijkstra(n, d, a, x, y):
	lower_cost = [inf] * n
	lower_cost[0] = 0
	visited = [False] * n
	for i in range(n - 1):
		position = minimum(n, visited, lower_cost)
		if position == n - 1:
			break
		visited[position] = True
		for k in range(n):
			if not visited[k]:
				diff = lower_cost[position] + d * (abs(x[k] - x[position]) + abs(y[k] - y[position])) - a[position]
				if lower_cost[k] > diff:
					lower_cost[k] = diff
	return lower_cost[-1]

def minimum(n, visited, lower_cost):
	lower_value = inf
	position = 0
	for j in range(n):
		if not visited[j] and lower_value > lower_cost[j]:
			lower_value = lower_cost[j]
			position = j
	return position

def __starting_point():
	stdout.write(''.join(str(main()) + '\n'))
__starting_point()


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return list(map(int, minp().split()))

def solve():
	(n, dc) = mints()
	a = list(mints())
	a.append(0)
	x = [0] * n
	y = [0] * n
	for i in range(n):
		(x[i], y[i]) = mints()
	d = [1 << 30] * n
	d[0] = 0
	was = [False] * n
	for i in range(n):
		m = 1 << 30
		mi = 0
		for j in range(n):
			if not was[j] and m > d[j]:
				m = d[j]
				mi = j
		j = mi
		was[j] = True
		for k in range(n):
			if not was[k]:
				dd = d[j] + (abs(x[k] - x[j]) + abs(y[k] - y[j])) * dc
				dd -= a[k - 1]
				if d[k] > dd:
					d[k] = dd
	print(d[-1])
solve()


(n, d) = list(map(int, input().split()))
a = [0] + list(map(int, input().split())) + [0]
X = []
Y = []
for i in range(n):
	(x, y) = list(map(int, input().split()))
	X.append(x)
	Y.append(y)
mon = [-1] * n
mon[0] = 0
Z = 0
while Z == 0:
	Z = 1
	for i in range(n):
		for j in range(1, n):
			if i != j and mon[i] != -1:
				costo = mon[i] + (abs(X[i] - X[j]) + abs(Y[i] - Y[j])) * d - a[j]
				if mon[j] == -1 or costo < mon[j]:
					mon[j] = costo
					Z = 0
print(mon[-1])


R = lambda : map(int, input().split())
(n, d) = R()
a = [0] + list(R()) + [0]
x = []
y = []
INF = float('inf')

def distance(x1, y1, x2, y2):
	return (abs(x1 - x2) + abs(y1 - y2)) * d
for i in range(n):
	(xi, yi) = R()
	x += [xi]
	y += [yi]
b = [INF] * n
b[0] = 0
c = True
while c:
	c = False
	for i in range(n):
		for j in range(1, n):
			if i != j and b[i] != -1:
				t = b[i] + distance(x[i], y[i], x[j], y[j]) - a[j]
				if t < b[j]:
					b[j] = t
					c = True
print(b[-1])


(n, d) = map(int, input().split())
a = [0] + list(map(int, input().split())) + [0]
x = []
y = []
for i in range(n):
	(xx, yy) = map(int, input().split())
	x += [xx]
	y += [yy]
b = [-1] * n
b[0] = 0
c = True
while c:
	c = False
	for i in range(n):
		for j in range(1, n):
			if i != j and b[i] != -1:
				t = b[i] + (abs(x[i] - x[j]) + abs(y[i] - y[j])) * d - a[j]
				if b[j] == -1 or t < b[j]:
					b[j] = t
					c = True
print(b[-1])


f = lambda : list(map(int, input().split()))
(n, d) = f()
r = range(n)
a = [0] + f() + [0]
p = [f() for i in r]
s = [1000000000.0] * n
(q, s[0]) = (1, 0)
while q:
	q = 0
	for i in r:
		for j in r:
			if i != j:
				t = s[i] + (abs(p[i][0] - p[j][0]) + abs(p[i][1] - p[j][1])) * d - a[j]
				if t < s[j]:
					(q, s[j]) = (1, t)
print(s[-1])


R = lambda : map(int, input().split())
(n, d) = R()
a = [0] + list(R()) + [0]
x = []
y = []
INF = float('inf')
for i in range(n):
	(xi, yi) = R()
	x += [xi]
	y += [yi]
b = [INF] * n
b[0] = 0
c = True
while c:
	c = False
	for i in range(n):
		for j in range(1, n):
			if i != j and b[i] != -1:
				t = b[i] + (abs(x[i] - x[j]) + abs(y[i] - y[j])) * d - a[j]
				if t < b[j]:
					b[j] = t
					c = True
print(b[-1])