This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/challenges/sources/UOA/UAPC/3086?year=2020"
#include "../algorithm/larsch.cpp"
#include "../container/segment_tree.cpp"
#include "../traits/max_monoid.cpp"
#include "../utility/infty.cpp"
#include "../utility/int_alias.cpp"
#include "../utility/rep.cpp"
#include <iostream>
#include <vector>
int main() {
int N, L;
std::cin >> N >> L;
std::vector<std::optional<i64>> A(N);
for (auto& x : A) {
i64 t;
std::cin >> t;
x = t;
}
SegmentTree<MaxMonoid<i64>> seg(A);
std::vector<i64> dp(N + 1);
const auto transit = [&](int i, int j) {
i += 1;
if (j + L > i) return -INFTY<i64>;
return dp[j] + *seg.fold(j, i);
};
CompLARSCH<i64, std::greater<i64>> larsch(N, transit);
for (const auto i : rep(0, N)) {
larsch.add_column();
dp[i + 1] = transit(i, larsch.get_argmin());
}
std::cout << dp[N] << '\n';
return 0;
}
#line 1 "test/larsch.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/challenges/sources/UOA/UAPC/3086?year=2020"
#line 2 "algorithm/larsch.cpp"
#include <algorithm>
#include <cassert>
#include <functional>
#include <memory>
#include <vector>
class LARSCH {
using Select = std::function<bool(int, int, int)>;
struct ReduceRow;
struct ReduceCol;
struct ReduceRow {
int n, m, x, k;
Select f;
std::unique_ptr<ReduceCol> rec;
explicit ReduceRow(const int n_, const Select& f_) : n(n_), m(0), x(0), k(0), f(f_), rec() {
const int h = n / 2;
if (h != 0) rec = std::make_unique<ReduceCol>(h, [&](int i, int j, int k) { return f(2 * i + 1, j, k); });
}
void add_column() {
if ((x & 1) and f(x, k, m)) k = m;
if (rec) rec->add_column();
m += 1;
}
int get_argmin() {
if (x & 1) {
x += 1;
return k;
} else {
int ret = k;
if (x + 1 == n)
k = m - 1;
else
k = rec->get_argmin();
for (int j = ret + 1; j <= k; j += 1)
if (f(x, ret, j)) ret = j;
x += 1;
return ret;
}
}
};
struct ReduceCol {
int n, m, x, y;
std::vector<int> c;
Select f;
ReduceRow rec;
explicit ReduceCol(const int n_, const Select& f_)
: n(n_), m(0), x(0), y(0), c(), f(f_), rec(n_, [&](int i, int j, int k) { return f(i, c[j], c[k]); }) {}
void add_column() {
while (true) {
const int i = c.size();
if (i <= x or !f(i - 1, c[i - 1], m)) break;
c.pop_back();
}
if ((int)c.size() != n) c.push_back(m);
m += 1;
}
int get_argmin() {
x += 1;
while (y < std::min(x, (int)c.size())) {
rec.add_column();
y += 1;
}
return c[rec.get_argmin()];
}
};
int row, col;
ReduceRow machine;
public:
explicit LARSCH(const int n, const Select& f) : row(n), col(0), machine(n, f) {}
void add_column() {
assert(row != 0);
col += 1;
machine.add_column();
}
int get_argmin() {
assert(row != 0 and col != 0);
row -= 1;
return machine.get_argmin();
}
};
template <class T, class Comp = std::less<T>> class CompLARSCH {
std::function<T(int, int)> func;
Comp comp;
LARSCH machine;
public:
explicit CompLARSCH(const int n, const std::function<T(int, int)>& f, const Comp& c = Comp())
: func(f), comp(c), machine(n, [&](int i, int j, int k) { return comp(func(i, k), func(i, j)); }) {}
void add_column() { machine.add_column(); }
int get_argmin() { return machine.get_argmin(); }
};
#line 2 "internal/enable_avx2.cpp"
#ifdef ENABLE_AVX2
#define TARGET_AVX2 __attribute__((target("avx2")))
#else
#define TARGET_AVX2
#endif
#line 2 "utility/int_alias.cpp"
#include <cstdint>
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
#line 4 "utility/countl_zero.cpp"
TARGET_AVX2 constexpr int countl_zero(u64 x) {
#ifdef __GNUC__
return x == 0 ? 64 : __builtin_clzll(x);
#else
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
return 64 - countr_zero(~x);
#endif
}
#line 4 "utility/bit_width.cpp"
TARGET_AVX2 constexpr int bit_width(const u64 x) { return 64 - countl_zero(x); }
#line 5 "utility/ceil_log2.cpp"
TARGET_AVX2 constexpr int ceil_log2(const u64 x) {
#ifdef __GNUC__
return x == 0 ? 0 : bit_width(x - 1);
#else
int e = 0;
while (((u64)1 << e) < x) ++e;
return e;
#endif
}
#line 3 "utility/rep.cpp"
class Range {
struct Iter {
int itr;
constexpr Iter(const int pos) noexcept : itr(pos) {}
constexpr void operator++() noexcept { ++itr; }
constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
constexpr int operator*() const noexcept { return itr; }
};
const Iter first, last;
public:
explicit constexpr Range(const int first, const int last) noexcept : first(first), last(std::max(first, last)) {}
constexpr Iter begin() const noexcept { return first; }
constexpr Iter end() const noexcept { return last; }
};
constexpr Range rep(const int l, const int r) noexcept { return Range(l, r); }
constexpr Range rep(const int n) noexcept { return Range(0, n); }
#line 3 "utility/revrep.cpp"
class ReversedRange {
struct Iter {
int itr;
constexpr Iter(const int pos) noexcept : itr(pos) {}
constexpr void operator++() noexcept { --itr; }
constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; }
constexpr int operator*() const noexcept { return itr; }
};
const Iter first, last;
public:
explicit constexpr ReversedRange(const int first, const int last) noexcept
: first(last - 1), last(std::min(first, last) - 1) {}
constexpr Iter begin() const noexcept { return first; }
constexpr Iter end() const noexcept { return last; }
};
constexpr ReversedRange revrep(const int l, const int r) noexcept { return ReversedRange(l, r); }
constexpr ReversedRange revrep(const int n) noexcept { return ReversedRange(0, n); }
#line 7 "container/segment_tree.cpp"
template <class M> class SegmentTree {
using T = typename M::Type;
int internal_size, seg_size;
std::vector<T> data;
void fetch(const int k) { data[k] = M::operation(data[2 * k], data[2 * k + 1]); }
public:
explicit SegmentTree(const int size = 0, const T& value = M::identity())
: SegmentTree(std::vector<T>(size, value)) {}
explicit SegmentTree(const std::vector<T>& vec) : internal_size(vec.size()) {
seg_size = 1 << ceil_log2(internal_size);
data = std::vector<T>(2 * seg_size, M::identity());
for (const int i : rep(internal_size)) data[seg_size + i] = vec[i];
for (const int i : revrep(1, seg_size)) fetch(i);
}
int size() const { return internal_size; }
void assign(int i, const T& value) {
assert(0 <= i and i < internal_size);
i += seg_size;
data[i] = value;
while (i > 1) {
i >>= 1;
fetch(i);
}
}
T fold() const { return data[1]; }
T fold(int l, int r) const {
assert(0 <= l and l <= r and r <= internal_size);
l += seg_size;
r += seg_size;
T ret_l = M::identity(), ret_r = M::identity();
while (l < r) {
if (l & 1) ret_l = M::operation(ret_l, data[l++]);
if (r & 1) ret_r = M::operation(data[--r], ret_r);
l >>= 1;
r >>= 1;
}
return M::operation(ret_l, ret_r);
}
template <class F> int max_right(int l, const F& f) const {
assert(0 <= l and l <= internal_size);
assert(f(M::identity()));
if (l == internal_size) return internal_size;
l += seg_size;
T sum = M::identity();
do {
while (!(l & 1)) l >>= 1;
if (!f(M::operation(sum, data[l]))) {
while (l < seg_size) {
l = 2 * l;
if (f(M::operation(sum, data[l]))) sum = M::operation(sum, data[l++]);
}
return l - seg_size;
}
sum = M::operation(sum, data[l++]);
} while ((l & -l) != l);
return internal_size;
}
template <class F> int min_left(int r, const F& f) const {
assert(0 <= r and r <= internal_size);
assert(f(M::identity()));
if (r == 0) return 0;
r += seg_size;
T sum = M::identity();
do {
r -= 1;
while (r > 1 and (r & 1)) r >>= 1;
if (!f(M::operation(data[r], sum))) {
while (r < seg_size) {
r = 2 * r + 1;
if (f(M::operation(data[r], sum))) sum = M::operation(data[r--], sum);
}
return r + 1 - seg_size;
}
sum = M::operation(data[r], sum);
} while ((r & -r) != r);
return 0;
}
};
#line 2 "traits/optional_monoid.cpp"
#include <optional>
#include <utility>
template <class S> struct OptionalMonoid {
using Type = std::optional<typename S::Type>;
static constexpr Type identity() { return std::nullopt; }
static constexpr Type operation(const Type& l, const Type& r) {
if (!l) return r;
if (!r) return l;
return Type(std::in_place, S::operation(*l, *r));
}
};
#line 4 "traits/max_monoid.cpp"
template <class T> struct MaxSemiGroup {
using Type = T;
static constexpr T operation(const T& l, const T& r) { return std::max(l, r); }
};
template <class T> using MaxMonoid = OptionalMonoid<MaxSemiGroup<T>>;
#line 2 "utility/infty.cpp"
#include <limits>
template <class T, T Div = 2> constexpr T INFTY = std::numeric_limits<T>::max() / Div;
#line 8 "test/larsch.test.cpp"
#include <iostream>
#line 10 "test/larsch.test.cpp"
int main() {
int N, L;
std::cin >> N >> L;
std::vector<std::optional<i64>> A(N);
for (auto& x : A) {
i64 t;
std::cin >> t;
x = t;
}
SegmentTree<MaxMonoid<i64>> seg(A);
std::vector<i64> dp(N + 1);
const auto transit = [&](int i, int j) {
i += 1;
if (j + L > i) return -INFTY<i64>;
return dp[j] + *seg.fold(j, i);
};
CompLARSCH<i64, std::greater<i64>> larsch(N, transit);
for (const auto i : rep(0, N)) {
larsch.add_column();
dp[i + 1] = transit(i, larsch.get_argmin());
}
std::cout << dp[N] << '\n';
return 0;
}