#include <bits/stdc++.h>   using namespace std;   const int N = 1e4 + 123;   int n, c;   int made[N], preparedToSell[N];   long long goods[N], limit[N];   long long toSell[N], toSend[N];   void readData() {     cin >> n >> c;     for (int i = 1; i <= n; ++i) {       cin >> made[i];     }     for (int i = 1; i <= n; ++i) {       cin >> preparedToSell[i];     }   }   void generateData() {     n = rand() % 6;     c = rand() % 4;     for (int i = 1; i <= n; ++i) {       made[i] = rand() % 6;     }     for (int i = 1; i <= n; ++i) {       preparedToSell[i] = rand() % 6;     }   }   void writeLog() {     cout << n <<     << c <<   n ;     for (int i = 1; i <= n; ++i) {       cout << made[i] <<    ;     }     cout <<   n ;     for (int i = 1; i <= n; ++i) {       cout << preparedToSell[i] <<    ;     }     cout <<   n ;   }   long long capacity[102][102], flow[102][102], excess[102];   int height[102];   set<int> overfilled, lowerChildren[102], higherChildren[102];   void push(int u, int v) {     long long additionalFlow = min(excess[u], capacity[u][v] - flow[u][v]);     if (!excess[v] && additionalFlow && v != n + 1) {       overfilled.insert(v);     }     if (excess[u] == additionalFlow) {       overfilled.erase(u);     }     flow[u][v] += additionalFlow;     flow[v][u] -= additionalFlow;     excess[u] -= additionalFlow;     excess[v] += additionalFlow;     if (flow[u][v] == capacity[u][v]) {       capacity[u][v] = 0;       lowerChildren[u].erase(v);       higherChildren[v].insert(u);     }   }   void relabel(int u) {     if (u == 0) {       overfilled.erase(0);       return;     }     int minHeight = height[0];     for (int v : higherChildren[u]) {       minHeight = min(minHeight, height[v]);     }     height[u] = minHeight + 1;     for (int i = 0; i <= n + 1; ++i) {       if (height[i] >= height[u]) continue;       auto it = higherChildren[u].find(i);       if (it != higherChildren[u].end()) {         higherChildren[u].erase(it);         lowerChildren[u].insert(i);       }     }     for (int i = 0; i <= n + 1; ++i) {       auto it = lowerChildren[i].find(u);       if (it != lowerChildren[i].end()) {         lowerChildren[i].erase(it);         higherChildren[i].insert(u);       }     }   }   long long flowSolve() {     memset(capacity, 0, sizeof(capacity));     memset(flow, 0, sizeof(flow));     memset(excess, 0, sizeof(excess));     overfilled.clear();     for (auto& s : higherChildren) {       s.clear();     }     for (auto& s : lowerChildren) {       s.clear();     }     for (int i = 1; i <= n; ++i) {       capacity[0][i] = made[i];       lowerChildren[0].insert(i);       for (int j = i + 1; j <= n; ++j) {         capacity[i][j] = c;         higherChildren[i].insert(j);       }       capacity[i][n + 1] = preparedToSell[i];       higherChildren[i].insert(n + 1);     }     excess[0] = 1e18;     height[0] = n + 2;     overfilled.insert(0);     while (!overfilled.empty()) {       int u = *(overfilled.begin());       if (!lowerChildren[u].empty()) {         int v = *(lowerChildren[u].begin());         push(u, v);       } else {         relabel(u);       }     }     return excess[n + 1];   }   long long table[2][N];   long long solve() {     long long cll = c;     for (int k = 1; k <= n; ++k) {       long long* oldRow = table[k & 1];       long long* newRow = table[!(k & 1)];       newRow[0] = oldRow[0] + made[k];       for (int i = 1; i < k; ++i) {         newRow[i] =             min(oldRow[i] + made[k] + i * cll, oldRow[i - 1] + preparedToSell[k]);       }       newRow[k] = oldRow[k - 1] + preparedToSell[k];     }     long long* lastRow = table[!(n & 1)];     long long result = lastRow[0];     for (int i = 1; i <= n; ++i) {       result = min(result, lastRow[i]);     }     return result;   }   int main() {     ios_base::sync_with_stdio(false);     srand(time(0));     readData();     cout << solve();     return 0;   }