Language: C++

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <forward_list>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#if __has_include(<optional>)
#include <optional>
#elif __has_include(<experimental/optional>)
#include <experimental/optional>
#endif
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#if __has_include(<variant>)
#include <variant>
#elif __has_include(<experimental/variant>)
#include <experimental/variant>
#endif
#include <vector>
// using namespace std;
constexpr int64_t MOD = 1000000007;

template <class T>
class read;

template <class T, size_t n>
class read<std::array<T, n>>
{
public:
	using ret_type = std::array<T, n>;
	ret_type operator()() const
	{
		ret_type v;
		for (size_t i = 0; i < n; i++) v[i] = read<T>()();
		return v;
	}
};

template <class T>
constexpr std::size_t _tuple_size_v = std::tuple_size<T>::value;
template <class F, class Tuple, std::size_t... I>
constexpr decltype(auto) _apply_impl(F&& f, Tuple&& t, std::index_sequence<I...>)
{
	return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
}
template <class F, class Tuple>
constexpr decltype(auto) _apply(F&& f, Tuple&& t)
{
	return _apply_impl(std::forward<F>(f), std::forward<Tuple>(t),
	                   std::make_index_sequence<_tuple_size_v<std::remove_reference_t<Tuple>>>{});
}

template <class... Ts>
class read<std::tuple<Ts...>>
{
public:
	using ret_type = std::tuple<Ts...>;
	ret_type operator()() const
	{
		ret_type v;
		::_apply([](auto&... args) { (void)std::initializer_list<int>{(void(f(args)), 0)...}; }, v);
		return v;
	}

private:
	template <class T>
	static int f(T& arg)
	{
		arg = read<T>()();
		return 0;
	}
};

template <class T1, class T2>
class read<std::pair<T1, T2>>
{
public:
	using ret_type = std::pair<T1, T2>;
	ret_type operator()() const { return ret_type{read<T1>()(), read<T2>()()}; }
};

template <class T, class Allocator>
class read<std::vector<T, Allocator>>
{
public:
	using ret_type = std::vector<T, Allocator>;
	ret_type operator()(size_t len) const
	{
		ret_type v;
		for (size_t i = 0; i < len; i++) v.push_back(read<T>()());
		return v;
	}
};

template <class T>
class read
{
public:
	using ret_type = T;
	ret_type operator()() const
	{
		ret_type v;
		std::cin >> v;
		return v;
	}
};

template <class T, size_t n>
std::ostream& operator<<(std::ostream& os, const std::array<T, n>& v)
{
	auto f = false;
	os << "array(";
	for (auto& e : v)
	{
		if (f) os << ", ";
		os << e;
		f = true;
	}
	return os << ")";
}

template <class T>
static int my_cout_impl(const T& arg, std::ostream& os, bool& f)
{
	if (f) os << ", ";
	os << arg;
	f = true;
	return 0;
}

template <class... Ts>
std::ostream& operator<<(std::ostream& os, const std::tuple<Ts...>& v)
{
	auto f = false;
	os << "tuple(";
	::_apply([&os, &f](const auto&... args) { (void)std::initializer_list<int>{(void(my_cout_impl(args, os, f)), 0)...}; }, v);
	return os << ")";
}

template <class T1, class T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& v)
{
	return os << "pair(" << v.first << ", " << v.second << ")";
}

template <class T, class Allocator>
std::ostream& operator<<(std::ostream& os, const std::vector<T, Allocator>& v)
{
	auto f = false;
	os << "vector(";
	for (auto& e : v)
	{
		if (f) os << ", ";
		os << e;
		f = true;
	}
	return os << ")";
}

template <class Key, class T, class Compare, class Allocator>
std::ostream& operator<<(std::ostream& os, const std::map<Key, T, Compare, Allocator>& v)
{
	auto f = false;
	os << "map(";
	for (auto& p : v)
	{
		if (f) os << ", ";
		os << p.first << ": " << p.second;
		f = true;
	}
	return os << ")";
}

template <class Key, class T, class Compare, class Allocator>
std::ostream& operator<<(std::ostream& os, const std::multimap<Key, T, Compare, Allocator>& v)
{
	auto f = false;
	os << "multimap(";
	for (auto& p : v)
	{
		if (f) os << ", ";
		os << p.first << ": " << p.second;
		f = true;
	}
	return os << ")";
}

template <class Key, class Compare, class Allocator>
std::ostream& operator<<(std::ostream& os, const std::set<Key, Compare, Allocator>& v)
{
	auto f = false;
	os << "set(";
	for (auto& p : v)
	{
		if (f) os << ", ";
		os << p;
		f = true;
	}
	return os << ")";
}

template <class Key, class Compare, class Allocator>
std::ostream& operator<<(std::ostream& os, const std::multiset<Key, Compare, Allocator>& v)
{
	auto f = false;
	os << "multiset(";
	for (auto& p : v)
	{
		if (f) os << ", ";
		os << p;
		f = true;
	}
	return os << ")";
}

template <class Key, class T, class Hash, class Pred, class Allocator>
std::ostream& operator<<(std::ostream& os, const std::unordered_map<Key, T, Hash, Pred, Allocator>& v)
{
	auto f = false;
	os << "unordered_map(";
	for (auto& p : v)
	{
		if (f) os << ", ";
		os << p.first << ": " << p.second;
		f = true;
	}
	return os << ")";
}

template <class Key, class T, class Hash, class Pred, class Allocator>
std::ostream& operator<<(std::ostream& os, const std::unordered_multimap<Key, T, Hash, Pred, Allocator>& v)
{
	auto f = false;
	os << "unordered_multimap(";
	for (auto& p : v)
	{
		if (f) os << ", ";
		os << p.first << ": " << p.second;
		f = true;
	}
	return os << ")";
}

template <class Key, class Hash, class Pred, class Allocator>
std::ostream& operator<<(std::ostream& os, const std::unordered_set<Key, Hash, Pred, Allocator>& v)
{
	auto f = false;
	os << "unordered_set(";
	for (auto& p : v)
	{
		if (f) os << ", ";
		os << p;
		f = true;
	}
	return os << ")";
}

template <class Key, class Hash, class Pred, class Allocator>
std::ostream& operator<<(std::ostream& os, const std::unordered_multiset<Key, Hash, Pred, Allocator>& v)
{
	auto f = false;
	os << "unordered_multiset(";
	for (auto& p : v)
	{
		if (f) os << ", ";
		os << p;
		f = true;
	}
	return os << ")";
}

template <class T, class Container>
std::ostream& operator<<(std::ostream& os, const std::queue<T, Container>& v)
{
	std::queue<T, Container> v2 = v;
	auto f = false;
	os << "queue(";
	while (!v2.empty())
	{
		os << (f ? ", " : "front: ") << v2.front();
		v2.pop();
		f = true;
	}
	return os << ")";
}

template <class T, class Container>
std::ostream& operator<<(std::ostream& os, const std::stack<T, Container>& v)
{
	std::stack<T, Container> v2 = v;
	auto f = false;
	os << "stack(";
	while (!v2.empty())
	{
		os << (f ? ", " : "top: ") << v2.top();
		v2.pop();
		f = true;
	}
	return os << ")";
}

template <class T, class Allocator>
std::ostream& operator<<(std::ostream& os, const std::deque<T, Allocator>& v)
{
	auto f = false;
	os << "deque(";
	for (auto& e : v)
	{
		if (f) os << ", ";
		os << e;
		f = true;
	}
	return os << ")";
}

template <class T, class Container, class Compare>
std::ostream& operator<<(std::ostream& os, const std::priority_queue<T, Container, Compare>& v)
{
	std::priority_queue<T, Container, Compare> v2 = v;
	auto f = false;
	os << "priority_queue(";
	while (!v2.empty())
	{
		os << (f ? ", " : "top: ") << v2.top();
		v2.pop();
		f = true;
	}
	return os << ")";
}

template <class T, class Allocator>
std::ostream& operator<<(std::ostream& os, const std::list<T, Allocator>& v)
{
	auto f = false;
	os << "list(";
	for (auto& e : v)
	{
		if (f) os << ", ";
		os << e;
		f = true;
	}
	return os << ")";
}

template <class T>
std::ostream& operator<<(std::ostream& os, const std::initializer_list<T>& v)
{
	auto f = false;
	os << "initializer_list(";
	for (auto& e : v)
	{
		if (f) os << ", ";
		os << e;
		f = true;
	}
	return os << ")";
}

template <class T, class Allocator>
std::ostream& operator<<(std::ostream& os, const std::forward_list<T, Allocator>& v)
{
	auto f = false;
	os << "forward_list(";
	for (auto& e : v)
	{
		if (f) os << ", ";
		os << e;
		f = true;
	}
	return os << ")";
}

std::map<int, int> compress(const std::vector<int>& v)
{
	std::map<int, int> m;
	for (auto x : v) m[x] = 0;
	int n = 0;
	for (auto& t : m) t.second = n++;
	return m;
}

// [b, ..., e] must be a shuffle of [0, 1, ..., len - 1] (len = e - b)
template <class RanIt>
std::vector<std::vector<typename std::iterator_traits<RanIt>::value_type>> to_cycles(RanIt b, RanIt e)
{
	using T = typename std::iterator_traits<RanIt>::value_type;
	std::vector<std::vector<T>> cycs;
	T len = e - b;
	std::vector<bool> done(len, false);
	for (T i = 0; i < len; ++i)
	{
		if (done[i]) continue;
		std::vector<T> cyc;
		cyc.push_back(i);
		auto j = b[i];
		while (j != i)
		{
			cyc.push_back(j);
			done[j] = true;
			j = b[j];
		}
		cycs.push_back(std::move(cyc));
	}
	return cycs;
}

template <class T>
auto to_cycles(const T& v)
{
	return to_cycles(std::begin(v), std::end(v));
}

template <class T, size_t n>
double norm(const std::array<T, n>& p)
{
	auto s = 0.0;
	for (int i = 0; i < n; ++i) s += double(p[i]) * p[i];
	return std::sqrt(s);
}
template <class T, size_t n>
double dist(const std::array<T, n>& p, const std::array<T, n>& q)
{
	std::array<T, n> d;
	for (int i = 0; i < n; ++i) d[i] = p[i] - q[i];
	return norm(d);
}

std::array<int64_t, 2> solve_linear(int64_t n, int64_t m)
{
	auto A = std::array<std::array<int64_t, 2>, 2>{std::array<int64_t, 2>{n < 0 ? -1 : 1, 0}, std::array<int64_t, 2>{0, m < 0 ? -1 : 1}};
	n = std::abs(n);
	m = std::abs(m);
	while (m > 0)
	{
		auto q = n / m, r = n % m;
		n = m;
		m = r;
		for (int i = 0; i < 2; ++i)
		{
			auto x = A[i][1];
			A[i][1] = A[i][0] - q * x;
			A[i][0] = x;
		}
	}
	return std::array<int64_t, 2>{A[0][0], A[1][0]};
}

int64_t mod(int64_t a, int64_t mod) { return (a % mod + mod) % mod; }

int64_t inverse(int64_t a, int64_t M) { return mod(solve_linear(mod(a, M), M)[0], M); }

int64_t calc(int N, int K, std::vector<int64_t>&& H)
{
	if (K >= N) return 0;
	// dp[n][x]: [0, n], n を残す, n を含めて x 個残す
	std::vector<std::vector<int64_t>> dp;
	constexpr int64_t inf = int64_t(1) << 60;
	dp.emplace_back(N + 1, inf);
	dp[0][1] = H[0];
	for (int i = 1; i < N; ++i)
	{
		std::vector<int64_t> next(N + 1, inf);
		next[1] = H[i];
		for (int x = 2; x <= i + 1; ++x)
			for (int j = 0; j < i; ++j) next[x] = std::min(next[x], dp[j][x - 1] + std::max(0LL, H[i] - H[j]));
		dp.push_back(std::move(next));
	}
	auto min = inf;
	for (int i = 0; i < N; ++i)
		for (int x = N - K; x <= N; ++x) min = std::min(min, dp[i][x]);
	return min;
}

int main()
{
	std::cin.tie(nullptr);
	std::ios::sync_with_stdio(false);
	auto N = read<int>()();
	auto K = read<int>()();
	auto H = read<std::vector<int64_t>>()(N);
	std::cout << calc(N, K, std::move(H)) << std::endl;
	std::cout << std::flush;
	return 0;
}


Compiler output:
a.cc: In function 'int64_t calc(int, int, std::vector<long int>&&)':
a.cc:492:104: error: no matching function for call to 'max(long long int, __gnu_cxx::__alloc_traits<std::allocator<long int>, long int>::value_type)'
  492 |                         for (int j = 0; j < i; ++j) next[x] = std::min(next[x], dp[j][x - 1] + std::max(0LL, H[i] - H[j]));
      |                                                                                                ~~~~~~~~^~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:492:104: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and '__gnu_cxx::__alloc_traits<std::allocator<long int>, long int>::value_type' {aka 'long int'})
  492 |                         for (int j = 0; j < i; ++j) next[x] = std::min(next[x], dp[j][x - 1] + std::max(0LL, H[i] - H[j]));
      |                                                                                                ~~~~~~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:492:104: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  492 |                         for (int j = 0; j < i; ++j) next[x] = std::min(next[x], dp[j][x - 1] + std::max(0LL, H[i] - H[j]));
      |                                                                                                ~~~~~~~~^~~~~~~~~~~~~~~~~~