Problem:
E: 総和の切り取り
問題
えびちゃんは数列
(a_1, a_2, ..., a_n)
を持っています。
えびちゃんは（空でもよい）部分配列の総和の最大値が気になるので、それを求めてください。
ここで、部分配列とは連続する部分列を指します。なお、空列の総和は
0
とします。
さらに、えびちゃんがこの数列を
q
回書き換えるので、各書き換えの直後にこの値を求めてください。
入力形式
n
q
a_1
a_2
...
a_n
k_1
x_1
...
k_q
x_q
各
k_j
,
x_j
(
1 \leq j \leq q
) は、
a_{k_j}
を
x_j
に書き換えることを意味します。
各書き換えは独立ではなく、その後の処理において配列は書き換えられたままであることに注意してください。
制約
1\leq n\leq 10^5
1\leq q\leq 10^5
1\leq k_j\leq n
|a_i|, |x_j| \leq 10^9
出力形式
q+1
行出力してください。
1
行目には書き換えを行う前の、
1+i
行目には
i
回目の書き換えの直後の部分配列の総和の最大値を出力してください。
入力例1
5 2
1 2 -3 4 -5
3 3
2 -6
出力例1
4
10
7
部分配列
(a_l, …, a_r)
を
a[l, r]
と表すことにすると、書き換え前は
a[1, 4]
および
a[4, 4]
の総和が
4
で、これが最大です。
1
回目の書き換えの後、数列は
(1, 2, 3, 4, -5)
となり、総和の最大値は
a[1, 4]
の
10
です。
2
回目の書き換えの後、数列は
(1, -6, 3, 4, -5)
となり、総和の最大値は
a[3, 4]
の
7
です。
入力例2
3 1
-1 -2 -3
1 1
出力例2
0
1
空列の総和は
0
であり、書き換え前はそれが最大です。

Submitted solutions:

code_snippet: #define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/3111"


#include <algorithm>
#include <concepts>
#include <optional>

namespace zawa {

template <std::totally_ordered T>
class SubarraySumMax {
public:

    SubarraySumMax() = default;

    explicit SubarraySumMax(T v) : m_ans{v}, m_sum{v}, m_pref{v}, m_suf{v} {}

    SubarraySumMax(T ans, T sum, T pref, T suf) : m_ans{ans}, m_sum{sum}, m_pref{pref}, m_suf{suf} {}

    inline T ans() const {
        return m_ans;
    }

    inline T sum() const {
        return m_sum;
    }

    inline T pref() const {
        return m_pref;
    }

    inline T suf() const {
        return m_suf;
    }

    static SubarraySumMax<T> merge(const SubarraySumMax<T>& lhs, const SubarraySumMax<T>& rhs) {
        T sum = lhs.sum() + rhs.sum();
        T pref = std::max(lhs.pref(), lhs.sum() + rhs.pref());
        T suf = std::max(rhs.suf(), lhs.suf() + rhs.sum());
        T ans = std::max({lhs.ans(), rhs.ans(), lhs.suf() + rhs.pref() });
        return {ans, sum, pref, suf};
    }

private:

    T m_ans{}, m_sum{}, m_pref{}, m_suf{};

};

template <std::totally_ordered T>
struct SubarraySumMaxMonoid {

    using Element = std::optional<SubarraySumMax<T>>;

    static Element identity() {
        return std::nullopt;
    }

    static Element operation(const Element& L, const Element& R) {
        if (!L) return R;
        if (!R) return L;
        return Element::value_type::merge(L.value(), R.value());
    }

    static Element convert(T v) {
        return Element{v};
    }
};

} // namespace zawa


#include <cstdint>
#include <cstddef>

namespace zawa {

using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

using usize = std::size_t;

} // namespace zawa



namespace zawa {

namespace concepts {

template <class T>
concept Semigroup = requires {
    typename T::Element;
    { T::operation(std::declval<typename T::Element>(), std::declval<typename T::Element>()) } -> std::same_as<typename T::Element>;
};

} // namespace concepts

} // namespace zawa


namespace zawa {

namespace concepts {

template <class T>
concept Identitiable = requires {
    typename T::Element;
    { T::identity() } -> std::same_as<typename T::Element>;
};

template <class T>
concept Monoid = Semigroup<T> and Identitiable<T>;

} // namespace

} // namespace zawa

#include <vector>
#include <cassert>
#include <functional>
#include <type_traits>
#include <ostream>

namespace zawa {

template <concepts::Monoid Monoid>
class SegmentTree {
public:

    using VM = Monoid;

    using V = typename VM::Element;

    using OM = Monoid;

    using O = typename OM::Element;

    SegmentTree() = default;

    explicit SegmentTree(usize n) : m_n{ n }, m_dat(n << 1, VM::identity()) {}

    explicit SegmentTree(const std::vector<V>& dat) : m_n{ dat.size() }, m_dat(dat.size() << 1, VM::identity()) {
        for (usize i{} ; i < m_n ; i++) {
            m_dat[i + m_n] = dat[i];
        }
        for (usize i{m_n} ; i-- ; ) {
            m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
        }
    }

    [[nodiscard]] inline usize size() const noexcept {
        return m_n;
    }

    [[nodiscard]] V get(usize i) const {
        assert(i < size());
        return m_dat[i + m_n];
    }

    [[nodiscard]] V operator[](usize i) const {
        assert(i < size());
        return m_dat[i + m_n];
    }

    void operation(usize i, const O& value) {
        assert(i < size());
        i += size();
        m_dat[i] = OM::operation(m_dat[i], value);
        while (i = parent(i), i) {
            m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
        }
    }

    void assign(usize i, const V& value) {
        assert(i < size());
        i += size();
        m_dat[i] = value;
        while (i = parent(i), i) {
            m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
        }
    }

    [[nodiscard]] V product(u32 l, u32 r) const {
        assert(l <= r and r <= size());
        V L{ VM::identity() }, R{ VM::identity() };
        for (l += size(), r += size() ; l < r ; l = parent(l), r = parent(r)) {
            if (l & 1) {
                L = VM::operation(L, m_dat[l++]);
            }
            if (r & 1) {
                R = VM::operation(m_dat[--r], R);
            }
        }
        return VM::operation(L, R);
    }

    template <class F>
    requires std::predicate<F, V>
    [[nodiscard]] usize maxRight(usize l, const F& f) {
        assert(l < size());
        static_assert(std::is_convertible_v<decltype(f), std::function<bool(V)>>, "maxRight's argument f must be function bool(T)");
        assert(f(VM::identity()));
        usize res{l}, width{1};
        V prod{ VM::identity() };
        // 現在の見ている頂点の幅をwidthで持つ
        // 境界がある頂点を含む部分木の根を探す
        // (折り返す時は必要以上の幅を持つ根になるが、widthを持っているのでオーバーしない)
        for (l += size() ; res + width <= size() ; l = parent(l), width <<= 1) if (l & 1) {
            if (not f(VM::operation(prod, m_dat[l]))) break; 
            res += width;
            prod = VM::operation(prod, m_dat[l++]);
        }
        // 根から下って、境界を発見する
        while (l = left(l), width >>= 1) {
            if (res + width <= size() and f(VM::operation(prod, m_dat[l]))) {
                res += width;
                prod = VM::operation(prod, m_dat[l++]);
            } 
        }
        return res;
    }

    template <class F>
    requires std::predicate<F, V>
    [[nodiscard]] usize minLeft(usize r, const F& f) const {
        assert(r <= size());
        static_assert(std::is_convertible_v<decltype(f), std::function<bool(V)>>, "minLeft's argument f must be function bool(T)");
        assert(f(VM::identity()));
        usize res{r}, width{1};
        V prod{ VM::identity() };
        for (r += size() ; res >= width ; r = parent(r), width <<= 1) if (r & 1) {
            if (not f(VM::operation(m_dat[r - 1], prod))) break;
            res -= width;
            prod = VM::operation(prod, m_dat[--r]);
        }
        while (r = left(r), width >>= 1) {
            if (res >= width and f(VM::operation(m_dat[r - 1], prod))) {
                res -= width;
                prod = VM::operation(m_dat[--r], prod);
            }
        }
        return res;
    }

    friend std::ostream& operator<<(std::ostream& os, const SegmentTree& st) {
        for (usize i{1} ; i < 2 * st.size() ; i++) {
            os << st.m_dat[i] << (i + 1 == 2 * st.size() ? "" : " ");
        }
        return os;
    }

private:

    constexpr u32 left(u32 v) const {
        return v << 1;
    }

    constexpr u32 right(u32 v) const {
        return v << 1 | 1;
    }

    constexpr u32 parent(u32 v) const {
        return v >> 1;
    }

    usize m_n;

    std::vector<V> m_dat;
};

} // namespace zawa

#include <iostream>

using namespace std;
using namespace zawa;
using M = SubarraySumMaxMonoid<long long>;
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    int N, Q;
    cin >> N >> Q;
    vector<M::Element> A(N);
    for (int i = 0 ; i < N ; i++) {
        int a;
        cin >> a;
        A[i] = M::convert(a);
    }
    SegmentTree<M> seg{A};
    auto prod = [&]() -> long long {
        auto pd = seg.product(0, N);
        return pd ? max(pd->ans(), 0LL) : 0LL;
    };
    cout << prod() << '\n';
    while (Q--) {
        int k, x;
        cin >> k >> x;
        k--;
        seg.assign(k, M::convert(x));
        cout << prod() << '\n';
    }
}
accuracy: 1
time_ms: 40
memory_kb: 15092
final_rank: 10

code_snippet: #define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/3111"


#include <algorithm>
#include <concepts>
#include <optional>

namespace zawa {

template <std::totally_ordered T>
class SubarraySumMax {
public:

    SubarraySumMax() = default;

    explicit SubarraySumMax(T v) : m_ans{v}, m_sum{v}, m_pref{v}, m_suf{v} {}

    SubarraySumMax(T ans, T sum, T pref, T suf) : m_ans{ans}, m_sum{sum}, m_pref{pref}, m_suf{suf} {}

    inline T ans() const {
        return m_ans;
    }

    inline T sum() const {
        return m_sum;
    }

    inline T pref() const {
        return m_pref;
    }

    inline T suf() const {
        return m_suf;
    }

    static SubarraySumMax<T> merge(const SubarraySumMax<T>& lhs, const SubarraySumMax<T>& rhs) {
        T sum = lhs.sum() + rhs.sum();
        T pref = std::max(lhs.pref(), lhs.sum() + rhs.pref());
        T suf = std::max(rhs.suf(), lhs.suf() + rhs.sum());
        T ans = std::max({lhs.ans(), rhs.ans(), lhs.suf() + rhs.pref(), sum});
        return {ans, sum, pref, suf};
    }

private:

    T m_ans{}, m_sum{}, m_pref{}, m_suf{};

};

template <std::totally_ordered T>
struct SubarraySumMaxMonoid {

    using Element = std::optional<SubarraySumMax<T>>;

    static Element identity() {
        return std::nullopt;
    }

    static Element operation(const Element& L, const Element& R) {
        if (!L) return R;
        if (!R) return L;
        return Element::value_type::merge(L.value(), R.value());
    }

    static Element convert(T v) {
        return Element{v};
    }
};

} // namespace zawa


#include <cstdint>
#include <cstddef>

namespace zawa {

using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

using usize = std::size_t;

} // namespace zawa



namespace zawa {

namespace concepts {

template <class T>
concept Semigroup = requires {
    typename T::Element;
    { T::operation(std::declval<typename T::Element>(), std::declval<typename T::Element>()) } -> std::same_as<typename T::Element>;
};

} // namespace concepts

} // namespace zawa


namespace zawa {

namespace concepts {

template <class T>
concept Identitiable = requires {
    typename T::Element;
    { T::identity() } -> std::same_as<typename T::Element>;
};

template <class T>
concept Monoid = Semigroup<T> and Identitiable<T>;

} // namespace

} // namespace zawa

#include <vector>
#include <cassert>
#include <functional>
#include <type_traits>
#include <ostream>

namespace zawa {

template <concepts::Monoid Monoid>
class SegmentTree {
public:

    using VM = Monoid;

    using V = typename VM::Element;

    using OM = Monoid;

    using O = typename OM::Element;

    SegmentTree() = default;

    explicit SegmentTree(usize n) : m_n{ n }, m_dat(n << 1, VM::identity()) {}

    explicit SegmentTree(const std::vector<V>& dat) : m_n{ dat.size() }, m_dat(dat.size() << 1, VM::identity()) {
        for (usize i{} ; i < m_n ; i++) {
            m_dat[i + m_n] = dat[i];
        }
        for (usize i{m_n} ; i-- ; ) {
            m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
        }
    }

    [[nodiscard]] inline usize size() const noexcept {
        return m_n;
    }

    [[nodiscard]] V get(usize i) const {
        assert(i < size());
        return m_dat[i + m_n];
    }

    [[nodiscard]] V operator[](usize i) const {
        assert(i < size());
        return m_dat[i + m_n];
    }

    void operation(usize i, const O& value) {
        assert(i < size());
        i += size();
        m_dat[i] = OM::operation(m_dat[i], value);
        while (i = parent(i), i) {
            m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
        }
    }

    void assign(usize i, const V& value) {
        assert(i < size());
        i += size();
        m_dat[i] = value;
        while (i = parent(i), i) {
            m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
        }
    }

    [[nodiscard]] V product(u32 l, u32 r) const {
        assert(l <= r and r <= size());
        V L{ VM::identity() }, R{ VM::identity() };
        for (l += size(), r += size() ; l < r ; l = parent(l), r = parent(r)) {
            if (l & 1) {
                L = VM::operation(L, m_dat[l++]);
            }
            if (r & 1) {
                R = VM::operation(m_dat[--r], R);
            }
        }
        return VM::operation(L, R);
    }

    template <class F>
    requires std::predicate<F, V>
    [[nodiscard]] usize maxRight(usize l, const F& f) {
        assert(l < size());
        static_assert(std::is_convertible_v<decltype(f), std::function<bool(V)>>, "maxRight's argument f must be function bool(T)");
        assert(f(VM::identity()));
        usize res{l}, width{1};
        V prod{ VM::identity() };
        // 現在の見ている頂点の幅をwidthで持つ
        // 境界がある頂点を含む部分木の根を探す
        // (折り返す時は必要以上の幅を持つ根になるが、widthを持っているのでオーバーしない)
        for (l += size() ; res + width <= size() ; l = parent(l), width <<= 1) if (l & 1) {
            if (not f(VM::operation(prod, m_dat[l]))) break; 
            res += width;
            prod = VM::operation(prod, m_dat[l++]);
        }
        // 根から下って、境界を発見する
        while (l = left(l), width >>= 1) {
            if (res + width <= size() and f(VM::operation(prod, m_dat[l]))) {
                res += width;
                prod = VM::operation(prod, m_dat[l++]);
            } 
        }
        return res;
    }

    template <class F>
    requires std::predicate<F, V>
    [[nodiscard]] usize minLeft(usize r, const F& f) const {
        assert(r <= size());
        static_assert(std::is_convertible_v<decltype(f), std::function<bool(V)>>, "minLeft's argument f must be function bool(T)");
        assert(f(VM::identity()));
        usize res{r}, width{1};
        V prod{ VM::identity() };
        for (r += size() ; res >= width ; r = parent(r), width <<= 1) if (r & 1) {
            if (not f(VM::operation(m_dat[r - 1], prod))) break;
            res -= width;
            prod = VM::operation(prod, m_dat[--r]);
        }
        while (r = left(r), width >>= 1) {
            if (res >= width and f(VM::operation(m_dat[r - 1], prod))) {
                res -= width;
                prod = VM::operation(m_dat[--r], prod);
            }
        }
        return res;
    }

    friend std::ostream& operator<<(std::ostream& os, const SegmentTree& st) {
        for (usize i{1} ; i < 2 * st.size() ; i++) {
            os << st.m_dat[i] << (i + 1 == 2 * st.size() ? "" : " ");
        }
        return os;
    }

private:

    constexpr u32 left(u32 v) const {
        return v << 1;
    }

    constexpr u32 right(u32 v) const {
        return v << 1 | 1;
    }

    constexpr u32 parent(u32 v) const {
        return v >> 1;
    }

    usize m_n;

    std::vector<V> m_dat;
};

} // namespace zawa

#include <iostream>

using namespace std;
using namespace zawa;
using M = SubarraySumMaxMonoid<long long>;
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    int N, Q;
    cin >> N >> Q;
    vector<M::Element> A(N);
    for (int i = 0 ; i < N ; i++) {
        int a;
        cin >> a;
        A[i] = M::convert(a);
    }
    SegmentTree<M> seg{A};
    auto prod = [&]() -> long long {
        auto pd = seg.product(0, N);
        return pd ? max(pd->ans(), 0LL) : 0LL;
    };
    cout << prod() << '\n';
    while (Q--) {
        int k, x;
        cin >> k >> x;
        k--;
        seg.assign(k, M::convert(x));
        cout << prod() << '\n';
    }
}
accuracy: 1
time_ms: 40
memory_kb: 15092
final_rank: 10

code_snippet: #define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/3111"


#include <algorithm>
#include <concepts>
#include <optional>

namespace zawa {

template <std::totally_ordered T>
class SubarraySumMax {
public:

    SubarraySumMax() = default;

    explicit SubarraySumMax(T v) : m_ans{v}, m_sum{v}, m_pref{v}, m_suf{v}, m_entire{true} {}

    SubarraySumMax(T ans, T sum, T pref, T suf, bool entire) : m_ans{ans}, m_sum{sum}, m_pref{pref}, m_suf{suf}, m_entire{entire} {}

    inline T ans() const {
        return m_ans;
    }

    inline T sum() const {
        return m_sum;
    }

    inline T pref() const {
        return m_pref;
    }

    inline T suf() const {
        return m_suf;
    }

    inline bool entire() const {
        return m_entire;
    }

    static SubarraySumMax<T> merge(const SubarraySumMax<T>& lhs, const SubarraySumMax<T>& rhs) {
        T sum = lhs.sum() + rhs.sum();
        T pref = std::max(lhs.pref(), lhs.sum() + rhs.pref());
        T suf = std::max(rhs.suf(), lhs.suf() + rhs.sum());
        T ans = std::max({lhs.ans(), rhs.ans(), lhs.suf() + rhs.pref(), sum});
        bool entire = (ans == sum);
        return {ans, sum, pref, suf, entire};
    }

private:

    T m_ans{}, m_sum{}, m_pref{}, m_suf{};

    bool m_entire{};
};

template <std::totally_ordered T>
struct SubarraySumMaxMonoid {

    using Element = std::optional<SubarraySumMax<T>>;

    static Element identity() {
        return std::nullopt;
    }

    static Element operation(const Element& L, const Element& R) {
        if (!L) return R;
        if (!R) return L;
        return Element::value_type::merge(L.value(), R.value());
    }

    static Element convert(T v) {
        return Element{v};
    }
};

} // namespace zawa


#include <cstdint>
#include <cstddef>

namespace zawa {

using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

using usize = std::size_t;

} // namespace zawa



namespace zawa {

namespace concepts {

template <class T>
concept Semigroup = requires {
    typename T::Element;
    { T::operation(std::declval<typename T::Element>(), std::declval<typename T::Element>()) } -> std::same_as<typename T::Element>;
};

} // namespace concepts

} // namespace zawa


namespace zawa {

namespace concepts {

template <class T>
concept Identitiable = requires {
    typename T::Element;
    { T::identity() } -> std::same_as<typename T::Element>;
};

template <class T>
concept Monoid = Semigroup<T> and Identitiable<T>;

} // namespace

} // namespace zawa

#include <vector>
#include <cassert>
#include <functional>
#include <type_traits>
#include <ostream>

namespace zawa {

template <concepts::Monoid Monoid>
class SegmentTree {
public:

    using VM = Monoid;

    using V = typename VM::Element;

    using OM = Monoid;

    using O = typename OM::Element;

    SegmentTree() = default;

    explicit SegmentTree(usize n) : m_n{ n }, m_dat(n << 1, VM::identity()) {}

    explicit SegmentTree(const std::vector<V>& dat) : m_n{ dat.size() }, m_dat(dat.size() << 1, VM::identity()) {
        for (usize i{} ; i < m_n ; i++) {
            m_dat[i + m_n] = dat[i];
        }
        for (usize i{m_n} ; i-- ; ) {
            m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
        }
    }

    [[nodiscard]] inline usize size() const noexcept {
        return m_n;
    }

    [[nodiscard]] V get(usize i) const {
        assert(i < size());
        return m_dat[i + m_n];
    }

    [[nodiscard]] V operator[](usize i) const {
        assert(i < size());
        return m_dat[i + m_n];
    }

    void operation(usize i, const O& value) {
        assert(i < size());
        i += size();
        m_dat[i] = OM::operation(m_dat[i], value);
        while (i = parent(i), i) {
            m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
        }
    }

    void assign(usize i, const V& value) {
        assert(i < size());
        i += size();
        m_dat[i] = value;
        while (i = parent(i), i) {
            m_dat[i] = VM::operation(m_dat[left(i)], m_dat[right(i)]);
        }
    }

    [[nodiscard]] V product(u32 l, u32 r) const {
        assert(l <= r and r <= size());
        V L{ VM::identity() }, R{ VM::identity() };
        for (l += size(), r += size() ; l < r ; l = parent(l), r = parent(r)) {
            if (l & 1) {
                L = VM::operation(L, m_dat[l++]);
            }
            if (r & 1) {
                R = VM::operation(m_dat[--r], R);
            }
        }
        return VM::operation(L, R);
    }

    template <class F>
    requires std::predicate<F, V>
    [[nodiscard]] usize maxRight(usize l, const F& f) {
        assert(l < size());
        static_assert(std::is_convertible_v<decltype(f), std::function<bool(V)>>, "maxRight's argument f must be function bool(T)");
        assert(f(VM::identity()));
        usize res{l}, width{1};
        V prod{ VM::identity() };
        // 現在の見ている頂点の幅をwidthで持つ
        // 境界がある頂点を含む部分木の根を探す
        // (折り返す時は必要以上の幅を持つ根になるが、widthを持っているのでオーバーしない)
        for (l += size() ; res + width <= size() ; l = parent(l), width <<= 1) if (l & 1) {
            if (not f(VM::operation(prod, m_dat[l]))) break; 
            res += width;
            prod = VM::operation(prod, m_dat[l++]);
        }
        // 根から下って、境界を発見する
        while (l = left(l), width >>= 1) {
            if (res + width <= size() and f(VM::operation(prod, m_dat[l]))) {
                res += width;
                prod = VM::operation(prod, m_dat[l++]);
            } 
        }
        return res;
    }

    template <class F>
    requires std::predicate<F, V>
    [[nodiscard]] usize minLeft(usize r, const F& f) const {
        assert(r <= size());
        static_assert(std::is_convertible_v<decltype(f), std::function<bool(V)>>, "minLeft's argument f must be function bool(T)");
        assert(f(VM::identity()));
        usize res{r}, width{1};
        V prod{ VM::identity() };
        for (r += size() ; res >= width ; r = parent(r), width <<= 1) if (r & 1) {
            if (not f(VM::operation(m_dat[r - 1], prod))) break;
            res -= width;
            prod = VM::operation(prod, m_dat[--r]);
        }
        while (r = left(r), width >>= 1) {
            if (res >= width and f(VM::operation(m_dat[r - 1], prod))) {
                res -= width;
                prod = VM::operation(m_dat[--r], prod);
            }
        }
        return res;
    }

    friend std::ostream& operator<<(std::ostream& os, const SegmentTree& st) {
        for (usize i{1} ; i < 2 * st.size() ; i++) {
            os << st.m_dat[i] << (i + 1 == 2 * st.size() ? "" : " ");
        }
        return os;
    }

private:

    constexpr u32 left(u32 v) const {
        return v << 1;
    }

    constexpr u32 right(u32 v) const {
        return v << 1 | 1;
    }

    constexpr u32 parent(u32 v) const {
        return v >> 1;
    }

    usize m_n;

    std::vector<V> m_dat;
};

} // namespace zawa

#include <iostream>

using namespace std;
using namespace zawa;
using M = SubarraySumMaxMonoid<long long>;
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    int N, Q;
    cin >> N >> Q;
    vector<M::Element> A(N);
    for (int i = 0 ; i < N ; i++) {
        int a;
        cin >> a;
        A[i] = M::convert(a);
    }
    SegmentTree<M> seg{A};
    auto prod = [&]() -> long long {
        auto pd = seg.product(0, N);
        return pd ? max(pd->ans(), 0LL) : 0LL;
    };
    cout << prod() << '\n';
    while (Q--) {
        int k, x;
        cin >> k >> x;
        k--;
        seg.assign(k, M::convert(x));
        cout << prod() << '\n';
    }
}
accuracy: 1
time_ms: 40
memory_kb: 17444
final_rank: 13

code_snippet: // AOJ #3111 Cutting Subarray
// 2025.3.21

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {  // 整数の入力
	int n = 0; int c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(ll n) { // 整数の表示（出力）
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

struct Node { ll s, pre, suf, ans; };

int n, q;

Node mergeN(const Node &l, const Node &r) {
    Node res;
    res.s   = l.s + r.s;
    res.pre = max(l.pre, l.s + r.pre);
    res.suf = max(r.suf, r.s + l.suf);
    res.ans = max({l.ans, r.ans, l.suf + r.pre});
    return res;
}

vector<Node> seg;

Node make_node(ll v) {
    Node ret;
    ret.s   = v;
    ret.pre = max(v, 0LL);
    ret.suf = max(v, 0LL);
    ret.ans = max(v, 0LL);
    return ret;
}

void build(int idx, int l, int r, const vector<ll>& a) {
    if(l == r) {
        seg[idx] = make_node(a[l-1]);
        return;
    }
    int mid = (l + r) >> 1;
    build(idx*2, l, mid, a);
    build(idx*2+1, mid+1, r, a);
    seg[idx] = mergeN(seg[idx*2], seg[idx*2+1]);
}

void update(int idx, int l, int r, int pos, ll v) {
    if(l == r) {
        seg[idx] = make_node(v);
        return;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid) update(idx*2, l, mid, pos, v);
    else update(idx*2+1, mid+1, r, pos, v);
    seg[idx] = mergeN(seg[idx*2], seg[idx*2+1]);
}

int main(){
    n = Cin(), q = Cin();
    vector<ll> a(n);
    for(int i = 0; i < n; i++) a[i] = Cin();

    seg.resize(n * 4);
    build(1, 1, n, a);

    Cout(seg[1].ans);

    for(int i = 0; i < q; i++){
        int pos = Cin();
        ll x = Cin();
        update(1, 1, n, pos, x);
        Cout(seg[1].ans);
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 16436
final_rank: 12

code_snippet: // AOJ #3111 Cutting Subarray
// 2025.3.21

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct Node { ll s, pre, suf, ans; };

int n, q;

Node mergeN(const Node &l, const Node &r) {
    Node res;
    res.s   = l.s + r.s;
    res.pre = max(l.pre, l.s + r.pre);
    res.suf = max(r.suf, r.s + l.suf);
    res.ans = max({l.ans, r.ans, l.suf + r.pre});
    return res;
}

vector<Node> seg;

Node make_node(ll v) {
    Node ret;
    ret.s   = v;
    ret.pre = max(v, 0LL);
    ret.suf = max(v, 0LL);
    ret.ans = max(v, 0LL);
    return ret;
}

void build(int idx, int l, int r, const vector<ll>& a) {
    if(l == r) {
        seg[idx] = make_node(a[l-1]);
        return;
    }
    int mid = (l + r) >> 1;
    build(idx*2, l, mid, a);
    build(idx*2+1, mid+1, r, a);
    seg[idx] = mergeN(seg[idx*2], seg[idx*2+1]);
}

void update(int idx, int l, int r, int pos, ll v) {
    if(l == r) {
        seg[idx] = make_node(v);
        return;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid) update(idx*2, l, mid, pos, v);
    else update(idx*2+1, mid+1, r, pos, v);
    seg[idx] = mergeN(seg[idx*2], seg[idx*2+1]);
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> q;
    vector<ll> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];

    seg.resize(n * 4);
    build(1, 1, n, a);

    cout << seg[1].ans << endl;

    for(int i = 0; i < q; i++){
        int pos; ll x;
        cin >> pos >> x;
        update(1, 1, n, pos, x);
        cout << seg[1].ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 16472
final_rank: 16

code_snippet: #include<bits/stdc++.h>

using namespace std;

using ll = long long;

struct S{
  ll min;
  ll max;
  ll best;
  ll lazy;

  S(ll min,ll max,ll best):min(min),max(max),best(best),lazy(0){}
  S():min(0),max(0),best(0){}
};

S f(ll v){
  return S(v,v,max<ll>(0,v));
}

S g(S a,S b){
  ll nmin = min(a.min, b.min);
  ll nmax = max(a.max, b.max);
  ll nbest = max(max(a.best, b.best), b.max - a.min);
  return S(nmin,nmax,nbest);
}

vector<ll> func(){
  int n;
  int q;
  cin >> n >> q;
  vector<S> sg(n*5+10);
  auto change = [&](auto change, int l,int r,int t,int p,ll v) -> void{
    if(t < l or r <= t)return;
    if(l + 1 == r){
      sg[p] = f(v);
      return;
    }
    int lp = p * 2 + 0;
    int rp = p * 2 + 1;
    int mid = (l + r) / 2;
    change(change, l, mid, t, lp, v);
    change(change, mid,r, t, rp, v);
    sg[p] = g(sg[lp], sg[rp]);
  };

  auto range = [&](auto range,int l,int r,int L,int R,int p,ll v) -> void {
    if(R <= l or r <= L)return;
    if(l <= L and R <= r){
      sg[p].lazy += v;
      sg[p].min += v;
      sg[p].max += v;
      return;
    }
    int MID = (L + R) / 2;
    int lp = p * 2 + 0;
    int rp = p * 2 + 1;
    if(sg[p].lazy){
      sg[lp].lazy += sg[p].lazy;
      sg[lp].min += sg[p].lazy;
      sg[lp].max += sg[p].lazy;
      sg[rp].lazy += sg[p].lazy;
      sg[rp].min += sg[p].lazy;
      sg[rp].max += sg[p].lazy;
      sg[p].lazy = 0;
    }
    range(range, l, r, L, MID, lp, v);
    range(range, l, r, MID, R, rp, v);
    sg[p] = g(sg[lp], sg[rp]);
  };

  vector<ll> as(n+1,0);
  vector<ll> vs(n+1,0);

  for(int i=0;i<n;++i){
    ll v;
    cin >> v;
    as[i+1] = as[i] + v;
    vs[i+1] = v;
    change(change, 0, n+1, i+1, 1, as[i+1]);
  }
  vector<ll> res;
  res.push_back(sg[1].best);
  for(int i=0;i<q;++i){
    int k;
    ll v;
    cin >> k >> v;
    range(range, k,n+1,0,n+1,1,v-vs[k]);
    vs[k] = v;
    res.push_back(sg[1].best);
  }
  return res;
}

int main(){
  for(auto i:func()){
    cout << i << endl;
  }
}
accuracy: 1
time_ms: 160
memory_kb: 21332
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const ll mod=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}
template<class T> T min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}



namespace atcoder {

namespace internal {

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

}  // namespace internal

template <class S,
          S (*op)(S, S),
          S (*e)(),
          class F,
          S (*mapping)(F, S),
          F (*composition)(F, F),
          F (*id)()>
struct lazy_segtree {
  public:
    lazy_segtree() : lazy_segtree(0) {}
    lazy_segtree(int n) : lazy_segtree(std::vector<S>(n, e())) {}
    lazy_segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        lz = std::vector<F>(size, id());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        return d[p];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return e();

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push(r >> i);
        }

        S sml = e(), smr = e();
        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }

        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    void apply(int p, F f) {
        assert(0 <= p && p < _n);
        p += size;
        for (int i = log; i >= 1; i--) push(p >> i);
        d[p] = mapping(f, d[p]);
        for (int i = 1; i <= log; i++) update(p >> i);
    }
    void apply(int l, int r, F f) {
        assert(0 <= l && l <= r && r <= _n);
        if (l == r) return;

        l += size;
        r += size;

        for (int i = log; i >= 1; i--) {
            if (((l >> i) << i) != l) push(l >> i);
            if (((r >> i) << i) != r) push((r - 1) >> i);
        }

        {
            int l2 = l, r2 = r;
            while (l < r) {
                if (l & 1) all_apply(l++, f);
                if (r & 1) all_apply(--r, f);
                l >>= 1;
                r >>= 1;
            }
            l = l2;
            r = r2;
        }

        for (int i = 1; i <= log; i++) {
            if (((l >> i) << i) != l) update(l >> i);
            if (((r >> i) << i) != r) update((r - 1) >> i);
        }
    }

    template <bool (*g)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return g(x); });
    }
    template <class G> int max_right(int l, G g) {
        assert(0 <= l && l <= _n);
        assert(g(e()));
        if (l == _n) return _n;
        l += size;
        for (int i = log; i >= 1; i--) push(l >> i);
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!g(op(sm, d[l]))) {
                while (l < size) {
                    push(l);
                    l = (2 * l);
                    if (g(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*g)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return g(x); });
    }
    template <class G> int min_left(int r, G g) {
        assert(0 <= r && r <= _n);
        assert(g(e()));
        if (r == 0) return 0;
        r += size;
        for (int i = log; i >= 1; i--) push((r - 1) >> i);
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!g(op(d[r], sm))) {
                while (r < size) {
                    push(r);
                    r = (2 * r + 1);
                    if (g(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;
    std::vector<F> lz;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
    void all_apply(int k, F f) {
        d[k] = mapping(f, d[k]);
        if (k < size) lz[k] = composition(f, lz[k]);
    }
    void push(int k) {
        all_apply(2 * k, lz[k]);
        all_apply(2 * k + 1, lz[k]);
        lz[k] = id();
    }
};

}  // namespace atcoder
using namespace atcoder;


struct lazy_S {
    ll ans;
	ll min_val;
	ll max_val;
};

using lazy_F = ll;

lazy_S lazy_op(lazy_S l, lazy_S r) {
    return lazy_S{
		max(max(l.ans,r.ans),r.max_val-l.min_val),min(l.min_val,r.min_val),max(l.max_val,r.max_val)
    };
}

lazy_S lazy_e() { return lazy_S{0,ILL,-ILL}; }

lazy_S mapping(lazy_F l, lazy_S r) {
	return lazy_S{
		r.ans,r.min_val+l,r.max_val+l
	};
}

//l(r(x))
lazy_F composition(lazy_F l, lazy_F r) {
	return l+r;
}

lazy_F lazy_id(){return 0;}

#define lazy_calc lazy_S,lazy_op,lazy_e,lazy_F,mapping,composition,lazy_id



void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	int N,Q;
	cin>>N>>Q;
	vector<ll> A(N);
	lazy_segtree<lazy_calc> seg(N+1);
	seg.set(0,{0,0,0});
	ll S=0;
	rep(i,N){
		cin>>A[i];
		S+=A[i];
		seg.set(i+1,{0,S,S});
	}
	rep(i,Q+1){
		cout<<seg.all_prod().ans<<"\n";
		if(i==Q) break;
		ll k,x;
		cin>>k>>x;
		seg.apply(k,N+1,-A[k-1]);
		A[k-1]=x;
		seg.apply(k,N+1,A[k-1]);
	}
}
accuracy: 1
time_ms: 80
memory_kb: 13504
final_rank: 9

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}

// 最大部分列をセグ木で求める
struct monoid{
    ll ans; // その区間における最大部分列
    ll L,R; // 左(右)側累積和最大値
    ll sum; // 区間の総和
    monoid(){
        ans=0;
        L=R=0;
        sum=0;
    }
    monoid(ll x){
        ans=max(x,0LL);
        L=R=max(x,0LL);
        sum=x;
    }
};
monoid f(monoid A,monoid B){
    monoid res;
    res.L=max(A.L,A.sum+B.L);
    res.R=max(B.R,B.sum+A.R);
    res.sum=A.sum+B.sum;
    res.ans=max(A.ans,B.ans);
    res.ans=max(res.ans,A.R+B.L);
    return res;
}
struct SegmentTree{
    int sz;
    vector<monoid> seg;
    SegmentTree(int n){
        sz=1;
        while(sz<n)sz<<=1;
        seg.resize(2*sz);
    }
    void set(int i,ll x){
        seg[i+sz]=monoid(x);
    }
    void init(){
        for(int i=sz-1;i>0;i--){
            seg[i]=f(seg[2*i],seg[2*i+1]);
        }
    }
    void update(int k,ll x){
        k+=sz;
        seg[k]=monoid(x);
        while(k>>=1){
            seg[k]=f(seg[2*k],seg[2*k+1]);
        }
    }
    monoid query(int l,int r){
        monoid L,R;
        for(l+=sz,r+=sz;l<r;l>>=1,r>>=1){
            if(l&1) L=f(L,seg[l++]);
            if(r&1) R=f(seg[--r],R);
        }
        return f(L,R);
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,q; cin >> n >> q;
    SegmentTree seg(n);
    for(int i=0;i<n;i++){
        ll a; cin >> a;
        seg.set(i,a);
    }
    seg.init();
    printf("%lld\n",seg.query(0,n).ans);
    while(q--){
        int i,x; cin >> i >> x;
        seg.update(i-1,x);
        printf("%lld\n",seg.query(0,n).ans);
    }
}
accuracy: 1
time_ms: 30
memory_kb: 11324
final_rank: 1

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
const double pi = 3.141592653589793238462643383279;
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

//repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const double EPS = 1e-10, PI = acos(-1);
//ここから編集
struct Node{
  ll sum, rsum, lsum, maxsum;
  Node(){}
  Node(ll a, ll b, ll c, ll d): sum(a), rsum(max(0LL,b)), lsum(max(0LL,c)), maxsum(max(0LL,d)){}
};

Node merge(Node &l, Node &r){
  Node res;
  res.sum = l.sum + r.sum;
  res.lsum = max(l.lsum, l.sum + r.lsum);
  res.rsum = max(r.rsum, r.sum + l.rsum);
  res.maxsum = max(max(l.maxsum, r.maxsum), l.rsum+r.lsum);
  return res;
}
struct SegmentTree{
  vector<Node> node;
  int N;
  SegmentTree(vector<ll> &v){
    int n = v.size();
    N = 1;
    while(N < n) N *= 2;
    
    node.resize(N*2-1);
    for(int i=0; i<n; i++) node[i+N-1] = Node(v[i], v[i], v[i], v[i]);
    for(int i=N-2; i>=0; i--) node[i] = merge(node[i*2+1], node[i*2+2]);
  }

  void update(int k, ll x){
    k += N-1;
    node[k] = Node(x, x, x, x);
    while(k > 0){
      k = (k-1)/2;
      node[k] = merge(node[k*2+1], node[k*2+2]);
    }
  }

  Node query(int a, int b, int k, int l, int r){
    if(r <= a || b <= l) return Node(0,0,0,0);
    if(a <= l && r <= b) return node[k];
    else{
      Node vl = query(a, b, 2*k+1, l, (l+r)/2);
      Node vr = query(a, b, 2*k+2, (l+r)/2, r);
      return merge(vl, vr);
    }
  }

  Node query(int l, int r){
    return query(l, r, 0, 0, N);
  }
};
int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int n, q; cin >> n >> q;
  vector<ll> a(n);
  REP(i,n) cin >> a[i];

  SegmentTree seg(a);
  cout << seg.query(0, n).maxsum << endl;
  REP(i,q){
    int k, x; cin >> k >> x;
    k--;
    seg.update(k, x);
    cout << seg.query(0, n).maxsum << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 10864
final_rank: 6

code_snippet: #pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-10;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << "debug: " << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << "debug: " << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

template<typename Monoid>
struct Segtree {
	using F = function<Monoid(Monoid, Monoid)>;
	int sz;
	vector<Monoid> seg;
	const F f;
	const Monoid M;

	Segtree(int n, const F f, const Monoid &M) : f(f), M(M){
		sz = 1;
		while(sz < n) sz <<= 1;
		seg.assign(2*sz, M);
	}

	void set(int k, const Monoid &x) {
		seg[k + sz] = x;
	}

	void build(){
		for(int k=sz-1; k>0; k--){
			seg[k] = f(seg[2*k], seg[2*k+1]);
		}
	}

	void update(int k, const Monoid &x){
		k += sz;
		seg[k] = x;
		while(k >>= 1){
			seg[k] = f(seg[2*k], seg[2*k+1]);
		}
	}

	Monoid query(int a, int b){
		Monoid L = M, R = M;
		for(a += sz, b += sz; a < b; a >>= 1, b >>= 1){
			if(a & 1) L = f(L, seg[a++]);
			if(b & 1) R = f(seg[--b], R);
		}
		return f(L, R);
	}

	Monoid operator[](const int &k) const {
		return seg[k + sz];
	}

	template<typename C>
	int find_subtree(int a, const C &check, Monoid &M0, bool type){
		while(a < sz){
			Monoid nxt = type ? f(seg[2*a]+type,M0) : f(M0,seg[2*a]+type);
			if(check(nxt)) a = 2 * a + type;
			else M0 = nxt, a = 2 * a + 1 - type;
		}
		return a - sz;
	}

	template<typename C>
	int find_first(int a, const C &check){
		Monoid L = M;
		if(a <= 0){
			if(check(f(L, seg[1]))) return find_subtree(1,check,L,false);
			return -1;
		}
		int b = sz;
		for(a += sz, b += sz; a < b; a >>= 1, b >>= 1){
			if(a & 1){
				Monoid nxt = f(L, seg[a]);
				if(check(nxt)) return find_subtree(a,L,check,false);
				L = nxt;
				++a;
			}
		}
		return -1;
	}

	template<typename C>
	int find_last(int b, const C &check){
		Monoid R = M;
		if(b >= sz){
			if(check(f(seg[1], R))) return find_subtree(1,check,R,true);
			return -1;
		}
		int a = sz;
		for(b += sz; a < b; a >>= 1, b >>= 1){
			if(b & 1){
				Monoid nxt = f(seg[--b], R);
				if(check(nxt)) return find_subtree(b,check,R,true);
				R = nxt;
			}
		}
		return -1;
	}
};

struct T{
	ll l, r, mx, all;
};

auto f = [](T a, T b){
	T res;
	res.mx = max({a.mx, b.mx, a.r + b.l});
	res.l = max(a.l, a.all + b.l);
	res.r = max(b.r, a.r + b.all);
	res.all = a.all + b.all;
	return res;
};

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n,q; cin >> n >> q;
	Segtree<T> seg(n,f,T{0,0,0,0});
	rep(i,n){
		ll a; cin >> a;
		seg.set(i,T{a,a,a,a});
	}
	seg.build();
	cout << seg.query(0,n).mx << "\n";
	while(q--){
		ll k,x; cin >> k >> x; k--;
		seg.update(k,T{x,x,x,x});
		cout << seg.query(0,n).mx << "\n";
	}
}
accuracy: 1
time_ms: 40
memory_kb: 10988
final_rank: 2

code_snippet: #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

using namespace std;

#include <algorithm>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {
 
namespace internal {
 
// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}
 
// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}
 
}  // namespace internal
 
}  // namespace atcoder
 
#include <cassert>
#include <vector>

namespace atcoder {

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

}  // namespace atcoder


 

using namespace atcoder;


struct S{
    ll all,left,right,mid;
};

S op(S a,S b){
    S res;
    res.all=a.all+b.all;
    res.left=max(a.left,a.all+b.left);
    res.right=max(b.all+a.right,b.right);
    res.mid=max({a.right+b.left,a.mid,b.mid});
    
    return res;
}

S e(){
    S res;
    res.all=0;
    res.left=0;
    res.right=0;
    res.mid=0;
    return res;
}


int main(){
    ios;
    ll n,q;
    cin >> n >> q;
    segtree<S,op,e> seg(n);
    rep(i,n){
        ll a;
        cin >> a;
        S res;
        res.all=a;
        res.left=max(0LL,a);
        res.right=max(0LL,a);
        res.mid=max(0LL,a);
        seg.set(i,res);
    }
    S ans=seg.all_prod();
    cout << max({ans.all,ans.left,ans.right,ans.mid}) << endl;
    while(q--){
        ll k,x;
        cin >> k >> x;
        k--;
        S v;
        v.all=x;
        v.left=max(0LL,x);
        v.right=max(0LL,x);
        v.mid=max(0LL,x);
        seg.set(k,v);
        S ans=seg.all_prod();
        cout << max({ans.all,ans.left,ans.right,ans.mid}) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 14420
final_rank: 7

code_snippet: #include <algorithm>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <vector>

namespace atcoder {

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

}  // namespace atcoder

using namespace atcoder;
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n-1; i >= 0; --i)
using namespace std;
using ll = long long;
template<typename T>
inline bool chmax(T& a, const T& b) {
    if (a < b){
        a = b;
        return true;
    }
    return false;
}
template<typename T>
inline bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
/**
 * @brief 多次元 vector の作成
 * @author えびちゃん
 */
namespace detail {
    template<typename T, int N>
    auto make_vec(vector<int>& sizes, T const& x) {
        if constexpr (N == 1) {
            return vector(sizes[0], x);
        } else {
            int size = sizes[N-1];
            sizes.pop_back();
            return vector(size, make_vec<T, N-1>(sizes, x));
        }
    }
}
template<typename T, int N>
auto make_vec(int const(&sizes)[N], T const& x = T()) {
    vector<int> s(N);
    for (int i = 0; i < N; ++i) s[i] = sizes[N-i-1];
    return detail::make_vec<T, N>(s, x);
}
__attribute__((constructor))
void fast_io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

struct S {
    ll sum, lmax, rmax, max; 
    S(ll sum = 0, ll lmax = 0, ll rmax = 0, ll max = 0) : sum(sum), lmax(lmax), rmax(rmax), max(max) {}
};

S op (S l, S r) {
    S res;
    res.sum = l.sum + r.sum;
    res.lmax = max(l.lmax, l.sum + r.lmax);
    res.rmax = max(l.rmax + r.sum, r.rmax);
    res.max = max({l.max, r.max, res.lmax, res.rmax, l.rmax + r.lmax});
    return res;
}

S e() {
    return S();
}

int main() {
    int n, q;
    cin >> n >> q;
    vector<S> a(n);
    rep(i, n) {
        int x;
        cin >> x;        
        a[i].sum = x;
        chmax<ll>(a[i].lmax, x);
        chmax<ll>(a[i].rmax, x);
        chmax<ll>(a[i].max, x);
    }
    segtree<S, op, e> seg(a);
    cout << seg.all_prod().max << '\n';
    while (q--) {
        int k, x;
        cin >> k >> x;
        k--;
        S update;
        update.sum = x;
        chmax<ll>(update.lmax, x);
        chmax<ll>(update.rmax, x);
        chmax<ll>(update.max, x);
        seg.set(k, update);
        cout << seg.all_prod().max << '\n';
    }
}
accuracy: 1
time_ms: 50
memory_kb: 14516
final_rank: 8

code_snippet: #include<bits/stdc++.h>
#include<array>
using namespace std;
using UL = unsigned int;
using ULL = unsigned long long;
using LL = long long;
#define rep(i,n) for(int i=0; i<(n); i++)

struct VRSQ{
 LL Mr,Ml;
 LL S;
 LL xD;
 VRSQ(LL a=0){
  if(a>=0){ Ml=Mr=S=xD=a; }
  else{ Ml=Mr=0; S=a; xD=0; }
 }
 VRSQ operator+(const VRSQ& r) const {
  VRSQ ans;
  ans.Mr = max(Mr+r.S,r.Mr);
  ans.Ml = max(Ml,r.Ml+S);
  ans.S = S + r.S;
  ans.xD = max(max(xD,r.xD),Mr+r.Ml);
  return ans;
 }
};

struct RSQ{
 int N;
 vector<VRSQ> V;
 void init(int n){
  N=1; while(N<n) N<<=1;
  V.resize(N*2);
 }
 void upd(int p,LL x){
  p+=N; V[p]=VRSQ(x);
  while(p!=1){
   p>>=1;
   V[p] = V[p<<1] + V[(p<<1)+1];
  }
 }
 LL query(){ return V[1].xD; }
};

int N,Q;
RSQ G;

int main() {
 scanf("%d%d",&N,&Q);
 G.init(N);
 rep(i,N){
  int a; scanf("%d",&a);
  G.upd(i,a);
 }

 printf("%lld\n",G.query());

 rep(i,Q){
  int k,x; scanf("%d%d",&k,&x); k--;
  G.upd(k,x);
  printf("%lld\n",G.query());
 }
 return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 11012
final_rank: 3

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template<typename T> inline bool chmax(T &a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T> inline bool chmin(T &a,T b){if(b<a){a=b;return 1;}return 0;}
template<typename T> vector<T> make_vec(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>
auto make_vec(size_t a,Ts... ts){return vector<decltype(make_vec<T>(ts...))>(a,make_vec<T>(ts...));}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T,U>::value>::type fill_v(U &u,const V... v){u=U(v...);}
template<typename T,typename U,typename... V>
typename enable_if<!is_same<T,U>::value>::type fill_v(U &u,const V... v){for(auto &e:u)fill_v<T>(e,v...);}

template <typename T>
struct segment_tree {
    using F = function<T(T, T)>;
    int n;
    vector<T> data;
    F operation;
    T identity;
    segment_tree(F operation, T identity)
        : operation(operation), identity(identity) {}
    void init(int n_) {
        n = 1;
        while (n < n_) n <<= 1;
        data.assign(n<<1, identity);
    }
    void build(const vector<T> &v) {
        int n_ = (int)v.size();
        init(n_);
        for (int i = 0; i < n_; ++i) data[n+i] = v[i];
        for (int i = n-1; i >= 1; --i) {
            data[i] = operation(data[i<<1|0], data[i<<1|1]);
        }
    }
    T operator[](int i) { return data[n+i];}
    void set(int i, T x) {
        i += n;
        data[i] = x;
        while (i > 1) {
            i >>= 1;
            data[i] = operation(data[i<<1|0], data[i<<1|1]);
        }
    }
    T fold(int l, int r) {
        l += n; r += n;
        T resl = identity, resr = identity;
        while (l < r) {
            if (l & 1) resl = operation(resl, data[l++]);
            if (r & 1) resr = operation(data[--r], resr);
            l >>= 1; r >>= 1;
        }
        return operation(resl, resr);
    }
    template <typename C>
    int find(int st, C &check, T &acc, int i, int l, int r) {
        if (l+1 == r) {
            acc = operation(acc, data[i]);
            return check(acc) ? i-n : -1;
        }
        int m = (l+r)>>1;
        if (m <= st) return find(st, check, acc, i<<1|1, m, r);
        if (st <= l && !check(operation(acc, data[i]))) {
            acc = operation(acc, data[i]);
            return -1;
        }
        int vl = find(st, check, acc, i<<1|0, l, m);
        if (vl != -1) return vl;
        return find(st, check, acc, i<<1|1, m, r);
    }
    // min idx s.t. st <= idx && !check(fold(st, idx-1)) && check(fold(st, idx))
    template <typename C>
    int find(int st, C &check) {
        T acc = identity;
        return find(st, check, identity, 1, 0, n);
    }
};

struct max_subarray {
    ll sum, l, r, mx;
    max_subarray(ll sum=0, ll l=0, ll r=0, ll mx=0) : sum(sum), l(l), r(r), mx(mx) {}
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    rep(i, n) cin >> a[i];

    auto f = [](max_subarray x, max_subarray y) {
                 max_subarray res;
                 res.sum = x.sum + y.sum;
                 res.l = max(x.l, x.sum + y.l);
                 res.r = max(y.r, x.r + y.sum);
                 res.mx = max({x.mx, y.mx, x.r + y.l});
                 return res;
             };
    max_subarray id;

    vector<max_subarray> b(n);
    rep(i, n) {
        ll relu = a[i] * (a[i] > 0);
        max_subarray val{a[i], relu, relu, relu};
        b[i] = val;
    }
    
    segment_tree<max_subarray> seg(f, id);
    seg.build(b);
    max_subarray res = seg.fold(0, n);
    cout << res.mx << endl;
    while (q--) {
        int k, x;
        cin >> k >> x;
        k--;
        ll relu = x * (x > 0);
        max_subarray val{x, relu, relu, relu};
        seg.set(k, val);
        max_subarray res = seg.fold(0, n);
        cout << res.mx << endl;
    }
}
accuracy: 1
time_ms: 130
memory_kb: 14452
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template<typename T> inline bool chmax(T &a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T> inline bool chmin(T &a,T b){if(b<a){a=b;return 1;}return 0;}
template<typename T> vector<T> make_vec(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>
auto make_vec(size_t a,Ts... ts){return vector<decltype(make_vec<T>(ts...))>(a,make_vec<T>(ts...));}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T,U>::value>::type fill_v(U &u,const V... v){u=U(v...);}
template<typename T,typename U,typename... V>
typename enable_if<!is_same<T,U>::value>::type fill_v(U &u,const V... v){for(auto &e:u)fill_v<T>(e,v...);}

template <typename T>
struct segment_tree {
    using F = function<T(T, T)>;
    int n;
    vector<T> data;
    F operation;
    T identity;
    segment_tree(F operation, T identity)
        : operation(operation), identity(identity) {}
    void init(int n_) {
        n = 1;
        while (n < n_) n <<= 1;
        data.assign(n<<1, identity);
    }
    void build(const vector<T> &v) {
        int n_ = (int)v.size();
        init(n_);
        for (int i = 0; i < n_; ++i) data[n+i] = v[i];
        for (int i = n-1; i >= 1; --i) {
            data[i] = operation(data[i<<1|0], data[i<<1|1]);
        }
    }
    T operator[](int i) { return data[n+i];}
    void set(int i, T x) {
        i += n;
        data[i] = x;
        while (i > 1) {
            i >>= 1;
            data[i] = operation(data[i<<1|0], data[i<<1|1]);
        }
    }
    T fold(int l, int r) {
        l += n; r += n;
        T resl = identity, resr = identity;
        while (l < r) {
            if (l & 1) resl = operation(resl, data[l++]);
            if (r & 1) resr = operation(data[--r], resr);
            l >>= 1; r >>= 1;
        }
        return operation(resl, resr);
    }
    template <typename C>
    int find(int st, C &check, T &acc, int i, int l, int r) {
        if (l+1 == r) {
            acc = operation(acc, data[i]);
            return check(acc) ? i-n : -1;
        }
        int m = (l+r)>>1;
        if (m <= st) return find(st, check, acc, i<<1|1, m, r);
        if (st <= l && !check(operation(acc, data[i]))) {
            acc = operation(acc, data[i]);
            return -1;
        }
        int vl = find(st, check, acc, i<<1|0, l, m);
        if (vl != -1) return vl;
        return find(st, check, acc, i<<1|1, m, r);
    }
    // min idx s.t. st <= idx && !check(fold(st, idx-1)) && check(fold(st, idx))
    template <typename C>
    int find(int st, C &check) {
        T acc = identity;
        return find(st, check, identity, 1, 0, n);
    }
};

struct max_subarray {
    ll sum, l, r, mx;
    max_subarray(ll sum=0, ll l=0, ll r=0, ll mx=0) : sum(sum), l(l), r(r), mx(mx) {}
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n, q;
    cin >> n >> q;
    vector<int> a(n);
    rep(i, n) cin >> a[i];

    auto f = [](max_subarray x, max_subarray y) {
                 max_subarray res;
                 res.sum = x.sum + y.sum;
                 res.l = max(x.l, x.sum + y.l);
                 res.r = max(y.r, x.r + y.sum);
                 res.mx = max({x.mx, y.mx, x.r + y.l});
                 return res;
             };
    max_subarray id;

    vector<max_subarray> b(n);
    rep(i, n) {
        ll relu = a[i] * (a[i] > 0);
        max_subarray val{a[i], relu, relu, relu};
        b[i] = val;
    }
    
    segment_tree<max_subarray> seg(f, id);
    seg.build(b);
    max_subarray res = seg.fold(0, n);
    cout << res.mx << endl;
    while (q--) {
        int k, x;
        cin >> k >> x;
        k--;
        ll relu = x * (x > 0);
        max_subarray val{x, relu, relu, relu};
        seg.set(k, val);
        max_subarray res = seg.fold(0, n);
        cout << res.mx << endl;
    }
    
}
accuracy: 1
time_ms: 130
memory_kb: 14452
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;


template <typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  Int n;
  F f;
  T ti;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(F f,T ti):f(f),ti(ti){}
  void init(Int n_){
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }
  void build(const vector<T> &v){
    Int n_=v.size();
    init(n_);
    for(Int i=0;i<n_;i++) dat[n+i]=v[i];
    for(Int i=n-1;i;i--)
      dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
  }
  void set_val(Int k,T x){
    dat[k+=n]=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
  }
  T query(Int a,Int b){
    T vl=ti,vr=ti;
    for(Int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
      if(l&1) vl=f(vl,dat[l++]);
      if(r&1) vr=f(dat[--r],vr);
    }
    return f(vl,vr);
  }
  template<typename C>
  Int find(Int st,C &check,T &acc,Int k,Int l,Int r){
    if(l+1==r){
      acc=f(acc,dat[k]);
      return check(acc)?k-n:-1;
    }
    Int m=(l+r)>>1;
    if(m<=st) return find(st,check,acc,(k<<1)|1,m,r);
    if(st<=l&&!check(f(acc,dat[k]))){
      acc=f(acc,dat[k]);
      return -1;
    }
    Int vl=find(st,check,acc,(k<<1)|0,l,m);
    if(~vl) return vl;
    return find(st,check,acc,(k<<1)|1,m,r);
  }
  template<typename C>
  Int find(Int st,C &check){
    T acc=ti;
    return find(st,check,acc,1,0,n);
  }
};

//INSERT ABOVE HERE
signed main(){
  Int n,q;
  cin>>n>>q;
  vector<Int> as(n);
  for(Int i=0;i<n;i++) cin>>as[i];

  const Int INF = 1e15;
  using T = tuple<Int,Int,Int,Int,Int>;
  T ti(-INF,-INF,-INF,-INF,-INF);

  auto f=[&](T a,T b){
           if(a==ti) return b;
           if(b==ti) return a;
           Int as,ava,avi,avl,avr;
           tie(as,ava,avi,avl,avr)=a;
           Int bs,bva,bvi,bvl,bvr;
           tie(bs,bva,bvi,bvl,bvr)=b;
           Int cs=as+bs;
           Int cva=ava+bva,cvi=max(avi,bvi),cvl=avl,cvr=bvr;
           cvi=max(cvi,avr+bvl);
           cvl=max(cvl,ava+bvl);
           cvr=max(cvr,avr+bva);
           return T(cs,cva,cvi,cvl,cvr);
         };
  SegmentTree<T> seg(f,ti);

  vector<T> vt;
  for(Int i=0;i<n;i++) vt.emplace_back(as[i],as[i],as[i],as[i],as[i]);
  seg.build(vt);

  auto print=
    [&](){
      auto res=seg.query(0,n);
      Int ans=max({get<0>(res),get<1>(res),get<2>(res),get<3>(res),get<4>(res)});
      chmax(ans,0);
      cout<<ans<<endl;
    };

    print();
  for(Int i=0;i<q;i++){
    Int k,x;
    cin>>k>>x;
    k--;
    seg.set_val(k,T(x,x,x,x,x));
    print();
  }
  return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 17824
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
using ll = long long;
template<class T> using vec = vector<T>;
template<class T> using vvec = vector<vec<T>>;
constexpr ll inf = 1LL<<60;

template<typename Monoid>
class SegmentTree{
private:
    using F = function<Monoid(Monoid&,Monoid&)>;
    int sz;
    vector<Monoid> seg;
    const F op;//演算
    const Monoid e;//単位元
public:
    SegmentTree(int n,const F op,const Monoid &e):op(op),e(e){
        sz = 1;
        while(sz<n) sz <<= 1;
        seg.assign(2*sz,e);
    }
    //代入
    void set(int k, const Monoid &x){
        seg[k+sz] = x;
    }
    //前計算(?)
    void build(){
        for(int i=sz-1;i>0;i--){
            seg[i] = op(seg[2*i],seg[2*i+1]);
        }
    }
    void update(int k,const Monoid &x){
        k += sz;
        seg[k] = x;
        while(k>>=1){
            seg[k] = op(seg[2*k],seg[2*k+1]);
        }
    }
    Monoid query(int l,int r){
        Monoid L = e,R = e;
        for(l+=sz,r+=sz;l<r;l>>=1,r>>=1){
            if(l&1) L = op(L,seg[l++]);
            if(r&1) R = op(seg[--r],R);
        }
        return op(L,R);
    }
    Monoid operator[](const int &k)const{
        return seg[k+sz];
    }
};

struct state{
    ll sum,lma,rma,ma;
};

state op(state& l,state& r){
    ll sum = 0,ma = -inf,lma = -inf,rma = -inf;
    sum = l.sum+r.sum;
    ma = max({l.ma,r.ma,l.rma+r.lma});
    lma = max(l.lma,l.sum+r.lma);
    rma = max(r.rma,r.sum+l.rma);
    return (state){sum,lma,rma,ma};
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,Q;
    cin >> N >> Q;
    SegmentTree<state> seg(N,op,(state){-inf,-inf,-inf});
    for(int i=0;i<N;i++){
        ll a;
        cin >> a;
        seg.set(i,(state){a,max(a,0LL),max(a,0LL),max(a,0LL)});
    }
    seg.build();
    cout << seg.query(0,N).ma << endl;
    for(int i=0;i<Q;i++){
        ll k,a;
        cin >> k >> a;
        k--;
        seg.update(k,(state){a,max(a,0LL),max(a,0LL),max(a,0LL)});
        cout << seg.query(0,N).ma << endl;
    }
}
accuracy: 1
time_ms: 120
memory_kb: 11032
final_rank: 5

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>

class SegTree {
	static SegTree* make(const std::vector<long long int>& initial, const int from, const int until);
public:
	virtual ~SegTree() {};
	virtual void update(const int from, const int until, const long long int diff) = 0;
	virtual long long int min_of(const int from, const int until) = 0;
	virtual long long int max_of(const int from, const int until) = 0;
	static std::shared_ptr<SegTree> make(const std::vector<long long int>& initial);
};
class Leaf : public SegTree {
	int position;
	long long int value;
public:
	Leaf(const int pos, const long long int val) : position{ pos }, value{ val }{};
	void update(const int from, const int until, const long long int diff) override {
		if (from <= position && position < until) {
			value += diff;
		}
	}
	long long int min_of(const int from, const int until) override {
		if (from <= position && position < until) return value;
		else return LLONG_MAX;
	}
	long long int max_of(const int from, const int until) override {
		if (from <= position && position < until) return value;
		else return LLONG_MIN;
	}
};
class Branch : public SegTree {
	int from_position, until_position;
	long long int min_cache, max_cache;
	SegTree* left, * right;
	long long int operation = 0;
	void update_inner() {
		if (operation != 0) {
			left->update(from_position, until_position, operation);
			right->update(from_position, until_position, operation);
			operation = 0;
		}
		min_cache = std::min(left->min_of(from_position, until_position), right->min_of(from_position, until_position));
		max_cache = std::max(left->max_of(from_position, until_position), right->max_of(from_position, until_position));
	}
public:
	Branch(const int from, const int until, SegTree* l, SegTree* r) :
		from_position{ from }, until_position{ until },
		left{ l }, right{ r },
		min_cache{ std::min(l->min_of(from, until), r->min_of(from, until)) }, max_cache{ std::max(l->max_of(from, until), r->max_of(from, until)) }{};
	~Branch() {
		delete left, right;
	}
	void update(const int from, const int until, const long long int diff) override {
		if (until <= from_position || until_position <= from) return;
		else if (from <= from_position && until_position <= until) operation += diff;
		else {
			left->update(from, until, diff);
			right->update(from, until, diff);
			update_inner();
		}
	}
	long long int min_of(const int from, const int until) override {
		if (until <= from_position || until_position <= from) return LLONG_MAX;
		else if (from <= from_position && until_position <= until) return min_cache + operation;
		else return std::min(left->min_of(from, until), right->min_of(from, until)) + operation;
	}
	long long int max_of(const int from, const int until) override {
		if (until <= from_position || until_position <= from) return LLONG_MIN;
		else if (from <= from_position && until_position <= until) return max_cache + operation;
		else return std::max(left->max_of(from, until), right->max_of(from, until)) + operation;
	}
};



int main() {
	int n, q; std::cin >> n >> q;
	std::vector<int> series(n, 0);
	for (auto& a : series) std::cin >> a;
	std::vector<long long int> sum_array(n + 1, 0);
	for (auto i = 0; i < n; ++i) {
		sum_array[i + 1] = sum_array[i] + series[i];
	}
	auto seg_tree = SegTree::make(sum_array);
	auto comparator = [](const std::pair<int, long long int>& a, const std::pair<int, long long int>& b) {return a.second < b.second; };
	std::priority_queue<std::pair<int, long long int>, std::vector<std::pair<int, long long int>>, decltype(comparator)> queue(comparator);
	for (auto i = 1; i <= n; ++i) {
		queue.emplace(i, seg_tree->max_of(i, n + 1) - seg_tree->min_of(0, i));
	}
	std::cout << std::max(0LL, queue.top().second) << '\n';
	for (auto i = 0; i < q; ++i) {
		int k, x; std::cin >> k >> x;
		seg_tree->update(k, n + 1, x - series[k - 1]);
		if (x > series[k - 1]) {
			queue.emplace(k, seg_tree->max_of(k, n + 1) - seg_tree->min_of(0, k));
		}
		else {
			queue.emplace(k + 1, seg_tree->max_of(k + 1, n + 1) - seg_tree->min_of(0, k + 1));
		}
		while (true) {
			auto top = queue.top();
			const auto max_subsequence = seg_tree->max_of(top.first, n + 1) - seg_tree->min_of(0, top.first);
			if (top.second == max_subsequence) break;
			queue.emplace(top.first, max_subsequence);
			queue.pop();
		}
		series[k - 1] = x;
		std::cout << std::max(0LL, queue.top().second) << '\n';
	}
}

SegTree* SegTree::make(const std::vector<long long int>& initial, const int from, const int until)
{
	if (from + 1 == until) return new Leaf(from, initial[from]);
	else return new Branch(from, until, make(initial, from, (from + until) / 2), make(initial, (from + until) / 2, until));
}

std::shared_ptr<SegTree> SegTree::make(const std::vector<long long int>& initial)
{
	return std::shared_ptr<SegTree>(make(initial, 0, initial.size()));
}
accuracy: 1
time_ms: 330
memory_kb: 17568
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>

class SegTree {
	static SegTree* make(const std::vector<long long int>& initial, const int from, const int until);
public:
	virtual void update(const int from, const int until, const long long int diff) = 0;
	virtual long long int min_of(const int from, const int until) = 0;
	virtual long long int max_of(const int from, const int until) = 0;
	static std::shared_ptr<SegTree> make(const std::vector<long long int>& initial);
};
class Leaf : public SegTree {
	int position;
	long long int value;
public:
	Leaf(const int pos, const long long int val) : position{ pos }, value{ val }{};
	void update(const int from, const int until, const long long int diff) override {
		if (from <= position && position < until) {
			value += diff;
		}
	}
	long long int min_of(const int from, const int until) override {
		if (from <= position && position < until) return value;
		else return LLONG_MAX;
	}
	long long int max_of(const int from, const int until) override {
		if (from <= position && position < until) return value;
		else return LLONG_MIN;
	}
};
class Branch : public SegTree {
	int from_position, until_position;
	long long int min_cache, max_cache;
	SegTree* left, *right;
	long long int operation = 0;
	void update_inner() {
		if (operation != 0) {
			left->update(from_position, until_position, operation);
			right->update(from_position, until_position, operation);
			operation = 0;
		}
		min_cache = std::min(left->min_of(from_position, until_position), right->min_of(from_position, until_position));
		max_cache = std::max(left->max_of(from_position, until_position), right->max_of(from_position, until_position));
	}
public:
	Branch(const int from, const int until, SegTree* l, SegTree* r) :
		from_position{ from }, until_position{ until },
		left{ l }, right{ r },
		min_cache{ std::min(l->min_of(from, until), r->min_of(from, until)) }, max_cache{ std::max(l->max_of(from, until), r->max_of(from, until)) }{};
	~Branch() {
		delete left, right;
	}
	void update(const int from, const int until, const long long int diff) override {
		if (until <= from_position || until_position <= from) return;
		else if (from <= from_position && until_position <= until) operation += diff;
		else {
			left->update(from, until, diff);
			right->update(from, until, diff);
			update_inner();
		}
	}
	long long int min_of(const int from, const int until) override {
		if (until <= from_position || until_position <= from) return LLONG_MAX;
		else if (from <= from_position && until_position <= until) return min_cache + operation;
		else return std::min(left->min_of(from, until), right->min_of(from, until)) + operation;
	}
	long long int max_of(const int from, const int until) override {
		if (until <= from_position || until_position <= from) return LLONG_MIN;
		else if (from <= from_position && until_position <= until) return max_cache + operation;
		else return std::max(left->max_of(from, until), right->max_of(from, until)) + operation;
	}
};



int main() {
	int n, q; std::cin >> n >> q;
	std::vector<int> series(n, 0);
	for (auto& a : series) std::cin >> a;
	std::vector<long long int> sum_array(n + 1, 0);
	for (auto i = 0; i < n; ++i) {
		sum_array[i + 1] = sum_array[i] + series[i];
	}
	auto seg_tree = SegTree::make(sum_array);
	auto comparator = [](const std::pair<int, long long int>& a, const std::pair<int, long long int>& b) {return a.second < b.second; };
	std::priority_queue<std::pair<int, long long int>, std::vector<std::pair<int, long long int>>, decltype(comparator)> queue(comparator);
	for (auto i = 1; i <= n; ++i) {
		queue.emplace(i, seg_tree->max_of(i, n + 1) - seg_tree->min_of(0, i));
	}
	std::cout << std::max(0LL, queue.top().second) << '\n';
	for (auto i = 0; i < q; ++i) {
		int k, x; std::cin >> k >> x;
		seg_tree->update(k, n + 1, x - series[k - 1]);
		if (x > series[k - 1]) {
			queue.emplace(k, seg_tree->max_of(k, n + 1) - seg_tree->min_of(0, k));
		}
		else {
			queue.emplace(k + 1, seg_tree->max_of(k + 1, n + 1) - seg_tree->min_of(0, k + 1));
		}
		while (true) {
			auto top = queue.top();
			const auto max_subsequence = seg_tree->max_of(top.first, n + 1) - seg_tree->min_of(0, top.first);
			if (top.second == max_subsequence) break;
			queue.emplace(top.first, max_subsequence);
			queue.pop();
		}
		series[k - 1] = x;
		std::cout << std::max(0LL, queue.top().second) << '\n';
	}
}

SegTree* SegTree::make(const std::vector<long long int>& initial, const int from, const int until)
{
	if (from + 1 == until) return new Leaf(from, initial[from]);
	else return new Branch(from, until, make(initial, from, (from + until) / 2), make(initial, (from + until) / 2, until));
}

std::shared_ptr<SegTree> SegTree::make(const std::vector<long long int>& initial)
{
	return std::shared_ptr<SegTree>(make(initial, 0, initial.size()));
}
accuracy: 1
time_ms: 310
memory_kb: 17576
final_rank: 19

code_snippet: /*  -*- coding: utf-8 -*-
 *
 * 3111.cc: Cutting Subarray
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_E2 = 1 << 18; // = 262144
const int INF = 1 << 30;

/* typedef */

typedef long long ll;

struct Elm {
  ll s, l, r, m;
  Elm() {}
  Elm(ll _s, ll _l, ll _r, ll _m): s(_s), l(_l), r(_r), m(_m) {}
  Elm(ll _s): s(_s), l(_s), r(_s), m(_s) {}

  Elm operator+(const Elm &e) const {
    Elm t;
    t.s = s + e.s;
    t.l = max(l, s + e.l);
    t.r = max(e.r, e.s + r);
    t.m = max(m, max(e.m, max(t.s, max(t.l, max(t.r, r + e.l)))));
    return t;
  }

  ll getmax() { return max<ll>(0, m); }
};

template <typename T, const int MAX_E2>
struct SegTree {
  int n, e2;
  T nodes[MAX_E2], def;
  SegTree() {}

  void init(int _n, T _def) {
    n = _n; def = _def;
    for (e2 = 1; e2 < n; e2 <<= 1);
    fill(nodes + e2 - 1, nodes + MAX_E2, def);
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
  }

  T &get(int i) { return nodes[e2 - 1 + i]; }
  T &root() { return nodes[0]; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
  }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
    }
  }
};

/* global variables */

SegTree<Elm,MAX_E2> st;

/* subroutines */

/* main */

int main() {
  int n, qn;
  scanf("%d%d", &n, &qn);

  st.init(n, Elm(0));

  for (int i = 0; i < n; i++) {
    int ai;
    scanf("%d", &ai);
    st.get(i) = ai;
  }
  st.setall();

  printf("%lld\n", st.root().getmax());

  while (qn--) {
    int ki, xi;
    scanf("%d%d", &ki, &xi);
    ki--;

    st.set(ki, Elm(xi));
    printf("%lld\n", st.root().getmax());
  }
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 11420
final_rank: 4