prithivMLmods/Coder-Stat huggingface dataset:
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;

#define EPS (1e-7)
const long long INF = 1LL << 60;
#define PI (acos(-1))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1}; // 移動方向

ll n, k;
ll h[100010];
ll dp[100010];

void input() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i)
    cin >> h[i];
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  input();
  for (int i = 0; i < n; ++i)
    dp[i] = INF;
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j <= k; ++j) {
      chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
    }
  }
  cout << dp[n - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;

#define EPS (1e-7)
const long long INF = 1LL << 60;
#define PI (acos(-1))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1}; // 移動方向

ll n, k;
ll h[100010];
ll dp[100010];

void input() {
  cin >> n >> k;
  for (int i = 0; i < n; ++i)
    cin >> h[i];
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  input();
  for (int i = 0; i < n; ++i)
    dp[i] = INF;
  dp[0] = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j <= k && i + j < 100010; ++j) {
      chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
const int INF = 1e9 + 5;
int main() {
  fastio();

  int n;
  cin >> n;
  int k;
  cin >> k;
  int h[n];
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  vector<int> dp(n, INF);
  dp[0] = 0;
  for (int i = 1; i < k; ++i) {
    dp[i] = abs(h[i] - h[0]);
  }
  for (int i = 0; i < n; i++) {
    // dp[i]=min(   dp[i-1]+abs(h[i]-h[i-1])   ,  dp[i-2]+abs(h[i]-h[i-2]) )
    for (int j = i + 1; j <= i + k; ++j) {
      dp[j] = min((dp[i] + abs(h[i] - h[j])), dp[j]);
    }
  }
  cout << dp[n - 1] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define fastio()                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
const int INF = 1e9 + 5;
int main() {
  fastio();

  int n;
  cin >> n;
  int k;
  cin >> k;
  int h[n];
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  vector<int> dp(n, INF);
  dp[0] = 0;
  // for (int i = 1; i < k; ++i)
  // {
  // 	dp[i]=abs(h[i]-h[0]);
  // }
  for (int i = 0; i < n; i++) {
    // dp[i]=min(   dp[i-1]+abs(h[i]-h[i-1])   ,  dp[i-2]+abs(h[i]-h[i-2]) )
    for (int j = i + 1; j <= i + k; ++j) {
      dp[j] = min((dp[i] + abs(h[i] - h[j])), dp[j]);
    }
  }
  cout << dp[n - 1] << endl;

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define INF 1000001
using namespace std;

int main() {
  int n = 0, k = 0, buf1 = 0;
  cin >> n >> k;
  int *dp = new int[n + 1];
  dp[0] = 0;
  dp[1] = 0;
  int *h = new int[k + 1];
  h[0] = INF;
  for (int i = 1; i < n + 1; ++i) {
    cin >> buf1;
    h[i] = buf1;
  }
  for (int i = 2; i < n + 1; ++i) {
    dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]);
    for (int j = 2; j < k + 1; ++j) {
      dp[i] = min(dp[i], dp[max(i - j, 0)] + abs(h[i] - h[max(i - j, 0)]));
    }
  }
  cout << dp[n];
  delete[] dp;
  delete[] h;
  return 0;
}


Fixed submission:
#include <iostream>
#define INF 1000001
using namespace std;

int main() {
  int n = 0, k = 0, buf1 = 0;
  cin >> n >> k;
  int *dp = new int[n + 1];
  dp[0] = 0;
  dp[1] = 0;
  int *h = new int[n + 1];
  h[0] = INF;
  for (int i = 1; i < n + 1; ++i) {
    cin >> buf1;
    h[i] = buf1;
  }
  for (int i = 2; i < n + 1; ++i) {
    dp[i] = dp[i - 1] + abs(h[i] - h[i - 1]);
    for (int j = 2; j < k + 1; ++j) {
      dp[i] = min(dp[i], dp[max(i - j, 0)] + abs(h[i] - h[max(i - j, 0)]));
    }
  }
  cout << dp[n];
  delete[] dp;
  delete[] h;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
int ABS(int a) { return max(a, -a); }
ll ABS(ll a) { return max(a, -a); }
double ABS(double a) { return max(a, -a); }

const int N = 100005;
int arr[N];
int dp[N];

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, k;
  cin >> n >> k;

  for (int i = 1; i <= n; i++)
    cin >> arr[i];

  for (int i = 1; i <= n; i++)
    dp[i] = INT_MAX;

  dp[1] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      if (i + j <= n)
        dp[i + j] = min(dp[i + j], dp[i] + ABS(arr[i] - arr[i + j]));
    }
  }

  cout << dp[n];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
int ABS(int a) { return max(a, -a); }
ll ABS(ll a) { return max(a, -a); }
double ABS(double a) { return max(a, -a); }

const int N = 100005;
int arr[N];
int dp[N];

int main() {
  /*#ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif*/

  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, k;
  cin >> n >> k;

  for (int i = 1; i <= n; i++)
    cin >> arr[i];

  for (int i = 1; i <= n; i++)
    dp[i] = INT_MAX;

  dp[1] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      if (i + j <= n)
        dp[i + j] = min(dp[i + j], dp[i] + ABS(arr[i] - arr[i + j]));
    }
  }

  cout << dp[n];
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <sstream>
#include <fstream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <math.h>
#include <queue>
#include <memory>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <cmath>
#include <bitset>
#include <list>
#include <iomanip>
using namespace std;
typedef long long ll;
int m[200005];
void go() {
  m[0] = 1;
  m[1] = 1;
  for (int i = 2; i < 40000; i++) {
    for (int j = i * i; j < 10000; j += i) {
      m[j] = 1;
    }
  }
}
ll dp[(ll)1e5 + 110];
ll n;
ll q[1000005];
ll k;
ll solve(ll index) {
  if (index + 1 == n)
    return 0;
  if (index > n)
    return 1e8;
  ll &re = dp[index];
  if (re != -1)
    return re;
  ll d = (ll)1e9;
  for (int i = 1; i <= k; i++) {
    re = min(abs(q[index] - q[index + i]) + solve(index + i),
             re == -1 ? (int)1e9 : re);
  }
  return re;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  cerr << "FUCK YOU" << endl;

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  memset(dp, -1, sizeof dp);
  memset(q, 0, sizeof q);
  int x;
  cin >> n;
  cin >> k;
  for (int i = 0; i < n; i++) {
    cin >> q[i];
  }
  cout << solve(0) << endl;
  ;
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <sstream>
#include <fstream>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <math.h>
#include <queue>
#include <memory>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <cmath>
#include <bitset>
#include <list>
#include <iomanip>
using namespace std;
typedef long long ll;
int m[200005];
void go() {
  m[0] = 1;
  m[1] = 1;
  for (int i = 2; i < 40000; i++) {
    for (int j = i * i; j < 10000; j += i) {
      m[j] = 1;
    }
  }
}
ll dp[(ll)1e5 + 110];
ll n;
ll q[1000005];
ll k;
ll solve(ll index) {
  if (index + 1 == n)
    return 0;
  if (index > n)
    return 1e8;
  ll &re = dp[index];
  if (re != -1)
    return re;
  ll d = (ll)1e9;
  for (int i = 1; i <= k; i++) {
    re = min(abs(q[index] - q[index + i]) + solve(index + i),
             re == -1 ? (int)1e9 : re);
  }
  return re;
}

int main() {
  cerr << "FUCK YOU" << endl;

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  memset(dp, -1, sizeof dp);
  memset(q, 0, sizeof q);
  int x;
  cin >> n;
  cin >> k;
  for (int i = 0; i < n; i++) {
    cin >> q[i];
  }
  cout << solve(0) << endl;
  ;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define IO                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define int long long
using namespace std;
const int MX = 1e5 + 5;
int n, k;
vector<int> v;
int dp[MX];

int solve(int pos) {
  if (pos > n)
    return 1e17;
  if (pos == n)
    return 0;
  if (dp[pos] != -1)
    return dp[pos];
  int ans = 1e17, curr_height = v[pos], next_height;
  for (int i = 1; i <= k; i++) {
    if (pos + i <= n) {
      next_height = v[pos + i];
      ans = min(ans, solve(pos + i) + abs(curr_height - next_height));
    }
  }
  return dp[pos] = ans;
}

int32_t main() {
  memset(dp, -1, sizeof dp);
  cin >> n >> k;
  v.resize(n);
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  cout << solve(1);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define IO                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define int long long
using namespace std;
const int MX = 1e5 + 5;
int n, k;
vector<int> v;
int dp[MX];

int solve(int pos) {
  if (pos > n)
    return 1e17;
  if (pos == n)
    return 0;
  if (dp[pos] != -1)
    return dp[pos];
  int ans = 1e17, curr_height = v[pos], next_height;
  for (int i = 1; i <= k; i++) {
    if (pos + i <= n) {
      next_height = v[pos + i];
      ans = min(ans, solve(pos + i) + abs(curr_height - next_height));
    }
  }
  return dp[pos] = ans;
}

int32_t main() {
  memset(dp, -1, sizeof dp);
  cin >> n >> k;
  v.resize(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  cout << solve(1);
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, s, n) for (int i = s; i < (n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
using namespace std;
using ll = long long;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007;
const ll INF = MOD * MOD;
const int inf = (1 << 29);

int main() {

  ll n, k;
  cin >> n >> k;
  vector<ll> h(n);
  REP(i, n) cin >> h[i];

  ll dp[2 * n];
  REP(i, 2 * n) dp[i] = inf;

  dp[0] = 0;

  RREP(i, 0, n + 1) {
    RREP(j, 1, k + 1) { mins(dp[i + j], dp[i] + abs(h[i] - h[i + j])); }
  }

  // REP(i, n) cout << dp[i] << endl;

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

  // cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, s, n) for (int i = s; i < (n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
using namespace std;
using ll = long long;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007;
const ll INF = MOD * MOD;
const int inf = (1 << 29);

int main() {

  ll n, k;
  cin >> n >> k;
  vector<ll> h(n);
  REP(i, n) cin >> h[i];

  ll dp[n + k + 3];
  REP(i, n + k + 3) dp[i] = INF;

  dp[0] = 0;

  RREP(i, 0, n + 1) {
    RREP(j, 1, k + 1) { mins(dp[i + j], dp[i] + abs(h[i] - h[i + j])); }
  }

  // REP(i, n) cout << dp[i] << endl;

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

  // cout << ans << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

template <typename T> bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

std::vector<int> dp;
int n, k;
std::vector<int> vec(n + 1, 0);
int INF = 1000000002;

int main() {
  std::cin >> n >> k;
  dp.assign(100100, INF);
  for (int i = 0; i < n; i++) {
    std::cin >> vec[i];
  }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int p = 1; p <= k; p++) {
      chmin(dp[i + p], dp[i] + std::abs(vec[i] - vec[i + p]));
    }
  }
  std::cout << dp[n - 1] << std::endl;
}


Fixed submission:
#include <bits/stdc++.h>

template <typename T> bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

std::vector<int> dp;
int n, k;
std::vector<int> vec(100000 + 101, 0);
int INF = 1000000002;

int main() {
  std::cin >> n >> k;
  dp.assign(100100, INF);
  for (int i = 0; i < n; i++) {
    std::cin >> vec[i];
  }
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int p = 1; p <= k; p++) {
      chmin(dp[i + p], dp[i] + std::abs(vec[i] - vec[i + p]));
    }
  }
  std::cout << dp[n - 1] << std::endl;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <bitset>
#include <map>
#include <set>
#define rep(i, a, b) for (i = a; i < b; i++)
#define itrep(at) for (auto it = at.begin(); it != at.end(); it++)
#define lli long long int
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define db double
#define M 1000000007
#define pi 3.1415926535897932384626433832795028841971

#define MAX 100005

using namespace std;
lli min(lli a, lli b) { return a < b ? a : b; }
lli min(lli a, lli b, lli c) {
  a = min(a, b);
  a = min(a, c);
  return a;
}
lli max(lli a, lli b) { return a > b ? a : b; }
lli max(lli a, lli b, lli c) {
  a = max(a, b);
  a = max(a, c);
  return a;
}
lli dsum(lli n) {
  lli ans = 0;
  while (n) {
    ans += (n % 10);
    n /= 10;
  }
  return ans;
}

struct pll {
  lli x, y;
};
bool comp(pll a, pll b) {
  if (a.x == b.x)
    return a.y < b.y;
  else
    return a.x < b.x;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  char ch;
  db aa, bb, cc;
  string s1, s2, s3;
  lli i, j, k, a, b, c, n, m, sum = 0, ans = 0, temp;
  cin >> n >> k;
  lli h[n];
  rep(i, 0, n) cin >> h[i];
  vector<lli> dp(n, M);
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);
  for (i = 2; i < n; i++) {
    m = M;
    for (j = 1; j <= k; j++) {
      m = min(m, dp[i - j] + abs(h[i] - h[i - j]));
    }
    dp[i] = m;
  }
  cout << dp[n - 1] << endl;

  return 0;
}

/* UJJWAL KESHRI */

Fixed submission:
#include <bits/stdc++.h>
#include <bitset>
#include <map>
#include <set>
#define rep(i, a, b) for (i = a; i < b; i++)
#define itrep(at) for (auto it = at.begin(); it != at.end(); it++)
#define lli long long int
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define db double
#define M 1000000007
#define pi 3.1415926535897932384626433832795028841971

#define MAX 100005

using namespace std;
lli min(lli a, lli b) { return a < b ? a : b; }
lli min(lli a, lli b, lli c) {
  a = min(a, b);
  a = min(a, c);
  return a;
}
lli max(lli a, lli b) { return a > b ? a : b; }
lli max(lli a, lli b, lli c) {
  a = max(a, b);
  a = max(a, c);
  return a;
}
lli dsum(lli n) {
  lli ans = 0;
  while (n) {
    ans += (n % 10);
    n /= 10;
  }
  return ans;
}

struct pll {
  lli x, y;
};
bool comp(pll a, pll b) {
  if (a.x == b.x)
    return a.y < b.y;
  else
    return a.x < b.x;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  char ch;
  db aa, bb, cc;
  string s1, s2, s3;
  lli i, j, k, a, b, c, n, m, sum = 0, ans = 0, temp;
  cin >> n >> k;
  lli h[n];
  rep(i, 0, n) cin >> h[i];
  vector<lli> dp(n, M);
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);
  for (i = 2; i < n; i++) {
    m = M;
    for (j = 1; j <= k && j <= i; j++) {
      m = min(m, dp[i - j] + abs(h[i] - h[i - j]));
    }
    dp[i] = m;
  }
  cout << dp[n - 1] << endl;

  return 0;
}

/* UJJWAL KESHRI */
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define speedhack                                                              \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);
#define fromfile                                                               \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define mod int(1e9 + 7)
#define MAXN int(1e9)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ull, ull> pull;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;

int n, x, l;

int main() {

  fromfile;
  speedhack;
  cin >> n >> x;
  vector<int> dp(n + 1, MAXN);
  vector<int> h;
  h.pb(0); // shifting index
  for (int j = 0; j < n; j++) {
    cin >> l;
    h.pb(l);
  }
  dp[1] = 0;
  for (int k = 2; k <= n; k++) {
    for (int j = 1; j <= x; j++) {
      if (k - j > 0) {
        dp[k] = min(dp[k], dp[k - j] + abs(h[k] - h[k - j]));
      }
    }
  }
  cout << dp[n];
}

Fixed submission:
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define speedhack                                                              \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr);
#define fromfile                                                               \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout);
#define mod int(1e9 + 7)
#define MAXN int(1e9)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ull, ull> pull;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;

int n, x, l;

int main() {

  // fromfile;
  speedhack;
  cin >> n >> x;
  vector<int> dp(n + 1, MAXN);
  vector<int> h;
  h.pb(0); // shifting index
  for (int j = 0; j < n; j++) {
    cin >> l;
    h.pb(l);
  }
  dp[1] = 0;
  for (int k = 2; k <= n; k++) {
    for (int j = 1; j <= x; j++) {
      if (k - j > 0) {
        dp[k] = min(dp[k], dp[k - j] + abs(h[k] - h[k - j]));
      }
    }
  }
  cout << dp[n];
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define FORC(cont, it)                                                         \
  for (decltype((cont).begin()) it = (cont).begin(); it != (cont).end(); it++)
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, k;
  cin >> n >> k;

  int h[n];

  FOR(i, 0, n) { cin >> h[i]; }

  long long dp[n + 5];

  dp[0] = 0;

  for (int i = 1; i <= k; i++) {
    dp[i] = abs(h[i] - h[0]);
  }

  FOR(i, k + 1, n) {
    int min = INT_MAX;
    for (int j = 1; j <= k && j <= i; j++) {
      min = (dp[i - j] + abs(h[i] - h[i - j])) < min
                ? (dp[i - j] + abs(h[i] - h[i - j]))
                : min;
    }
    dp[i] = min;
  }

  cout << dp[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define FORC(cont, it)                                                         \
  for (decltype((cont).begin()) it = (cont).begin(); it != (cont).end(); it++)
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, k;
  cin >> n >> k;

  int h[n];

  FOR(i, 0, n) { cin >> h[i]; }

  long long dp[n + 5];

  dp[0] = 0;

  for (int i = 1; i <= k && i < n; i++) {
    dp[i] = abs(h[i] - h[0]);
  }

  FOR(i, k + 1, n) {
    int min = INT_MAX;
    for (int j = 1; j <= k && j <= i; j++) {
      min = (dp[i - j] + abs(h[i] - h[i - j])) < min
                ? (dp[i - j] + abs(h[i] - h[i - j]))
                : min;
    }
    dp[i] = min;
  }

  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define pb push_back
#define ll long long int
#define mod 1000000007
const int N = 1e5 + 5;

int n, k;
int cache[N];
int val[N];

int dp(int pos) {
  if (pos == 0)
    return cache[pos];
  for (int i = pos - 1; i >= max(0, pos - k); i--) {
    cache[i] = min(cache[i], cache[pos] + abs(val[pos] - val[i]));
  }
  return dp(pos - 1);
}

int32_t main() {
  fastIO;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int i;
  cin >> n >> k;
  for (i = 0; i < n; i++)
    cin >> val[i];
  for (i = 0; i < n; i++)
    cache[i] = INT_MAX;
  cache[n - 1] = 0;

  cout << dp(n - 1);

  return 0;
}

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

#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define pb push_back
#define ll long long int
#define mod 1000000007
const int N = 1e5 + 5;

int n, k;
int cache[N];
int val[N];

int dp(int pos) {
  if (pos == 0)
    return cache[pos];
  for (int i = pos - 1; i >= max(0, pos - k); i--) {
    cache[i] = min(cache[i], cache[pos] + abs(val[pos] - val[i]));
  }
  return dp(pos - 1);
}

int32_t main() {
  fastIO;

  int i;
  cin >> n >> k;
  for (i = 0; i < n; i++)
    cin >> val[i];
  for (i = 0; i < n; i++)
    cache[i] = INT_MAX;
  cache[n - 1] = 0;

  cout << dp(n - 1);

  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define mp make_pair
#define ff first
#define ss second
#define inf 9e18
#define pii pair<int, int>
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
int32_t main() {

  fastio

      int n,
      k;
  cin >> n >> k;
  int dp[n + 2], a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    dp[i] = INT_MAX;

  dp[n - 1] = 0;
  if (n - 2 >= 0)
    dp[n - 2] = abs(a[n - 1] - a[n - 2]);
  for (int i = n - 3; i >= 0; i--)
    for (int j = i + 1; j < min(n, i + k + 1); j++)
      dp[i] = min(abs(a[i] - a[i + j]) + dp[i + j], dp[i]);

  cout << dp[0];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define mp make_pair
#define ff first
#define ss second
#define inf 9e18
#define pii pair<int, int>
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
int32_t main() {

  fastio

      int n,
      k;
  cin >> n >> k;
  int dp[n + 2], a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    dp[i] = INT_MAX;

  dp[n - 1] = 0;
  if (n - 2 >= 0)
    dp[n - 2] = abs(a[n - 1] - a[n - 2]);
  for (int i = n - 3; i >= 0; i--)
    for (int j = i + 1; j < min(n, i + k + 1); j++)
      dp[i] = min(abs(a[i] - a[j]) + dp[j], dp[i]);

  cout << dp[0];
  return 0;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
typedef long long ll;
const int INF = 1000000000;
const long INF64 = 1000000000000000ll;
const ll MOD = 1000000007ll;

int main() {
  int n, k;
  std::cin >> n >> k;
  std::vector<int> a(n), ans(n, 0);
  rep(i, n) std::cin >> a[i];
  ans[0] = 0;

  for (int i = 1; i < n; i++) {
    /* code */
    int dans = INF;
    for (int j = 1; j < max(k, i); j++) {
      ans[i] = min(dans, ans[i - j] + abs(a[i] - a[i - j]));
    }
  }
  std::cout << ans[n - 1] << std::endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
typedef long long ll;
const int INF = 1000000000;
const long INF64 = 1000000000000000ll;
const ll MOD = 1000000007ll;

int main() {
  int n, k;
  std::cin >> n >> k;
  std::vector<int> a(n), ans(n, 0);
  rep(i, n) std::cin >> a[i];
  ans[0] = 0;

  for (int i = 1; i < n; i++) {
    /* code */
    ans[i] = INF;
    for (int j = 1; j <= min(k, i); j++) {
      ans[i] = min(ans[i], ans[i - j] + abs(a[i] - a[i - j]));
    }
  }
  std::cout << ans[n - 1] << std::endl;
}
Problem p03161 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> dp(n, INT32_MAX), h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  dp[0] = 0;
  for (int i = 1; i < k; i++) {
    for (int j = 0; j < i; j++) {
      dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]));
    }
  }
  for (int i = k; i < n; i++) {
    for (int j = 0; j < k; j++) {
      dp[i] = min(dp[i], dp[i - k + j] + abs(h[i] - h[i - k + j]));
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> dp(n, INT32_MAX), h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  dp[0] = 0;
  for (int i = 1; i < k && i < n; i++) {
    for (int j = 0; j < i; j++) {
      dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]));
    }
  }
  for (int i = k; i < n; i++) {
    for (int j = 0; j < k; j++) {
      dp[i] = min(dp[i], dp[i - k + j] + abs(h[i] - h[i - k + j]));
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define MAXN 100100
int n, a[MAXN][3], dp[MAXN][3], k;

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i == 0) {
        dp[i][j] = a[i][j];
      } else {
        dp[i][j] =
            max(dp[i - 1][(j + 1) % 3], dp[i - 1][(j + 2) % 3]) + a[i][j];
      }
    }
  }
  cout << max(max(dp[n - 1][0], dp[n - 1][1]), dp[n - 1][2]);
  return 0;
}


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

#define MAXN 100100
int n, a[MAXN][3], dp[MAXN][3], k;

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 3; ++j) {
      if (i == 0) {
        dp[i][j] = a[i][j];
      } else {
        dp[i][j] =
            max(dp[i - 1][(j + 1) % 3], dp[i - 1][(j + 2) % 3]) + a[i][j];
      }
    }
  }
  cout << max(max(dp[n - 1][0], dp[n - 1][1]), dp[n - 1][2]);
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<long long> vl;
typedef pair<long, long> pll;
typedef vector<pll> vll;
typedef set<ll> sl;
typedef map<string, int> msi;

#define pb push_back
#define mp make_pair
#define f first
#define s second
#define inf 0x3f3f3f3f
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))

const ll md = 1000000007;
// void mod(ll &x){if (x >= md) x -= md; if (x < 0) x += md;}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, i, j, ans;
  cin >> n;
  ll abc[n][4];
  ll dp[n][4];
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= 3; j++) {
      cin >> abc[i][j];
    }
  }
  dp[1][1] = abc[1][1];
  dp[1][2] = abc[1][2];
  dp[1][3] = abc[1][3];
  for (i = 2; i <= n; i++) {
    dp[i][1] = abc[i][1] + max(dp[i - 1][2], dp[i - 1][3]);
    dp[i][2] = abc[i][2] + max(dp[i - 1][1], dp[i - 1][3]);
    dp[i][3] = abc[i][3] + max(dp[i - 1][1], dp[i - 1][2]);
  }
  ans = max(dp[n][1], max(dp[n][2], dp[n][3]));
  cout << ans;
  return 0;
}


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

typedef long long ll;
typedef vector<long long> vl;
typedef pair<long, long> pll;
typedef vector<pll> vll;
typedef set<ll> sl;
typedef map<string, int> msi;

#define pb push_back
#define mp make_pair
#define f first
#define s second
#define inf 0x3f3f3f3f
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))

const ll md = 1000000007;
// void mod(ll &x){if (x >= md) x -= md; if (x < 0) x += md;}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, i, j, ans;
  cin >> n;
  ll abc[n + 1][4];
  ll dp[n + 1][4];
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= 3; j++) {
      cin >> abc[i][j];
    }
  }
  dp[1][1] = abc[1][1];
  dp[1][2] = abc[1][2];
  dp[1][3] = abc[1][3];
  for (i = 2; i <= n; i++) {
    dp[i][1] = abc[i][1] + max(dp[i - 1][2], dp[i - 1][3]);
    dp[i][2] = abc[i][2] + max(dp[i - 1][1], dp[i - 1][3]);
    dp[i][3] = abc[i][3] + max(dp[i - 1][1], dp[i - 1][2]);
  }
  ans = max(dp[n][1], max(dp[n][2], dp[n][3]));
  cout << ans;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define clr(i, j) memset(i, j, sizeof(i))
#define pb push_back

typedef long long ll;

//-*-*-*-*-*-*-*-*-

ll n;
ll dp[10000][3];
ll arr[10000][3];
int main() {
  ll n, k;
  cin >> n;
  clr(dp, -1);

  for (int i = 0; i < n; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
    // cout<<arr[i][0];
  }
  dp[0][0] = arr[0][0];
  dp[0][1] = arr[0][1];
  dp[0][2] = arr[0][2];

  for (int i = 1; i < n; i++) {
    for (ll j = 0; j <= 2; j++) {

      for (ll h = 0; h <= 2; h++) {

        if (j != h) {
          if (dp[i][j] != -1) {
            dp[i][j] = max(dp[i - 1][h] + arr[i][j], dp[i][j]);
          } else {
            dp[i][j] = dp[i - 1][h] + arr[i][j];
          }
        }
      }
    }
  }
  cout << max(max(dp[n - 1][0], dp[n - 1][1]), dp[n - 1][2]);
}


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

#define clr(i, j) memset(i, j, sizeof(i))
#define pb push_back

typedef long long ll;

//-*-*-*-*-*-*-*-*-

ll n;
ll dp[100000][3];
ll arr[100000][3];
int main() {
  ll n, k;
  cin >> n;
  clr(dp, -1);

  for (int i = 0; i < n; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
    // cout<<arr[i][0];
  }
  dp[0][0] = arr[0][0];
  dp[0][1] = arr[0][1];
  dp[0][2] = arr[0][2];

  for (int i = 1; i < n; i++) {
    for (ll j = 0; j <= 2; j++) {

      for (ll h = 0; h <= 2; h++) {

        if (j != h) {
          if (dp[i][j] != -1) {
            dp[i][j] = max(dp[i - 1][h] + arr[i][j], dp[i][j]);
          } else {
            dp[i][j] = dp[i - 1][h] + arr[i][j];
          }
        }
      }
    }
  }
  cout << max(max(dp[n - 1][0], dp[n - 1][1]), dp[n - 1][2]);
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int maxN = 10005;
const int oo = 1e9 + 7;
int dp[maxN][4];
int a[maxN][4];
int n;

int main() {
  // freopen("Vacation.inp","r",stdin);
  // freopen("Vacation.out","w",stdout);

  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= 3; j++) {
      cin >> a[i][j];
      dp[i][j] = -oo;
    }

  for (int j = 1; j <= 3; j++)
    dp[0][j] = 0;

  // Tinh bang phuong an dp[i][j]
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 3; j++) {
      if (j == 1)
        dp[i][j] = max(a[i][j] + dp[i - 1][2], a[i][j] + dp[i - 1][3]);
      if (j == 2)
        dp[i][j] = max(a[i][j] + dp[i - 1][1], a[i][j] + dp[i - 1][3]);
      if (j == 3)
        dp[i][j] = max(a[i][j] + dp[i - 1][2], a[i][j] + dp[i - 1][1]);
    }
  }
  cout << max(dp[n][1], max(dp[n][2], dp[n][3]));
  return 0;
}


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

const int maxN = 100005;
const int oo = 1e9 + 7;
int dp[maxN][4];
int a[maxN][4];
int n;

int main() {
  // freopen("Vacation.inp","r",stdin);
  // freopen("Vacation.out","w",stdout);

  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= 3; j++) {
      cin >> a[i][j];
      dp[i][j] = -oo;
    }

  for (int j = 1; j <= 3; j++)
    dp[0][j] = 0;

  // Tinh bang phuong an dp[i][j]
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 3; j++) {
      if (j == 1)
        dp[i][j] = max(a[i][j] + dp[i - 1][2], a[i][j] + dp[i - 1][3]);
      if (j == 2)
        dp[i][j] = max(a[i][j] + dp[i - 1][1], a[i][j] + dp[i - 1][3]);
      if (j == 3)
        dp[i][j] = max(a[i][j] + dp[i - 1][2], a[i][j] + dp[i - 1][1]);
    }
  }
  cout << max(dp[n][1], max(dp[n][2], dp[n][3]));
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define pl pair<ll, ll>
#define vl vector<ll>
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define N 100005
ll dp[10005][5];

int main() {
  ll n;
  cin >> n;
  ll a[n][3];
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < 3; j++) {
      cin >> a[i][j];
    }
  }
  for (ll i = 0; i < 3; i++)
    dp[0][i] = a[0][i];
  for (ll i = 1; i < n; i++) {
    for (ll j = 0; j < 3; j++) {
      if (j == 0)
        dp[i][j] = a[i][j] + max(dp[i - 1][j + 1], dp[i - 1][j + 2]);
      else if (j == 1)
        dp[i][j] = a[i][j] + max(dp[i - 1][j - 1], dp[i - 1][j + 1]);
      else if (j == 2)
        dp[i][j] = a[i][j] + max(dp[i - 1][j - 1], dp[i - 1][j - 2]);
    }
  }
  ll mx = 0;
  for (ll i = 0; i < 3; i++)
    mx = max(mx, dp[n - 1][i]);
  cout << mx << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define pl pair<ll, ll>
#define vl vector<ll>
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define N 100005
ll dp[100005][5];

int main() {
  ll n;
  cin >> n;
  ll a[n][3];
  for (ll i = 0; i < n; i++) {
    for (ll j = 0; j < 3; j++) {
      cin >> a[i][j];
    }
  }
  for (ll i = 0; i < 3; i++)
    dp[0][i] = a[0][i];
  for (ll i = 1; i < n; i++) {
    for (ll j = 0; j < 3; j++) {
      if (j == 0)
        dp[i][j] = a[i][j] + max(dp[i - 1][j + 1], dp[i - 1][j + 2]);
      else if (j == 1)
        dp[i][j] = a[i][j] + max(dp[i - 1][j - 1], dp[i - 1][j + 1]);
      else if (j == 2)
        dp[i][j] = a[i][j] + max(dp[i - 1][j - 1], dp[i - 1][j - 2]);
    }
  }
  ll mx = 0;
  for (ll i = 0; i < 3; i++)
    mx = max(mx, dp[n - 1][i]);
  cout << mx << "\n";
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <utility>
#include <vector>
#define MAX_DOUBLE 40000000000.0
#include <math.h>
#include <unistd.h>

using namespace std;

int min(int a, int b) {
  if (a > b)
    return b;
  return a;
}

int max(int a, int b) {
  if (a < b)
    return b;
  return a;
}

int days[10010][3];

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> days[i][0] >> days[i][1] >> days[i][2];
  }

  int maxA = days[0][0];
  int maxB = days[0][1];
  int maxC = days[0][2];

  for (int i = 1; i < n; i++) {
    int tmpA = max(maxB, maxC) + days[i][0];
    int tmpB = max(maxA, maxC) + days[i][1];
    int tmpC = max(maxB, maxA) + days[i][2];

    maxA = tmpA;
    maxB = tmpB;
    maxC = tmpC;
  }

  cout << max(max(maxA, maxB), maxC);
}


Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <utility>
#include <vector>
#define MAX_DOUBLE 40000000000.0
#include <math.h>
#include <unistd.h>

using namespace std;

int min(int a, int b) {
  if (a > b)
    return b;
  return a;
}

int max(int a, int b) {
  if (a < b)
    return b;
  return a;
}

int days[100010][3];

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> days[i][0] >> days[i][1] >> days[i][2];
  }

  int maxA = days[0][0];
  int maxB = days[0][1];
  int maxC = days[0][2];

  for (int i = 1; i < n; i++) {
    int tmpA = max(maxB, maxC) + days[i][0];
    int tmpB = max(maxA, maxC) + days[i][1];
    int tmpC = max(maxB, maxA) + days[i][2];

    maxA = tmpA;
    maxB = tmpB;
    maxC = tmpC;
  }

  cout << max(max(maxA, maxB), maxC);
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define VRSORT(v) sort(v.rbegin(), v.rend()); // vectorの降順ソート
#define ll long long
#define pb(a) push_back(a)
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

typedef vector<unsigned int> vec;
typedef vector<vec> mat;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  ll dp[10010][3];
  REP(i, 10010) REP(j, 3) dp[i][j] = 0;
  for (int i = 0; i < n; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    dp[i + 1][0] = max(dp[i][1], dp[i][2]) + a; // aを選ぶ
    dp[i + 1][1] = max(dp[i][0], dp[i][2]) + b; // bを選ぶ
    dp[i + 1][2] = max(dp[i][0], dp[i][1]) + c; // cを選ぶ
  }
  ll ans = 0;
  REP(i, 3) { ans = max(ans, dp[n][i]); }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define VRSORT(v) sort(v.rbegin(), v.rend()); // vectorの降順ソート
#define ll long long
#define pb(a) push_back(a)
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

typedef vector<unsigned int> vec;
typedef vector<vec> mat;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<vector<ll>> dp(1e5 + 10, vector<ll>(3, 0));

  for (int i = 0; i < n; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    dp[i + 1][0] = max(dp[i][1], dp[i][2]) + a; // aを選ぶ
    dp[i + 1][1] = max(dp[i][0], dp[i][2]) + b; // bを選ぶ
    dp[i + 1][2] = max(dp[i][0], dp[i][1]) + c; // cを選ぶ
  }
  ll ans = 0;
  REP(i, 3) { ans = max(ans, dp[n][i]); }
  cout << ans << endl;
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n, a[3][10000], h[3][10000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> a[j][i];
    }
  }
  h[0][0] = a[0][0];
  h[1][0] = a[1][0];
  h[2][0] = a[2][0];
  for (int i = 1; i < n; i++) {
    h[0][i] = a[0][i] + max(h[2][i - 1], h[1][i - 1]);
    h[1][i] = a[1][i] + max(h[2][i - 1], h[0][i - 1]);
    h[2][i] = a[2][i] + max(h[1][i - 1], h[0][i - 1]);
    // cout <<h[0][i]<<' '<<h[1][i]<<' '<<h[2][i]<<' '<<endl;
  }
  cout << max(h[0][n - 1], max(h[1][n - 1], h[2][n - 1]));
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n, a[3][100000], h[3][100000];
  // int n,a[3][3];
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      cin >> a[j][i];
    }
  }
  h[0][0] = a[0][0];
  h[1][0] = a[1][0];
  h[2][0] = a[2][0];
  for (int i = 1; i < n; i++) {
    h[0][i] = a[0][i] + max(h[2][i - 1], h[1][i - 1]);
    h[1][i] = a[1][i] + max(h[2][i - 1], h[0][i - 1]);
    h[2][i] = a[2][i] + max(h[1][i - 1], h[0][i - 1]);
    // cout <<h[0][i]<<' '<<h[1][i]<<' '<<h[2][i]<<' '<<endl;
  }
  cout << max(h[0][n - 1], max(h[1][n - 1], h[2][n - 1]));
  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<vector<long long>> dp(n, vector<long long>(3, -1));
  vector<vector<int>> h(n, vector<int>(3));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 3; j++)
      cin >> h[i][j];
  for (int i = 1; i < n; i++) {
    h[i][0] = h[i][0] + max(h[i - 11][1], h[i - 1][2]);
    h[i][1] = h[i][1] + max(h[i - 1][0], h[i - 1][2]);
    h[i][2] = h[i][2] + max(h[i - 1][1], h[i - 1][0]);
  }
  int m = max({h[n - 1][0], h[n - 1][1], h[n - 1][2]});
  cout << m;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<vector<long long>> dp(n, vector<long long>(3, -1));
  vector<vector<int>> h(n, vector<int>(3));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 3; j++)
      cin >> h[i][j];
  for (int i = 1; i < n; i++) {
    h[i][0] = h[i][0] + max(h[i - 1][1], h[i - 1][2]);
    h[i][1] = h[i][1] + max(h[i - 1][0], h[i - 1][2]);
    h[i][2] = h[i][2] + max(h[i - 1][1], h[i - 1][0]);
  }
  int m = max({h[n - 1][0], h[n - 1][1], h[n - 1][2]});
  cout << m;
}
Problem p03162 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pie 3.142857143
#define ll long long int
#define fr(i, a, n) for (ll i = a; i < n; i++)
#define vi vector<int>
#define vlli vector<ll>
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fv(i, a) for (auto i : a)
#define sz(a) int((a).size())
#define all(c) (c).begin(), (c).end()
#define frv(i, c) for (auto i = c.begin(); i != c.end(); i++)
const int N = 1e5 + 5;
int dp[N][3];
int a[N][3];
ll yogi(int n, int k) {
  ll x, y;
  if (k == 0) {
    x = 1;
    y = 2;
  }
  if (k == 1) {
    x = 0;
    y = 2;
  }
  if (k == 2) {
    x = 0;
    y = 1;
  }
  if (n == 0)
    return max(a[0][x], a[0][y]);
  ll s = +max(a[n][x] + yogi(n - 1, x), a[n][y] + yogi(n - 1, y));
  return s;
}
void solve() {
  ll n;
  cin >> n;
  fr(i, 0, n) cin >> a[i][0] >> a[i][1] >> a[i][2];
  ll x = a[n][0] + yogi(n - 1, 0);
  ll y = a[n][1] + yogi(n - 1, 1);
  ll z = a[n][2] + yogi(n - 1, 2);
  x = max(x, max(y, z));
  cout << x << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pie 3.142857143
#define ll long long int
#define fr(i, a, n) for (ll i = a; i < n; i++)
#define vi vector<int>
#define vlli vector<ll>
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fv(i, a) for (auto i : a)
#define sz(a) int((a).size())
#define all(c) (c).begin(), (c).end()
#define frv(i, c) for (auto i = c.begin(); i != c.end(); i++)
const int N = 1e5 + 5;
int dp[N][3];
int a[N][3];
ll yogi(int n, int k) {
  ll x, y;
  if (k == 0) {
    x = 1;
    y = 2;
  }
  if (k == 1) {
    x = 0;
    y = 2;
  }
  if (k == 2) {
    x = 0;
    y = 1;
  }
  if (n == 0)
    return max(a[0][x], a[0][y]);
  if (dp[n - 1][0] == 0)
    dp[n - 1][0] = yogi(n - 1, 0);
  if (dp[n - 1][1] == 0)
    dp[n - 1][1] = yogi(n - 1, 1);
  if (dp[n - 1][2] == 0)
    dp[n - 1][2] = yogi(n - 1, 2);
  ll s = max(a[n][x] + dp[n - 1][x], a[n][y] + dp[n - 1][y]);
  return s;
}
void solve() {
  ll n;
  cin >> n;
  fr(i, 0, n) cin >> a[i][0] >> a[i][1] >> a[i][2];
  ll x = a[n][0] + yogi(n - 1, 0);
  ll y = a[n][1] + yogi(n - 1, 1);
  ll z = a[n][2] + yogi(n - 1, 2);
  x = max(x, max(y, z));
  cout << x << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }

  return 0;
}
Problem p03162 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;

  int v1, v2, v3;

  int arr[3][n];

  for (int i = 0; i < n; i++) {

    cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
  }

  v1 = arr[0][0];
  v2 = arr[0][1];
  v3 = arr[0][2];

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

    int temp1 = v1;
    int temp2 = v2;
    int temp3 = v3;

    v1 = arr[i][0] + max(temp2, temp3);
    v2 = arr[i][1] + max(temp1, temp3);
    v3 = arr[i][2] + max(temp1, temp2);
  }

  cout << max(max(v1, v2), v3) << endl;

  return 0;
}

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;

  int v1, v2, v3;

  int arr[n][3];

  for (int i = 0; i < n; i++) {

    cin >> arr[i][0] >> arr[i][1] >> arr[i][2];
  }

  v1 = arr[0][0];
  v2 = arr[0][1];
  v3 = arr[0][2];

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

    int temp1 = v1;
    int temp2 = v2;
    int temp3 = v3;

    v1 = arr[i][0] + max(temp2, temp3);
    v2 = arr[i][1] + max(temp1, temp3);
    v3 = arr[i][2] + max(temp1, temp2);
  }

  cout << max(max(v1, v2), v3) << endl;

  return 0;
}