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

//============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :???8:45 9:41
// 2nd 10:43 11:55
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

using namespace std;
const long long INF = 2000000000000001;

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

  long long R = 0;
  long long dp[1001] = {};
  long long dMax[1001] = {};
  long long dMin[1001] = {};
  for (long long j = M; j > 0; --j) {
    dMin[j] = INF;
    dp[j] = INF;
  }
  for (int i = 0; i < N; ++i) {
    cin >> A;
    R = R + K;
    for (long long j = M; j > 1; --j) {
      dMax[j] = max(dMax[j - 1], A);
      dMin[j] = min(dMin[j - 1], A);
      dp[j] = dp[j - 1] - (j - 1) * (dMax[j - 1] - dMin[j - 1]) +
              j * (dMax[j] - dMin[j]);
      R = min(R, dp[j]);
    }
    dp[1] = dp[0] + K;
    dMax[1] = A;
    dMin[1] = A;
    dp[0] = R;
  }

  cout << R << endl;
}

Error: MLE

Fixed submission:

//============================================================================
// Name        : JOI.cpp
// Author      :2015 ho3
// Version     :???11:08
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

using namespace std;
long long N, M, K;
long long A[20001] = {};
long long dp[20001][1001];
long long maxD[20001][1001];
long long minD[20001][1001];
const long long INF = 100000000000000000;

long long calc(long long n1, long long m1) {
  long long ans = INF;
  if (dp[n1][m1] > -1)
    return dp[n1][m1];
  if (m1 > 0) {
    ans = calc(n1, 0) + K + m1 * (maxD[n1][m1] - minD[n1][m1]);
  } else {
    for (long long i = 1; i < M + 1 && i < n1 + 1; ++i) {
      ans = min(ans, calc(n1 - i, i));
    }
  }
  dp[n1][m1] = ans;
  return ans;
}
int main() {
  cin >> N >> M >> K;
  for (long long i = 1; i < N + 1; i++)
    cin >> A[i];

  fill(dp[0], dp[20001], -1);
  for (int i = 0; i < N + 1; i++) {
    maxD[i][0] = 0;
    minD[i][0] = INF;
    for (int j = 1; j < M + 1; j++) {
      maxD[i][j] = max(maxD[i][j - 1], A[i + j]);
      minD[i][j] = min(minD[i][j - 1], A[i + j]);
    }
  }
  dp[0][0] = 0;
  long long R;
  for (int i = 1; i < N + 1; i++)
    R = calc(i, 0);
  cout << R << endl;
  //	cout<<calc(N,0)<<endl;
  return 0;
}
Problem p00548 (C++) — buggy submission, verdict: Runtime Error

//============================================================================
// Name        : JOI.cpp
// Author      :2015 ho3
// Version     :???11:08
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

using namespace std;
long long N, M, K;
long long A[20001] = {};
long long dp[20001];
long long mmD[20001][1001];
const long long INF = 100000000000000000;

long long calc(long long n1) {
  if (dp[n1] > -1)
    return dp[n1];
  long long ans = INF;
  for (long long i = 1; i < M + 1 && i < n1 + 1; ++i) {
    ans = min(ans, calc(n1 - i) + K + i * mmD[n1 - i][i]);
  }
  dp[n1] = ans;
  return ans;
}

int main() {
  cin >> N >> M >> K;
  for (long long i = 1; i < N + 1; i++)
    cin >> A[i];

  fill(dp, dp + 20001, -1);
  long long maxD[1001] = {};
  long long minD[1001] = {};

  for (int i = 0; i < N + 1; i++) {
    maxD[0] = 0;
    minD[0] = INF;
    for (int j = 1; j < M + 1 && (i + j) < N + 1; j++) {
      maxD[j] = max(maxD[j - 1], A[i + j]);
      minD[j] = min(minD[j - 1], A[i + j]);
      mmD[i][j] = maxD[j] - minD[j];
    }
  }
  dp[0] = 0;
  //	long long R;
  //	for(int i=1;i<N;i++){
  //		for(int j=0;j<M+1;j++)
  //		R=calc(i,j);
  //	}
  //	R=calc(N,0);
  //	cout<<R<<endl;
  cout << calc(N) << endl;
  return 0;
}

Error: -11

Fixed submission:

//============================================================================
// Name        : JOI.cpp
// Author      :2015 ho3
// Version     :???11:08
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>

using namespace std;
long long N, M, K;
long long A[20001] = {};
long long dp[20001];
long long mmD[20001][1001];
const long long INF = 100000000000000000;

long long calc(long long n1) {
  if (dp[n1] > -1)
    return dp[n1];
  long long ans = INF;
  for (long long i = 1; i < M + 1 && i < n1 + 1; ++i) {
    ans = min(ans, calc(n1 - i) + K + i * mmD[n1 - i][i]);
  }
  dp[n1] = ans;
  return ans;
}

int main() {
  cin >> N >> M >> K;
  for (long long i = 1; i < N + 1; i++)
    cin >> A[i];

  fill(dp, dp + 20001, -1);
  long long maxD[1001] = {};
  long long minD[1001] = {};

  for (int i = 0; i < N + 1; i++) {
    maxD[0] = 0;
    minD[0] = INF;
    for (int j = 1; j < M + 1; j++) {
      maxD[j] = max(maxD[j - 1], A[i + j]);
      minD[j] = min(minD[j - 1], A[i + j]);
      mmD[i][j] = maxD[j] - minD[j];
    }
  }
  dp[0] = 0;
  //	long long R;
  //	for(int i=1;i<N;i++){
  //		for(int j=0;j<M+1;j++)
  //		R=calc(i,j);
  //	}
  //	R=calc(N,0);
  //	cout<<R<<endl;
  cout << calc(N) << endl;
  return 0;
}
Problem p00548 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[2][1010];
ll a[2][1010], b[2][1010];
int main() {
  ll n, m, k;
  cin >> n >> m >> k;
  vector<ll> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < m; j++) {
      dp[i][j] = LLONG_MAX;
      a[i][j] = INT_MIN;
      b[i][j] = INT_MAX;
    }
  }
  dp[0][0] = k;
  a[0][0] = b[0][0] = v[0];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (dp[0][j] == LLONG_MAX)
        continue;
      // その箱に追加する場合
      if (j + 1 < m) {
        dp[1][j + 1] = dp[0][j];
        a[1][j + 1] = max((ll)a[0][j], v[i]);
        b[1][j + 1] = min((ll)b[0][j], v[i]);
      }
      // 新しく箱を作る場合
      ll num = k + dp[0][j];
      num += (a[0][j] - b[0][j]) * (j + 1);
      dp[1][0] = min(dp[1][0], num);
      a[1][0] = b[1][0] = v[i];
    }
    for (int j = 0; j < m; j++) {
      dp[0][j] = dp[1][j];
      dp[1][j] = LLONG_MAX;
      a[0][j] = a[1][j];
      a[1][j] = INT_MIN;
      b[0][j] = b[1][j];
      b[1][j] = INT_MAX;
    }
  }
  ll ans = LLONG_MAX;
  for (int i = 0; i < m; i++) {
    ll num = dp[0][i];
    num += (a[0][i] - b[0][i]) * (i + 1);
    ans = min(ans, num);
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 200001
const int inf = 1e18;

int mn[2][1010], mx[2][1010];
int a[MAX], dp[2][1010];
signed main() {
  int n, m, k;
  fill((int *)dp, (int *)(dp + 2), inf);
  fill((int *)mn, (int *)(mn + 2), inf);
  scanf("%lld%lld%lld", &n, &m, &k);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);

  dp[0][0] = k;
  mn[0][0] = mx[0][0] = a[0];
  for (int i = 1; i < n; i++) {
    for (int j = 1; j < m; j++) {
      if (dp[0][j - 1] == inf)
        continue;
      mn[1][j] = min(mn[0][j - 1], a[i]);
      mx[1][j] = max(mx[0][j - 1], a[i]);
      dp[1][j] = dp[0][j - 1];
    }
    for (int j = 0; j < m; j++) {
      if (dp[0][j] == inf)
        continue;
      int sum = dp[0][j] + (j + 1) * (mx[0][j] - mn[0][j]) + k;
      dp[1][0] = min(dp[1][0], sum);
      mx[1][0] = mn[1][0] = a[i];
    }
    for (int j = 0; j < m; j++) {
      mn[0][j] = mn[1][j];
      mn[1][j] = inf;
      mx[0][j] = mx[1][j];
      mx[1][j] = 0;
      dp[0][j] = dp[1][j];
      dp[1][j] = inf;
    }
  }

  int ans = dp[0][0];
  for (int i = 1; i < n; i++) {
    if (dp[0][i] == inf)
      continue;
    int sum = dp[0][i] + (i + 1) * (mx[0][i] - mn[0][i]);
    ans = min(ans, sum);
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p00548 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 200001
const int inf = 1e18;

int mn[2][1010], mx[2][1010];
int a[MAX], dp[2][1010];
signed main() {
  int n, m, k;
  fill((int *)dp, (int *)(dp + 2), inf);
  fill((int *)mn, (int *)(mn + 2), inf);
  scanf("%lld%lld%lld", &n, &m, &k);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);

  dp[0][0] = k;
  mn[0][0] = mx[0][0] = a[0];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < m; j++) {
      mn[1][j] = inf;
      mx[1][j] = 0;
      dp[1][j] = inf;
    }
    for (int j = 1; j < m; j++) {
      if (dp[0][j - 1] == inf)
        continue;
      mn[1][j] = min(mn[0][j - 1], a[i]);
      mx[1][j] = max(mx[0][j - 1], a[i]);
      dp[1][j] = dp[0][j - 1];
    }
    for (int j = 0; j < m; j++) {
      if (dp[0][j] == inf)
        continue;
      int sum = dp[0][j] + (j + 1) * (mx[0][j] - mn[0][j]) + k;
      dp[1][0] = min(dp[1][0], sum);
      mx[1][0] = mn[1][0] = a[i];
    }
    for (int j = 0; j < m; j++) {
      mn[0][j] = mn[1][j];
      mx[0][j] = mx[1][j];
      dp[0][j] = dp[1][j];
    }
  }

  int ans = dp[0][0];
  for (int i = 1; i < m; i++) {
    int sum = dp[0][i] + (i + 1) * (mx[0][i] - mn[0][i]);
    ans = min(ans, sum);
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 200001
const int inf = 1e18;

int mn[2][1010], mx[2][1010];
int a[MAX], dp[2][1010];
signed main() {
  int n, m, k;
  fill((int *)dp, (int *)(dp + 2), inf);
  fill((int *)mn, (int *)(mn + 2), inf);
  scanf("%lld%lld%lld", &n, &m, &k);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);

  dp[0][0] = k;
  mn[0][0] = mx[0][0] = a[0];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < n; j++) {
      mn[1][j] = inf;
      mx[1][j] = 0;
      dp[1][j] = inf;
    }
    for (int j = 1; j < m; j++) {
      if (dp[0][j - 1] == inf)
        continue;
      mn[1][j] = min(mn[0][j - 1], a[i]);
      mx[1][j] = max(mx[0][j - 1], a[i]);
      dp[1][j] = dp[0][j - 1];
    }
    for (int j = 0; j < m; j++) {
      if (dp[0][j] == inf)
        continue;
      int sum = dp[0][j] + (j + 1) * (mx[0][j] - mn[0][j]) + k;
      dp[1][0] = min(dp[1][0], sum);
      mx[1][0] = mn[1][0] = a[i];
    }
    for (int j = 0; j < m; j++) {
      mn[0][j] = mn[1][j];
      mx[0][j] = mx[1][j];
      dp[0][j] = dp[1][j];
    }
  }

  int ans = dp[0][0];
  for (int i = 1; i < m; i++) {
    int sum = dp[0][i] + (i + 1) * (mx[0][i] - mn[0][i]);
    ans = min(ans, sum);
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p00548 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#define INF 1000000000000000000
typedef long long LL;
using namespace std;
LL N, M, K;
LL ary[20010], dp[20010];
int main() {
  cin >> N >> M >> K;
  for (int i = 1; i <= N; i++) {
    cin >> ary[i];
  }
  fill(dp, dp + N + 1, INF);
  LL a, b;
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    a = b = ary[i + 1];
    for (int j = 1; j <= M && (i + j) <= N; j++) {
      a = max(a, ary[i + j]);
      b = min(b, ary[i + j]);
      dp[i + j] = min(dp[i + j], dp[i] + K + j * (a - b));
    }
  }
  cout << dp[N] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#define INF 10000000000000000
typedef long long LL;
using namespace std;
LL N, M, K;
LL ary[20010], dp[20010];
int main() {
  cin >> N >> M >> K;
  for (LL i = 1; i <= N; i++) {
    cin >> ary[i];
  }
  fill(dp, dp + N + 1, INF);
  LL a, b;
  dp[0] = 0;
  for (LL i = 0; i < N; i++) {
    a = b = ary[i + 1];
    for (LL j = 1; j <= M; j++) {
      a = max(a, ary[i + j]);
      b = min(b, ary[i + j]);
      dp[i + j] = min(dp[i + j], dp[i] + K + j * (a - b));
    }
  }
  cout << dp[N] << endl;
  return 0;
}
Problem p00548 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
ll a[20000], dp[20001];
ll n, m, k;
const ll inf = 98989899898989;
int main() {
  cin >> n >> m >> k;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    dp[i] = inf;
  }
  dp[n] = inf;
  dp[0] = 0;
  ll mi, ma;
  for (ll i = 0; i < n; i++) {
    ma = a[i];
    mi = a[i];
    for (ll j = 1; j <= m && i + j <= n; j++) {
      ma = max(ma, a[i + j - 1]);
      mi = min(mi, a[i + j - 1]);
      dp[i + j] = min(dp[i + j], (dp[i] + k + j * (ma - mi)));
    }
  }
  cout << dp[n] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
ll a[20000], shou[80000], dai[80000], dp[20000], n, m, k2;
const ll inf = 99999999999999999;
int start, ruend;
ll ma, mi;
void query(int a, int b, int k, int l, int r) {
  // a??????b???????°????//k???l??\\???r??\\????????????
  if (b < l || a > r)
    return;
  if (l >= a && r <= b) {
    ma = max(ma, dai[k]);
    mi = min(mi, shou[k]);
  } else {
    query(a, b, k * 2 + 1, l, floor((l + r) / 2.0));
    query(a, b, k * 2 + 2, ceil((l + r) / 2.0), r);
  }
}
ll solve(int i) {
  if (i == n)
    return 0;
  ll sum = inf;
  for (int j = 1; j <= m && i + j <= n; j++) {
    ll res = k2;
    ma = -1;
    mi = inf;
    query(start + i, start + i + j - 1, 0, start, ruend);
    res += (j * (ma - mi));
    if (dp[i + j] != -1)
      sum = min(sum, res + dp[i + j]);
    else {
      sum = min(sum, res + (dp[i + j] = solve(i + j)));
    }
  }
  return sum;
}
int main() {
  cin >> n >> m >> k2;
  for (ll i = 0; i < n * 4; i++) {
    shou[i] = inf;
    dai[i] = -1;
  }
  ll io = 0, iop = 1;
  while (iop < n) {
    io += iop;
    iop *= 2;
  }
  start = io;
  ruend = io + iop - 1;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    dp[i] = -1;
    shou[start + i] = a[i];
    dai[start + i] = a[i];
  }
  for (int i = start - 1; i >= 0; i--) {
    shou[i] = min(shou[i * 2 + 1], shou[i * 2 + 2]);
    dai[i] = max(dai[i * 2 + 1], dai[i * 2 + 2]);
  }
  cout << solve(0) << endl;
}
Problem p00548 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll LLINF = 1LL << 60;
int main() {
  ll N, M, K;
  ll dp[20005];
  ll A[20005];
  fill_n(dp, 20005, LLINF);
  cin >> N >> M >> K;
  dp[0] = 0;
  for (ll i = 0; i < N; i++)
    cin >> A[i];
  for (ll i = 0; i < N; i++) {
    ll maxv = -1, minv = LLINF;
    for (ll j = 0; j < M; j++) {
      if (i + j >= N)
        continue;
      maxv = max(A[i + j], maxv);
      minv = min(A[i + j], minv);
      dp[i + j + 1] = min(dp[i + j + 1], dp[i] + K + (j + 1) * (maxv - minv));
    }
  }
  cout << dp[N] << endl;
  return (0);
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll LLINF = 1LL << 60;
int main() {
  ll N, M, K;
  ll dp[20005];
  ll A[20005];
  fill_n(dp, 20005, LLINF);
  cin >> N >> M >> K;
  dp[0] = 0;
  for (ll i = 0; i < N; i++)
    cin >> A[i];
  for (ll i = 0; i < N; i++) {
    ll maxv = -1, minv = LLINF;
    for (ll j = 0; j < M; j++) {
      maxv = max(A[i + j], maxv);
      minv = min(A[i + j], minv);
      dp[i + j + 1] = min(dp[i + j + 1], dp[i] + K + (j + 1) * (maxv - minv));
    }
  }
  cout << dp[N] << endl;
  return (0);
}
Problem p00548 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int N, M, K;
ll dp[20004];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M >> K;
  int A[N];
  FOR(i, 0, N) cin >> A[i];
  FOR(i, 0, N + 1) dp[i] = 1e18;
  dp[0] = 0;
  FOR(i, 0, N) {
    int mn = 1e9;
    int mx = 0;
    for (int j = i; j >= 0 && j >= i - M + 1; j--) {
      mx = max(mx, A[j]);
      mn = min(mn, A[j]);
      dp[i + 1] = min(dp[i + 1], dp[j] + K + ll(i - j + 1) * (mx - mn));
    }
  }
  cout << dp[N] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FOR(I, A, B) for (int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
ll N, M, K;
// ?????°???????????¨ [i,j)???????°????????????§??????logn??§
const int NMAX = 50000;
int n, dat_min[2 * NMAX - 1], dat_max[2 * NMAX - 1];
void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  FOR(i, 0, 2 * n - 1) {
    dat_min[i] = 1e9;
    dat_max[i] = 0;
  }
}
// k????????????a?????????
void update(int k, int a) {
  k += n - 1;
  dat_min[k] = a;
  dat_max[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat_min[k] = min(dat_min[k * 2 + 1], dat_min[k * 2 + 2]);
    dat_max[k] = max(dat_max[k * 2 + 1], dat_max[k * 2 + 2]);
  }
}
// [l(????????????), r(??????))????????§???????°????query(l,r,0,0,n)
int query(int a, int b, int k, int l, int r, bool MAX) {
  if (r <= a || b <= l)
    return MAX ? 0 : 1e9;
  if (a <= l && r <= b)
    return MAX ? dat_max[k] : dat_min[k];
  else {
    int vl = MAX ? query(a, b, k * 2 + 1, l, (l + r) / 2, 1)
                 : query(a, b, k * 2 + 1, l, (l + r) / 2, 0);
    int vr = MAX ? query(a, b, k * 2 + 2, (l + r) / 2, r, 1)
                 : query(a, b, k * 2 + 2, (l + r) / 2, r, 0);
    return MAX ? max(vl, vr) : min(vl, vr);
  }
}
ll dp[20005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M >> K;
  init(N + 1);
  ll A[N];
  FOR(i, 0, N) {
    cin >> A[i];
    update(i + 1, A[i]);
  }
  FOR(i, 0, N + 1) dp[i] = 1e18;
  dp[0] = 0;
  FOR(i, 1, N + 1) {
    FOR(j, max(1LL, i - M + 1), i + 1) {
      dp[i] = min(dp[i], dp[j - 1] + K +
                             ll(i - j + 1) * (query(j, i + 1, 0, 0, n, 1) -
                                              query(j, i + 1, 0, 0, n, 0)));
    }
  }
  cout << dp[N] << endl;
  return 0;
}
Problem p00548 (C++) — buggy submission, verdict: Runtime Error

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

// #define DEBUG
#define int long long
#define INF 1000000000000000000
#define REP(i, n) for (int i = 1; i <= n; i++)
int n, m, k;
int a[20002];
int cs[20002][1002];
int sm[20002];

signed main() {
  cin >> n >> m >> k;
  REP(i, n) cin >> a[i];
  REP(i, n) {
    int mx = 0;
    int mn = INF;
    REP(j, m) {
      if (n < i + j - 1)
        continue;
      mx = max(mx, a[i + j - 1]);
      mn = min(mn, a[i + j - 1]);
      cs[i][j] = k + j * (mx - mn);
    }
  }
  REP(i, n) sm[i] = INF;
  REP(i, n) REP(j, m) {
    if (n < i - 1 + j)
      continue;
    sm[i - 1 + j] = min(sm[i - 1 + j], sm[i - 1] + cs[i][j]);
  }
#ifdef DEBUG
  REP(i, n) {
    REP(j, m) cout << cs[i][j] << " ";
    cout << endl;
  }
  REP(i, n) cout << sm[i] << " ";
  cout << endl;
#endif
  cout << sm[n] << endl;
  return 0;
}

Error: -11

Fixed submission:

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

// #define DEBUG
#define int long long
#define INF 1000000000000000000
#define REP(i, n) for (int i = 1; i <= n; i++)
int n, m, k;
int a[20002];
int cs[20002][1002];
int sm[20002];

signed main() {
  cin >> n >> m >> k;
  REP(i, n) cin >> a[i];
  REP(i, n) {
    int mx = 0;
    int mn = INF;
    REP(j, m) {
      mx = max(mx, a[i + j - 1]);
      mn = min(mn, a[i + j - 1]);
      cs[i][j] = k + j * (mx - mn);
    }
  }
  REP(i, n) sm[i] = INF;
  REP(i, n) REP(j, m) {
    if (n < i - 1 + j)
      continue;
    sm[i - 1 + j] = min(sm[i - 1 + j], sm[i - 1] + cs[i][j]);
  }
#ifdef DEBUG
  REP(i, n) {
    REP(j, m) cout << cs[i][j] << " ";
    cout << endl;
  }
  REP(i, n) cout << sm[i] << " ";
  cout << endl;
#endif
  cout << sm[n] << endl;
  return 0;
}
Problem p00548 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define INF 1e+18
#define int long long
using namespace std;

signed main() {
  int n, m, k, a[20000];
  static int dp[20001][1001];
  cin >> n >> m >> k;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= 1000; j++)
      dp[i][j] = INF;
  }
  dp[0][0] = 0;
  dp[0][1000] = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    int mi = a[i - 1], ma = a[i - 1];
    for (int j = 0; j < min(m, i); j++) {
      dp[i][j] = dp[i - j - 1][1000] + k + (j + 1) * (ma - mi);
      dp[i][1000] = min(dp[i][1000], dp[i][j]);
      if (i - j - 2 >= 0) {
        mi = min(mi, a[i - j - 2]);
        ma = max(ma, a[i - j - 2]);
      }
    }
  }
  cout << dp[n][1000] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#define INF 1e+18
#define int long long
using namespace std;

signed main() {
  int n, m, k, a[20000];
  int dp[20001][1001];
  cin >> n >> m >> k;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= 1000; j++)
      dp[i][j] = INF;
  }
  dp[0][0] = 0;
  dp[0][1000] = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    int mi = a[i - 1], ma = a[i - 1];
    for (int j = 0; j < min(m, i); j++) {
      dp[i][j] = dp[i - j - 1][1000] + k + (j + 1) * (ma - mi);
      dp[i][1000] = min(dp[i][1000], dp[i][j]);
      if (i - j - 2 >= 0) {
        mi = min(mi, a[i - j - 2]);
        ma = max(ma, a[i - j - 2]);
      }
    }
  }
  cout << dp[n][1000] << endl;
  return 0;
}
Problem p00548 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

int N, M, K;
int64_t A[20000];
constexpr int64_t INF = 1000000000000000;

int64_t dp[20000];
//[i?????°??????
int64_t func(int i) {
  if (i >= N) {
    return 0;
  }
  if (dp[i] > 0) {
    return dp[i];
  }

  int64_t min = A[i], max = A[i];
  int64_t min_cost = INF;
  for (int nexti = i + 1, i_end = std::min(i + M, N); nexti <= i_end; ++nexti) {
    min_cost =
        std::min(min_cost, func(nexti) + K + int64_t(nexti - i) * (max - min));
    min = std::min(min, A[nexti]);
    max = std::max(max, A[nexti]);
  }
  return dp[i] = min_cost;
}

int main() {
  std::cin.sync_with_stdio(false);
  std::cin >> N >> M >> K;
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];
  }
  std::cout << func(0) << std::endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <utility>

int N, M, K;
int64_t A[20009];

int64_t func(int i, int j, int64_t min_a, int64_t max_a) {
  if (i >= N) {
    return (max_a - min_a) * (M - j) + K;
  }
  int64_t res = 0;
  if (j <= 0) {
    res += (max_a - min_a) * (M - 0) + K;
    res += func(i + 1, M - 1, A[i], A[i]);
  } else {
    res += std::min(
        [&]() {
          return (max_a - min_a) * (M - j) + K + func(i + 1, M - 1, A[i], A[i]);
        }(),
        [&]() {
          return func(i + 1, j - 1, std::min(min_a, A[i]),
                      std::max(max_a, A[i]));
        }());
  }
  return res;
}
int main() {
  scanf("%d %d %d", &N, &M, &K);
  for (int i = 0; i < N; ++i) {
    scanf("%lld", &A[i]);
  }

  printf("%lld\\n", func(1, M - 1, A[0], A[0]));
}