#include<bits/extc++.h>
#ifndef __unix__
#define getchar_unlocked _getchar_nolock
#endif
#ifdef _debug 
#define dout(i) cout << #i << ' ' << i << ' '
#else 
#include<bits/stdc++.h>
#define dout(i) 0&&0
#endif
#pragma GCC optimize("unroll-loops,no-stack-protector,O3")
#define priority_queue __gnu_pbds::priority_queue
#define unordered_map __gnu_pbds::gp_hash_table
#define rep(_n) for(int __i = _n;__i--;)
#define rep0(_i, _n) for(int _i = 0; (_i)<(_n) ;++_i)
#define rep1(_i, _n) for(int _i = 1; (_i)<=(_n) ;++_i)
#define yn(i) ((i)?"Yes" : "No")
#define eout(i) {cout << (i) << '\n';exit(0);}
#define rout(i) {return cout << (i) << '\n', 0;}
#define multi int _T;for(cin >> _T;_T--;)
using namespace std;
using ll = long long;using ull = unsigned long long;using ul = unsigned;using db = double;
using __gnu_pbds::null_type;
inline void read(char &a){static char p; while(isspace(p = getchar_unlocked())); a = p; }
inline void read(char *a){static char p; while(isspace(p = getchar_unlocked())); *a = p, ++a; while(!isspace(p = getchar_unlocked()))*a = p, ++a;}
inline void read(string &a){static char p; while(isspace(p = getchar_unlocked())); a = p; while(!isspace(p = getchar_unlocked()))a.push_back(p);}
template<typename T> inline void read(T& a){static char p; static bool b;while (!isdigit(p = getchar_unlocked()))b = p == '-'; a = p ^ '0'; while (isdigit(p = getchar_unlocked())) a *= 10, a += p ^ '0'; if(b)a *= -1; }
template<typename T1, typename T2> inline void read(pair<T1, T2> &a){read(a.first), read(a.second);}
template<typename T> inline void read(T begin, const T &end){while(begin != end)read(*begin), ++begin;}
inline void R(){} template<class T1, class... T2> inline void R(T1 &h, T2 &... e){read(h), R(e...);}
template<typename T> inline void write(T begin, const T &end){while(begin != end)cout << *begin << " \n"[next(begin) == end], ++begin;}
template<typename T> using rbtree = __gnu_pbds::tree<T,__gnu_pbds::null_type,less<T>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>;
template<typename T> inline ostream& operator << (ostream &ou, const vector<T> &a){for(auto it = a.begin();it != a.end();++it)ou << (it != a.begin() ? " " : "") << *it;return ou;}
template<typename T1, typename T2>inline istream& operator >> (istream &in, pair<T1, T2> &a){return in >> a.first >> a.second;}
template<typename T1, typename T2>inline ostream& operator << (ostream &ou, const pair<T1, T2> &b){return ou << b.first << ' ' << b.second;}
template<typename T> inline long long pow(long long v, long long t, const T &p){long long res = 1;for(;t;t >>= 1, v = v * v % p)if(t & 1)res = res * v % p;return res;}
const int maxn = 401;
int n, k, h, a[maxn], c[maxn];
const ll inf = 10'000'000'000'000;
//          H     K
ll dp[maxn][maxn][maxn];
signed main(){
	ios_base::sync_with_stdio(0), cin.tie(0);
	for(auto &i : dp)for(auto &j : i)for(auto &s : j)s = inf;
	R(n, k);
	read(a+1, a+1+n);
	rep1(i, n)c[i] = a[i];
	sort(c, c+n+1);
	rep1(i, n)a[i] = lower_bound(c, c+n+1, a[i]) - c;
	dp[0][0][0] = 0;
	rep1(i, n+1){
		rep0(j, k+1)for(int h = n-1;h >= 0;--h)dp[i-1][h][j] = min(dp[i-1][h][j], dp[i-1][h+1][j]);
		rep0(j, k+1)for(int h = 0;h <= n;++h)dp[i][a[i]][j] = min(dp[i][a[i]][j], dp[i-1][h][j] + max(0, c[a[i]]-c[h]));
		rep0(j, k)for(int h = 0;h <= n;++h)dp[i][h][j+1] = min(dp[i][h][j+1], dp[i-1][h][j]);
	}
	ll res = inf;
	rep0(i, k+1)res = min(res, dp[n+1][0][i]);
	rout(res);
}


Measured memory_bytes: 504064