alexjercan/bugnet huggingface dataset:
Problem p00546 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int zonbi[1 << 17];
priority_queue<pair<long, int>> Q;
vector<int> G[1 << 17];
long p, q;
long d[1 << 17];
main() {
  int n, m, k, s;
  cin >> n >> m >> k >> s;
  cin >> p >> q;
  for (int i = 0; i < k; i++) {
    int a;
    cin >> a;
    a--;
    Q.push(make_pair(0, a));
    zonbi[a] = -1;
  }
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  while (!Q.empty()) {
    pair<int, int> a = Q.top();
    Q.pop();
    int u = a.second, v = -a.first;
    if (v >= s)
      continue;
    v++;
    for (int i = 0; i < G[u].size(); i++) {
      int next = G[u][i];
      if (zonbi[next] != 0)
        continue;
      zonbi[next] = 1;
      Q.push(make_pair(-v, next));
    }
  }
  for (int i = 0; i < n; i++)
    d[i] = 1LL << 60;
  Q.push(make_pair(0, 0));
  d[0] = 0;
  while (!Q.empty()) {
    pair<long, int> a = Q.top();
    Q.pop();
    long u = a.second, v = -a.first;
    if (d[u] < v)
      continue;
    for (int i = 0; i < G[u].size(); i++) {
      int next = G[u][i];
      if (zonbi[next] == -1)
        continue;
      long cost = (zonbi[next] ? q : p);
      if (next == n - 1)
        cost = 0;
      if (d[next] <= v + cost)
        continue;
      d[next] = v + cost;
      Q.push(make_pair(-d[next], next));
    }
  }
  cout << d[n - 1] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

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

const int MAX = 1e5;

vector<int> G[MAX];
int n, m, k, s;
int p, q;
bool iso[MAX];
bool isz[MAX];
ll d[MAX];

void dfs(int p, int v, int depth) {
  if (depth > s)
    return;
  if (v != 0 && v != n - 1)
    iso[v] = true;
  for (int i = 0; i < G[v].size(); i++)
    if (G[v][i] != p)
      dfs(v, G[v][i], depth + 1);
  return;
}

void dijkstra() {
  for (int i = 0; i < n; i++)
    d[i] = 1LL << 62;
  d[0] = 0;
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push(P(d[0], 0));
  while (!Q.empty()) {
    P pa = Q.top();
    Q.pop();
    if (pa.first > d[pa.second] || isz[pa.second])
      continue;
    int u = pa.second;
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      if (d[v] > d[u] + (iso[u] ? q : p)) {
        d[v] = d[u] + (iso[u] ? q : p);
        Q.push(P(d[v], v));
      }
    }
  }
  return;
}

int main() {
  cin >> n >> m >> k >> s;
  cin >> p >> q;
  int u, v;
  memset(iso, 0, sizeof(iso));
  memset(isz, 0, sizeof(isz));
  vector<int> zonbi;
  for (int i = 0; i < k; i++) {
    cin >> u;
    u--;
    isz[u] = true;
    zonbi.push_back(u);
  }
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    u--;
    v--;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  for (int i = 0; i < k; i++)
    dfs(-1, zonbi[i], 0);
  dijkstra();
  cout << d[n - 1] - p << endl;
  return 0;
}
Problem p00548 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <stdlib.h>
#define inf 10e18
typedef long long ll;

int main(void) {
  ll i, j, k, n, m;
  scanf("%lld%lld%lld", &n, &m, &k);
  ll a[n], **sum, dp[n];
  sum = (ll **)malloc(sizeof(ll *) * (n + 10));
  for (i = 0; i < n; ++i)
    sum[i] = (ll *)malloc(sizeof(ll) * (m + 10));
  for (i = 0; i < n; ++i)
    scanf("%lld", &a[i]);
  for (i = 0; i < n; ++i)
    dp[i] = inf;
  dp[0] = k;
  for (i = 0; i < n; ++i) {
    ll min = inf, max = 0;
    for (j = 0; j < m; ++j) {
      if (i - j >= 0) {
        if (min > a[i - j])
          min = a[i - j];
        if (max < a[i - j])
          max = a[i - j];
        sum[i][j] = k + (j + 1) * (max - min);
      }
    }
  }
  for (i = 1; i < n; ++i) {
    for (j = 0; j < m; ++j) {
      if (i - j > 0) {
        if (dp[i] > sum[i][j] + dp[i - j - 1])
          dp[i] = sum[i][j] + dp[i - j - 1];
      } else if (!(i - j)) {
        if (dp[i] > sum[i][j])
          dp[i] = sum[i][j];
      }
    }
  }
  printf("%lld\\n", dp[n - 1]);
  for (i = 0; i < n; ++i)
    free(sum[i]);
  free(sum);
  return 0;
}


Error: 0

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#define inf 10e18
typedef long long ll;

int main(void) {
  ll i, j, k, n, m;
  scanf("%lld%lld%lld", &n, &m, &k);
  ll a[n], **sum, dp[n];
  sum = (ll **)malloc(sizeof(ll *) * (n + 10));
  for (i = 0; i < n; ++i)
    sum[i] = (ll *)malloc(sizeof(ll) * (m + 10));
  for (i = 0; i < n; ++i)
    scanf("%lld", &a[i]);
  for (i = 0; i < n; ++i)
    dp[i] = inf;
  dp[0] = k;
  for (i = 0; i < n; ++i) {
    ll min = inf, max = 0;
    for (j = 0; j < n; ++j) {
      if (i - j >= 0) {
        if (min > a[i - j])
          min = a[i - j];
        if (max < a[i - j])
          max = a[i - j];
        sum[i][j] = k + (j + 1) * (max - min);
      }
    }
  }
  for (i = 1; i < n; ++i) {
    for (j = 0; j < m; ++j) {
      if (i - j > 0) {
        if (dp[i] > sum[i][j] + dp[i - j - 1])
          dp[i] = sum[i][j] + dp[i - j - 1];
      } else if (!(i - j)) {
        if (dp[i] > sum[i][j])
          dp[i] = sum[i][j];
      }
    }
  }
  printf("%lld\\n", dp[n - 1]);
  for (i = 0; i < n; ++i)
    free(sum[i]);
  free(sum);
  return 0;
}
Problem p00548 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "algorithm"
#include "climits"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "math.h"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "utility"

using namespace std;
const long long int MOD = 1000000007;

int N, M, K;
long long int num[20001];
long long int dp[20001];

int main() {
  ios::sync_with_stdio(false);
  cin >> N >> M >> K;
  for (int i = 1; i <= N; i++)
    cin >> num[i];
  for (int i = 1; i <= N; i++)
    dp[i] = LLONG_MAX;
  for (int i = 0; i < N; i++) {
    long long int mi = LLONG_MAX;
    long long int MA = LLONG_MIN;
    for (int j = i + 1; j <= min(i + M, N); j++) {
      mi = min(mi, num[j]);
      MA = max(MA, num[j]);
      dp[j] = min(dp[j], dp[i] + K + (j - i) * (MA - mi));
    }
  }
  cout << dp[N] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "algorithm"
#include "climits"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "math.h"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "utility"

using namespace std;
const long long int MOD = 1000000007;

int N, M, K;
long long int num[20001];
long long int dp[20001];

int main() {
  ios::sync_with_stdio(false);
  cin >> N >> M >> K;
  for (int i = 1; i <= N; i++)
    cin >> num[i];
  for (int i = 1; i <= N; i++)
    dp[i] = LLONG_MAX;
  for (int i = 0; i < N; i++) {
    set<long long int> S;
    for (int j = i + 1; j <= min(i + M, N); j++) {
      S.insert(num[j]);
      dp[j] = min(dp[j], dp[i] + K + (j - i) * (*S.rbegin() - *S.begin()));
    }
  }
  cout << dp[N] << endl;
  return 0;
}
Problem p00548 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"
long long int dp[20001];
int main() {
  for (int i = 0; i < 20001; ++i) {
    dp[i] = 1e18;
  }
  dp[0] = 0;
  int N, M, K;
  cin >> N >> M >> K;
  vector<long long int> ws(N);
  for (int i = 0; i < N; ++i) {
    cin >> ws[i];
  }
  for (int i = 0; i < N; ++i) {
    long long int amin = 1e18;
    long long int amax = 0;
    long long int cost = K;
    for (int j = 0; j < min(N - i, M); ++j) {
      amin = min(amin, ws[i + j]);
      amax = max(amax, ws[i + j]);
      dp[i + j + 1] =
          min(dp[i + j + 1], dp[i] + cost + (j + 1) * (amax - amin));
    }
  }
  cout << dp[N] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"
long long int dp[20001];
int main() {
  fill(dp, dp + sizeof(dp), static_cast<long long int>(1e18));
  dp[0] = 0;
  int N, M, K;
  cin >> N >> M >> K;
  vector<long long int> ws(N);
  for (int i = 0; i < N; ++i) {
    cin >> ws[i];
  }
  for (int i = 0; i < N; ++i) {
    long long int amin = 1e18;
    long long int amax = 0;
    long long int cost = K;
    for (int j = 0; j < min(N - i, M); ++j) {
      amin = min(amin, ws[i + j]);
      amax = max(amax, ws[i + j]);
      dp[i + j + 1] =
          min(dp[i + j + 1], dp[i] + cost + (j + 1) * (amax - amin));
    }
  }
  cout << dp[N] << endl;
  return 0;
}
Problem p00548 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N, M, K;
ll A[100005];

ll dp[20005];

const ll INF = 1000000000000000000LL;

ll solve() {
  fill(dp, dp + N + 1, INF);
  dp[0] = 0;
  for (ll i = 1; i <= N; i++) {
    ll a = -INF, b = INF;
    for (ll j = i - 1; j >= max(0LL, i - M); j--) {
      b = min(A[j], b);
      a = max(A[j], a);
      ll c = K + (i - j) * (a - b);
      assert(c >= 0);
      dp[i] = min(dp[i], dp[j] + c);
    }
  }
  return dp[N];
}

int main() {
  cin >> N >> M >> K;
  assert(N <= 20000);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  cout << solve() << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N, M, K;
ll A[100005];

ll dp[20005];

const ll INF = 1000000000000000000LL;

ll solve() {
  fill(dp, dp + N + 1, INF);
  dp[0] = 0;
  for (ll i = 1; i <= N; i++) {
    ll a = -INF, b = INF;
    for (ll j = i - 1; j >= max(0LL, i - M); j--) {
      b = min(A[j], b);
      a = max(A[j], a);
      ll c = K + (i - j) * (a - b);
      assert(c >= 0);
      dp[i] = min(dp[i], dp[j] + c);
    }
  }
  return dp[N];
}

int main() {
  cin >> N >> M >> K;
  assert(N < 20000);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  cout << solve() << endl;
}
Problem p00548 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#define INF 1001001001001001
using namespace std;
typedef long long ll;

ll dp[20010];
ll ary[20010];
int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  for (int i = 1; i <= N; i++) {
    cin >> ary[i];
  }
  /*
  fill(dp[0],dp[0]+N+1,INF);
  fill(dp[1],dp[1]+N+1,INF);
  dp[0][0]=0;
  int in=0,ou=1;
  ll res=INF;
  for(int i=0;i<N;i++){
          fill(dp[ou],dp[ou]+N+1,INF);
          ll cnt,ma,mi;
          for(int j=0;j<N;j++){
                  cnt=0;ma=0;mi=INF;
                  for(int k=1;k<=M&&j+k<=N;k++){
                          ma=max(ma,ary[j+k]);
                          mi=min(mi,ary[j+k]);
                          dp[ou][j+k]=min(dp[ou][j+k],dp[in][j]+K+k*(ma-mi));
                  }
          }
          res=min(res,dp[ou][N]);
          swap(in,ou);
  }
  */
  fill(dp, dp + N + 1, INF);
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    ll ma = 0, mi = INF;
    for (int j = 1; j <= M && i + j <= N; j++) {
      ma = max(ma, ary[i + j]);
      mi = min(mi, ary[i + j]);
      dp[i + j] = min(dp[i + j], dp[i] + K + j * (ma - mi));
    }
  }
  cout << dp[N] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define INF 1001001001001001
using namespace std;
typedef long long ll;

ll dp[2][20010];
ll ary[20010];
int main() {
  ll N, M, K;
  cin >> N >> M >> K;
  for (int i = 1; i <= N; i++) {
    cin >> ary[i];
  }
  fill(dp[0], dp[0] + N + 1, INF);
  fill(dp[1], dp[1] + N + 1, INF);
  dp[0][0] = 0;
  int in = 0, ou = 1;
  ll res = INF;
  for (int i = 0; i < N; i++) {
    fill(dp[ou], dp[ou] + N + 1, INF);
    ll cnt, ma, mi;
    for (int j = 0; j < N; j++) {
      cnt = 0;
      ma = 0;
      mi = INF;
      for (int k = 1; k <= M && j + k <= N; k++) {
        ma = max(ma, ary[j + k]);
        mi = min(mi, ary[j + k]);
        dp[ou][j + k] = min(dp[ou][j + k], dp[in][j] + K + k * (ma - mi));
      }
    }
    res = min(res, dp[ou][N]);
    swap(in, ou);
  }
  cout << res << endl;
  return 0;
}
Problem p00548 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>

constexpr int MAX_N = 20001, MAX_M = 1001;
constexpr long long int INF = (long long int)1e18;

int n, m, k;
long long int dp[MAX_N];
long long int a[MAX_N];

int main() {
  std::cin >> n >> m >> k;
  for (int i = 1; i <= n; ++i)
    std::cin >> a[i];
  std::fill(dp + 1, dp + n + 1, INF);
  long long int max, min;
  for (int i = 1; i <= n; ++i) {
    max = 0;
    min = INF;
    for (int j = 1; j <= m && j <= i; ++j) {
      max = std::max(max, a[i - j + 1]);
      min = std::min(min, a[i - j + 1]);
      dp[i] = std::min(dp[i], dp[i - j] + (max - min) * j + k);
    }
  }
  std::cout << dp[n] << std::endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>

constexpr int MAX_N = 20001, MAX_M = 1001;
constexpr long long int INF = (long long int)1e15;

int n, m, k;
long long int dp[MAX_N];
long long int a[MAX_N];
long long int max[MAX_N][MAX_M], min[MAX_N][MAX_M];

int main() {
  std::cin >> n >> m >> k;
  for (int i = 1; i <= n; ++i)
    std::cin >> a[i];
  std::fill(dp + 1, dp + n + 1, INF);

  for (int i = 1; i <= n; ++i) {
    long long int _max = 0, _min = INF;
    for (int j = 1; j <= m && j <= i; ++j) {
      _max = std::max(_max, a[i - j + 1]);
      max[i][j] = _max;
      _min = std::min(_min, a[i - j + 1]);
      min[i][j] = _min;
    }
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m && j <= i; ++j)
      dp[i] = std::min(dp[i], dp[i - j] + (max[i][j] - min[i][j]) * j + k);
  }
  std::cout << dp[n] << std::endl;
  return 0;
}
Problem p00548 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll N, M, K;
ll ookisa[20000];
ll dp[20000][1001];

P get(int a, int b) {
  P res;
  res.first = *max_element(ookisa + a, ookisa + b + 1);
  res.second = *min_element(ookisa + a, ookisa + b + 1);
  return res;
}

int main() {
  cin >> N >> M >> K;
  for (int i = 0; i < N; i++)
    cin >> ookisa[i];
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      dp[i][j] = 1145141919810893;

  dp[0][0] = K;

  for (int i = 1; i < N; i++) {

    P minmax;
    minmax.first = ookisa[i], minmax.second = ookisa[i];

    for (int j = 1; j < M; j++) {
      if (i - j >= 0) {
        minmax.first = max(minmax.first, ookisa[i - j]);
        minmax.second = min(minmax.second, ookisa[i - j]);
        dp[i][j] = min(dp[i][j],
                       dp[i - j][0] + (j + 1) * (minmax.first - minmax.second));
      }
    }

    for (int j = 0; j < M; j++) {
      dp[i][0] = min(dp[i][0], dp[i - 1][j] + K);
    }
  }

  ll ans = 1145141919810893;

  for (int i = 0; i < M; i++)
    ans = min(ans, dp[N - 1][i]);
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

ll N, M, K;
ll ookisa[20000];
ll dp[20000][1001];

P get(int a, int b) {
  P res;
  res.first = *max_element(ookisa + a, ookisa + b + 1);
  res.second = *min_element(ookisa + a, ookisa + b + 1);
  return res;
}

int main() {
  cin >> N >> M >> K;
  for (int i = 0; i < N; i++)
    cin >> ookisa[i];
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      dp[i][j] = 1145141919810893;

  dp[0][M - 1] = K;

  for (int i = 0; i < N - 1; i++) {
    for (int j = 0; j < M; j++) {
      if (dp[i][j] == 1145141919810893)
        continue;
      if (j == 0) {
        dp[i + 1][M - 1] = min(dp[i + 1][M - 1], dp[i][j] + K);
      } else {
        P minmax = get(i - (M - j - 1), i + 1);

        dp[i + 1][j - 1] = min(
            dp[i + 1][j - 1], dp[i - (M - j - 1)][M - 1] +
                                  (M - j + 1) * (minmax.first - minmax.second));

        dp[i + 1][M - 1] = min(dp[i + 1][M - 1], dp[i][j] + K);
        // cout << i << " " << j << " " << dp[i][j] << endl;
      }
    }
  }

  ll ans = 1145141919810893;

  for (int i = 0; i < M; i++)
    ans = min(ans, dp[N - 1][i]);
  cout << ans << endl;

  return 0;
}
Problem p00548 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

#define INF (10000000000000000LL)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

int main() {
  int N, M;
  long long K;
  cin >> N >> M >> K;

  vector<long long> A;

  for (int i = 0; i < N; ++i) {
    long long temp;
    cin >> temp;
    A.push_back(temp);
  }

  vector<long long> dp(N);
  long long mx = -1;
  long long mn = INF;
  for (int i = 0; i < N; ++i) { // initialize
    mx = MAX(mx, A[i]);
    mn = MIN(mn, A[i]);
    if (i < M)
      dp[i] = K + (i + 1) * (mx - mn);
    else
      dp[i] = INF;
  }

  int indx = 1;
  while (indx < N) {
    mx = -1;
    mn = INF;
    long long temp = dp[indx];
    for (int k = 1; k <= M && indx - k >= 0; ++k) { // add one more box
      mx = MAX(mx, A[indx - k + 1]);
      mn = MIN(mn, A[indx - k + 1]);
      temp = MIN(temp, dp[indx - k] + K + k * (mx - mn));
    }
    if (dp[indx] == temp)
      ++indx;
    else
      dp[indx] = temp;
  }

  cout << dp[N - 1] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

#define INF (10000000000000000LL)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

int main() {
  int N, M;
  long long K;
  cin >> N >> M >> K;

  vector<long long> A;
  for (int i = 0; i < N; ++i) {
    long long temp;
    cin >> temp;
    A.push_back(temp);
  }

  long long dp[2][N];
  long long mn = INF;
  long long mx = -1;

  for (int i = 0; i < N; ++i) {
    if (i < M) {
      mn = MIN(mn, A[i]);
      mx = MAX(mx, A[i]);
      dp[0][i] = K + (i + 1) * (mx - mn);
    } else {
      dp[0][i] = INF;
    }
  }

  for (int i = 1; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      long long temp = INF;
      mn = INF;
      mx = -1;
      for (int k = 1; k <= M && j - k >= 0; ++k) {
        mn = MIN(mn, A[j - k + 1]);
        mx = MAX(mx, A[j - k + 1]);
        temp = MIN(temp, dp[(i - 1) % 2][j - k] + K + k * (mx - mn));
      }
      dp[i % 2][j] = MIN(temp, dp[(i - 1) % 2][j]);
    }
  }

  cout << dp[(N - 1) % 2][N - 1] << endl;

  return 0;
}
Problem p00548 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdint>
#include <iostream>

#define MAX_N 20001

using namespace std;

uint64_t N, M, K, A[MAX_N], T[MAX_N];

uint64_t min(uint64_t a, uint64_t b) {
  if (a < b)
    return a;
  else
    return b;
}

uint64_t max(uint64_t a, uint64_t b) {
  if (a > b)
    return a;
  else
    return b;
}
int prev_end_min, prev_begin_min, prev_end_max, prev_begin_max, prev_min,
    prev_max;
uint64_t getmin(int begin, int end) {
  if (prev_end_min == end && prev_begin_min == begin + 1) {
    if (prev_min > A[begin]) {
      prev_min = A[begin];
    }
    prev_begin_min = begin;
    return prev_min;
  } else {
    uint64_t res = UINT64_MAX;
    for (int i = begin; i <= end; i++)
      res = min(res, A[i]);
    prev_begin_min = begin;
    prev_end_min = end;
    prev_min = res;
    return res;
  }
}

uint64_t getmax(int begin, int end) {
  if (prev_end_max == end && prev_begin_max == begin + 1) {
    if (prev_max < A[begin]) {
      prev_max = A[begin];
    }
    prev_begin_max = begin;
    return prev_max;
  } else {
    uint64_t res = 0;
    for (int i = begin; i <= end; i++)
      res = max(res, A[i]);
    prev_begin_max = begin;
    prev_end_max = end;
    prev_max = res;
    return res;
  }
}

int main(void) {
  cin >> N >> M >> K;
  for (int i = 0; i < MAX_N; i++) {
    T[i] = UINT64_MAX;
  }
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  T[N] = 0;
  for (int i = N - 1; i >= 0; i--) {
    for (int k = 1; k <= M && (i + k) <= N; k++) {
      uint64_t cost = K + (k * (getmax(N - (i + k), (N - i) - 1) -
                                getmin(N - (i + k), (N - i) - 1)));
      T[i] = min(T[i], T[i + k] + cost);
    }
  }
  cout << T[0] << endl;
}

Error: TLE

Fixed submission:

#include <cstdint>
#include <iostream>

#define MAX_N 20001

using namespace std;

uint64_t N, M, K, A[MAX_N], T[MAX_N];

uint64_t min(uint64_t a, uint64_t b) {
  if (a < b)
    return a;
  else
    return b;
}

uint64_t max(uint64_t a, uint64_t b) {
  if (a > b)
    return a;
  else
    return b;
}

uint64_t getmin(int begin, int end) {
  uint64_t res = UINT64_MAX;
  for (int i = begin; i <= end; i++)
    res = min(res, A[i]);
  return res;
}

uint64_t getmax(int begin, int end) {
  uint64_t res = 0;
  for (int i = begin; i <= end; i++)
    res = max(res, A[i]);
  return res;
}

uint64_t getsum(int begin, int end) {
  uint64_t res = 0;
  for (int i = begin; i <= end; i++)
    res = res + A[i];
  return res;
}

int main(void) {
  cin >> N >> M >> K;
  for (int i = 0; i < MAX_N; i++) {
    T[i] = UINT64_MAX;
  }
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  T[N] = 0;
  for (int i = N - 1; i >= 0; i--) {
    for (int k = 1; k <= M && (i + k) <= N; k++) {
      uint64_t cost = K + (k * (getmax(N - (i + k), (N - i) - 1) -
                                getmin(N - (i + k), (N - i) - 1)));
      T[i] = min(T[i], T[i + k] + cost);
    }
  }
  cout << T[0] << endl;
}
Problem p00548 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;

long long int n, m, k;
long long int a[20010];
long long int c[20010];

int main() {
  scanf("%lld %lld %lld", &n, &m, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  c[1] = k;
  for (int i = 2; i <= n; ++i) {
    long long int t = 1e18;
    long long int ma = 0;
    long long int mi = 1e18;
    for (int j = 1; j <= m; ++j) {
      if (i - j < 0)
        break;
      ma = max(ma, a[i - j + 1]);
      mi = min(mi, a[i - j + 1]);
      t = min(t, c[i - j] + j * (ma - mi) + k);
    }
    c[i] = t;
  }
  printf("%lld\\n", c[n]);
  /*	for(int i=1; i<=n; ++i) printf("%lld\\t", c[i]);
          printf("\\n");
  */
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;

long long int n, m, k;
long long int a[20010];
long long int c[20010];

long long int mini(int l, int r) {
  long long int res = 1e18;
  for (int i = 0; i < r - l + 1; ++i)
    res = min(res, a[l + i]);
  return res;
}

long long int maxi(int l, int r) {
  long long int res = 0;
  for (int i = 0; i < r - l + 1; ++i)
    res = max(res, a[l + i]);
  return res;
}

int main() {
  scanf("%lld %lld %lld", &n, &m, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  c[1] = k;
  for (int i = 2; i <= n; ++i) {
    long long int t = 1e18;
    for (int j = 1; j <= m; ++j) {
      if (i - j < 0)
        break;
      long long int mi, ma;
      mi = mini(i - j + 1, i);
      ma = maxi(i - j + 1, i);
      t = min(t, c[i - j] + j * (ma - mi) + k);
    }
    c[i] = t;
  }
  printf("%lld\\n", c[n]);
  /*	for(int i=1; i<=n; ++i) printf("%lld\\t", c[i]);
          printf("\\n");
  */
}