#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define MOD 1000000007
#define INF (1<<29)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

Int n, d;
Int a[216000];
Int sums[216000];
Int difs[216000];
Int res;
int main(){
  cin >> n >> d;
  for(int i = 0;i < n;i++){
    cin >> a[i];
    sums[i] = a[i] + i * d;
    difs[i] = a[i] - i * d;
    res += 2 * a[i];
  }
  res += (n-1)*d;
  res -= a[0] + a[n-1];
  for(int i = 1;i < n;i++)difs[i] = min(difs[i], difs[i-1]);
  for(int i = n-2;i >= 0;i--)sums[i] = min(sums[i], sums[i+1]);
  for(int i = 1;i < n-1;i++){
    //    cout << difs[i] << " " << sums[i] << endl;
    Int ltmp = -a[i] + i*d + difs[i];
    Int rtmp = -a[i] - i*d + sums[i];
    //    cout << ltmp << " " << rtmp << endl;
    res += min(ltmp, rtmp);
  }

  cout << res << endl; 
  return 0;
}
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,i,j,mx,t,x,ans,f[19][19],a[19],b[19];
char s[101];
int main(){
	scanf("%lld",&n);
	scanf("%s",s+1);
	mx=1ll<<n;mx--;
	for(t=0;t<=mx;t++){
		a[0]=b[0]=0;
		for(i=n*2;i>n;i--){
			x=n*2-i;
			if(t&(1ll<<x))a[++a[0]]=s[i]-'a';
			 else b[++b[0]]=s[i]-'a';
		}
		memset(f,0,sizeof(f));
		f[0][0]=1;
		for(i=1;i<=n;i++){
			for(j=0;j<=min(a[0],i-1);j++){
				if(i-1-j>b[0])continue;
				if(j<a[0]&&s[i]-'a'==a[j+1])f[i][j+1]+=f[i-1][j];
				if(i-j<=b[0]&&s[i]-'a'==b[i-j])f[i][j]+=f[i-1][j];
			}
		}
		ans+=f[n][a[0]];
	}
	printf("%lld",ans);
}