#include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define all(x) begin(x), end(x) template bool chmin(T& x, T y) { return x > y ? (x = y, true) : false; } template bool chmax(T& x, T y) { return x < y ? (x = y, true) : false; } struct io_setup { io_setup() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); } } io_setup; vector solve(vector a, vector b) { int n = a.size(); assert((int)b.size() == n); vector> mna, mnb; auto get_sum = [&](int x, const vector>& vp) { assert(x >= 0); ll res = 0; ll bf = -1; auto itr = lower_bound(vp.begin(), vp.end(), array{x, -1, -1}); assert(itr != vp.end()); if (itr != vp.begin()) { auto bitr = prev(itr); res += (*bitr)[2]; bf = (*bitr)[0]; } res += (x - bf) * (*itr)[1]; return res; }; auto get_val = [&](int x, const vector>& vp) -> ll { assert(x >= 0); return (*lower_bound(vp.begin(), vp.end(), array{x, -1, -1}))[1]; }; vector res(n); rep(i, 0, n) { while (!mna.empty() && mna.back()[1] >= a[i]) mna.pop_back(); while (!mnb.empty() && mnb.back()[1] >= b[i]) mnb.pop_back(); if (mna.empty()) mna.push_back({i, a[i], (i + 1) * a[i]}); else mna.push_back( {i, a[i], mna.back()[2] + (i - mna.back()[0]) * a[i]}); if (mnb.empty()) mnb.push_back({i, b[i], (i + 1) * b[i]}); else mnb.push_back( {i, b[i], mnb.back()[2] + (i - mnb.back()[0]) * b[i]}); if (get_val(0, mnb) >= a[i]) { res[i] = get_sum(i, mna); continue; } if (get_val(0, mna) >= b[i]) { res[i] = get_sum(i, mnb); continue; } auto f = [&](int x) { return get_val(x, mna) < get_val(i - x, mnb); }; int up = i; int dw = 0; assert(f(dw)); assert(!f(up)); while (up - dw > 1) { int md = (up + dw) / 2; if (f(md)) dw = md; else up = md; } res[i] += get_sum(dw, mna); res[i] += get_sum(i - up, mnb); } return res; } int main() { int n; cin >> n; vector a(n), b(n); rep(i, 0, n) cin >> a[i]; rep(i, 0, n) cin >> b[i]; auto c = solve(a, b); rep(i, 0, n) cout << c[i] << " \n"[i == n - 1]; }