#include #define rep(i,s,n) for (int i = (int)(s); i < (int)(n); i++) #define reb(i,s,n) for (int i = (int)(n)-1; i >= (int)(s); i--) #define all(v) begin(v), end(v) using namespace std; using ll = long long; bool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; } bool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; } template ostream &operator<<(ostream &os, const vector &a){ if (a.empty()) return os; os << a.front(); for (auto e : a | views::drop(1)){ os << ' ' << e; } return os; } void dump(auto ...vs){ ((cout << vs << ' '), ...) << endl; } #include struct S { ll sum; int len; }; S op(S a, S b){ S c; c.sum = a.sum + b.sum; c.len = a.len + b.len; return c; } S e(){ return S{0,0}; } S mapping(ll f, S x){ x.sum += x.len * f; return x; } ll composition(ll f, ll g){ return f + g; } ll ident(){ return 0; } void solve(){ int n; cin >> n; vector a(n), b(n); rep(i,0,n) cin >> a[i]; rep(i,0,n) cin >> b[i]; using dty = atcoder::lazy_segtree; dty aseg(vector(n,S{0,1})), bseg(vector(n,S{0,1})); stack ast, bst; auto push = [&](int i, vector &ab, dty &seg, stack &st){ int ir = i; ll val = ab[i]; while (!st.empty() && ab[st.top()] > ab[i]){ int pos = st.top(); st.pop(); seg.apply(pos+1,ir,-val); ir = pos+1; val = ab[pos]; } if (!st.empty()){ int pos = st.top(); seg.apply(pos+1,ir,-val); seg.apply(pos+1,i+1,ab[i]); } else { seg.apply(0,ir,-val); seg.apply(0,i+1,ab[i]); } st.push(i); }; vector c(n); rep(i,0,n){ push(i,a,aseg,ast); push(i,b,bseg,bst); int le = 0, ri = i+2; while (ri - le > 1){ int md = midpoint(le,ri); if (aseg.get(md-1).sum < bseg.get(i-md+1).sum){ le = md; } else { ri = md; } } c[i] = aseg.prod(0,le).sum + bseg.prod(0,i+1-le).sum; } cout << c << '\n'; } int main(){ cin.tie(0)->sync_with_stdio(0); solve(); }