Problem p00548 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1 << 30
#define LINF 1LL << 60

/*
<url:https://www.ioi-jp.org/joi/2015/2016-ho/2016-ho.pdf>
?????????============================================================
N(1<=N<=20000)???????????¬???????????????????????¬??????i?????§?????????Ai??§?????????

???????????¬????????????????????????????????????????????±??????????????????????????¨?????????????????¨????????±??????
??£?¶?????????????????????¬??????????????????????????¨?????§????????????

?????¨????????±???????????§??§M(1<=M<=1000)?????§?????¬???????????????????????¨?????§??????
??±????????¬?????????????????????????????????????????????????????±?????????????????§????????¬???????????§?????????a,
??±????????????????°?????????¬???????????§?????????b,
??±?????????????????¬??????????????°???s??¨????????¨???, K+s*(a-b)??¨?????????
K?????±???????????????????????§???????????¨????????±??§??±???

??????????????°?????±?????¨?????????????????????????????¬????????????????????±??°??????????????¨??§??±??°???????????????
???????????????????????§?????????????°????????????????
=================================================================
?§£??¬=============================================================
================================================================
*/
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, M, K;
  cin >> N >> M >> K;
  vector<int> A(N);
  for (auto &in : A)
    cin >> in;

  //	vector<vector<pair<ll, pair<int, int>>>> dp(N + 1, vector<pair<ll,
  //pair<int, int>>>(M + 1, { LINF,{-INF,INF} })); 	dp[0][0].first = 0;
  vector<vector<pair<ll, pair<int, int>>>> dp(
      2, vector<pair<ll, pair<int, int>>>(M, {LINF, {-INF, INF}}));
  int cur = 0, next = 1;
  dp[0][0].first = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      dp[next][j] = {LINF, {-INF, INF}};
    }
    for (int j = 0; j < M; j++) {
      if (dp[cur][j].first == LINF)
        continue;
      if (j != M - 1 && i != N - 1) {
        dp[next][j + 1] =
            min(dp[next][j + 1], {dp[cur][j].first,
                                  {max(dp[cur][j].second.first, A[i]),
                                   min(dp[cur][j].second.second, A[i])}});
        //				dp[i + 1][j + 1] = min(dp[i + 1][j + 1], {
        //dp[i][j].first,{max(dp[i][j].second.first,A[i]),min(dp[i][j].second.second,A[i])}
        //});
      }
      dp[next][0] =
          min(dp[next][0],
              {dp[cur][j].first + (ll)K +
                   (ll)(j + 1) * ((ll)max(dp[cur][j].second.first, A[i]) -
                                  (ll)min(dp[cur][j].second.second, A[i])),
               {-INF, INF}});
      //			dp[i + 1][0] = min(dp[i + 1][0], {
      //dp[i][j].first + (ll)K + (ll)(j+1)*((ll)max(dp[i][j].second.first,A[i])
      //- (ll)min(dp[i][j].second.second,A[i])),{-INF,INF} });
    }
    cur = !cur;
    next = !next;
  }
  cout << dp[cur][0].first << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1 << 30
#define LINF 1LL << 60

/*
<url:https://www.ioi-jp.org/joi/2015/2016-ho/2016-ho.pdf>
?????????============================================================
N(1<=N<=20000)???????????¬???????????????????????¬??????i?????§?????????Ai??§?????????

???????????¬????????????????????????????????????????????±??????????????????????????¨?????????????????¨????????±??????
??£?¶?????????????????????¬??????????????????????????¨?????§????????????

?????¨????????±???????????§??§M(1<=M<=1000)?????§?????¬???????????????????????¨?????§??????
??±????????¬?????????????????????????????????????????????????????±?????????????????§????????¬???????????§?????????a,
??±????????????????°?????????¬???????????§?????????b,
??±?????????????????¬??????????????°???s??¨????????¨???, K+s*(a-b)??¨?????????
K?????±???????????????????????§???????????¨????????±??§??±???

??????????????°?????±?????¨?????????????????????????????¬????????????????????±??°??????????????¨??§??±??°???????????????
???????????????????????§?????????????°????????????????
=================================================================
?§£??¬=============================================================
================================================================
*/
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, M, K;
  cin >> N >> M >> K;
  vector<int> A(N);
  for (auto &in : A)
    cin >> in;

  vector<vector<pair<ll, pair<int, int>>>> dp(
      N + 1, vector<pair<ll, pair<int, int>>>(M + 1, {LINF, {-INF, INF}}));
  dp[0][0].first = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (dp[i][j].first == LINF)
        continue;
      if (j != M - 1 && i != N - 1) {
        dp[i + 1][j + 1] =
            min(dp[i + 1][j + 1], {dp[i][j].first,
                                   {max(dp[i][j].second.first, A[i]),
                                    min(dp[i][j].second.second, A[i])}});
      }
      dp[i + 1][0] =
          min(dp[i + 1][0],
              {dp[i][j].first + (ll)K +
                   (ll)(j + 1) * ((ll)max(dp[i][j].second.first, A[i]) -
                                  (ll)min(dp[i][j].second.second, A[i])),
               {-INF, INF}});
    }
  }
  cout << dp[N][0].first << endl;
  return 0;
}