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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
const ll M = 1e5 + 6;
ll dp[M], a[M];
ll rec(ll index) {
  if (index <= 2)
    return dp[index];
  if (dp[index] != -1)
    return dp[index];

  else
    return min(abs(a[index] - a[index - 1]) + rec(index - 1),
               abs(a[index] - a[index - 2]) + rec(index - 2));
}
int main() {
  ll n, i, j, k, x, y, t, m;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i], dp[i] = -1;
  dp[1] = 0;
  dp[2] = abs(a[2] - a[1]);
  cout << rec(n) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
const ll M = 1e5 + 6;
ll dp[M], a[M];
ll rec(ll index) {
  if (index <= 2)
    return dp[index];
  if (dp[index] != -1)
    return dp[index];

  else {
    dp[index] = min(abs(a[index] - a[index - 1]) + rec(index - 1),
                    abs(a[index] - a[index - 2]) + rec(index - 2));
    return dp[index];
  }
}
int main() {
  ll n, i, j, k, x, y, t, m;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> a[i], dp[i] = -1;
  dp[1] = 0;
  dp[2] = abs(a[2] - a[1]);
  cout << rec(n) << endl;
}
Problem p03160 (C++)

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

void solve() {
  int i, n;
  cin >> n;
  vector<int> v(n);
  vector<int> dp(n, 0);
  for (i = 0; i < n; i++)
    cin >> v[i];
  dp[0] = 0;
  dp[1] = abs(v[1] - v[0]);
  if (n == 1) {
    cout << dp[0] << "\n";
    return;
  }
  if (n == 2) {
    cout << dp[1] << "\n";
    return;
  }
  for (i = 2; i < n; i++) {
    dp[i] =
        min(dp[i - 1] + abs(v[i] - v[i - 1]), dp[i - 2] + abs(v[i] - v[i - 2]));
  }
  cout << dp[n - 1] << "\n";
}

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


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

void solve() {
  int i, n;
  cin >> n;
  vector<int> v(n);
  vector<int> dp(n, 0);
  for (i = 0; i < n; i++)
    cin >> v[i];
  dp[0] = 0;
  dp[1] = abs(v[1] - v[0]);
  if (n == 1) {
    cout << dp[0] << "\n";
    return;
  }
  if (n == 2) {
    cout << dp[1] << "\n";
    return;
  }
  for (i = 2; i < n; i++) {
    dp[i] =
        min(dp[i - 1] + abs(v[i] - v[i - 1]), dp[i - 2] + abs(v[i] - v[i - 2]));
  }
  cout << dp[n - 1] << "\n";
}

int main() {

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

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

using namespace std;

#define MP make_pair
#define PB push_back
#define F first
#define S second
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define all(x) x.begin(), x.end()

typedef pair<int, int> pi;
typedef long long ll;
const int MAX_INT = 2000000000;

int dp[100001];
int h[100001];

int cost(int i, int j) { return abs(h[i] - h[j]); }

int main() {
  int N;
  cin >> N;
  FOR(i, 1, N) { cin >> h[i]; }

  dp[N] = 0;
  dp[N - 1] = cost(N - 1, N);

  for (int i = N - 2; i >= 1; i++) {
    dp[i] = min(cost(i, i + 1) + dp[i + 1], cost(i, i + 2) + dp[i + 2]);
  }

  cout << dp[1];

  return 0;
}


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

using namespace std;

#define MP make_pair
#define PB push_back
#define F first
#define S second
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define all(x) x.begin(), x.end()

typedef pair<int, int> pi;
typedef long long ll;
const int MAX_INT = 2000000000;

int dp[100001];
int h[100001];

int cost(int i, int j) { return abs(h[i] - h[j]); }

int main() {
  int N;
  cin >> N;
  FOR(i, 1, N) { cin >> h[i]; }

  dp[N] = 0;
  dp[N - 1] = cost(N - 1, N);

  for (int i = N - 2; i >= 1; i--) {
    dp[i] = min(cost(i, i + 1) + dp[i + 1], cost(i, i + 2) + dp[i + 2]);
  }

  cout << dp[1];

  return 0;
}
Problem p03160 (C++)

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

using namespace std;

#define pan(i, n) for (int i = 0; i < n; i++)
#define pans(i, n, a) for (int i = a; i < n; i++)
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define INF 1000000007
#define PI 3.141592653
#define inf 100007
#define pll pair<ll, ll>
#define pii pair<int, int>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int main() {
  int n;
  cin >> n;
  vl dp(n + 1, inf), a(n + 1, inf);
  ll ans = 0;
  pans(i, n + 1, 1) cin >> a[i];
  dp[1] = 0, dp[2] = abs(a[2] - a[1]), dp[3] = abs(a[3] - a[1]);
  pans(i, n + 1, 4) {
    dp[i] =
        min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i - 2] - a[i]));
  }
  cout << dp[n];
}

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

using namespace std;

#define pan(i, n) for (int i = 0; i < n; i++)
#define pans(i, n, a) for (int i = a; i < n; i++)
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define INF 1000000007
#define PI 3.141592653
#define inf 100007
#define pll pair<ll, ll>
#define pii pair<int, int>
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int main() {
  int n;
  cin >> n;
  vl dp(n + 1, inf), a(n + 1, inf);
  ll ans = 0;
  pans(i, n + 1, 1) cin >> a[i];
  dp[0] = 0, dp[1] = 0;
  pans(i, n + 1, 2) {
    dp[i] =
        min(dp[i - 1] + abs(a[i] - a[i - 1]), dp[i - 2] + abs(a[i - 2] - a[i]));
  }
  cout << dp[n];
}
Problem p03160 (C++)

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

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

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  vector<int> h(n), v(n);
  for (auto &it : h)
    cin >> it;

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

  for (int i = 2; i < n; ++i) {
    v[i] =
        min(abs(h[i] - h[i - 1]) + v[i - 1], abs(h[i] - h[i - 2]) + v[i - 2]);
  }
  cout << v[n - 1];
  return 0;
}

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

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

  int n;
  cin >> n;
  vector<int> h(n), v(n);
  for (auto &it : h)
    cin >> it;

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

  for (int i = 2; i < n; ++i) {
    v[i] =
        min(abs(h[i] - h[i - 1]) + v[i - 1], abs(h[i] - h[i - 2]) + v[i - 2]);
  }
  cout << v[n - 1];
  return 0;
}
Problem p03160 (C++)

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

int main() {
  int N;
  int h[10002];
  int dp[10002];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> h[i];
    dp[i] = 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] << endl;
}


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

int main() {
  int N;
  int h[100002];
  int dp[100002];
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> h[i];
    dp[i] = 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] << endl;
}
Problem p03160 (C++)

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

void print(ll x[], ll n) {
  for (int i = 0; i < n; i++)
    cout << x[i] << " ";
  cout << endl;
}
int x[10009], n;
int dp[100009];
ll cost(int i) {
  if (i == n - 1)
    return 0;
  if (i >= n)
    return 100000000000;
  if (dp[i] != -1)
    return dp[i];
  dp[i] = min(abs(x[i + 1] - x[i]) + cost(i + 1),
              abs(x[i + 2] - x[i]) + cost(i + 2));
  return dp[i];
}

int main() {
  ios::sync_with_stdio(false);

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> x[i];
  memset(dp, -1, sizeof dp);
  cout << cost(0) << endl;

  return 0;
}

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

void print(ll x[], ll n) {
  for (int i = 0; i < n; i++)
    cout << x[i] << " ";
  cout << endl;
}
int x[100009], n;
int dp[100009];
ll cost(int i) {
  if (i == n - 1)
    return 0;
  if (i >= n)
    return 100000000000;
  if (dp[i] != -1)
    return dp[i];
  dp[i] = min(abs(x[i + 1] - x[i]) + cost(i + 1),
              abs(x[i + 2] - x[i]) + cost(i + 2));
  return dp[i];
}

int main() {
  ios::sync_with_stdio(false);

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> x[i];
  memset(dp, -1, sizeof dp);
  cout << cost(0) << endl;

  return 0;
}
Problem p03160 (C++)

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

int h[10005];
int dp[10005];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> 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(abs(h[i] - h[i - 1]) + dp[i - 1], abs(h[i] - h[i - 2]) + dp[i - 2]);
  }
  cout << dp[N - 1] << '\n';
}

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

int h[100005];
int dp[100005];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> 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(abs(h[i] - h[i - 1]) + dp[i - 1], abs(h[i] - h[i - 2]) + dp[i - 2]);
  }
  cout << dp[N - 1] << '\n';
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int dp[10001];
int main() {
#ifdef EVAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int N;
  cin >> N;
  int vet[N];
  for (int i = 0; i < N; i++) {
    cin >> vet[i];
  }
  fill(dp, dp + N, INT_MAX);
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    dp[i + 1] = min(dp[i + 1], dp[i] + abs(vet[i] - vet[i + 1]));
    dp[i + 2] = min(dp[i + 2], dp[i] + abs(vet[i] - vet[i + 2]));
  }
  cout << dp[N - 1];
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int dp[100001];
int main() {
#ifdef EVAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int N;
  cin >> N;
  int vet[N];
  for (int i = 0; i < N; i++) {
    cin >> vet[i];
  }
  fill(dp, dp + N, INT_MAX);
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    dp[i + 1] = min(dp[i + 1], dp[i] + abs(vet[i] - vet[i + 1]));
    dp[i + 2] = min(dp[i + 2], dp[i] + abs(vet[i] - vet[i + 2]));
  }
  cout << dp[N - 1];
}
Problem p03160 (C++)

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

int dp[1000], vet[100005];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &vet[i]);
  memset(dp, 0x3f3f3f3f, sizeof dp);
  dp[(n - 1)] = 0;
  for (int i = n - 2; i >= 0; i--) {
    dp[i] = min(dp[i], dp[i + 1] + abs(vet[i] - vet[i + 1]));
    if (i + 2 < n)
      dp[i] = min(dp[i], dp[i + 2] + abs(vet[i] - vet[i + 2]));
    // cout << i << ' ' << dp[i] << endl;
  }
  printf("%d\n", dp[0]);
}

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

int dp[100005], vet[100005];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &vet[i]);
  memset(dp, 0x3f3f3f3f, sizeof dp);
  dp[(n - 1)] = 0;
  for (int i = n - 2; i >= 0; i--) {
    dp[i] = min(dp[i], dp[i + 1] + abs(vet[i] - vet[i + 1]));
    if (i + 2 < n)
      dp[i] = min(dp[i], dp[i + 2] + abs(vet[i] - vet[i + 2]));
    // cout << i << ' ' << dp[i] << endl;
  }
  printf("%d\n", dp[0]);
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
/**
 *      purpose :
 *      author  : kyomukyomupurin
 *      created :
 **/

// input/output
#include <fstream>
#include <iostream>
#include <sstream>
// container class
#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// math, algorithm
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
// etc
#include <bitset>
#include <cctype>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <random>
#include <utility>
// using-directive
using namespace std;
// alias template
using int64 = int64_t;
using pii = pair<int, int>;
using pll = pair<int64_t, int64_t>;
// text macro replacement
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define print(x) cout << (x) << '\n'
#define debug(x) cout << #x << ": " << (x) << '\n'

const int INF = 1e9;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  int h[n];
  rep(i, n) cin >> h[i];

  int dp[10101]; // dp[i] : i + 1 番目の足場にたどりつくための最小コスト
  fill(dp, dp + 10101, INF);

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

  for (int i = 1; i < n; ++i) {
    dp[i + 1] =
        min(dp[i - 1] + abs(h[i + 1] - h[i - 1]), dp[i] + abs(h[i + 1] - h[i]));
  }
  print(dp[n - 1]);

  return 0;
}

Fixed submission:
/**
 *      purpose :
 *      author  : kyomukyomupurin
 *      created :
 **/

// input/output
#include <fstream>
#include <iostream>
#include <sstream>
// container class
#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// math, algorithm
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
// etc
#include <bitset>
#include <cctype>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <random>
#include <utility>
// using-directive
using namespace std;
// alias template
using int64 = int64_t;
using pii = pair<int, int>;
using pll = pair<int64_t, int64_t>;
// text macro replacement
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define print(x) cout << (x) << '\n'
#define debug(x) cout << #x << ": " << (x) << '\n'

const int INF = 1e9;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  int h[n];
  rep(i, n) cin >> h[i];

  int dp[101010]; // dp[i] : i + 1 番目の足場にたどりつくための最小コスト
  fill(dp, dp + 101010, INF);

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

  for (int i = 1; i < n; ++i) {
    dp[i + 1] =
        min(dp[i - 1] + abs(h[i + 1] - h[i - 1]), dp[i] + abs(h[i + 1] - h[i]));
  }
  print(dp[n - 1]);

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#include <vector>
int main() {
  int n;
  cin >> n;
  const int INF = 1e9 + 7;
  vector<long long int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  vector<long long int> s(n, INF);
  s[0] = 0;
  for (long long int i = 0; i < n; i++) {
    for (long long int j = i + 1; j <= i + 2; j++) {
      s[j] = min(s[j], s[i] + abs(h[j] - h[i]));
    }
  }
  cout << s[n - 1];
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
#include <vector>
int main() {
  int n;
  cin >> n;
  const int INF = 1e9 + 7;
  vector<long long int> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  vector<long long int> s(n, INF);
  s[0] = 0;
  for (long long int i = 0; i < n; i++) {
    for (long long int j = i + 1; j <= i + 2; j++) {
      if (j < n)
        s[j] = min(s[j], s[i] + abs(h[j] - h[i]));
    }
  }
  cout << s[n - 1];
  return 0;
}
Problem p03160 (C++)

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

int main() {
  int n;
  scanf("%d", &n);

  long long arr[10010];
  vector<long long> distance(10010, INT_MAX);

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

  distance[0] = 0ll;

  for (int i = 0; i < n; i++) {
    if (i + 1 < n)
      distance[i + 1] =
          min(distance[i + 1], distance[i] + abs(arr[i + 1] - arr[i]));

    if (i + 2 < n)
      distance[i + 2] =
          min(distance[i + 2], distance[i] + abs(arr[i + 2] - arr[i]));
  }

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

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

int main() {
  int n;
  scanf("%d", &n);

  vector<long long> arr(n);
  vector<long long> distance(n, INT_MAX);

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

  distance[0] = 0ll;

  for (int i = 0; i < n; i++) {
    if (i + 1 < n)
      distance[i + 1] =
          min(distance[i + 1], distance[i] + abs(arr[i + 1] - arr[i]));

    if (i + 2 < n)
      distance[i + 2] =
          min(distance[i + 2], distance[i] + abs(arr[i + 2] - arr[i]));
  }

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

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

#define int long long
#define pb push_back
#define vi vector<int>
#define usi unordered_set<int>
#define si set<int>
#define umii unordered_map<int, int>
#define mii map<int, int>
#define all(a) a.begin(), a.end()
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define mod (int)(1e9 + 7)
#define INF INT_MAX

signed main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
// for writing output to output.txt
// freopen("output.txt", "w", stdout);
#endif

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

  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  int dp[n];
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (int i = 1; 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>
using namespace std;

#define int long long
#define pb push_back
#define vi vector<int>
#define usi unordered_set<int>
#define si set<int>
#define umii unordered_map<int, int>
#define mii map<int, int>
#define all(a) a.begin(), a.end()
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define mod (int)(1e9 + 7)
#define INF INT_MAX

signed main() {
  // #ifndef ONLINE_JUDGE
  // // for getting input from input.txt
  // freopen("input.txt", "r", stdin);
  // // for writing output to output.txt
  // //freopen("output.txt", "w", stdout);
  // #endif

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

  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  int dp[n];
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (int i = 1; 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 <bits/stdc++.h>
#define pb push_back
#define fr first
#define sc second
#define MOD 1000000007
#define len(s) s.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define f(i, a, n) for (int i = a; i < n; i++)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef map<int, int> mii;

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);
  cout.tie(NULL);

  // int t; cin>>t; while(t--) {
  int n;
  cin >> n;
  vi h(n);
  for (int &x : h)
    cin >> x;
  vi dp(n, MOD);
  dp[0] = 0;

  f(i, 0, n) {
    for (int j : {i + 1, i + 2}) {
      if (j < n)
        dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
    }
  }
  cout << dp[n - 1];

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define fr first
#define sc second
#define MOD 1000000007
#define len(s) s.size()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define all(v) v.begin(), v.end()
#define f(i, a, n) for (int i = a; i < n; i++)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef map<int, int> mii;

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);
  cout.tie(NULL);

  // int t; cin>>t; while(t--) {
  int n;
  cin >> n;
  vi h(n);
  for (int &x : h)
    cin >> x;
  vi dp(n, MOD);
  dp[0] = 0;

  f(i, 0, n) {
    for (int j : {i + 1, i + 2}) {
      if (j < n)
        dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
    }
  }
  cout << dp[n - 1];

  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
/*huzaifa242 copyright*/
#include <bits/stdc++.h>
using namespace std;
#define PI 3.1415926535897932384626
#define mod 1000000007
#define MAX 100005
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, i;
  cin >> n;
  int a[n], h[n];
  for (i = 0; i < n; i++) {
    cin >> h[i];
    if (i == 0)
      a[i] = 0;
    else if (i == 1)
      a[i] = abs(h[i] - h[0]);
    else
      a[i] =
          min(a[i - 1] + abs(h[i] - h[i - 1]), a[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << a[n - 1] << "\n";
  return 0;
}

Fixed submission:
/*huzaifa242 copyright*/
#include <bits/stdc++.h>
using namespace std;
#define PI 3.1415926535897932384626
#define mod 1000000007
#define MAX 100005
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n, i;
  cin >> n;
  int a[n], h[n];
  for (i = 0; i < n; i++) {
    cin >> h[i];
    if (i == 0)
      a[i] = 0;
    else if (i == 1)
      a[i] = abs(h[i] - h[0]);
    else
      a[i] =
          min(a[i - 1] + abs(h[i] - h[i - 1]), a[i - 2] + abs(h[i] - h[i - 2]));
  }
  cout << a[n - 1] << "\n";
  return 0;
}
Problem p03160 (C++)

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)

void FIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

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

int frog1(int index) {
  if (index == n - 1)
    return 0;

  if (dp[n] != -1)
    return dp[n];

  int left = abs(a[index] - a[index + 1]) + frog1(index + 1);

  int right = INT_MAX;

  if (index + 2 < n) {
    right = abs(a[index] - a[index + 2]) + frog1(index + 2);
  }

  return dp[index] = min(left, right);
}

int32_t main() {
  FIO();
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  memset(dp, -1, sizeof(dp));

  cout << frog1(0);
  return 0;
}

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)

void FIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

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

int frog1(int index) {
  if (index == n - 1)
    return 0;

  if (dp[index] != -1)
    return dp[index];

  int left = abs(a[index] - a[index + 1]) + frog1(index + 1);

  int right = INT_MAX;

  if (index + 2 < n) {
    right = abs(a[index] - a[index + 2]) + frog1(index + 2);
  }

  return dp[index] = min(left, right);
}

int32_t main() {
  FIO();
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }

  memset(dp, -1, sizeof(dp));

  cout << frog1(0);
  return 0;
}
Problem p03160 (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 REP(i, f, n) for (int i = f; i < n; i++)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  int d[10000];
  fill(d, d + n, 0);
  rep(i, n) cin >> a.at(i);
  d[1] = abs(a.at(1) - a.at(0));
  REP(i, 2, n) {
    d[i] = min(abs(a.at(i) - a.at(i - 1)) + d[i - 1],
               abs(a.at(i) - a.at(i - 2)) + d[i - 2]);
  }
  cout << d[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, f, n) for (int i = f; i < n; i++)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n);
  int d[100005];
  fill(d, d + n, 0);
  rep(i, n) cin >> a.at(i);
  d[1] = abs(a.at(1) - a.at(0));
  REP(i, 2, n) {
    d[i] = min(abs(a.at(i) - a.at(i - 1)) + d[i - 1],
               abs(a.at(i) - a.at(i - 2)) + d[i - 2]);
  }
  cout << d[n - 1] << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v;
  vector<int> cost;
  for (int i = 1; i < n + 1; ++i) {
    cin >> v[i];
  }
  cost[1] = 0;
  cost[2] = cost[1] + abs(v[1] - v[2]);
  for (int i = 3; i < n + 1; ++i) {
    cost[i] = min(cost[i - 2] + abs(v[i - 2] - v[i]),
                  cost[i - 1] + abs(v[i - 1] - v[i]));
  }
  cout << cost[n];
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v(n + 1, 0);
  vector<int> cost(n + 1, 0);
  for (int i = 1; i < n + 1; ++i) {
    cin >> v[i];
  }
  cost[1] = 0;
  cost[2] = cost[1] + abs(v[1] - v[2]);
  for (int i = 3; i < n + 1; ++i) {
    cost[i] = min(cost[i - 2] + abs(v[i - 2] - v[i]),
                  cost[i - 1] + abs(v[i - 1] - v[i]));
  }
  cout << cost[n];
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  vector<int> height;
  vector<int> dp(N);

  cin >> N;
  int x;
  for (int i = 0; i < N; i++) {
    cin >> x;
    height.push_back(x);
  }

  dp[0] = 0;
  dp[1] = abs(height[0] - height[1]);
  for (int i = 2; i < N; i++)
    dp[i] = min(dp[i - 1] + abs(height[i - 1] - height[i]),
                dp[i - 2] + abs(height[i - 2] - height[i]));

  return dp[N - 1];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  vector<int> height;
  vector<int> dp(N);

  cin >> N;
  int x;
  for (int i = 0; i < N; i++) {
    cin >> x;
    height.push_back(x);
  }

  dp[0] = 0;
  dp[1] = abs(height[0] - height[1]);
  for (int i = 2; i < N; i++)
    dp[i] = min(dp[i - 1] + abs(height[i - 1] - height[i]),
                dp[i - 2] + abs(height[i - 2] - height[i]));

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

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>
#include <vector>
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;
}

const long long INF = 1LL << 60;

int N;
long long h[10010];
long long dp[10010];

long long rec(int i) {
  if (dp[i] < INF)
    return dp[i];
  if (i == 0)
    return 0;
  long long res = INF;
  chmin(res, rec(i - 1) + abs(h[i] - h[i - 1]));
  if (i > 1)
    chmin(res, rec(i - 2) + abs(h[i] - h[i - 2]));
  return dp[i] = res;
}

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

  // 初期化 (最小化問題なので INF に初期化)
  for (int i = 0; i < 100010; ++i)
    dp[i] = INF;

  // 答え
  cout << rec(N - 1) << endl;
}


Fixed submission:
#include <cstdlib>
#include <iostream>
#include <vector>
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;
}

const long long INF = 1LL << 60;

int N;
long long h[100010];
long long dp[100010];

long long rec(int i) {
  if (dp[i] < INF)
    return dp[i];
  if (i == 0)
    return 0;
  long long res = INF;
  chmin(res, rec(i - 1) + abs(h[i] - h[i - 1]));
  if (i > 1)
    chmin(res, rec(i - 2) + abs(h[i] - h[i - 2]));
  return dp[i] = res;
}

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

  // 初期化 (最小化問題なので INF に初期化)
  for (int i = 0; i < 100010; ++i)
    dp[i] = INF;

  // 答え
  cout << rec(N - 1) << endl;
}
Problem p03160 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long int f(int *h, int i) {
  if (i <= 0)
    return 0;
  if (i == 1)
    return abs(h[i] - h[0]);
  long long int ans1 = f(h, i - 2) + abs(h[i] - h[i - 2]);
  long long int ans2 = f(h, i - 1) + abs(h[i] - h[i - 1]);
  return min(ans1, ans2);
}
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  cout << f(a, n - 1);
  //	long long int dp[n];
  //	dp[0]=0;
  //	dp[1] = abs(a[1]-a[0]);
  //	for(int i=2;i<n;i++) dp[i] =
  //min(dp[i-1]+abs(a[i]-a[i-1]),dp[i-2]+a[i]-a[i-2]); 	cout<<dp[n-1];
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long int f(int *h, int i) {
  if (i <= 0)
    return 0;
  if (i == 1)
    return abs(h[i] - h[0]);
  long long int ans1 = f(h, i - 2) + abs(h[i] - h[i - 2]);
  long long int ans2 = f(h, i - 1) + abs(h[i] - h[i - 1]);
  return min(ans1, ans2);
}
int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  // cout<<f(a,n-1);
  long long int dp[n];
  dp[0] = 0;
  dp[1] = abs(a[1] - a[0]);
  for (int 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];
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j;
  cin >> n;

  vector<int> dp(n), h(n);

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

  for (i = 0; i < n; ++i) {
    if (i != 0) {
      dp[i + 1] = min(dp[i] + abs(h[i] - h[i + 1]),
                      dp[i - 1] + abs(h[i + 1] - h[i - 1]));
    } else {
      dp[i + 1] = dp[i] + abs(h[i] - h[i + 1]);
    }
  }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j;
  cin >> n;

  vector<int> dp(n), h(n);

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

  for (i = 0; i < n - 1; ++i) {
    if (i != 0) {
      dp[i + 1] = min(dp[i] + abs(h[i] - h[i + 1]),
                      dp[i - 1] + abs(h[i + 1] - h[i - 1]));
    } else {
      dp[i + 1] = dp[i] + abs(h[i] - h[i + 1]);
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, temp;
  cin >> n;
  vector<int> h(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> temp;
    h[i] = temp;
  }
  vector<int> dp(n + 1, INT_MAX);
  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]));
  }
  return dp[n - 1];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, temp;
  cin >> n;
  vector<int> h(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> temp;
    h[i] = temp;
  }
  vector<int> dp(n + 1, INT_MAX);
  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];
}
Problem p03160 (C++)

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

int n, a[100001], f[100001];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("FROG1.inp", "r", stdin);
  freopen("FROG1.out", "w", stdout);
#endif // ONLINE_JUDGE
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  f[1] = 0;
  f[2] = abs(a[2] - a[1]);
  for (int i = 3; i <= n; i++) {
    f[i] =
        min(f[i - 1] + abs(a[i] - a[i - 1]), f[i - 2] + abs(a[i] - a[i - 2]));
  }
  cout << f[n];
  return 0;
}


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

int n, a[100001], f[100001];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  f[1] = 0;
  f[2] = abs(a[2] - a[1]);
  for (int i = 3; i <= n; i++) {
    f[i] =
        min(f[i - 1] + abs(a[i] - a[i - 1]), f[i - 2] + abs(a[i] - a[i - 2]));
  }
  cout << f[n];
  return 0;
}
Problem p03160 (C++)

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

#define FOR(i, n, m) for (int i = (n); i < (m); i++)
#define REP(i, n) FOR(i, 0, n)
#define nax 10009

typedef vector<int> vi;

int main() {
  int n, dp[nax];
  cin >> n;
  vi v(n);
  REP(i, n) cin >> v[i];

  memset(dp, 100000000, sizeof dp);
  dp[0] = 0;
  dp[1] = abs(v[1] - v[0]);

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

  cout << dp[n - 1];

  return 0;
}

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

#define FOR(i, n, m) for (int i = (n); i < (m); i++)
#define REP(i, n) FOR(i, 0, n)
#define nax 100009

typedef vector<int> vi;

int main() {
  int n, dp[nax];
  cin >> n;
  vi v(n);
  REP(i, n) cin >> v[i];

  memset(dp, 100000000, sizeof dp);
  dp[0] = 0;
  dp[1] = abs(v[1] - v[0]);

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

  cout << dp[n - 1];

  return 0;
}
Problem p03160 (C++)

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

#define pb push_back
#define fst first
#define snd second
#define fill(a, c) memset(&a, c, sizeof(a))
#define fore(i, x, y) for (ll i = x; i < y; i++)
#define FIN                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

typedef long long ll;
using namespace std;

int main() {
  FIN;
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
#endif
  int n;
  cin >> n;
  int ar[n];
  fore(i, 0, n) cin >> ar[i];
  int weight[n];
  fore(i, 0, n) weight[i] = -1;
  weight[0] = 0;
  weight[1] = abs(ar[1] - ar[0]);
  fore(i, 2, n) {
    weight[i] = min(abs(ar[i] - ar[i - 1]) + weight[i - 1],
                    abs(ar[i] - ar[i - 2]) + weight[i - 2]);
  }
  cout << weight[n - 1] << "\n";
  return 0;
}


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

#define pb push_back
#define fst first
#define snd second
#define fill(a, c) memset(&a, c, sizeof(a))
#define fore(i, x, y) for (ll i = x; i < y; i++)
#define FIN                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)

typedef long long ll;
using namespace std;

int main() {
  FIN;
  int n;
  cin >> n;
  int ar[n];
  fore(i, 0, n) cin >> ar[i];
  int weight[n];
  fore(i, 0, n) weight[i] = -1;
  weight[0] = 0;
  weight[1] = abs(ar[1] - ar[0]);
  fore(i, 2, n) {
    weight[i] = min(abs(ar[i] - ar[i - 1]) + weight[i - 1],
                    abs(ar[i] - ar[i - 2]) + weight[i - 2]);
  }
  cout << weight[n - 1] << "\n";
  return 0;
}
Problem p03160 (C++)

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

#define END '\n'
#define int long long
#define pb push_back
#define pii pair<int, int>
#define loop(i, a, b) for (int i = (a); i < (b); i++)
#define loopb(i, b, a) for (int i = (b); i > (a); --i)
void fastscan(int &x) {
  bool neg = false;
  register int c;
  x = 0;
  c = getchar();
  if (c == '-') {
    neg = true;
    c = getchar();
  }
  for (; (c > 47 && c < 58); c = getchar())
    x = (x << 1) + (x << 3) + c - 48;
  if (neg)
    x *= -1;
}
string alpha = "abcdefghijklmnopqrstuvwxyz";
const int mod = 1e9 + 7;
const int inf = 2e18 + 5;
const int nax = 100010;
const int mod1 = 998244353;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  int n;
  cin >> n;
  vector<int> v;
  loop(i, 0, n) {
    int x;
    cin >> x;
    v.pb(x);
  }
  int dp[n];
  dp[n - 2] = abs(v[n - 2] - v[n - 1]);
  dp[n - 3] = abs(v[n - 3] - v[n - 1]);
  loopb(i, n - 3, 0) {
    dp[i - 1] =
        min(dp[i] + abs(v[i] - v[i - 1]), dp[i + 1] + abs(v[i + 1] - v[i - 1]));
  }
  cout << dp[0];
  return 0;
}

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

#define END '\n'
#define int long long
#define pb push_back
#define pii pair<int, int>
#define loop(i, a, b) for (int i = (a); i < (b); i++)
#define loopb(i, b, a) for (int i = (b); i > (a); --i)
void fastscan(int &x) {
  bool neg = false;
  register int c;
  x = 0;
  c = getchar();
  if (c == '-') {
    neg = true;
    c = getchar();
  }
  for (; (c > 47 && c < 58); c = getchar())
    x = (x << 1) + (x << 3) + c - 48;
  if (neg)
    x *= -1;
}
string alpha = "abcdefghijklmnopqrstuvwxyz";
const int mod = 1e9 + 7;
const int inf = 2e18 + 5;
const int nax = 100010;
const int mod1 = 998244353;

signed main() {
  int n;
  cin >> n;
  vector<int> v;
  loop(i, 0, n) {
    int x;
    cin >> x;
    v.pb(x);
  }
  int dp[n];
  dp[n - 2] = abs(v[n - 2] - v[n - 1]);
  dp[n - 3] = abs(v[n - 3] - v[n - 1]);
  loopb(i, n - 3, 0) {
    dp[i - 1] =
        min(dp[i] + abs(v[i] - v[i - 1]), dp[i + 1] + abs(v[i + 1] - v[i - 1]));
  }
  cout << dp[0];
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
// Author: nimishkhurana
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (i = a; i < b; i++)
#define rrep(i, b, a) for (i = b; i >= a; i--)
#define ld long double
#define ll long long
#define umapi unordered_map<int, int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define vll vector<pll>
#define pb push_back
#define mk make_pair
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define INF 1000000000
template <typename T>
ostream &operator<<(ostream &stream, const pair<T, T> &p) {
  return stream << "(" << p.first << "," << p.second << ") ";
}

int n;
const int mxn = 1e5;
int h[mxn];
int dp[mxn];

int solve(int i) {
  if (i == n - 1)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  if (i + 1 == n - 1)
    return dp[i] = abs(h[i + 1] - h[i]);

  return dp[i] = min(abs(h[i + 1] - h[i]) + solve(i + 1),
                     abs(h[i + 2] - h[i]) + solve(i + 2));
}

int main() {
  fastIO
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  memset(dp, -1, sizeof(dp));

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];

  cout << solve(0);

  return 0;
}

Fixed submission:
// Author: nimishkhurana
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (i = a; i < b; i++)
#define rrep(i, b, a) for (i = b; i >= a; i--)
#define ld long double
#define ll long long
#define umapi unordered_map<int, int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define vll vector<pll>
#define pb push_back
#define mk make_pair
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define INF 1000000000
template <typename T>
ostream &operator<<(ostream &stream, const pair<T, T> &p) {
  return stream << "(" << p.first << "," << p.second << ") ";
}

int n;
const int mxn = 1e5;
int h[mxn];
int dp[mxn];

int solve(int i) {
  if (i == n - 1)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  if (i + 1 == n - 1)
    return dp[i] = abs(h[i + 1] - h[i]);

  return dp[i] = min(abs(h[i + 1] - h[i]) + solve(i + 1),
                     abs(h[i + 2] - h[i]) + solve(i + 2));
}

int main() {
  fastIO
      // #ifndef ONLINE_JUDGE
      //     freopen("input.txt", "r", stdin);
      //     freopen("output.txt", "w", stdout);
      // #endif
      memset(dp, -1, sizeof(dp));

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> h[i];

  cout << solve(0);

  return 0;
}
Problem p03160 (C++)

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

int main() {

  int N;
  cin >> N;

  vector<int> h(N);

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

  int answer = 0;

  vector<int> dp(N + 1);

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

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

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

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

int main() {

  int N;
  cin >> N;

  vector<int> h(N + 1);

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

  int answer = 0;

  vector<int> dp(N + 1);

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 1000000000
typedef long long ll;
typedef pair<int, int> P;

int dp[10000] = {}, h[100001] = {};

int main() {
  int n, i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> h[i];
    if (i == 0)
      dp[i] = 0;
    else if (i == 1)
      dp[i] = abs(h[i - 1] - h[i]);
    else
      dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]),
                  dp[i - 2] + abs(h[i - 2] - h[i]));
  }
  cout << dp[n - 1] << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 1000000000
typedef long long ll;
typedef pair<int, int> P;

int dp[100001] = {}, h[100001] = {};

int main() {
  int n, i;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> h[i];
    if (i == 0)
      dp[i] = 0;
    else if (i == 1)
      dp[i] = abs(h[i - 1] - h[i]);
    else
      dp[i] = min(dp[i - 1] + abs(h[i] - h[i - 1]),
                  dp[i - 2] + abs(h[i - 2] - h[i]));
  }
  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, i;
  cin >> n;
  int h[10000];
  int dp[10000];

  for (i = 0; i < n; i++)
    cin >> h[i];
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);
  for (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] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i;
  cin >> n;
  vector<int> h;
  h.resize(n);
  vector<int> dp(n);

  for (i = 0; i < n; i++)
    cin >> h[i];
  dp[0] = 0;
  dp[1] = abs(h[0] - h[1]);
  for (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] << endl;
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mk make_pair
#define ft first
#define sc second
#define ppb pop_back
#define sz size
#define forn(i, a, n) for (i = a; i < n; i++)
#define M 1000000007
#define inf 2e18

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);
  cout.tie(NULL);

  ll t;
  cin >> t;
  ll i, arr[t];
  for (i = 0; i < t; i++)
    cin >> arr[i];
  ll dp[t];
  memset(dp, 0, sizeof dp);
  dp[1] = abs(arr[1] - arr[0]);
  for (i = 2; i < t; i++)
    dp[i] = min(dp[i - 1] + abs(arr[i] - arr[i - 1]),
                dp[i - 2] + abs(arr[i] - arr[i - 2]));

  cout << dp[t - 1];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mk make_pair
#define ft first
#define sc second
#define ppb pop_back
#define sz size
#define forn(i, a, n) for (i = a; i < n; i++)
#define M 1000000007
#define inf 2e18

int main() {

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

  ll t;
  cin >> t;
  ll i, arr[t];
  for (i = 0; i < t; i++)
    cin >> arr[i];
  ll dp[t];
  memset(dp, 0, sizeof dp);
  dp[1] = abs(arr[1] - arr[0]);
  for (i = 2; i < t; i++)
    dp[i] = min(dp[i - 1] + abs(arr[i] - arr[i - 1]),
                dp[i - 2] + abs(arr[i] - arr[i - 2]));

  cout << dp[t - 1];
  return 0;
}
Problem p03160 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
const long long INF = 1LL << 60;
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> h(N);
  for (int i = 0; i < N; i++) {
    cin >> h.at(i);
  }
  long long dp[10010];
  for (int i = 0; i < 10010; i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  dp[1] = abs(h.at(1) - h.at(0));
  for (int i = 2; i < N; i++) {
    long long d1 = dp[i - 1] + abs(h.at(i) - h.at(i - 1));
    long long d2 = dp[i - 2] + abs(h.at(i) - h.at(i - 2));
    dp[i] = min(dp[i], d1);
    dp[i] = min(dp[i], d2);
  }
  cout << dp[N - 1] << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
const long long INF = 1LL << 60;
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> h(N);
  for (int i = 0; i < N; i++) {
    cin >> h.at(i);
  }
  long long dp[100100];
  for (int i = 0; i < 100100; i++) {
    dp[i] = INF;
  }
  dp[0] = 0;
  dp[1] = abs(h.at(1) - h.at(0));
  for (int i = 2; i < N; i++) {
    long long d1 = dp[i - 1] + abs(h.at(i) - h.at(i - 1));
    long long d2 = dp[i - 2] + abs(h.at(i) - h.at(i - 2));
    dp[i] = min(dp[i], d1);
    dp[i] = min(dp[i], d2);
  }
  cout << dp[N - 1] << endl;
}