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

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

using ll = long long;
using ld = long double;
constexpr ll inf = static_cast<ll>(1e17);
constexpr int iinf = static_cast<int>(1e9);
constexpr ll mod = static_cast<ll>(1e9 + 7);

int n, q;
ll a[100005];
ll sum[100005], sum2[100005];
int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  std::cin >> n >> q;

  for (int i = 1; i <= n; ++i) {
    std::cin >> a[i];
    sum[i] = sum[i - 1] + a[i];
    sum2[i] = sum2[i - 1];
    if (i % 2 == 1)
      sum2[i] += a[i];
  }

  for (int i = 0; i < q; ++i) {
    ll x;
    std::cin >> x;

    int l = 1, r = n;
    while (r - l > 0) {
      auto aa = (l + r) / 2;
      auto bb = (aa + n) / 2;
      auto aval = std::abs(x - a[aa]);
      auto bval = std::abs(x - a[bb]);

      if (aval < bval) {
        r = aa;
      } else if (aval > bval) {
        l = aa;
      } else {
        l = r = aa;
      }
    }

    r = l + (n - l + 1) / 2 - 1;

    ll ans = sum[n] - sum[r];
    if (n - r == r - l + 1) {
      if ((l - 1) % 2 == 1)
        ans += sum2[l - 1];
      else
        ans += sum[l - 1] - sum2[l - 1];
    } else if (l > 1) {
      if ((l - 2) % 2 == 1)
        ans += sum2[l - 2];
      else
        ans += sum[l - 2] - sum2[l - 2];
    }

    std::cout << ans << std::endl;
  }

  return 0;
}


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

using ll = long long;
using ld = long double;
constexpr ll inf = static_cast<ll>(1e17);
constexpr int iinf = static_cast<int>(1e9);
constexpr ll mod = static_cast<ll>(1e9 + 7);

int n, q;
ll a[100005];
ll sum[100005], sum2[100005];
int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  std::cin >> n >> q;

  for (int i = 1; i <= n; ++i) {
    std::cin >> a[i];
    sum[i] = sum[i - 1] + a[i];
    sum2[i] = sum2[i - 1];
    if (i % 2 == 1)
      sum2[i] += a[i];
  }

  for (int i = 0; i < q; ++i) {
    ll x;
    std::cin >> x;

    int l = 1, r = n;
    while (r - l > 0) {
      auto aa = (l + r) / 2;
      auto bb = (aa + n) / 2;
      auto aval = std::abs(x - a[aa]);
      auto bval = std::abs(x - a[bb]);

      if (aval < bval) {
        r = aa;
      } else if (aval > bval) {
        if (l == aa)
          l = aa + 1;
        else
          l = aa;
      } else {
        l = r = aa;
      }
    }

    r = l + (n - l + 1) / 2 - 1;

    ll ans = sum[n] - sum[r];
    if (n - r == r - l + 1) {
      if ((l - 1) % 2 == 1)
        ans += sum2[l - 1];
      else
        ans += sum[l - 1] - sum2[l - 1];
    } else if (l > 1) {
      if ((l - 2) % 2 == 1)
        ans += sum2[l - 2];
      else
        ans += sum[l - 2] - sum2[l - 2];
    }

    std::cout << ans << std::endl;
  }

  return 0;
}
Problem p03158 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
const int INF = 2147483647;
const ll MOD = 1000000007;

using namespace std;

int main() {
  int n, q;
  cin >> n >> q;
  vl a(n), x(n);
  REP(i, n) { cin >> a[i]; }
  REP(i, q) { cin >> x[i]; }

  vl cum(n + 1), cumtobi(n + 1);
  cum[0] = 0;
  cumtobi[0] = 0;
  cum[1] = a[0];
  cumtobi[1] = a[0];
  FOR(i, 2, n + 1) {
    cum[i] = cum[i - 1] + a[i - 1];
    cumtobi[i] = cumtobi[i - 2] + a[i - 1];
  }

  vector<pair<ll, ll>> map;

  for (int i = 1; i <= (n + 1) / 2; i++) {
    if (n - 2 * i - 1 < 0) {
      ll tx = 0;
      ll v = cum[n] - cum[n / 2];
      map.push_back(make_pair(tx, v));
    } else {
      ll tx = (a[n - i - 1] + a[n - 2 * i - 1]) / 2;
      ll v = cum[n] - cum[n - i] + cumtobi[n - 2 * i];
      map.push_back(make_pair(tx, v));
    }
  }

  sort(map.begin(), map.end());

  REP(i, q) {
    int idx = (upper_bound(map.begin(), map.end(), make_pair(x[i], 0LL)) -
               map.begin()) -
              1;
    cout << map[idx].second << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
const int INF = 2147483647;
const ll MOD = 1000000007;

using namespace std;

int main() {
  int n, q;
  cin >> n >> q;
  vl a(n), x(q);
  REP(i, n) { cin >> a[i]; }
  REP(i, q) { cin >> x[i]; }

  vl cum(n + 1), cumtobi(n + 1);
  cum[0] = 0;
  cumtobi[0] = 0;
  cum[1] = a[0];
  cumtobi[1] = a[0];
  FOR(i, 2, n + 1) {
    cum[i] = cum[i - 1] + a[i - 1];
    cumtobi[i] = cumtobi[i - 2] + a[i - 1];
  }

  vector<pair<ll, ll>> map;

  for (int i = 1; i <= (n + 1) / 2; i++) {
    if (n - 2 * i - 1 < 0) {
      ll tx = 0;
      ll v = cum[n] - cum[n / 2];
      map.push_back(make_pair(tx, v));
    } else {
      ll tx = (a[n - i - 1] + a[n - 2 * i - 1]) / 2;
      ll v = cum[n] - cum[n - i] + cumtobi[n - 2 * i];
      map.push_back(make_pair(tx, v));
    }
  }

  sort(map.begin(), map.end());

  REP(i, q) {
    int idx = (upper_bound(map.begin(), map.end(), make_pair(x[i], 0LL)) -
               map.begin()) -
              1;
    cout << map[idx].second << endl;
  }
}
Problem p03158 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = (a); i <= (b); ++i)
#define fd(i, a, b) for (int i = (a); i >= (b); --i)
#define edge(i, u)                                                             \
  for (int i = head[u], v = e[i].v; i; i = e[i].nxt, v = e[i].v)
#define mod 1000000007
#define ll long long
#define N 405
#define pb push_back
ll n, q, x, a[N], s[N], sum[N], l, r, ss;
int main() {
  scanf("%lld %lld", &n, &q);
  fo(i, 1, n) scanf("%lld", &a[i]);
  s[1] = a[1];
  s[2] = a[2];
  sum[1] = a[1];
  sum[2] = a[1] + a[2];
  fo(i, 3, n) {
    s[i] = s[i - 2] + a[i];
    sum[i] = sum[i - 1] + a[i];
  }
  ss = sum[n];
  fo(i, 1, q) {
    scanf("%lld\n", &x);
    if (x <= a[1]) {
      printf("%lld\n", ss - sum[n >> 1]);
      continue;
    }
    l = x;
    r = 1e9;
    ll rp, rpos, ans, lp, lpos;
    while (l < r) {
      rp = l + r >> 1;
      rpos = std::upper_bound(a + 1, a + n, rp) - a - 1;
      lp = x * 2 - rp;
      lpos = std::lower_bound(a + 1, a + n, lp) - a;
      if (n - rpos > rpos - lpos + 1)
        l = rp + 1;
      else
        r = rp;
    }
    rp = l;
    rpos = std::upper_bound(a + 1, a + n, rp) - a - 1;
    lp = x * 2 - rp;
    lpos = std::lower_bound(a + 1, a + n, lp) - a;
    if (n - rpos < rpos - lpos + 1)
      --rpos;
    ans = sum[rpos] - sum[lpos - 1];
    if (n - rpos == rpos - lpos + 1) {
      if (lpos > 2)
        ans += s[lpos - 2];
    } else {
      ans += s[lpos - 1];
    }
    printf("%lld\n", ss - ans);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = (a); i <= (b); ++i)
#define fd(i, a, b) for (int i = (a); i >= (b); --i)
#define edge(i, u)                                                             \
  for (int i = head[u], v = e[i].v; i; i = e[i].nxt, v = e[i].v)
#define mod 1000000007
#define ll long long
#define N 100005
#define pb push_back
ll n, q, x, a[N], s[N], sum[N], l, r, ss;
int main() {
  scanf("%lld %lld", &n, &q);
  fo(i, 1, n) scanf("%lld", &a[i]);
  s[1] = a[1];
  s[2] = a[2];
  sum[1] = a[1];
  sum[2] = a[1] + a[2];
  fo(i, 3, n) {
    s[i] = s[i - 2] + a[i];
    sum[i] = sum[i - 1] + a[i];
  }
  ss = sum[n];
  fo(i, 1, q) {
    scanf("%lld\n", &x);
    if (x <= a[1]) {
      printf("%lld\n", ss - sum[n >> 1]);
      continue;
    }
    l = x;
    r = 1e9;
    ll rp, rpos, ans, lp, lpos;
    while (l < r) {
      rp = l + r >> 1;
      rpos = std::upper_bound(a + 1, a + n, rp) - a - 1;
      lp = x * 2 - rp;
      lpos = std::lower_bound(a + 1, a + n, lp) - a;
      if (n - rpos > rpos - lpos + 1)
        l = rp + 1;
      else
        r = rp;
    }
    rp = l;
    rpos = std::upper_bound(a + 1, a + n, rp) - a - 1;
    lp = x * 2 - rp;
    lpos = std::lower_bound(a + 1, a + n, lp) - a;
    if (n - rpos < rpos - lpos + 1)
      --rpos;
    ans = sum[rpos] - sum[lpos - 1];
    if (n - rpos == rpos - lpos + 1) {
      if (lpos > 2)
        ans += s[lpos - 2];
    } else {
      ans += s[lpos - 1];
    }
    printf("%lld\n", ss - ans);
  }
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"

using namespace std;

int *h;
int *dp;
bool *v;
int solve(int n, const int &max) {
  if (n == max - 1)
    return 0;

  if (n == max) {
    return 1e9;
  }

  if (v[n])
    return dp[n];
  int cost = dp[n] = min(solve(n + 1, max) + abs(h[n] - h[n + 1]),
                         solve(n + 2, max) + abs(h[n] - h[n + 2]));

  return cost;
}

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

  h = new int[N];
  dp = new int[N];
  v = new bool[N];

  fill(dp, dp + N, 0);
  fill(v, v + N, 0);

  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }

  cout << solve(0, N) << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

int *h;
int *dp;
bool *v;
int solve(int n, const int &max) {
  if (n == max - 1)
    return 0;

  if (n == max) {
    return 1e9;
  }

  if (v[n])
    return dp[n];
  int cost = dp[n] = min(solve(n + 1, max) + abs(h[n] - h[n + 1]),
                         solve(n + 2, max) + abs(h[n] - h[n + 2]));
  v[n] = 1;

  return cost;
}

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

  h = new int[N];
  dp = new int[N];
  v = new bool[N];

  fill(dp, dp + N, 0);
  fill(v, v + N, 0);

  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }

  cout << solve(0, N) << endl;
  return 0;
}
Problem p03160 (C++)

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

using namespace std;

vector<int64_t> dp(100010, INT64_MAX);

int64_t dfs(vector<int> &arr, int i) {
  if (dp[i] != INT64_MAX) {
    return dp[i];
  }

  if (i <= 0) {
    return 0;
  }

  //
  //  i - 1 と、i - 2 への遷移を再帰的に集めてきて
  //  それの min を返り値とする
  //
  //  これは、同じ i であれば、返り値が同じになる
  //  ことがメモ化の鍵となる。
  //

  //  i - 1 からジャンプ
  //  abs(arr[i] - arr[i - 1]) だけコストが増えていく
  int64_t result = INT64_MAX;
  result = min(result, dfs(arr, i - 1) + abs(arr[i] - arr[i - 1]));

  //  i - 2 からジャンプ
  if (i > 1) {
    result = min(result, dfs(arr, i - 2) + abs(arr[i] - arr[i - 2]));
  }

  return result;
}

int main(int argc, char *argv[]) {
  int N;
  cin >> N;

  vector<int> arr;
  int cur;
  for (int i = 0; i < N; i++) {
    cin >> cur;
    arr.push_back(cur);
  }

  cout << dfs(arr, N - 1) << endl;
  return 0;
}

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

using namespace std;

vector<int64_t> dp(100010, INT64_MAX);

int64_t dfs(vector<int> &arr, int i) {
  if (dp[i] != INT64_MAX) {
    return dp[i];
  }

  if (i <= 0) {
    return 0;
  }

  //
  //  i - 1 と、i - 2 への遷移を再帰的に集めてきて
  //  それの min を返り値とする
  //
  //  これは、同じ i であれば、返り値が同じになる
  //  ことがメモ化の鍵となる。
  //

  //  i - 1 からジャンプ
  //  abs(arr[i] - arr[i - 1]) だけコストが増えていく
  int64_t result = INT64_MAX;
  result = min(result, dfs(arr, i - 1) + abs(arr[i] - arr[i - 1]));

  //  i - 2 からジャンプ
  if (i > 1) {
    result = min(result, dfs(arr, i - 2) + abs(arr[i] - arr[i - 2]));
  }

  dp[i] = result;
  return result;
}

int main(int argc, char *argv[]) {
  int N;
  cin >> N;

  vector<int> arr;
  int cur;
  for (int i = 0; i < N; i++) {
    cin >> cur;
    arr.push_back(cur);
  }

  cout << dfs(arr, N - 1) << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef vector<ll> vl;
typedef vector<vl> vv;
#define pb push_back
#define MOD 1000000007
#define INF 1000000000
#define endl '\n'
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}

int main() {

  fast();
#ifndef ONLINE_JUDGE
  freopen("F:\\Codes\\input.txt", "r", stdin);
  freopen("F:\\Codes\\output.txt", "w", stdout);
#endif

  ll n, i;
  cin >> n;

  vl hgt(n), dp(n);

  for (i = 0; i < n; i++) {
    cin >> hgt[i];
  }

  dp[1] = abs(hgt[1] - hgt[0]);

  for (i = 2; i < n; i++) {
    dp[i] = min((dp[i - 1] + abs(hgt[i] - hgt[i - 1])),
                (dp[i - 2] + abs(hgt[i] - hgt[i - 2])));
  }
  cout << dp[n - 1] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef vector<ll> vl;
typedef vector<vl> vv;
#define pb push_back
#define MOD 1000000007
#define INF 1000000000
#define endl '\n'
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}

int main() {

  fast();
  // #ifndef ONLINE_JUDGE
  // freopen("F:\\Codes\\input.txt", "r", stdin);
  // freopen("F:\\Codes\\output.txt", "w", stdout);
  // #endif

  ll n, i;
  cin >> n;

  vl hgt(n), dp(n);

  for (i = 0; i < n; i++) {
    cin >> hgt[i];
  }

  dp[1] = abs(hgt[1] - hgt[0]);

  for (i = 2; i < n; i++) {
    dp[i] = min((dp[i - 1] + abs(hgt[i] - hgt[i - 1])),
                (dp[i - 2] + abs(hgt[i] - hgt[i - 2])));
  }
  cout << dp[n - 1] << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define erep(i, n) for (ll i = 1; i <= (n)i++)
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;
// input
ll h[10010];
// dp tabel
ll dp[100010];

int main(void) {
  ll n;
  cin >> n;
  rep(i, n) cin >> h[i];
  rep(i, 100010) dp[i] = 1e9;
  dp[0] = 0;
  rep(i, n) {
    mins(dp[i + 1], dp[i] + abs(h[i + 1] - h[i]));
    mins(dp[i + 2], dp[i] + abs(h[i + 2] - h[i]));
  }
  cout << dp[n - 1] << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define erep(i, n) for (ll i = 1; i <= (n)i++)
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;
// input
ll h[100010];
// dp tabel
ll dp[100010];

int main(void) {
  ll n;
  cin >> n;
  rep(i, n) cin >> h[i];
  rep(i, 100010) dp[i] = 1e9;
  dp[0] = 0;
  rep(i, n) {
    mins(dp[i + 1], dp[i] + abs(h[i + 1] - h[i]));
    mins(dp[i + 2], dp[i] + abs(h[i + 2] - h[i]));
  }
  cout << dp[n - 1] << '\n';
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#define ll long long
#define fab(a, b, i) for (int i = a; i < b; i++)
#define pb push_back
#define db double
#define mp make_pair
#define endl "\n"
#define f first
#define se second
#define all(x) x.begin(), x.end()
#define MOD 1000000007
#define quick                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
using namespace std;

int main() {
  quick;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  int h[n];
  fab(0, n, i) cin >> h[i];
  vector<int> dp(n);
  dp[n - 1] = 0;
  dp[n - 2] = abs(h[n - 1] - h[n - 2]);

  for (int i = n - 3; i >= 0; i--) {
    dp[i] =
        min(dp[i + 1] + abs(h[i + 1] - h[i]), dp[i + 2] + abs(h[i + 2] - h[i]));
  }
  cout << dp[0] << endl;

  return 0;
}

Fixed submission:
// #pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#define ll long long
#define fab(a, b, i) for (int i = a; i < b; i++)
#define pb push_back
#define db double
#define mp make_pair
#define endl "\n"
#define f first
#define se second
#define all(x) x.begin(), x.end()
#define MOD 1000000007
#define quick                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
using namespace std;

int main() {
  quick;

  int n;
  cin >> n;
  int h[n];
  fab(0, n, i) cin >> h[i];
  vector<int> dp(n);
  dp[n - 1] = 0;
  dp[n - 2] = abs(h[n - 1] - h[n - 2]);

  for (int i = n - 3; i >= 0; i--) {
    dp[i] =
        min(dp[i + 1] + abs(h[i + 1] - h[i]), dp[i + 2] + abs(h[i + 2] - h[i]));
  }
  cout << dp[0] << endl;

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define F(i, a, b) for (ll i = a; i < b; i++)
#define RF(i, a, b) for (ll i = a; i > b; i--)
#define pii pair<ll, ll>
#define ff first
#define ss second
#define pb(x) push_back(x)
#define pob pop_back
#define mp(x, y) make_pair(x, y)
#define mod 1000000007
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
using namespace std;
const int inf = (int)1e9;
char flip_bits(char c) { return (c == '0') ? '1' : '0'; }

int smallestDivisor(int n) {
  // if divisible by 2
  if (n % 2 == 0)
    return 2;

  // iterate from 3 to sqrt(n)
  for (int i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return i;
  }

  return n;
}
int main() {

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

#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  ll a[100000];
  ll dp[100000];
  ll n;
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll i, j, prev;
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (i = 2; i < n; i++) {
    dp[i] = min((dp[i - 1] + abs(a[i] - a[i - 1])),
                (dp[i - 2] + abs(a[i] - a[i - 2])));
  }
  cout << dp[n - 1];

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define F(i, a, b) for (ll i = a; i < b; i++)
#define RF(i, a, b) for (ll i = a; i > b; i--)
#define pii pair<ll, ll>
#define ff first
#define ss second
#define pb(x) push_back(x)
#define pob pop_back
#define mp(x, y) make_pair(x, y)
#define mod 1000000007
#define w(t)                                                                   \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
using namespace std;
const int inf = (int)1e9;
char flip_bits(char c) { return (c == '0') ? '1' : '0'; }

int smallestDivisor(int n) {
  // if divisible by 2
  if (n % 2 == 0)
    return 2;

  // iterate from 3 to sqrt(n)
  for (int i = 3; i * i <= n; i += 2) {
    if (n % i == 0)
      return i;
  }

  return n;
}
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);
  ll a[100000];
  ll dp[100000];
  ll n;
  cin >> n;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll i, j, prev;
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (i = 2; i < n; i++) {
    dp[i] = min((dp[i - 1] + abs(a[i] - a[i - 1])),
                (dp[i - 2] + abs(a[i] - a[i - 2])));
  }
  cout << dp[n - 1];

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define ll long long
using namespace std;
ll int min(ll int a, ll int b) {
  if (a > b)
    return b;
  return a;
}
ll int abs(ll int a) {
  if (a > 0)
    return a;
  return -a;
}
ll int x[10000];
int main() {
  int a;
  cin >> a;
  int b[a + 6];
  for (int i = 0; i < a; i++)
    cin >> b[i];
  x[1] = abs(b[1] - b[0]);
  for (int i = 2; i < a; i++)
    x[i] =
        min(x[i - 2] + abs(b[i] - b[i - 2]), x[i - 1] + abs(b[i] - b[i - 1]));
  cout << x[a - 1];
}

Fixed submission:
#include <iostream>
#define ll long long
using namespace std;
ll int min(ll int a, ll int b) {
  if (a > b)
    return b;
  return a;
}
ll int abs(ll int a) {
  if (a > 0)
    return a;
  return -a;
}
ll int x[200000];
int main() {
  int a;
  cin >> a;
  int b[a + 6];
  for (int i = 0; i < a; i++)
    cin >> b[i];
  x[1] = abs(b[1] - b[0]);
  for (int i = 2; i < a; i++)
    x[i] =
        min(x[i - 2] + abs(b[i] - b[i - 2]), x[i - 1] + abs(b[i] - b[i - 1]));
  cout << x[a - 1];
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// DP1
#include <bits/stdc++.h>
#include <cfloat>
#include <climits>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <string>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h.at(i);
  }
  vector<int> cost(n);
  cost.at(0) = 0;
  cost.at(1) = abs(h.at(1) - h.at(0));
  for (int i = 0; i < n; i++) {
    cost.at(i) = min(abs(h.at(i) - h.at(i - 1)) +
                         abs(h.at(i - 1) - h.at(i - 2)) + cost.at(i - 2),
                     abs(h.at(i) - h.at(i - 2)) + cost.at(i - 1));
  }
  cout << cost.at(n - 1) << endl;
  return 0;
}

Fixed submission:
// DP1
#include <bits/stdc++.h>
#include <cfloat>
#include <climits>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <string>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h.at(i);
  }
  vector<int> cost(n);
  cost.at(0) = 0;
  cost.at(1) = abs(h.at(0) - h.at(1));
  for (int i = 2; i < n; i++) {
    cost.at(i) = min(cost.at(i - 2) + abs(h.at(i) - h.at(i - 2)),
                     cost.at(i - 1) + abs(h.at(i) - h.at(i - 1)));
  }
  cout << cost.at(n - 1) << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
// #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;

int main() {
  int n = 0;
  cin >> n;
  vector<long long> dp(10);

  vector<long long> h(n);
  rep(i, n) cin >> h.at(i);
  rep(i, dp.size()) dp.at(i) = INF;
  dp.at(0) = 0;
  for (int i = 1; i < n; i++) {
    chmin(dp.at(i), dp.at(i - 1) + abs(h.at(i) - h.at(i - 1)));
    if (i > 1) {
      chmin(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
    }
  }
  cout << dp[n - 1];
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
// #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;

int main() {
  int n = 0;
  cin >> n;
  vector<long long> dp(n);

  vector<long long> h(n);
  rep(i, n) cin >> h.at(i);
  rep(i, dp.size()) dp.at(i) = INF;
  dp.at(0) = 0;
  for (int i = 1; i < n; i++) {
    chmin(dp.at(i), dp.at(i - 1) + abs(h.at(i) - h.at(i - 1)));
    if (i > 1) {
      chmin(dp[i], dp[i - 2] + abs(h[i] - h[i - 2]));
    }
  }
  cout << dp[n - 1];
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
const int N = 1e18;

int32_t main() {

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

  int n;
  cin >> n;
  int mincost[n + 1];
  int A[n + 1];
  int cost1, cost2;

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

  if (n == 1)
    return cout << 0 << endl, 0;

  mincost[2] = abs(A[2] - A[1]);

  for (int i = 3; i <= n; i++) {
    cost1 = mincost[i - 2] + abs(A[i] - A[i - 2]);
    cost2 = mincost[i - 1] + abs(A[i] - A[i - 1]);
    mincost[i] = min(cost1, cost2);
  }

  cout << mincost[n] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long
#define watch(x) cout << (#x) << " is " << (x) << endl
#define pb push_back
const int N = 1e18;

int32_t main() {
  IOS;

  int n;
  cin >> n;
  int mincost[n + 1];
  int A[n + 1];
  int cost1, cost2;

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

  if (n == 1)
    return cout << 0 << endl, 0;

  mincost[2] = abs(A[2] - A[1]);

  for (int i = 3; i <= n; i++) {
    cost1 = mincost[i - 2] + abs(A[i] - A[i - 2]);
    cost2 = mincost[i - 1] + abs(A[i] - A[i - 1]);
    mincost[i] = min(cost1, cost2);
  }

  cout << mincost[n] << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
// #include <bits/stdc++.h>
#define lson l, mid, rt << 1

#define rson mid + 1, r, rt << 1 | 1

const int N = 5e4 + 100;
using namespace std;

int a[N];
int dp[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  dp[1] = 0;
  dp[2] = abs(a[1] - a[2]);
  for (int i = 3; i <= n; i++) {
    dp[i] =
        min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i] - a[i - 2]));
  }
  //    for(int i=1;i<=n;i++) cout<<dp[i]<<" ";
  //    cout<<endl;
  printf("%d\n", dp[n]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
// #include <bits/stdc++.h>
#define lson l, mid, rt << 1

#define rson mid + 1, r, rt << 1 | 1

const int N = 5e5 + 100;
using namespace std;

int a[N];
int dp[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  dp[1] = 0;
  dp[2] = abs(a[1] - a[2]);
  for (int i = 3; i <= n; i++) {
    dp[i] =
        min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i] - a[i - 2]));
  }
  //    for(int i=1;i<=n;i++) cout<<dp[i]<<" ";
  //    cout<<endl;
  printf("%d\n", dp[n]);
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/dp/tasks/dp_a
// 29-02-2020
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<int> h(n), dp(n, 0);
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < n; ++i) {
    dp[i] =
        min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << dp[n - 1] << '\n';
}

Fixed submission:
// https://atcoder.jp/contests/dp/tasks/dp_a
// 29-02-2020
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // #ifndef ONLINE_JUDGE
  // freopen ("input.txt", "r", stdin);
  // freopen ("output.txt", "w", stdout);
  // #endif
  int n;
  cin >> n;
  vector<int> h(n), dp(n, 0);
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < n; ++i) {
    dp[i] =
        min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << dp[n - 1] << '\n';
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define lc 2 * v
#define rc 2 * v + 1
#define mid (s + e) / 2
#define ll long long
#define int long long
#define ld long double
#define pii pair<int, int>
#define pll pair<long long, long long>
#define FAST                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

const int maxn = 130, N = 1e5 + 5, SQ = 600, base = 1999, mod = 1e9 + 7,
          INF = 1e18, lg = 17;

int n, a[maxn], dp[maxn];

int32_t main() {
  FAST cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  fill(dp, dp + maxn, INF);
  dp[0] = 0;

  for (int i = 0; i < n; ++i) {
    if (i - 1 >= 0) {
      dp[i] = min(dp[i], dp[i - 1] + abs(a[i] - a[i - 1]));
    }
    if (i - 2 >= 0) {
      dp[i] = min(dp[i], dp[i - 2] + abs(a[i] - a[i - 2]));
    }
  }
  cout << dp[n - 1] << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define lc 2 * v
#define rc 2 * v + 1
#define mid (s + e) / 2
#define ll long long
#define int long long
#define ld long double
#define pii pair<int, int>
#define pll pair<long long, long long>
#define FAST                                                                   \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")

const int maxn = 3e5, N = 1e5 + 5, SQ = 600, base = 1999, mod = 1e9 + 7,
          INF = 1e18, lg = 17;

int n, a[maxn], dp[maxn];

int32_t main() {
  FAST cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  fill(dp, dp + maxn, INF);
  dp[0] = 0;

  for (int i = 0; i < n; ++i) {
    if (i - 1 >= 0) {
      dp[i] = min(dp[i], dp[i - 1] + abs(a[i] - a[i - 1]));
    }
    if (i - 2 >= 0) {
      dp[i] = min(dp[i], dp[i - 2] + abs(a[i] - a[i - 2]));
    }
  }
  cout << dp[n - 1] << '\n';
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define rep(n) for (int i = 0; i < n; i++)
#define rrep(n) for (int i = n; i >= 0; i--)
#define Rep(a, b) for (int i = a; i < b; i++)
#define Rrep(a, b) for (int i = a; i >= b; i--)
#define gcd(x, y) __gcd(x, y)
#define mp make_pair
#define fi first
#define se second
#define fixed(x) fixed << setprecision(x)
const ll MOD = (1e9) + 7;
const ll mod = 998244353;
const ll inf = (1LL << 62);
template <class A, class B> inline bool chmax(A &a, const B &b) {
  return b > a && (a = b, true);
}
template <class A, class B> inline bool chmin(A &a, const B &b) {
  return b < a && (a = b, true);
}
template <class A> inline A abs(A &a) { return (a < 0 ? -a : a); }
using namespace std;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  ll h[100000], dp[100000];
  cin >> n;
  rep(n) {
    cin >> h[i];
    dp[i] = inf;
  }

  dp[0] = 0;

  rep(n) {
    chmin(dp[i + 1], dp[i] + abs(h[i] - h[i + 1]));
    chmin(dp[i + 2], dp[i] + abs(h[i] - h[i + 2]));
  }

  cout << dp[n - 1] << "\n";

  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define rep(n) for (int i = 0; i < n; i++)
#define rrep(n) for (int i = n; i >= 0; i--)
#define Rep(a, b) for (int i = a; i < b; i++)
#define Rrep(a, b) for (int i = a; i >= b; i--)
#define gcd(x, y) __gcd(x, y)
#define mp make_pair
#define fi first
#define se second
#define fixed(x) fixed << setprecision(x)
const ll MOD = (1e9) + 7;
const ll mod = 998244353;
const ll inf = (1LL << 62);
template <class A, class B> inline bool chmax(A &a, const B &b) {
  return b > a && (a = b, true);
}
template <class A, class B> inline bool chmin(A &a, const B &b) {
  return b < a && (a = b, true);
}
template <class A> inline A abs(A &a) { return (a < 0 ? -a : a); }
using namespace std;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  ll h[1000000], dp[1000000];
  cin >> n;
  rep(n) {
    cin >> h[i];
    dp[i] = inf;
  }

  dp[0] = 0;

  rep(n) {
    chmin(dp[i + 1], dp[i] + abs(h[i] - h[i + 1]));
    chmin(dp[i + 2], dp[i] + abs(h[i] - h[i + 2]));
  }

  cout << dp[n - 1] << "\n";

  return (0);
}
Problem p03160 (C++)

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

int main() {
  int N;
  cin >> N;
  int dp[1100];
  for (int i = 0; i < 110000; i++)
    dp[i] = 1000000000;
  int h[N];
  for (int i = 0; i < N; i++)
    cin >> h[i];

  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  // cout<<dp[0]<<endl<<dp[1]<<endl;

  for (int i = 2; i < N; i++) {
    dp[i] =
        min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
    // cout<<dp[i]<<endl;
  }

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

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

int main() {
  int N;
  cin >> N;
  int dp[110000];
  for (int i = 0; i < 110000; i++)
    dp[i] = 1000000000;
  int h[N];
  for (int i = 0; i < N; i++)
    cin >> h[i];

  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  // cout<<dp[0]<<endl<<dp[1]<<endl;

  for (int i = 2; i < N; i++) {
    dp[i] =
        min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
    // cout<<dp[i]<<endl;
  }

  cout << dp[N - 1] << endl;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;
const int mxn = 1e5 + 100;
int a[mxn], dp[mxn], n;
bool cal[mxn];
int abs(int x) { return (x > 0 ? x : -x); }
int dpf(int x) {

  if (cal[x] == true)
    return dp[x];
  dp[x] = dpf(x - 1) + abs(a[x] - a[x - 1]);
  if (x > 1)
    dp[x] = min(dp[x], dpf(x - 2) + abs(a[x] - a[x - 2]));
  return dp[x];
}
int main() {
  cin >> n;
  cal[0] = true;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << dpf(n - 1);
  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;
const int mxn = 1e5 + 100;
int a[mxn], dp[mxn], n;
bool cal[mxn];
int abs(int x) { return (x > 0 ? x : -x); }
int dpf(int x) {

  if (cal[x] == true)
    return dp[x];
  cal[x] = true;
  dp[x] = dpf(x - 1) + abs(a[x] - a[x - 1]);
  if (x > 1)
    dp[x] = min(dp[x], dpf(x - 2) + abs(a[x] - a[x - 2]));
  return dp[x];
}
int main() {
  cin >> n;
  cal[0] = true;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << dpf(n - 1);
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const LL INF = 1e15;
LL memo[100005];
LL datas[100005];
int N;
LL solve(int ix) {
  if (ix == N)
    return 0;
  if (ix > N)
    return INF;
  if (memo[ix] != -1)
    return memo[ix];
  return min(abs(datas[ix] - datas[ix + 1]) + solve(ix + 1),
             abs(datas[ix] - datas[ix + 2]) + solve(ix + 2));
}

int main() {
  scanf("%d", &N);
  for (int a = 1; a <= N; a++) {
    scanf("%lld", &datas[a]);
  }
  memset(memo, -1, sizeof(memo));
  printf("%lld\n", solve(1));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const LL INF = 1e15;
LL memo[100005];
LL datas[100005];
int N;
LL solve(int ix) {
  if (ix == N)
    return 0;
  if (ix > N)
    return INF;
  if (memo[ix] != -1)
    return memo[ix];
  return memo[ix] = min(abs(datas[ix] - datas[ix + 1]) + solve(ix + 1),
                        abs(datas[ix] - datas[ix + 2]) + solve(ix + 2));
}

int main() {
  scanf("%d", &N);
  for (int a = 1; a <= N; a++) {
    scanf("%lld", &datas[a]);
  }
  memset(memo, -1, sizeof(memo));
  printf("%lld\n", solve(1));
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;

int main() {
  int n;
  scanf("%d", &n);
  vector<int> h(n);
  for (int &x : h) {
    scanf("%d", &x);
  }
  vector<int> ans(n, 0);
  ans[n - 2] = abs(h[n - 1] - h[n - 2]);
  ans[n - 3] = abs(h[n - 3] - h[n - 1]);
  for (int i = n - 4; i >= 0; i--) {
    ans[i] = min(ans[i + 1] + abs(h[i] - h[i + 1]),
                 ans[i + 2] + abs(h[i] - h[i + 2]));
  }
  printf("%d", ans[0]);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;

int main() {
  int n;
  scanf("%d", &n);
  vector<int> h(n);
  for (int &x : h) {
    scanf("%d", &x);
  }
  vector<int> ans(n, 0);
  ans[n - 2] = abs(h[n - 1] - h[n - 2]);
  if (n >= 3)
    ans[n - 3] = abs(h[n - 3] - h[n - 1]);
  for (int i = n - 4; i >= 0; i--) {
    ans[i] = min(ans[i + 1] + abs(h[i] - h[i + 1]),
                 ans[i + 2] + abs(h[i] - h[i + 2]));
  }
  printf("%d", ans[0]);
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

const int maxN = 1e4 + 5, INF = 1e9 + 5;

vector<int> dp(maxN, INF), a(maxN);

int rc(int in) {
  if (dp[in] != INF)
    return dp[in];
  if (in == 0) {
    return dp[0] = 0;
  }
  if (in == 1) {
    return dp[1] = abs(a[1] - a[0]);
  }
  return dp[in] = min(rc(in - 1) + abs(a[in] - a[in - 1]),
                      rc(in - 2) + abs(a[in] - a[in - 2]));
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << rc(n - 1);
  return 0;
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

const int maxN = 2e5 + 5, INF = 1e9 + 5;

vector<int> dp(maxN, INF), a(maxN);

int rc(int in) {
  if (dp[in] != INF)
    return dp[in];
  if (in == 0) {
    return dp[0] = 0;
  }
  if (in == 1) {
    return dp[1] = abs(a[1] - a[0]);
  }
  return dp[in] = min(rc(in - 1) + abs(a[in] - a[in - 1]),
                      rc(in - 2) + abs(a[in] - a[in - 2]));
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << rc(n - 1);
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;

ll N;
vector<ll> h;

ll memo[10001];

ll func(ll pos) {
  if (memo[pos] != -1)
    return memo[pos];

  if (pos == 0)
    return 0;
  if (pos == 1)
    return abs(h[1] - h[0]);

  memo[pos] = min(func(pos - 1) + abs(h[pos - 1] - h[pos]),
                  func(pos - 2) + abs(h[pos - 2] - h[pos]));

  return memo[pos];
}

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

  cin >> N;
  h.resize(N);

  for (ll i = 0; i < N; ++i)
    cin >> h[i];
  for (auto &e : memo)
    e = -1;

  cout << func(N - 1) << endl;

  // system("pause");
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;

ll N;
vector<ll> h;

ll memo[100001];

ll func(ll pos) {
  if (memo[pos] != -1)
    return memo[pos];

  if (pos == 0)
    return 0;
  if (pos == 1)
    return abs(h[1] - h[0]);

  memo[pos] = min(func(pos - 1) + abs(h[pos - 1] - h[pos]),
                  func(pos - 2) + abs(h[pos - 2] - h[pos]));

  return memo[pos];
}

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

  cin >> N;
  h.resize(N);

  for (ll i = 0; i < N; ++i)
    cin >> h[i];
  for (auto &e : memo)
    e = -1;

  cout << func(N - 1) << endl;

  // system("pause");
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define Phuong // <3
typedef pair<int, int> ii;

const int inf = 1e9 + 1;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif // ONLINE_JUDGE
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> dp(n, inf);
  vector<int> h(n);
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < n; ++i) {
    dp[i] =
        min(dp[i - 1] + abs(h[i - 1] - h[i]), dp[i - 2] + abs(h[i - 2] - h[i]));
  }
  cout << dp[n - 1];
  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define Phuong // <3
typedef pair<int, int> ii;

const int inf = 1e9 + 1;

int main() {
#ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
#endif // ONLINE_JUDGE
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> dp(n, inf);
  vector<int> h(n);
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);
  for (int i = 2; i < n; ++i) {
    dp[i] =
        min(dp[i - 1] + abs(h[i - 1] - h[i]), dp[i - 2] + abs(h[i - 2] - h[i]));
  }
  cout << dp[n - 1];
  return (0);
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define INF 1000000000
using namespace std;
int n, h[100005], mem[100005];
int dp(int i) {
  if (i == 1)
    return 0;
  if (i < 1)
    return INF;
  int a1 = dp(i - 1) + abs(h[i - 1] - h[i]);
  int a2 = dp(i - 2) + abs(h[i - 2] - h[i]);
  return mem[i] = min(a1, a2);
}
int main() {
  cin >> n;
  memset(mem, -1, sizeof(mem));
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  cout << dp(n) << '\n';
  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
#define INF 1000000000
using namespace std;
int n, h[100005], mem[100005];
int dp(int i) {
  if (i == 1)
    return 0;
  if (i < 1)
    return INF;
  if (mem[i] != -1)
    return mem[i];
  int a1 = dp(i - 1) + abs(h[i - 1] - h[i]);
  int a2 = dp(i - 2) + abs(h[i - 2] - h[i]);
  return mem[i] = min(a1, a2);
}
int main() {
  cin >> n;
  memset(mem, -1, sizeof(mem));
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  cout << dp(n) << '\n';
  return (0);
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// Inbornhandsome
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define FIO                                                                    \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout)
#define endl '\n'
#define int long long

const int N = 1e5 + 7;

int stones[N], cost[N], n;

int find_cost(int stone) {
  if (stone > n)
    return 1e9;
  if (stone == n)
    return 0;

  int &ans = cost[stone];

  if (ans != -1)
    return ans;

  ans = min(abs(stones[stone] - stones[stone + 1]) + find_cost(stone + 1),
            abs(stones[stone] - stones[stone + 2]) + find_cost(stone + 2));
  return ans;
}

int32_t main() {
  IOS;
  FIO;
  cin >> n;
  memset(cost, -1, sizeof(cost));
  for (int i = 1; i <= n; ++i)
    cin >> stones[i];
  cout << find_cost(1);
  return 0;
}

Fixed submission:
// Inbornhandsome
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define FIO                                                                    \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout)
#define endl '\n'
#define int long long

const int N = 1e5 + 7;

int stones[N], cost[N], n;

int find_cost(int stone) {
  if (stone > n)
    return 1e9;
  if (stone == n)
    return 0;

  int &ans = cost[stone];

  if (ans != -1)
    return ans;

  ans = min(abs(stones[stone] - stones[stone + 1]) + find_cost(stone + 1),
            abs(stones[stone] - stones[stone + 2]) + find_cost(stone + 2));
  return ans;
}

int32_t main() {
  IOS; // FIO;
  cin >> n;
  memset(cost, -1, sizeof(cost));
  for (int i = 1; i <= n; ++i)
    cin >> stones[i];
  cout << find_cost(1);
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
long double PI = 3.141592653589793238463;
const int N = 1e5 + 5;
const int MOD = 1e9 + 7;

ll a[100005];
ll dp[100005][4];
int n;

int D(int x) {
  for (int i = x; i < n; i++) {
    dp[i][0] = (abs(a[i] - a[i - 1]) + min(dp[i - 1][0], dp[i - 1][1]));
    dp[i][1] = (abs(a[i] - a[i - 2]) + min(dp[i - 2][0], dp[i - 2][1]));
  }
}

int main() {
  // int t; cin>>t;

  // while(t--){
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    dp[i][0] = N * N;
    dp[i][1] = N * N;
  }
  dp[0][0] = 0;
  dp[1][0] = (abs(a[0] - a[1]));
  D(2);

  cout << min(dp[n - 1][0], dp[n - 1][1]);

  //}

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
long double PI = 3.141592653589793238463;
const int N = 1e5 + 5;
const int MOD = 1e9 + 7;

ll a[100005];
ll dp[100005][4];
int n;

void D(int x) {
  for (int i = x; i < n; i++) {
    dp[i][0] = (abs(a[i] - a[i - 1]) + min(dp[i - 1][0], dp[i - 1][1]));
    dp[i][1] = (abs(a[i] - a[i - 2]) + min(dp[i - 2][0], dp[i - 2][1]));
  }
}

int main() {
  // int t; cin>>t;

  // while(t--){
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    dp[i][0] = N * N;
    dp[i][1] = N * N;
  }
  dp[0][0] = 0;
  dp[1][0] = (abs(a[0] - a[1]));
  D(2);

  cout << min(dp[n - 1][0], dp[n - 1][1]);

  //}

  return 0;
}
Problem p03160 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define EACH(i, a, b) for (int i = (a); i <= (b); i++)
#define REACH(i, a, b) for (int i = (b); i >= (a); i--)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()

const int MOD = 1e9 + 7;
const int INF = 1 << 29;
const double EPS = 1e-10;

using ll = long long;
using P = pair<int, int>;
template <class T = int> using V = vector<T>;

template <class T> bool inline chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool inline chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T = int> T inline input() {
  T x;
  cin >> x;
  return (x);
}
template <class T> void inline print(T &x) { cout << x << '\n'; }
#define debug(x) cerr << #x << ": " << x << endl;

int dp[10010];

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  int h[N];
  REP(i, N) cin >> h[i];

  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);

  FOR(i, 2, N) {
    dp[i] =
        min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
  }

  print(dp[N - 1]);

  return 0;
}


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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define EACH(i, a, b) for (int i = (a); i <= (b); i++)
#define REACH(i, a, b) for (int i = (b); i >= (a); i--)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()

const int MOD = 1e9 + 7;
const int INF = 1 << 29;
const double EPS = 1e-10;

using ll = long long;
using P = pair<int, int>;
template <class T = int> using V = vector<T>;

template <class T> bool inline chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool inline chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T = int> T inline input() {
  T x;
  cin >> x;
  return (x);
}
template <class T> void inline print(T &x) { cout << x << '\n'; }
#define debug(x) cerr << #x << ": " << x << endl;

int dp[100100];

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  int h[N];
  REP(i, N) cin >> h[i];

  dp[0] = 0;
  dp[1] = abs(h[1] - h[0]);

  FOR(i, 2, N) {
    dp[i] =
        min(dp[i - 1] + abs(h[i] - h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]));
  }

  print(dp[N - 1]);

  return 0;
}