Language: C++

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

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for (ll(i) = (m); (i) < (n); ++(i))
#define REP(i, n) FOR(i, n, 0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

class SegmentTree
{
public:
    SegmentTree(int n)
    {
        ll cnt = 1;
        while (cnt < n)
            cnt <<= 1;
        node.resize(2 * cnt - 1);
        REP(i, 2 * cnt - 1)
        {
            node[i] = 0;
        }
        mN = cnt - 1;
    }
    ~SegmentTree()
    {
        node.resize(0);
    }

    //! 0-index get
    ll get(int left, int right, int l = 0, int r = -1, int index = 0)
    {
        if (r == -1)
            r = mN;

        if (r < left || right < l)
            return 0;
        if (left <= l && r <= right)
            return node[index];
        return std::max(get(left, right, l, (l + r) / 2, 2 * index + 1),
                        get(left, right, (l + r) / 2 + 1, r, 2 * index + 2));
    }

    //! 0-index set
    void set(int index, ll value)
    {
        node[mN + index] = value;
        //! update
        int par = (mN + index);
        while (par > 0)
        {
            par = (par - 1) / 2;
            node[par] = std::max(node[2 * par + 1], node[2 * par + 2]);
        }
    }

private:
    ll mN;
    vector<ll> node;
    const ll inf = (ll)1e15;
};

ll H[305];
//! nまでにk買い替えた時の=塗りつぶす最小操作
//ll dp[305][305][305];
ll V[305][305];

ll dp[305][305];

int main()
{
    ll N, K;
    cin >> N >> K;
    vector<ll> v;
    REP(i, N)
    {
        cin >> H[i];
        v.push_back(H[i]);
    }
    v.push_back(0);
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());

    if (K == N)
    {
        cout << 0 << endl;
        return 0;
    }

    SegmentTree seg(305);
    //!
    REP(i, N + 1)
    {
        REP(j, K + 1)
        {
            REP(k, v.size() + 1)
            {
                dp[i][j] = INF;
            }
        }
    }
    //! 3番目の0は変わっていない場合
    dp[0][0] = 0;
    REP(n, N)
    {
        REP(k, std::min(K + 1, n + 1))
        {
            {
                ll index = lower_bound(v.begin(), v.end(), H[i]) - v.begin();
                //! 変えない
                dp[i + 1][k] = std::min(dp[i + 1][k], dp[i][k] + V[index]);
            }

            {
                //! 変える
                REP(i, v.size())
                {
                    dp[i + 1][k + 1] = std::min(dp[i + 1][k + 1], dp[i][k] + V[i]);
                    seg.set(i, v[i] - dp[i + 1 [k + 1]]);
                }
            }

            REP(i, v.size())
            {
                V[i] = std::max(V[i], seg.get(0, i));
            }
        }
    }

    ll min = INF;
    REP(i, K + 1)
    {
        min = std::min(min, dp[N][i]);
    }
    cout << min << endl;
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:113:62: error: 'i' was not declared in this scope
  113 |                 ll index = lower_bound(v.begin(), v.end(), H[i]) - v.begin();
      |                                                              ^
a.cc:122:48: error: no matching function for call to 'min(ll&, ll*)'
  122 |                     dp[i + 1][k + 1] = std::min(dp[i + 1][k + 1], dp[i][k] + V[i]);
      |                                        ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:122:48: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'll*' {aka 'long long int*'})
  122 |                     dp[i + 1][k + 1] = std::min(dp[i + 1][k + 1], dp[i][k] + V[i]);
      |                                        ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281: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:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:122:48: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
  122 |                     dp[i + 1][k + 1] = std::min(dp[i + 1][k + 1], dp[i][k] + V[i]);
      |                                        ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:123:48: error: invalid types 'int[ll {aka long long int}]' for array subscript
  123 |                     seg.set(i, v[i] - dp[i + 1 [k + 1]]);
      |                                                ^
a.cc:129:32: error: no matching function for call to 'max(ll [305], ll)'
  129 |                 V[i] = std::max(V[i], seg.get(0, i));
      |                        ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
/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:129:32: note:   deduced conflicting types for parameter 'const _Tp' ('long long int [305]' and 'll' {aka 'long long int'})
  129 |                 V[i] = std::max(V[i], seg.get(0, i));
      |                        ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
/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
/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:129:32: note:   mismatched types 'std::initializer_list<_Tp>' and 'll*' {aka 'long long int*'}
  129 |                 V[i] = std::max(V[i], seg.get(0, i));
      |                        ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~