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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
#define chmin(x, y) x = min(x, y)

const int INF = 1001001001;
int dp[100][2];

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  s += '0';
  int n = s.size();
  rep(i, n + 1) rep(j, 2) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, n) rep(j, 2) {
    int x = s[i] - '0';
    x += j;
    if (x < 10)
      chmin(dp[i + 1][0], dp[i][j] + x);
    if (x > 0)
      chmin(dp[i + 1][1], dp[i][j] + (10 - x));
  }
  int ans = dp[n][0];
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
#define chmin(x, y) x = min(x, y)

const int INF = 1001001001;
int dp[1000005][2];

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  s += '0';
  int n = s.size();
  rep(i, n + 1) rep(j, 2) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, n) rep(j, 2) {
    int x = s[i] - '0';
    x += j;
    if (x < 10)
      chmin(dp[i + 1][0], dp[i][j] + x);
    if (x > 0)
      chmin(dp[i + 1][1], dp[i][j] + (10 - x));
  }
  int ans = dp[n][0];
  cout << ans << endl;
  return 0;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define MAX 1000001
#define ll long long
const ll INF = 1e18 + 7;

string s;
ll num[MAX], sum[MAX], dp[MAX][2];

int main() {
  cin >> s;
  s = '0' + s;
  int len = s.length();
  for (int i = 0; i < len; ++i) {
    num[i] = s[i] - '0';
    sum[i + 1] = sum[i] + num[i];
  }

  for (int i = 0; i <= len; ++i)
    for (int j = 0; j < 2; ++j)
      dp[i][j] = INF;

  dp[0][0] = 0;
  for (int i = 0; i < len; ++i) {
    dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + num[i]);
    dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + num[i] + 1);
    dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] - num[i] + 10);
    dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] - num[i] + 9);
  }
  cout << dp[len][0] << '\n';

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
#define MAX 1000002
#define ll long long
const ll INF = 1e18 + 7;

string s;
ll num[MAX], sum[MAX], dp[MAX][2];

int main() {
  cin >> s;
  s = '0' + s;
  int len = s.length();
  for (int i = 0; i < len; ++i) {
    num[i] = s[i] - '0';
    sum[i + 1] = sum[i] + num[i];
  }

  for (int i = 0; i <= len; ++i)
    for (int j = 0; j < 2; ++j)
      dp[i][j] = INF;

  dp[0][0] = 0;
  for (int i = 0; i < len; ++i) {
    dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + num[i]);
    dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + num[i] + 1);
    dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] - num[i] + 10);
    dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] - num[i] + 9);
  }
  cout << dp[len][0] << '\n';

  return 0;
}
Problem p02775 (C++)

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

const int INF = 0x3f3f3f3f;

int dp[200000][2];

int main() {
  string s;
  cin >> s;
  memset(dp, 0x3f, sizeof(dp));
  s = "0" + s;
  dp[s.size()][0] = 0;
  for (int i = s.size(); i > 0; i--)
    rep(j, 2) {
      if (dp[i][j] == INF)
        continue;
      for (int k = 0; k <= 9; k++) {
        int x = k - (int)(s[i - 1] - '0') - j;
        bool b = 0;
        if (x < 0)
          b = 1, x += 10;
        dp[i - 1][b] = min(dp[i - 1][b], dp[i][j] + k + x);
      }
    }
  cout << dp[0][0] << endl;
}


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

const int INF = 0x3f3f3f3f;

int dp[2000000][2];

int main() {
  string s;
  cin >> s;
  memset(dp, 0x3f, sizeof(dp));
  s = "0" + s;
  dp[s.size()][0] = 0;
  for (int i = s.size(); i > 0; i--)
    rep(j, 2) {
      if (dp[i][j] == INF)
        continue;
      for (int k = 0; k <= 9; k++) {
        int x = k - (int)(s[i - 1] - '0') - j;
        bool b = 0;
        if (x < 0)
          b = 1, x += 10;
        dp[i - 1][b] = min(dp[i - 1][b], dp[i][j] + k + x);
      }
    }
  cout << dp[0][0] << endl;
}
Problem p02775 (Python)

Buggy submission (status: Time Limit Exceeded):
s = input()
keta = len(s)
n = int(s)
infi = 10**10
dp = [[infi] * (2) for _ in range(keta + 1)]
dp[0][0] = 0
s = s[::-1]
for i in range(keta):
    for j in range(2):
        num = int(s[i])
        if j == 1:
            num += 1
        ni = i + 1
        if num == 10:
            nj = 1
            num = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num < 5:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num > 5:
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
        else:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
print(min(dp[keta][0], dp[keta][1] + 1))


Fixed submission:
s = input()
keta = len(s)
infi = 10**10
dp = [[infi] * (2) for _ in range(keta + 1)]
dp[0][0] = 0
s = s[::-1]
for i in range(keta):
    for j in range(2):
        num = int(s[i])
        if j == 1:
            num += 1
        ni = i + 1
        if num == 10:
            nj = 1
            num = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num < 5:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
        elif num > 5:
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
        else:
            nj = 0
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + num)
            nj = 1
            dp[ni][nj] = min(dp[ni][nj], dp[i][j] + 10 - num)
print(min(dp[keta][0], dp[keta][1] + 1))
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v)
    out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 200010

int dp[SIZE][2];

int main() {
  char S[SIZE];

  scanf("%s", S);
  int N = strlen(S);

  for (int i = 0; i <= N; i++)
    dp[i][0] = dp[i][1] = INF;

  dp[0][0] = 0;
  dp[0][1] = 1;

  for (int i = 0; i < N; i++) {
    int p = S[i] - '0';

    dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + p);
    dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + 10 - p);

    dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + p + 1);
    dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + 10 - p - 1);
  }

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

  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v)
    out << item << ", ";
  out << "\b\b}";
  return out;
}
#endif

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 1000010

int dp[SIZE][2];

int main() {
  char S[SIZE];

  scanf("%s", S);
  int N = strlen(S);

  for (int i = 0; i <= N; i++)
    dp[i][0] = dp[i][1] = INF;

  dp[0][0] = 0;
  dp[0][1] = 1;

  for (int i = 0; i < N; i++) {
    int p = S[i] - '0';

    dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + p);
    dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + 10 - p);

    dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + p + 1);
    dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + 10 - p - 1);
  }

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

  return 0;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
// agrawal117
// chahatagrawal117
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long int ll;
#define MAX 1005
ll dp[1000009][2];
string s;
ll solve(int i, int carry) {
  if (i < 0)
    return carry;
  if (dp[i][carry] != -1)
    return dp[i][carry];
  if (carry == 1) {
    int n = s[i] - '0';
    ll y;
    if (n == 9) {
      n = 0;
      y = solve(i - 1, 1);
    } else {
      n++;
      y = min(n + solve(i - 1, 0), 10 - n + solve(i - 1, 1));
    }
    dp[i][carry] = y;
    return y;
  } else {
    int n = s[i] - '0';
    ll y = min(n + solve(i - 1, 0), 10 - n + solve(i - 1, 1));
    dp[i][carry] = y;
    return y;
  }
}
int main() {
  cin >> s;
  for (int i = 0; i < s.size() + 1; i++) {
    dp[i][0] = -1;
    dp[i][1] = -1;
  }
  ll ans = solve(s.size() - 1, 0);
  cout << ans << endl;
}

Fixed submission:
// agrawal117
// chahatagrawal117
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long int ll;
#define MAX 1005
ll dp[1000009][4];
string s;
ll solve(int i, int carry) {
  if (i < 0)
    return carry;
  if (dp[i][carry] != -1)
    return dp[i][carry];
  if (carry == 1) {
    int n = s[i] - '0';
    ll y;
    if (n == 9) {
      n = 0;
      y = solve(i - 1, 1);
    } else {
      n++;
      y = min(n + solve(i - 1, 0), 10 - n + solve(i - 1, 1));
    }
    dp[i][carry] = y;
    return y;
  } else {
    int n = s[i] - '0';
    ll y = min(n + solve(i - 1, 0), 10 - n + solve(i - 1, 1));
    dp[i][carry] = y;
    return y;
  }
}
int main() {
  cin >> s;
  for (int i = 0; i < s.size() + 1; i++) {
    dp[i][0] = -1;
    dp[i][1] = -1;
  }
  ll ans = solve(s.size() - 1, 0);
  cout << ans << endl;
}
Problem p02775 (C++)

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

using namespace std;
#define _rep(n, a, b) for (ll n = (a); n <= (b); ++n)
#define _rev(n, a, b) for (ll n = (a); n >= (b); --n)
#define _for(n, a, b) for (ll n = (a); n < (b); ++n)
#define _rof(n, a, b) for (ll n = (a); n > (b); --n)
#define oo 0x3f3f3f3f3f3f
#define ll long long
#define db double
#define eps 1e-6
#define bin(x) cout << bitset<10>(x) << endl;
#define what_is(x) cerr << #x << " is " << x << endl
#define met(a, b) memset(a, b, sizeof(a))
#define mp(a, b) make_pair(a, b)
#define all(x) x.begin(), x.end()
#define pii pair<ll, ll>
#define pdd pair<db, db>
#define pi acos(-1.0)
const ll maxn = 2e5 + 10;
ll dp[maxn][2];
signed main() {
  string s;
  cin >> s;
  dp[0][1] = 1;
  _for(i, 0, s.size()) {
    int x = s[i] - '0';
    dp[i + 1][0] = min(dp[i][0] + x, dp[i][1] + 10 - x);
    dp[i + 1][1] = min(dp[i][0] + x + 1, dp[i][1] + 10 - x - 1);
  }
  cout << min(dp[s.size()][0], dp[s.size()][1] + 1) << endl;
}

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

using namespace std;
#define _rep(n, a, b) for (ll n = (a); n <= (b); ++n)
#define _rev(n, a, b) for (ll n = (a); n >= (b); --n)
#define _for(n, a, b) for (ll n = (a); n < (b); ++n)
#define _rof(n, a, b) for (ll n = (a); n > (b); --n)
#define oo 0x3f3f3f3f3f3f
#define ll long long
#define db double
#define eps 1e-6
#define bin(x) cout << bitset<10>(x) << endl;
#define what_is(x) cerr << #x << " is " << x << endl
#define met(a, b) memset(a, b, sizeof(a))
#define mp(a, b) make_pair(a, b)
#define all(x) x.begin(), x.end()
#define pii pair<ll, ll>
#define pdd pair<db, db>
#define pi acos(-1.0)
const ll maxn = 1e6 + 10;
ll dp[maxn][2];
signed main() {
  string s;
  cin >> s;
  dp[0][1] = 1;
  _for(i, 0, s.size()) {
    int x = s[i] - '0';
    dp[i + 1][0] = min(dp[i][0] + x, dp[i][1] + 10 - x);
    dp[i + 1][1] = min(dp[i][0] + x + 1, dp[i][1] + 10 - x - 1);
  }
  cout << min(dp[s.size()][0], dp[s.size()][1] + 1) << endl;
}
Problem p02775 (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 rep2(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define ALL(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;

int main() {
  string N;
  cin >> N;
  ll n = N.size();
  ll inc = 0;
  vector<ll> f(n), g(n);
  f[0] = 0;
  g[0] = 1;
  rep(i, n) {
    int a = N[i] - '0';
    f[i + 1] = min(f[i] + a, g[i] + 10 - a);
    g[i + 1] = min(f[i] + a + 1, g[i] + 9 - a);
  }
  cout << f[n] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep2(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define ALL(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;

int main() {
  string N;
  cin >> N;
  ll n = N.size();
  ll inc = 0;
  vector<ll> f(n + 1), g(n + 1);
  f[0] = 0;
  g[0] = 1;
  rep(i, n) {
    int a = N[i] - '0';
    f[i + 1] = min(f[i] + a, g[i] + 10 - a);
    g[i + 1] = min(f[i] + a + 1, g[i] + 9 - a);
  }
  cout << f[n] << endl;
}
Problem p02775 (C++)

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

#include <map>
#include <string>

#include <list>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdlib>

using namespace std;

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

  typedef pair<int, int> answer;
  vector<answer> que;
  que.push_back(answer(0, 0));

  vector<answer> nextQue;

  for (int index = n.size() - 1; index >= 0; --index) {
    int d = n[index] - '0';

    while (!que.empty()) {
      answer ans = que.back();
      que.pop_back();

      int c = d + ans.second;
      int nextCarry = 0;

      if (c >= 10) {
        c -= 10;
        ++nextCarry;
      }

      if (c <= 5)
        nextQue.push_back(answer(ans.first + c, nextCarry));

      if (c >= 5)
        nextQue.push_back(answer(ans.first + 10 - c, nextCarry + 1));
    }

    que.swap(nextQue);
    nextQue.clear();

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

    int min = que[0].first + que[1].second;

    for (int i = 1; i < que.size(); ++i) {
      if (min + 2 <= que[i].first + que[i].second)
        que.erase(que.begin() + i);
    }
  }

  int ans = 1 << 30;

  for (auto a : que) {
    ans = min(a.first + a.second, ans);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <iostream>

#include <map>
#include <string>

#include <list>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdlib>

using namespace std;

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

  typedef pair<int, int> answer;
  vector<answer> que;
  que.push_back(answer(0, 0));

  vector<answer> nextQue;

  for (int index = n.size() - 1; index >= 0; --index) {
    int d = n[index] - '0';

    while (!que.empty()) {
      answer ans = que.back();
      que.pop_back();

      int c = d + ans.second;
      int nextCarry = 0;

      if (c >= 10) {
        c -= 10;
        ++nextCarry;
      }

      if (c <= 5)
        nextQue.push_back(answer(ans.first + c, nextCarry));

      if (c >= 5)
        nextQue.push_back(answer(ans.first + 10 - c, nextCarry + 1));
    }

    que.swap(nextQue);
    nextQue.clear();

    sort(que.begin(), que.end());
    que.erase(unique(que.begin(), que.end()), que.end());

    int min = que[0].first + que[1].second;

    for (int i = 1; i < que.size(); ++i) {
      if (min + 2 <= que[i].first + que[i].second)
        que.erase(que.begin() + i);
    }
  }

  int ans = 1 << 30;

  for (auto a : que) {
    ans = min(a.first + a.second, ans);
  }

  cout << ans << endl;

  return 0;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <memory.h>

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define MOD 998244353
#define INIT -1

int main() {
  string n;
  cin >> n;
  // result[i][0] : i - 1桁めまで払ってる　繰り下がりはない
  // result[i][1] : i - 1桁目まで払ってる　繰り下がりあり
  int result[1000001][2] = {};
  result[0][0] = 0;
  result[0][1] = 1;
  for (int i = 1; i <= n.length(); i++) {
    result[i][0] = min(result[i - 1][0] + (n[i - 1] - '0'),
                       result[i - 1][1] + (10 - (n[i - 1] - '0')));
    result[i][1] = min(result[i - 1][1] + (10 - (n[i - 1] - '0') - 1),
                       result[i - 1][0] + (n[i - 1] - '0' + 1));
  }
  cout << result[n.length()][0] << endl;
}

Fixed submission:
#include <memory.h>

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define MOD 998244353
#define INIT -1

int main() {
  string n;
  cin >> n;
  // result[i][0] : i - 1桁めまで払ってる　繰り下がりはない
  // result[i][1] : i - 1桁目まで払ってる　繰り下がりあり
  int result[1000010][2] = {};
  result[0][0] = 0;
  result[0][1] = 1;
  for (int i = 1; i <= n.length(); i++) {
    result[i][0] = min(result[i - 1][0] + (n[i - 1] - '0'),
                       result[i - 1][1] + (10 - (n[i - 1] - '0')));
    result[i][1] = min(result[i - 1][1] + (10 - (n[i - 1] - '0') - 1),
                       result[i - 1][0] + (n[i - 1] - '0' + 1));
  }
  cout << result[n.length()][0] << endl;
}
Problem p02775 (C++)

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

typedef long long ll;

string s;

const int N = 1e5;
int dp[2][N + 5];

int calc(int st, int pos) {
  if (pos == -1)
    return st;
  int &res = dp[st][pos];
  if (res != -1)
    return res;
  res = 1e9;
  int val = s[pos] - '0';
  if (st == 0) {
    res = min(calc(0, pos - 1) + val, calc(1, pos - 1) + 10 - val);
  } else {
    int nval = val + 1;
    if (nval != 10)
      res = min(calc(0, pos - 1) + nval, calc(1, pos - 1) + 10 - nval);
    else
      res = calc(1, pos - 1) + 10 - nval;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> s;
  memset(dp, -1, sizeof dp);
  cout << calc(0, s.size() - 1) << endl;
  return 0;
}


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

typedef long long ll;

string s;

const int N = 1e6;
int dp[2][N + 5];

int calc(int st, int pos) {
  if (pos == -1)
    return st;
  int &res = dp[st][pos];
  if (res != -1)
    return res;
  res = 1e9;
  int val = s[pos] - '0';
  if (st == 0) {
    res = min(calc(0, pos - 1) + val, calc(1, pos - 1) + 10 - val);
  } else {
    int nval = val + 1;
    if (nval != 10)
      res = min(calc(0, pos - 1) + nval, calc(1, pos - 1) + 10 - nval);
    else
      res = calc(1, pos - 1) + 10 - nval;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> s;
  memset(dp, -1, sizeof dp);
  cout << calc(0, s.size() - 1) << endl;
  return 0;
}
Problem p02775 (C++)

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

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

int main() {
  string str;
  cin >> str;

  while (1) {
    if (str.at(str.size() - 1) == '0') {
      str.pop_back();
    } else {
      break;
    }
  }
  if (str.size() == 1) {
    int a = str.at(0) - '0';
    cout << min(a, 11 - a) << endl;
  }
  vector<int> n(str.size());
  for (size_t i = 0; i < n.size(); i++) {
    n.at(i) = str.at(i) - '0';
  }
  vector<int> m(str.size());
  for (size_t i = 0; i < m.size() - 1; i++) {
    m.at(i) = 9 - n.at(i);
  }
  m.at(m.size() - 1) = 10 - n.at(m.size() - 1);
  int64_t ans = 0;
  vector<int> s1(str.size());
  vector<int> s2(str.size());
  s1.at(n.size() - 1) = n.at(n.size() - 1);
  s2.at(m.size() - 1) = m.at(m.size() - 1);
  for (size_t i = n.size() - 2; 1; i--) {
    s1.at(i) = min(s1.at(i + 1), s2.at(i + 1) + 1) + n.at(i);
    s2.at(i) = min(s2.at(i + 1), s1.at(i + 1) + 1) + m.at(i);
    if (i == 0) {
      break;
    }
  }
  ans = min(s1.at(0), s2.at(0) + 1);
  cout << ans << endl;
}

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

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

int main() {
  string str;
  cin >> str;

  while (1) {
    if (str.at(str.size() - 1) == '0') {
      str.pop_back();
    } else {
      break;
    }
  }
  if (str.size() == 1) {
    int a = str.at(0) - '0';
    cout << min(a, 11 - a) << endl;
    return 0;
  }
  vector<int> n(str.size());
  for (size_t i = 0; i < n.size(); i++) {
    n.at(i) = str.at(i) - '0';
  }
  vector<int> m(str.size());
  for (size_t i = 0; i < m.size() - 1; i++) {
    m.at(i) = 9 - n.at(i);
  }
  m.at(m.size() - 1) = 10 - n.at(m.size() - 1);
  int64_t ans = 0;
  vector<int> s1(str.size());
  vector<int> s2(str.size());
  s1.at(n.size() - 1) = n.at(n.size() - 1);
  s2.at(m.size() - 1) = m.at(m.size() - 1);
  for (size_t i = n.size() - 2; 1; i--) {
    s1.at(i) = min(s1.at(i + 1), s2.at(i + 1) + 1) + n.at(i);
    s2.at(i) = min(s2.at(i + 1), s1.at(i + 1) + 1) + m.at(i);
    if (i == 0) {
      break;
    }
  }
  ans = min(s1.at(0), s2.at(0) + 1);
  cout << ans << endl;
}
Problem p02775 (C++)

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

#define rep(i, N) for (int i = 0; i < N; i++)
#define rep2(i, N) for (int i = 1; i <= N; i++)
using namespace std;
long long INF = 1e18;
long long mod = 1e9 + 7;
long long dp[100010][2];
// status unsolved
long long solve(string s) {

  fill((long long *)dp, (long long *)dp + sizeof(dp) / sizeof(long long), INF);

  int L = s.size();
  // dp[i][k]:=i桁目において、状態kになるための最小枚数
  // i:=桁数　
  // k=0 繰り上下がりなし　
  // k=1 繰り下がりあり
  dp[0][0] = 0;

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

    // 繰り下がり
    for (int j = 0; j < 2; j++) {
      int D = s[i] - '0';
      D += j;
      for (int d = 0; d < 10; d++) {
        int b = d - D;
        int nj = 0;
        if (b < 0) {
          nj = 1;
          b += 10;
        }
        // 次の桁の繰り下がり状態nj
        //=min(現在の値,今の桁の状態j+お客が払うコイン+お店が払うコイン)
        dp[i + 1][nj] = min(dp[i + 1][nj], dp[i][j] + b + d);
      }
    }
  }

  return dp[L][0];
}
int main() {

  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  s += '0';
  cout << solve(s) << endl;
  ;

  return 0;
}
/*


*/


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

#define rep(i, N) for (int i = 0; i < N; i++)
#define rep2(i, N) for (int i = 1; i <= N; i++)
using namespace std;
long long INF = 1e18;
long long mod = 1e9 + 7;
long long dp[1000010][2];
// status unsolved
long long solve(string s) {

  fill((long long *)dp, (long long *)dp + sizeof(dp) / sizeof(long long), INF);

  int L = s.size();
  // dp[i][k]:=i桁目において、状態kになるための最小枚数
  // i:=桁数　
  // k=0 繰り上下がりなし　
  // k=1 繰り下がりあり
  dp[0][0] = 0;

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

    // 繰り下がり
    for (int j = 0; j < 2; j++) {
      int D = s[i] - '0';
      D += j;
      for (int d = 0; d < 10; d++) {
        int b = d - D;
        int nj = 0;
        if (b < 0) {
          nj = 1;
          b += 10;
        }
        // 次の桁の繰り下がり状態nj
        //=min(現在の値,今の桁の状態j+お客が払うコイン+お店が払うコイン)
        dp[i + 1][nj] = min(dp[i + 1][nj], dp[i][j] + b + d);
      }
    }
  }

  return dp[L][0];
}
int main() {

  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  s += '0';
  cout << solve(s) << endl;
  ;

  return 0;
}
/*


*/
Problem p02775 (C++)

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

using namespace std;
#define ll long long
const int N = 2e5 + 5;
string s;
int dp[N][2];
int solve(int idx, int c) {
  if (idx < 0)
    return c;
  int &ret = dp[idx][c];
  if (~ret)
    return ret;
  int cur = s[idx] - '0' + c;
  ret = solve(idx - 1, 0) + cur;
  ret = min(ret, solve(idx - 1, 1) + 10 - cur);
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> s;
  memset(dp, -1, sizeof dp);
  cout << solve((int)s.size() - 1, 0) << endl;
  return 0;
}


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

using namespace std;
#define ll long long
const int N = 2e6 + 5;
string s;
int dp[N][2];
int solve(int idx, int c) {
  if (idx < 0)
    return c;
  int &ret = dp[idx][c];
  if (~ret)
    return ret;
  int cur = s[idx] - '0' + c;
  ret = solve(idx - 1, 0) + cur;
  ret = min(ret, solve(idx - 1, 1) + 10 - cur);
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> s;
  memset(dp, -1, sizeof dp);
  cout << solve((int)s.size() - 1, 0) << endl;
  return 0;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
// GYM problem J ArabellaCPC 2019

#include <bits/stdc++.h>
#include <set>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
using namespace std;
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define pb push_back
#define ff first
#define ss second
#define sz size()
#define mp make_pair
#define int long long int
#define double long double
#define bp __builtin_popcountll
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ROR(i, a, b) for (int i = a; i >= b; i--)
const int M = 1e9 + 7;
const int N = 1e5 + 5;
const int inf = 1e18;
string s;
int n;
int dp[N][2];
int solve(int idx, int vaddi) {
  if (idx == 0) {
    if (vaddi == 1)
      return 1;
    return 0;
  }
  int &ans = dp[idx][vaddi];
  if (ans != -1)
    return ans;
  ans = 0;
  char pp;
  if (vaddi == 1) {
    pp = s[idx] + 1;
    if (s[idx] + 1 > '9')
      ans = solve(idx - 1, 1);
    else
      ans = min((pp - '0') + solve(idx - 1, 0),
                (10 + '0' - pp) + solve(idx - 1, 1));

  } else
    ans = min((s[idx] - '0') + solve(idx - 1, 0),
              (10 + '0' - s[idx]) + solve(idx - 1, 1));
  return ans;
}
int32_t main() {
  FAST cin >> s;
  s = '0' + s;
  n = s.size();
  // cout<<s;
  memset(dp, -1, sizeof(dp));
  cout << (solve(n, 0) + 48);
}

Fixed submission:
// GYM problem J ArabellaCPC 2019

#include <bits/stdc++.h>
#include <set>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
using namespace std;
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define pb push_back
#define ff first
#define ss second
#define sz size()
#define mp make_pair
#define int long long int
#define double long double
#define bp __builtin_popcountll
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ROR(i, a, b) for (int i = a; i >= b; i--)
const int M = 1e9 + 7;
const int N = 1e6 + 5;
const int inf = 1e18;
string s;
int n;
int dp[N][2];
int solve(int idx, int vaddi) {
  if (idx == 0) {
    if (vaddi == 1)
      return 1;
    return 0;
  }
  int &ans = dp[idx][vaddi];
  if (ans != -1)
    return ans;
  ans = 0;
  char pp;
  if (vaddi == 1) {
    pp = s[idx] + 1;
    if (s[idx] + 1 > '9')
      ans = solve(idx - 1, 1);
    else
      ans = min((pp - '0') + solve(idx - 1, 0),
                (10 + '0' - pp) + solve(idx - 1, 1));

  } else
    ans = min((s[idx] - '0') + solve(idx - 1, 0),
              (10 + '0' - s[idx]) + solve(idx - 1, 1));
  return ans;
}
int32_t main() {
  FAST cin >> s;
  s = '0' + s;
  n = s.size();
  // cout<<s;
  memset(dp, -1, sizeof(dp));
  cout << (solve(n, 0) + 48);
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
using P = pair<int, int>;
const int INF = 1000100100;

/* 大文字を小文字に変換 */
char tolower(char c) { return (c + 0x20); }
/* 小文字を大文字に変換 */
char toupr(char c) { return (c - 0x20); }

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

int dp[100005][2];

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  s += '0'; // 桁が1つだけ上る可能性があるので
  int n = s.size();
  rep(i, n + 1) rep(j, 2) dp[i][j] = INF;
  dp[0][0] = 0;

  rep(i, n) rep(j, 2) {
    int x = s[i] - '0'; // 今の桁、nのi桁目
    x += j; // 繰り下がりを引く数をプラス1することで対応している

    rep(a, 10) {
      // ni：次の桁に行くだけなのでi+1
      // nj：繰り下がりが遷移先で起こる場合1、起こらない場合0、デフォルトは0
      int ni = i + 1, nj = 0; // 次の遷移先
      int b = a - x;
      if (b < 0) {
        nj = 1; // 繰り下がりを1にして、次の桁で繰り下がりが起こるようにする
        b += 10;
      }
      dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b);
    }
  }
  int ans = dp[n][0];
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
using P = pair<int, int>;
const int INF = 1000100100;

/* 大文字を小文字に変換 */
char tolower(char c) { return (c + 0x20); }
/* 小文字を大文字に変換 */
char toupr(char c) { return (c - 0x20); }

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"

// イテレータ要素のインデックス
distance(A.begin(), itr);
*/

int dp[1000005][2];

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  s += '0'; // 桁が1つだけ上る可能性があるので
  int n = s.size();
  rep(i, n + 1) rep(j, 2) dp[i][j] = INF;
  dp[0][0] = 0;

  rep(i, n) rep(j, 2) {
    int x = s[i] - '0'; // 今の桁、nのi桁目
    x += j; // 繰り下がりを引く数をプラス1することで対応している

    rep(a, 10) {
      // ni：次の桁に行くだけなのでi+1
      // nj：繰り下がりが遷移先で起こる場合1、起こらない場合0、デフォルトは0
      int ni = i + 1, nj = 0; // 次の遷移先
      int b = a - x;
      if (b < 0) {
        nj = 1; // 繰り下がりを1にして、次の桁で繰り下がりが起こるようにする
        b += 10;
      }
      dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b);
    }
  }
  int ans = dp[n][0];
  cout << ans << endl;
  return 0;
}
Problem p02775 (C++)

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

using namespace std;
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define pb push_back
#define ff first
#define ss second
#define sz size()
#define mp make_pair
#define int long long int
#define double long double
#define bp __builtin_popcountll
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ROR(i, a, b) for (int i = a; i >= b; i--)
const int M = 1e9 + 7;
const int N = 1e5 + 5;
const int inf = 1e18;
string s;
int n;
int dp[N][2];
int solve(int idx, int vaddi) {
  if (idx == 0) {
    if (vaddi == 1)
      return 1;
    return 0;
  }
  int &ans = dp[idx][vaddi];
  if (ans != -1)
    return ans;
  ans = 0;
  char pp;
  if (vaddi == 1) {
    pp = s[idx] + 1;
    if (s[idx] + 1 > '9')
      ans = solve(idx - 1, 1);
    else
      ans = min((pp - '0') + solve(idx - 1, 0),
                (10 + '0' - pp) + solve(idx - 1, 1));

  } else
    ans = min((s[idx] - '0') + solve(idx - 1, 0),
              (10 + '0' - s[idx]) + solve(idx - 1, 1));
  return ans;
}
int32_t main() {
  FAST cin >> s;
  s = '0' + s;
  n = s.size();
  // cout<<s;
  memset(dp, -1, sizeof(dp));
  cout << (solve(n, 0) + 48);
}

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

using namespace std;
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define pb push_back
#define ff first
#define ss second
#define sz size()
#define mp make_pair
#define int long long int
#define double long double
#define bp __builtin_popcountll
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ROR(i, a, b) for (int i = a; i >= b; i--)
const int M = 1e9 + 7;
const int N = 1e6 + 5;
const int inf = 1e18;
string s;
int n;
int dp[N][2];
int solve(int idx, int vaddi) {
  if (idx == 0) {
    if (vaddi == 1)
      return 1;
    return 0;
  }
  int &ans = dp[idx][vaddi];
  if (ans != -1)
    return ans;
  ans = 0;
  char pp;
  if (vaddi == 1) {
    pp = s[idx] + 1;
    if (s[idx] + 1 > '9')
      ans = solve(idx - 1, 1);
    else
      ans = min((pp - '0') + solve(idx - 1, 0),
                (10 + '0' - pp) + solve(idx - 1, 1));

  } else
    ans = min((s[idx] - '0') + solve(idx - 1, 0),
              (10 + '0' - s[idx]) + solve(idx - 1, 1));
  return ans;
}
int32_t main() {
  FAST cin >> s;
  s = '0' + s;
  n = s.size();
  // cout<<s;
  memset(dp, -1, sizeof(dp));
  cout << (solve(n, 0) + 48);
}
Problem p02775 (C++)

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

const int mxN = 1e5 + 5;
long long dp[mxN][2];

int main() {
  string s;
  cin >> s;
  int n = s.size();
  dp[0][1] = 1;
  for (int i = 0; i < n; ++i) {
    int cur = s[i] - '0';
    dp[i + 1][0] = min(dp[i][0] + cur, dp[i][1] + 10 - cur);
    dp[i + 1][1] = min(dp[i][0] + cur + 1, dp[i][1] + 10 - cur - 1);
  }
  cout << dp[n][0];
}


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

const int mxN = 1e6 + 5;
long long dp[mxN][2];

int main() {
  string s;
  cin >> s;
  int n = s.size();
  dp[0][1] = 1;
  for (int i = 0; i < n; ++i) {
    int cur = s[i] - '0';
    dp[i + 1][0] = min(dp[i][0] + cur, dp[i][1] + 10 - cur);
    dp[i + 1][1] = min(dp[i][0] + cur + 1, dp[i][1] + 10 - cur - 1);
  }
  cout << dp[n][0];
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
char c[200009];
int n, m, i, j, k, l, t, x, y, cost[200009], cost1[200009];
int main() {
  scanf("%s", &c);
  n = strlen(c);
  m = 0;
  cost[n - 1] = c[n - 1] - '0';
  cost1[n - 1] = 10 - cost[n - 1] + 1;
  for (i = n - 2; i >= 0; i--) {
    l = c[i] - '0';
    x = 10 - l + 1;
    cost1[i] = min(cost[i + 1] + x, cost1[i + 1] + x - 2);
    cost[i] = min(cost1[i + 1], cost[i + 1]) + l;
  }
  printf("%d\n", min(cost[0], cost1[0]));
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
char c[2000009];
int n, m, i, j, k, l, t, x, y, cost[2000009], cost1[2000009];
int main() {
  scanf("%s", &c);
  n = strlen(c);
  m = 0;
  cost[n - 1] = c[n - 1] - '0';
  cost1[n - 1] = 10 - cost[n - 1] + 1;
  for (i = n - 2; i >= 0; i--) {
    l = c[i] - '0';
    x = 10 - l + 1;
    cost1[i] = min(cost[i + 1] + x, cost1[i + 1] + x - 2);
    cost[i] = min(cost1[i + 1], cost[i + 1]) + l;
  }
  printf("%d\n", min(cost[0], cost1[0]));
}
Problem p02775 (C++)

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

string s;
const long long SZ = 1000000;
long long dp[SZ + 1][2];

int main() {
  cin >> s;
  long long ans = 0, N = s.size();
  dp[0][1] = 0x3fffffff;
  for (long long i = 1; i <= N; i++) {
    long long n = s[N - i] - '0';
    if (n < 9) {
      dp[i][0] = min(dp[i - 1][0] + n, dp[i - 1][1] + n + 1);
      dp[i][1] = min(dp[i - 1][0] + 10 - n, dp[i - 1][1] + 10 - (n + 1));
    } else {
      dp[i][0] = dp[i - 1][0] + n;
      dp[i][1] = min(dp[i - 1][0] + 10 - n, dp[i - 1][1] + 10 - (n + 1));
    }
  }
  cout << min(dp[N][0], dp[N][1] + 1);
}


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

string s;
const long long SZ = 1000009;
long long dp[SZ + 1][2];

int main() {
  cin >> s;
  long long ans = 0, N = s.size();
  dp[0][1] = 0x3fffffff;
  for (long long i = 1; i <= N; i++) {
    long long n = s[N - i] - '0';
    if (n < 9) {
      dp[i][0] = min(dp[i - 1][0] + n, dp[i - 1][1] + n + 1);
      dp[i][1] = min(dp[i - 1][0] + 10 - n, dp[i - 1][1] + 10 - (n + 1));
    } else {
      dp[i][0] = dp[i - 1][0] + n;
      dp[i][1] = min(dp[i - 1][0] + 10 - n, dp[i - 1][1] + 10 - (n + 1));
    }
  }
  cout << min(dp[N][0], dp[N][1] + 1);
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
using namespace std;

int ctoi(char c) { return c - '0'; }

int main() {
  string s;
  long long dp[100000][2];
  cin >> s;
  reverse(s.begin(), s.end());

  dp[0][0] = ctoi(s[0]);
  dp[0][1] = 10 - ctoi(s[0]);
  for (int i = 1; i < s.length(); i++) {
    dp[i][0] = min(dp[i - 1][0] + ctoi(s[i]), dp[i - 1][1] + ctoi(s[i]) + 1);
    dp[i][1] =
        min(dp[i - 1][0] + 10 - ctoi(s[i]), dp[i - 1][1] + 9 - ctoi(s[i]));
  }

  cout << min(dp[s.length() - 1][0], dp[s.length() - 1][1] + 1) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
using namespace std;

int ctoi(char c) { return c - '0'; }

int main() {
  string s;
  long long dp[1000000][2];
  cin >> s;
  reverse(s.begin(), s.end());

  dp[0][0] = ctoi(s[0]);
  dp[0][1] = 10 - ctoi(s[0]);
  for (int i = 1; i < s.length(); i++) {
    dp[i][0] = min(dp[i - 1][0] + ctoi(s[i]), dp[i - 1][1] + ctoi(s[i]) + 1);
    dp[i][1] =
        min(dp[i - 1][0] + 10 - ctoi(s[i]), dp[i - 1][1] + 9 - ctoi(s[i]));
  }

  cout << min(dp[s.length() - 1][0], dp[s.length() - 1][1] + 1) << endl;
  return 0;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

const int maxn = 2e5 + 5;

int dp[maxn][2];

int main() {
  //	freopen("in","r",stdin);
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  dp[0][0] = s[0] - '0';
  dp[0][1] = 10 - (s[0] - '0');
  for (int i = 1; i < s.size(); i++) {
    int num = s[i] - '0';
    if (num == 9)
      dp[i][0] = dp[i - 1][0] + num;
    else
      dp[i][0] = min(dp[i - 1][0] + num, dp[i - 1][1] + num + 1);
    dp[i][1] = min(dp[i - 1][0] + 10 - num, dp[i - 1][1] + 9 - num);
  }
  cout << min(dp[s.size() - 1][0], dp[s.size() - 1][1] + 1) << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

const int maxn = 1e6 + 7;

int dp[maxn][2];

int main() {
  //	freopen("in","r",stdin);
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  dp[0][0] = s[0] - '0';
  dp[0][1] = 10 - (s[0] - '0');
  for (int i = 1; i < s.size(); i++) {
    int num = s[i] - '0';
    if (num == 9)
      dp[i][0] = dp[i - 1][0] + num;
    else
      dp[i][0] = min(dp[i - 1][0] + num, dp[i - 1][1] + num + 1);
    dp[i][1] = min(dp[i - 1][0] + 10 - num, dp[i - 1][1] + 9 - num);
  }
  cout << min(dp[s.size() - 1][0], dp[s.size() - 1][1] + 1) << endl;
}
Problem p02775 (C++)

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

int main() {
  string N;
  cin >> N;
  ll dp[1000001][2];
  dp[0][1] = 1;
  dp[0][0] = 0;

  for (int i = 0; i < (int)N.size(); i++) {
    int n = N[i] - '0';
    dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + 10 - n);
    dp[i + 1][1] = min(dp[i][0] + n + 1, dp[i][1] + 10 - n - 1);
  }

  cout << dp[N.size()][0] << endl;
  return 0;
}

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

int main() {
  string N;
  cin >> N;
  ll dp[1000002][2];
  dp[0][1] = 1;
  dp[0][0] = 0;

  for (int i = 0; i < (int)N.size(); i++) {
    int n = N[i] - '0';
    dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + 10 - n);
    dp[i + 1][1] = min(dp[i][0] + n + 1, dp[i][1] + 10 - n - 1);
  }

  cout << dp[N.size()][0] << endl;
  return 0;
}
Problem p02775 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1010101010

using namespace std;

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  s += '0';
  int n = s.size();

  int dp[n][2];
  rep(i, n) rep(j, 2) dp[i][j] = INF;
  dp[0][0] = 0;

  rep(i, n) rep(j, 2) {
    int x = s[i] - '0' + j;

    if (x < 10)
      dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + x);
    if (x > 0)
      dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + (10 - x));
  }

  cout << dp[n][0] << endl;

  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1010101010

using namespace std;

int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  s += '0';
  int n = s.size();

  int dp[n + 1][2];
  rep(i, n + 1) rep(j, 2) dp[i][j] = INF;
  dp[0][0] = 0;

  rep(i, n) rep(j, 2) {
    int x = s[i] - '0' + j;

    if (x < 10)
      dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + x);
    if (x > 0)
      dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + (10 - x));
  }

  cout << dp[n][0] << endl;

  return 0;
}
Problem p02775 (C++)

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

using namespace std;
using ll = long long;

constexpr ll MOD = 1e9 + 7;
int mx[] = {-1, 1, 0, 0}, my[] = {0, 0, -1, 1};

string s;
int dp[1000001][2];
bool visited[1000001][2];

int dfs(int n, bool m) {
  if (n == (int)s.length())
    return 0;
  if (visited[n][m])
    return dp[n][m];
  visited[n][m] = 1;
  int t = s[n] - '0';
  if (m == 0) {
    return dp[n][m] = min(dfs(n + 1, 0) + t, dfs(n + 1, 1) + t + 1);
  } else {
    if (n == (int)s.length() - 1)
      t = 10 - t;
    else
      t = 9 - t;
    return dp[n][m] = min(dfs(n + 1, 0) + t + 1, dfs(n + 1, 1) + t);
  }
}

int main() {
  cin >> s;
  s = "0" + s;
  cout << dfs(0, 0);
}

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

using namespace std;
using ll = long long;

constexpr ll MOD = 1e9 + 7;
int mx[] = {-1, 1, 0, 0}, my[] = {0, 0, -1, 1};

string s;
int dp[2000001][2];
bool visited[2000000][2];

int dfs(int n, bool m) {
  if (n == (int)s.length())
    return 0;
  if (visited[n][m])
    return dp[n][m];
  visited[n][m] = 1;
  int t = s[n] - '0';
  if (m == 0) {
    return dp[n][m] = min(dfs(n + 1, 0) + t, dfs(n + 1, 1) + t + 1);
  } else {
    if (n == (int)s.length() - 1)
      t = 10 - t;
    else
      t = 9 - t;
    return dp[n][m] = min(dfs(n + 1, 0) + t + 1, dfs(n + 1, 1) + t);
  }
}

int main() {
  cin >> s;
  s = "0" + s;
  cout << dfs(0, 0);
}
Problem p02775 (C++)

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

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;

int dp[2][1001000];

int f(string n, int k) { return n[n.length() - 1 - k] - '0'; }

signed main() {
  string N;
  cin >> N;
  dp[0][0] = f(N, 0);
  dp[1][0] = 10 - f(N, 0);
  for (int i = 1; i < N.length(); ++i) {
    int fni = f(N, i);
    dp[0][i] = min(dp[0][i - 1] + fni, dp[1][i - 1] + fni + 1);
    dp[1][i] = min(dp[0][i - 1] + 10 - fni, dp[1][i - 1] + 10 - (fni + 1));
  }
  cout << min(dp[0][N.length() - 1], dp[1][N.length() - 1] + 1) << endl;
  return 0;
}


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

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;

int dp[2][1001000];

int f(string &n, int k) { return n[n.length() - 1 - k] - '0'; }

signed main() {
  string N;
  cin >> N;
  dp[0][0] = f(N, 0);
  dp[1][0] = 10 - f(N, 0);
  for (int i = 1; i < N.length(); ++i) {
    int fni = f(N, i);
    dp[0][i] = min(dp[0][i - 1] + fni, dp[1][i - 1] + fni + 1);
    dp[1][i] = min(dp[0][i - 1] + 10 - fni, dp[1][i - 1] + 10 - (fni + 1));
  }
  cout << min(dp[0][N.length() - 1], dp[1][N.length() - 1] + 1) << endl;
  return 0;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#define first f
#define second s
#define ll long long
#define mp make_pair
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#include <bits/stdc++.h>
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = 2e5 + 5;
const double PI = acos(-1);
const double e = 2.718281828459;

char s[maxn];

int main() {
  scanf("%s", s + 1);
  int len = strlen(s + 1);
  s[0] = '0';
  int p = 0, num = 0;
  for (int i = len, j; i >= 0; i--) {
    j = s[i] - '0';
    j += p;
    p = 0;
    if (j == 5) {
      if (i - 1 >= 0 && s[i - 1] >= '5') {
        p++;
        num += (10 - j);
      } else {
        num += j;
      }
    } else if (j < 5) {
      num += j;
    } else {
      p++;
      num += (10 - j);
    }
  }
  cout << num << endl;
  return 0;
}


Fixed submission:
#define first f
#define second s
#define ll long long
#define mp make_pair
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#include <bits/stdc++.h>
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = 1e6 + 5;
const double PI = acos(-1);
const double e = 2.718281828459;

char s[maxn];

int main() {
  scanf("%s", s + 1);
  int len = strlen(s + 1);
  s[0] = '0';
  int p = 0, num = 0;
  for (int i = len, j; i >= 0; i--) {
    j = s[i] - '0';
    j += p;
    p = 0;
    if (j == 5) {
      if (i - 1 >= 0 && s[i - 1] >= '5') {
        p++;
        num += (10 - j);
      } else {
        num += j;
      }
    } else if (j < 5) {
      num += j;
    } else {
      p++;
      num += (10 - j);
    }
  }
  cout << num << endl;
  return 0;
}
Problem p02777 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define allr(x) x.rbegin(), x.rend()
#define all(x) x.begin(), x.end()
#define sp " "
#define double long double
#define pb push_back
#define ff first
#define int long long
#define ss second
#define vi vector<int>
#define mem(ara, a) memset(ara, a, sizeof(ara))

long long inf = 2e18;
long long minf = -inf;

using namespace std;

signed main() {
  freopen("bruh.txt", "r", stdin);
  ios::sync_with_stdio(false);
  cin.tie(0);
  int a, b;
  string s, sr, t;
  cin >> s >> sr;
  if (s == s) {
    cin >> a >> b;
  } else if (s != s) {
    cin >> b >> a;
  }
  cin >> t;
  if (t == s) {
    cout << a - 1 << " " << b << endl;
  } else if (t != s) {
    cout << a << " " << b - 1 << endl;
  }
}


Fixed submission:
#include "bits/stdc++.h"
#define allr(x) x.rbegin(), x.rend()
#define all(x) x.begin(), x.end()
#define sp " "
#define double long double
#define pb push_back
#define ff first
#define int long long
#define ss second
#define vi vector<int>
#define mem(ara, a) memset(ara, a, sizeof(ara))

long long inf = 2e18;
long long minf = -inf;

using namespace std;

signed main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  int a, b;
  string s, sr, t;
  cin >> s >> sr;
  if (s == s) {
    cin >> a >> b;
  } else if (s != s) {
    cin >> b >> a;
  }
  cin >> t;
  if (t == s) {
    cout << a - 1 << " " << b << endl;
  } else if (t != s) {
    cout << a << " " << b - 1 << endl;
  }
}