inlineintrd(){ int a = 1, b = 0; char c = getchar(); while (!isdigit(c)) a = c == '-' ? 0 : 1, c = getchar(); while (isdigit(c)) b = b * 10 + c - '0', c = getchar(); return a ? b : -b; }
constint N = 1e6 + 233; typedeflonglong ll; int n, m, a[N], w[N]; ll s[N], ans;
intmain(){ n = rd(), m = rd(); for (int i = 1; i <= n; ++i) a[i] = rd(); for (int i = 1; i <= n; ++i) w[i] = rd(); for (int i = n; i; --i) s[i] = s[i + 1] + w[i];
for (int i = 1, now = 0; i <= n; ++i) { if (s[i] > 0) { ans += m * s[i]; que.push({ m * 2, s[i] }); now += m; } else { ans -= m * s[i]; now -= m; } int c = now - a[i]; now = std::min(now, a[i]); while (c > 0 && !que.empty()) { Node node = que.top(); que.pop(); int tmp = std::min(c, node.flow); ans -= tmp * node.cost; c -= tmp; node.flow -= tmp; if (node.flow) que.push(node); } }