Language: C++

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cout;
using std::endl;
using std::cin;

template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

int main() {
	int n, K; scanf("%d%d", &n, &K);
	std::vector<i64> h(n);
	for(int i = 0; i < n; i++) scanf("%lld", &h[i]);

	if(K == n) {
		printf("0\n");
		return 0;
	}

	auto cost = [&](int i, int j) -> i64 {
		return std::max(h[j] - h[i], 0LL);
	};

	auto dp = make_v<i64>(n + 1, n, K + 1);
	for(auto & A: dp) for(auto & B: A) for(auto & C: B) C = 1LL << 60;
	for(int i = 0; i < n; i++) dp[1][i][!!i] = h[i];
	for(int i = 1; i < n; i++) {
		for(int j = 0; j < n; j++) {
			for(int k = 0; k <= K; k++) {
				i64 from = dp[i][j][k];
				if(from == 1LL << 60) continue;
				
				// 変更なし
				dp[i + 1][i][k] = std::min(dp[i + 1][i][k], from + cost(j, i));

				// 変更あり
				if(k + 1 <= K) dp[i + 1][j][k + 1] = std::min(dp[i + 1][j][k + 1], from + cost(j, j));
			}
		}

		/*
		for(int j = 0; j < n; j++) {
			for(int k = 0; k <= K; k++) {
				if(dp[i + 1][j][k] == 1LL << 60) cout << -1 << " ";
				else cout << dp[i + 1][j][k] << " ";
			}
			cout << endl;
		}
		cout << endl;
		*/
	}
	
	i64 ans = 1LL << 60;
	for(int i = 0; i < n; i++) for(int j = 0; j <= K; j++) ans = std::min(ans, dp[n][i][j]);
	printf("%lld\n", ans);
	return 0;
}



Compiler output:
a.cc: In lambda function:
a.cc:27:32: error: no matching function for call to 'max(__gnu_cxx::__alloc_traits<std::allocator<long int>, long int>::value_type, long long int)'
   27 |                 return std::max(h[j] - h[i], 0LL);
      |                        ~~~~~~~~^~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 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:27:32: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'long long int')
   27 |                 return std::max(h[j] - h[i], 0LL);
      |                        ~~~~~~~~^~~~~~~~~~~~~~~~~~
/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:27:32: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
   27 |                 return std::max(h[j] - h[i], 0LL);
      |                        ~~~~~~~~^~~~~~~~~~~~~~~~~~