#include <bits/stdc++.h>
using namespace std;
namespace {
using int64 = long long;
const int64 inf = 1e15;
// f(x) = min(max(x + a, b), c)
struct func {
int64 a, b, c;
func() : a(0), b(-inf), c(inf) {}
func(int64 a, int64 b, int64 c) : a(a), b(b), c(c) {}
int64 operator()(int64 x) {
return min(max(x + a, b), c);
}
void apply_add(int64 x) {
a += x, b += x, c += x;
}
void apply_min(int64 x) {
c = min(c, x);
}
void apply_max(int64 x) {
b = max(b, x), c = max(c, x);
}
func operator+(const func &r) {
func ans = *this;
ans.apply_add(r.a);
ans.apply_max(r.b);
ans.apply_min(r.c);
return ans;
}
};
class lazy_segment_tree {
int n;
vector<func> seg, lazy;
void push(int v) {
seg[v] = seg[v] + lazy[v];
if (v < 2 * n) {
lazy[2 * v] = lazy[2 * v] + lazy[v];
lazy[2 * v + 1] = lazy[2 * v + 1] + lazy[v];
}
lazy[v] = func{};
}
void apply(int v, int tl, int tr, int l, int r, func f) {
push(v);
if (tr < l || r < tl) {
return;
}
if (l <= tl && tr <= r) {
lazy[v] = lazy[v] + f;
push(v);
return;
}
int tm = (tl + tr) / 2;
apply(2 * v, tl, tm, l, r, f);
apply(2 * v + 1, tm + 1, tr, l, r, f);
}
void get(int v, int tl, int tr, vector<int> &res) {
push(v);
if (tl == tr) {
res.push_back(seg[v](0));
return;
}
int tm = (tl + tr) / 2;
get(2 * v, tl, tm, res);
get(2 * v + 1, tm + 1, tr, res);
}
public:
lazy_segment_tree(int n) : n(n), seg(4 * n), lazy(4 * n) {}
void add(int l, int r, int64 x) {
apply(1, 0, n - 1, l, r, {x, -inf, inf});
}
void chmin(int l, int r, int64 x) {
apply(1, 0, n - 1, l, r, {0, -inf, x});
}
void chmax(int l, int r, int64 x) {
apply(1, 0, n - 1, l, r, {0, x, inf});
}
vector<int> get() {
vector<int> ans;
get(1, 0, n - 1, ans);
return ans;
}
};
} // namespace
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> v) {
const int n = c.size(), q = v.size();
lazy_segment_tree st(n);
for (int i = 0; i < q; ++i) {
st.add(l[i], r[i], v[i]);
st.chmax(0, n - 1, 0);
st.chmin(0, n - 1, c[0]);
}
return st.get();
}