#include using namespace std; #define all(v) (v).begin(),(v).end() #define pb emplace_back #define rep(i, n) for(int i=0;i<(n);i++) #define foa(e, v) for(auto& e : v) #define dout(a) cout< using pqr = priority_queue, greater>; template inline bool chmax(T1 &a, T2 b) { bool compare = a < b; if(compare) a = b; return compare; } template inline bool chmin(T1 &a, T2 b) { bool compare = a > b; if(compare) a = b; return compare; } template inline T back(std::set &s) { return *s.rbegin(); } template inline T back(std::multiset &s) { return *s.rbegin(); } template inline T pop_back(std::set &s) { auto it = prev(s.end()); T val = *it; s.erase(it); return val; } template inline T pop_back(std::multiset &s) { auto it = prev(s.end()); T val = *it; s.erase(it); return val; } const int dy[8] = {-1, 0, 0, 1, 1, -1, 1, -1}; const int dx[8] = {0, -1, 1, 0, -1, -1, 1, 1}; const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (3LL << 59); const int inf = 1 << 30; const char br = '\n'; void solve() { ll n, s; cin >> n >> s; assert(n <= 5000 and s <= 1000000 and n >= 1 and s >= 1); vector x(n); rep(i, n) cin >> x[i]; rep(i, n - 1) assert(x[i + 1] > x[i]); assert(x[0] > 0 and x.back() <= 1000000); assert(count(all(x), s) == 0); vector w(n); rep(i, n) cin >> w[i]; assert(*max_element(all(w)) <= 1000000 and *min_element(all(w)) > 0); vector dp(n, vector(n + 1, vector(2, INF))); vector sum(n + 1, 0); rep(i, n) sum[i + 1] = sum[i] + w[i]; rep(i, n) { rep(k, 2) dp[i][i + 1][k] = abs(x[i] - s) * sum[n]; } for(int l = n - 1; l >= 0; l --) { for(int r = l + 1; r <= n; r ++) { ll val = sum[n] - sum[r] + sum[l]; if(r < n) { chmin(dp[l][r + 1][1], dp[l][r][0] + val * abs(x[l] - x[r])); chmin(dp[l][r + 1][1], dp[l][r][1] + val * abs(x[r - 1] - x[r])); } if(l > 0) { chmin(dp[l - 1][r][0], dp[l][r][0] + val * abs(x[l] - x[l - 1])); chmin(dp[l - 1][r][0], dp[l][r][1] + val * abs(x[r - 1] - x[l - 1])); } } } cout << min(dp[0][n][0], dp[0][n][1]) << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); int testcase = 1; // cin >> testcase; while(testcase --) solve(); return 0; }