Buggy python submission (verdict: Runtime Error):
import itertools
from operator import itemgetter
class UnionFind():
    def __init__(self, num):
        self.par = [-1 for _ in range(num)]
    
    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            x = self.par[x]
            return self.find(x)
    
    def union(self, x, y):
        rx = self.find(x)
        ry = self.find(y)
        if rx != ry:
            if self.par[rx] < self.par[ry]:
                self.par[ry] = rx
            if self.par[rx] > self.par[ry]:
                self.par[rx] = ry
            else:
                self.par[rx] -= 1
                self.par[ry] = rx
        return

def inpl(): return list(map(int, input().split()))
def cost(x, y):
    return D * abs(x - y) + A[x] + A[y]
N, D = inpl()
A = inpl()
minx = A.index(min(A))
Right = [(v - i*D, i) for i, v in enumerate(A)]
Left = [(v + i*D, i) for i, v in enumerate(A)][::-1]
Right = list(itertools.accumulate(Right[minx:], min))
Left = list(itertools.accumulate(Left[N - 1 - minx:], min))
hub = [0]
for _, i in itertools.chain(Left[::-1],Right,(0, N-1)):
    if hub[-1] != i:
        hub.append(i)
edge = [(cost(hub[i], k), hub[i], k) for i in range(len(hub)-1) for k in range(hub[i]+1, hub[i+1])]\
+ [(cost(hub[i+1], k), hub[i+1], k) for i in range(len(hub)-1) for k in range(hub[i], hub[i+1])]
edge.sort(reverse = True)
Uf = UnionFind(N)
ctr = 1
ans = 0
while ctr < N:
    v, i, j = edge.pop()
    if Uf.find(i) != Uf.find(j):
        Uf.union(i, j)
        ctr += 1
        ans += v
print(ans)

Fixed submission:
from operator import itemgetter
import itertools
def inpl(): return list(map(int, input().split()))
def cost(x, y):
    if x == y:
        return float('inf')
    return D * abs(x - y) + A[x] + A[y]
N, D = inpl()
A = inpl()
Right = [(v - i*D, i) for i, v in enumerate(A)]
Left = [(v + i*D, i) for i, v in enumerate(A)][::-1]
Right = list(map(itemgetter(1), itertools.accumulate(Right, min)))
Left = list(map(itemgetter(1), itertools.accumulate(Left, min)))[::-1]
ans = 0
hub = []
for i, (l, r) in enumerate(zip(Left, Right)) :
    if i == l == r:
        hub.append(i)
    else:
        ans += min(cost(i, r), cost(i, l))
print(ans + sum([cost(i, j) for i, j in zip(hub, hub[1:])]))

Buggy python submission (verdict: Runtime Error):
import heapq
import itertools
from operator import itemgetter
from collections import defaultdict
class UnionFind():
    def __init__(self, num):
        self.par = [-1 for _ in range(num)]
    
    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            x = self.par[x]
            return self.find(x)
    
    def union(self, x, y):
        rx = self.find(x)
        ry = self.find(y)
        if rx != ry:
            if self.par[rx] < self.par[ry]:
                self.par[ry] = rx
            if self.par[rx] > self.par[ry]:
                self.par[rx] = ry
            else:
                self.par[rx] -= 1
                self.par[ry] = rx
        return

def inpl(): return list(map(int, input().split()))
def cost(x, y):
    return D * abs(x - y) + A[x] + A[y]
N, D = inpl()
A = inpl()
minx = A.index(min(A))
Right = [(v - i*D, i) for i, v in enumerate(A)]
Left = [(v + i*D, i) for i, v in enumerate(A)][::-1]
Right = itertools.accumulate(Right[minx:], min)
Left = itertools.accumulate(Left[N - 1 - minx:], min)
hub = set(map(itemgetter(1), Right))
hub.update(map(itemgetter(1), Left))
hub.add(0)
hub.add(N-1)
hub = list(sorted(hub))
edge = [(cost(hub[i], k), hub[i], k) for i in range(len(hub)-1) for k in range(hub[i], hub[i+1])]\
+ [(cost(hub[i+1], k), hub[i+1], k) for i in range(len(hub)-1) for k in range(hub[i], hub[i+1])]
edge.sort(reverse = True)
Uf = UnionFind(N)
ctr = 1
ans = 0
print(edge)
while ctr < N:
    v, i, j = edge.pop()
    if Uf.find(i) != Uf.find(j):
        Uf.union(i, j)
        ctr += 1
        ans += v
print(ans)

Fixed submission:
from operator import itemgetter
import itertools
def inpl(): return list(map(int, input().split()))
def cost(x, y):
    if x == y:
        return float('inf')
    return D * abs(x - y) + A[x] + A[y]
N, D = inpl()
A = inpl()
Right = [(v - i*D, i) for i, v in enumerate(A)]
Left = [(v + i*D, i) for i, v in enumerate(A)][::-1]
Right = list(map(itemgetter(1), itertools.accumulate(Right, min)))
Left = list(map(itemgetter(1), itertools.accumulate(Left, min)))[::-1]
ans = 0
hub = []
for i, (l, r) in enumerate(zip(Left, Right)) :
    if i == l == r:
        hub.append(i)
    else:
        ans += min(cost(i, r), cost(i, l))
print(ans + sum([cost(i, j) for i, j in zip(hub, hub[1:])]))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import sys
def input(): return sys.stdin.readline()
from collections import defaultdict
class UnionFind():
    def __init__(self, num):
        self.par = [-1 for _ in range(num)]
    
    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            x = self.par[x]
            return self.find(x)
    
    def union(self, x, y):
        rx = self.find(x)
        ry = self.find(y)
        if rx != ry:
            if self.par[rx] < self.par[ry]:
                self.par[ry] = rx
            if self.par[rx] > self.par[ry]:
                self.par[rx] = ry
            else:
                self.par[rx] -= 1
                self.par[ry] = rx
        return
def f(nb, nc):
    if nb != N-1:
        return (nb+1, nc)
    return 0
def g(nb, nc):
    if nc != N-1:
        return (nb, nc+1)
    return 0
def inpl(): return [int(i) for i in input().split()]

N, D = inpl()
A = inpl()
B = [(i*D + v, i) for i, v in enumerate(A, 1)]
C = [(-i*D + v, i) for i, v in enumerate(A, 1)]
B.sort()
C.sort()
Z = UnionFind(N+1)
Q = []
ctr = 0
ans = 0
H = defaultdict(lambda: False)
heapq.heappush(Q, (C[0][0] + B[0][0], (0, 0)))
H[(0, 0)] = True
H[0] = True
while ctr < N-1:
    v, (bi, ci) = heapq.heappop(Q)
    _, bj = B[bi]
    _, cj = C[ci]
    if not H[f(bi, ci)]:
        H[(bi+1, ci)] = True
        heapq.heappush(Q, (B[bi+1][0] + C[ci][0], (bi+1, ci)))
    if not H[g(bi, ci)]:
        H[(bi, ci+1)] = True
        heapq.heappush(Q, (B[bi][0] + C[ci+1][0], (bi, ci+1)))
    if bj <= cj:
        continue
    if Z.find(bj) == Z.find(cj):
        continue
    Z.union(bj, cj)
    ans += v
    ctr += 1
print(ans)

Fixed submission:
from operator import itemgetter
import itertools
def inpl(): return list(map(int, input().split()))
def cost(x, y):
    if x == y:
        return float('inf')
    return D * abs(x - y) + A[x] + A[y]
N, D = inpl()
A = inpl()
Right = [(v - i*D, i) for i, v in enumerate(A)]
Left = [(v + i*D, i) for i, v in enumerate(A)][::-1]
Right = list(map(itemgetter(1), itertools.accumulate(Right, min)))
Left = list(map(itemgetter(1), itertools.accumulate(Left, min)))[::-1]
ans = 0
hub = []
for i, (l, r) in enumerate(zip(Left, Right)) :
    if i == l == r:
        hub.append(i)
    else:
        ans += min(cost(i, r), cost(i, l))
print(ans + sum([cost(i, j) for i, j in zip(hub, hub[1:])]))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
from collections import defaultdict
class UnionFind():
    def __init__(self, num):
        self.par = [-1 for _ in range(num)]
    
    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            x = self.par[x]
            return self.find(x)
    
    def union(self, x, y):
        rx = self.find(x)
        ry = self.find(y)
        if rx != ry:
            if self.par[rx] < self.par[ry]:
                self.par[ry] = rx
            if self.par[rx] > self.par[ry]:
                self.par[rx] = ry
            else:
                self.par[rx] -= 1
                self.par[ry] = rx
        return
def f(nb, nc):
    if nb != N-1:
        return (nb+1, nc)
    return 0
def g(nb, nc):
    if nc != N-1:
        return (nb, nc+1)
    return 0
def inpl(): return [int(i) for i in input().split()]

N, D = inpl()
A = inpl()
B = [(i*D + v, i) for i, v in enumerate(A, 1)]
C = [(-i*D + v, i) for i, v in enumerate(A, 1)]
B.sort()
C.sort()
Z = UnionFind(N+1)
Q = []
ctr = 0
ans = 0
H = defaultdict(lambda: False)
heapq.heappush(Q, (C[0][0] + B[0][0], (0, 0)))
H[(0, 0)] = True
H[0] = True
while ctr < N-1:
    v, (bi, ci) = heapq.heappop(Q)
    _, bj = B[bi]
    _, cj = C[ci]
    if not H[f(bi, ci)]:
        H[(bi+1, ci)] = True
        heapq.heappush(Q, (B[bi+1][0] + C[ci][0], (bi+1, ci)))
    if not H[g(bi, ci)]:
        H[(bi, ci+1)] = True
        heapq.heappush(Q, (B[bi][0] + C[ci+1][0], (bi, ci+1)))
    if bj <= cj:
        continue
    if Z.find(bj) == Z.find(cj):
        continue
    Z.union(bj, cj)
    ans += v
    ctr += 1
print(ans)

Fixed submission:
from operator import itemgetter
import itertools
def inpl(): return list(map(int, input().split()))
def cost(x, y):
    if x == y:
        return float('inf')
    return D * abs(x - y) + A[x] + A[y]
N, D = inpl()
A = inpl()
Right = [(v - i*D, i) for i, v in enumerate(A)]
Left = [(v + i*D, i) for i, v in enumerate(A)][::-1]
Right = list(map(itemgetter(1), itertools.accumulate(Right, min)))
Left = list(map(itemgetter(1), itertools.accumulate(Left, min)))[::-1]
ans = 0
hub = []
for i, (l, r) in enumerate(zip(Left, Right)) :
    if i == l == r:
        hub.append(i)
    else:
        ans += min(cost(i, r), cost(i, l))
print(ans + sum([cost(i, j) for i, j in zip(hub, hub[1:])]))