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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (ll i = x; i <= n; i++)
#define rep3(i, x, n) for (ll i = x; i >= n; i--)
#define elif else if
#define sp setprecision
#define pb(x) push_back(x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ld, ld> pdd;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = 1e8;
const ll INF = 1e16;
const string alpha = "abcdefghijklmnopqrstuvwxyz";

int main() {
  ll N, K;
  cin >> N >> K;
  ll A[N];
  rep(i, N) cin >> A[i];
  sort(A, A + N);
  // 解は[l,r]に存在
  ll l = -1e18, r = 1e18;
  while (l < r) {
    ll n = (l + r) / 2;
    // n以下になるものが何個あるか判定
    ll sum = 0;
    rep(i, N) {
      if (A[i] < 0) {
        ll x = n / A[i];
        rep2(j, x - 1, x + 1) {
          if (A[i] * j <= n) {
            sum += A + N - lower_bound(A, A + N, j);
            break;
          }
        }
      }
      elif (A[i] == 0) {
        if (n >= 0)
          sum += N;
      }
      else {
        ll x = n / A[i];
        rep3(j, x + 1, x - 1) {
          if (A[i] * j <= n) {
            sum += N - (A + N - lower_bound(A, A + N, j + 1));
            break;
          }
        }
      }
      if (A[i] * A[i] <= n)
        sum--;
    }
    sum /= 2;
    if (sum < K)
      l = n + 1;
    else
      r = n;
  }
  cout << l << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (ll i = x; i <= n; i++)
#define rep3(i, x, n) for (ll i = x; i >= n; i--)
#define elif else if
#define sp setprecision
#define pb(x) push_back(x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ld, ld> pdd;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = 1e8;
const ll INF = 1e16;
const string alpha = "abcdefghijklmnopqrstuvwxyz";

int main() {
  ll N, K;
  cin >> N >> K;
  ll A[N];
  rep(i, N) cin >> A[i];
  sort(A, A + N);
  // 解は[l,r]に存在
  ll l = -1e18, r = 1e18;
  while (l < r) {
    ll n = (l + r) / 2;
    if (l + r < 0 && 2 * n != l + r)
      n--;
    // n以下になるものが何個あるか判定
    ll sum = 0;
    rep(i, N) {
      if (A[i] < 0) {
        ll x = n / A[i];
        rep2(j, x - 1, x + 1) {
          if (A[i] * j <= n) {
            sum += A + N - lower_bound(A, A + N, j);
            break;
          }
        }
      }
      elif (A[i] == 0) {
        if (n >= 0)
          sum += N;
      }
      else {
        ll x = n / A[i];
        rep3(j, x + 1, x - 1) {
          if (A[i] * j <= n) {
            sum += N - (A + N - lower_bound(A, A + N, j + 1));
            break;
          }
        }
      }
      if (A[i] * A[i] <= n)
        sum--;
    }
    sum /= 2;
    if (sum < K)
      l = n + 1;
    else
      r = n;
  }
  cout << l << endl;
}
Problem p02774 (C++)

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

using namespace std;

using ll = long long;

int main(void) {
  ll n, k;
  cin >> n >> k;

  vector<ll> a(n);
  for (auto &e : a)
    cin >> e;

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

  ll ng = 2e18, ok = -2e18;
  // ok譛ｪ貅縺ｮ蛟区焚 < k
  while (ng - ok > 1) {
    ll m = (ok + ng) / 2;

    ll s = 0;
    for (ll i = 0; i < n; i++) {
      auto chk = [&]() {
        ll r = 0;
        for (ll j = 0; j < n; j++) {
          if (i == j)
            continue;
          r += a[j] * a[i] < m;
        }
        return r;
      };
      if (a[i] < 0) {
        // a[x] * a[i] < m
        ll j = [&]() {
          ll ok = n, ng = -1;
          while (abs(ng - ok) > 1) {
            ll mm = (ok + ng) / 2;
            (a[mm] * a[i] < m ? ok : ng) = mm;
          }
          return ok;
        }();
        // ng ng [ok] ok ok
        if (i >= j)
          j++;
        s += max(n - j, 0ll);
        assert(max(n - j, 0ll) == chk());
      }
      if (a[i] > 0) {
        // a[x] * a[i] < m
        ll j = [&]() {
          ll ok = 0, ng = n;
          if (!(a[0] * a[i] < m))
            return -1ll;
          while (abs(ng - ok) > 1) {
            ll mm = (ok + ng) / 2;
            (a[mm] * a[i] < m ? ok : ng) = mm;
          }
          return ok;
        }();
        // ok ok [ok] ng ng
        if (i <= j)
          j--;
        s += max(j + 1, 0ll);
      }
      if (a[i] == 0) {
        // a[x] * 0 < m
        s += m > 0 ? n - 1 : 0;
      }
    }
    assert(s % 2 == 0);
    s /= 2;

    (s < k ? ok : ng) = m;
  }
  cout << ok << endl;
}


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

using namespace std;

using ll = long long;

int main(void) {
  ll n, k;
  cin >> n >> k;

  vector<ll> a(n);
  for (auto &e : a)
    cin >> e;

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

  ll ng = 2e18, ok = -2e18;
  // ok譛ｪ貅縺ｮ蛟区焚 < k
  while (ng - ok > 1) {
    ll m = (ok + ng) / 2;

    ll s = 0;
    for (ll i = 0; i < n; i++) {
      auto chk = [&]() {
        ll r = 0;
        for (ll j = 0; j < n; j++) {
          if (i == j)
            continue;
          r += a[j] * a[i] < m;
        }
        return r;
      };
      if (a[i] < 0) {
        // a[x] * a[i] < m
        ll j = [&]() {
          ll ok = n, ng = -1;
          while (abs(ng - ok) > 1) {
            ll mm = (ok + ng) / 2;
            (a[mm] * a[i] < m ? ok : ng) = mm;
          }
          return ok;
        }();
        // ng ng [ok] ok ok
        if (i >= j)
          j++;
        s += max(n - j, 0ll);
      }
      if (a[i] > 0) {
        // a[x] * a[i] < m
        ll j = [&]() {
          ll ok = 0, ng = n;
          if (!(a[0] * a[i] < m))
            return -1ll;
          while (abs(ng - ok) > 1) {
            ll mm = (ok + ng) / 2;
            (a[mm] * a[i] < m ? ok : ng) = mm;
          }
          return ok;
        }();
        // ok ok [ok] ng ng
        if (i <= j)
          j--;
        s += max(j + 1, 0ll);
      }
      if (a[i] == 0) {
        // a[x] * 0 < m
        s += m > 0 ? n - 1 : 0;
      }
    }
    assert(s % 2 == 0);
    s /= 2;

    (s < k ? ok : ng) = m;
  }
  cout << ok << endl;
}
Problem p02774 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 100005;

int n;
LL m;
int a[N];
vector<LL> b, c;
LL zero;
bool check(LL x) {
  LL cnt = 0;
  if (x < 0) {
    x = -x;
    for (auto u : b) {
      LL t = (x + u - 1) / u;
      cnt += c.end() - lower_bound(c.begin(), c.end(), t);
    }
    for (auto u : c) {
      LL t = (x + u - 1) / u;
      cnt += b.end() - lower_bound(b.begin(), b.end(), t);
    }
  } else if (x == 0) {
    cnt +=
        zero * (zero - 1) + zero * (n - zero) * 2 + (LL)b.size() * c.size() * 2;
  } else {
    cnt +=
        zero * (zero - 1) + zero * (n - zero) * 2 + (LL)b.size() * c.size() * 2;
    for (auto u : b) {
      LL t = x / u;
      cnt += upper_bound(b.begin(), b.end(), t) - b.begin();
      if (u <= t)
        cnt--;
    }
    for (auto u : c) {
      LL t = x / u;
      cnt += upper_bound(c.begin(), c.end(), t) - c.begin();
      if (u <= t)
        cnt--;
    }
  }

  return cnt / 2 >= m;
}
int main() {
  scanf("%d%lld", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (a[i] < 0) {
      b.push_back(-a[i]);
    } else if (a[i] > 0) {
      c.push_back(a[i]);
    } else {
      zero++;
    }
  }
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  LL l = -1e18, r = 1e18;
  while (l < r) {
    LL mid = l + (r - l) / 2;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << l << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 200005;

int n;
LL m;
int a[N];
vector<LL> b, c;
LL zero;
bool check(LL x) {
  LL cnt = 0;
  if (x < 0) {
    x = -x;
    for (auto u : b) {
      LL t = (x + u - 1) / u;
      cnt += c.end() - lower_bound(c.begin(), c.end(), t);
    }
    for (auto u : c) {
      LL t = (x + u - 1) / u;
      cnt += b.end() - lower_bound(b.begin(), b.end(), t);
    }
  } else if (x == 0) {
    cnt +=
        zero * (zero - 1) + zero * (n - zero) * 2 + (LL)b.size() * c.size() * 2;
  } else {
    cnt +=
        zero * (zero - 1) + zero * (n - zero) * 2 + (LL)b.size() * c.size() * 2;
    for (auto u : b) {
      LL t = x / u;
      cnt += upper_bound(b.begin(), b.end(), t) - b.begin();
      if (u <= t)
        cnt--;
    }
    for (auto u : c) {
      LL t = x / u;
      cnt += upper_bound(c.begin(), c.end(), t) - c.begin();
      if (u <= t)
        cnt--;
    }
  }

  return cnt / 2 >= m;
}
int main() {
  scanf("%d%lld", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (a[i] < 0) {
      b.push_back(-a[i]);
    } else if (a[i] > 0) {
      c.push_back(a[i]);
    } else {
      zero++;
    }
  }
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  LL l = -1e18, r = 1e18;
  while (l < r) {
    LL mid = l + (r - l) / 2;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << l << endl;
  return 0;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int dp[N][2];
int main() {
  string s;
  cin >> s;
  dp[0][0] = s[0] - '0';
  dp[0][1] = 1 + 10 - s[0] + '0';
  for (int i = 1; i < s.size(); i++) {
    int x = s[i] - '0';
    dp[i][0] = min(dp[i - 1][0] + x, dp[i - 1][1] + x);
    dp[i][1] = min(dp[i - 1][0] + 10 - x + 1, dp[i - 1][1] - 1 + 10 - x);
  }
  cout << min(dp[s.size() - 1][1], dp[s.size() - 1][0]) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int dp[N][2];
int main() {
  string s;
  cin >> s;
  dp[0][0] = s[0] - '0';
  dp[0][1] = 1 + 10 - s[0] + '0';
  for (int i = 1; i < s.size(); i++) {
    int x = s[i] - '0';
    dp[i][0] = min(dp[i - 1][0] + x, dp[i - 1][1] + x);
    dp[i][1] = min(dp[i - 1][0] + 10 - x + 1, dp[i - 1][1] - 1 + 10 - x);
  }
  cout << min(dp[s.size() - 1][1], dp[s.size() - 1][0]) << endl;
  return 0;
}
Problem p02775 (C++)

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

int main() {
  string s;
  cin >> s;
  int N = s.size();
  ll dp[100005][2];
  dp[0][1] = 1;
  REP(i, N) {
    int num = s[i] - '0';
    int num2 = 10 - num;
    dp[i + 1][0] = min(dp[i][0] + num, dp[i][1] + num2);
    dp[i + 1][1] = min(dp[i][0] + num + 1, dp[i][1] + num2 - 1);
  }
  cout << dp[N][0] << endl;
  return 0;
}

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

int main() {
  string s;
  cin >> s;
  int N = s.size();
  ll dp[1000005][2];
  dp[0][1] = 1;
  REP(i, N) {
    int num = s[i] - '0';
    int num2 = 10 - num;
    dp[i + 1][0] = min(dp[i][0] + num, dp[i][1] + num2);
    dp[i + 1][1] = min(dp[i][0] + num + 1, dp[i][1] + num2 - 1);
  }
  cout << dp[N][0] << endl;
  return 0;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
// #define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  do {                                                                         \
    cout << "0" << endl;                                                       \
    return 0;                                                                  \
  } while (false)
#define MM << " " <<
#define Endl endl

signed main(void) {
  string N;
  cin >> N;
  const int MEM = 100;
  int dp[MEM][2];
  REP(i, MEM) {
    REP(j, 2) { dp[i][j] = INF; }
  }
  dp[0][0] = N[0] - '0';
  dp[0][1] = 11 - (N[0] - '0');

  REP(i, N.size() - 1) {
    dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + N[i + 1] - '0');
    dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + N[i + 1] - '0');

    dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + 11 - (N[i + 1] - '0'));
    dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + 9 - (N[i + 1] - '0'));
  }
  cout << min(dp[N.size() - 1][0], dp[N.size() - 1][1]) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1145141919810893364
// #define INF 810114514
// #define PI 3.141592653589
typedef pair<int, int> PP;
typedef long long ll;
#define int ll
#define setdouble setprecision
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define GOODBYE                                                                \
  do {                                                                         \
    cout << "0" << endl;                                                       \
    return 0;                                                                  \
  } while (false)
#define MM << " " <<
#define Endl endl

signed main(void) {
  string N;
  cin >> N;
  const int MEM = 1114514;
  int dp[MEM][2];
  REP(i, MEM) {
    REP(j, 2) { dp[i][j] = INF; }
  }
  dp[0][0] = N[0] - '0';
  dp[0][1] = 11 - (N[0] - '0');

  REP(i, N.size() - 1) {
    dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + N[i + 1] - '0');
    dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + N[i + 1] - '0');

    dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + 11 - (N[i + 1] - '0'));
    dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + 9 - (N[i + 1] - '0'));
  }
  cout << min(dp[N.size() - 1][0], dp[N.size() - 1][1]) << endl;
  return 0;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (long long i = 0; i < x; i++)
#define rrep(i, x) for (long long i = x - 1; i >= 0; i--)
#define repn(i, x) for (long long i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
vector<pair<string, P>> vec;
// vector<vector<int>> data(3, vector<int>(4));

ll dp[1000001][2];

int main() {
  string N;
  cin >> N;
  ll res = 0;
  dp[0][1] = INF;
  int n = N.size();
  rep(i, n) {
    ll c = N[n - 1 - i] - '0';
    if (c != 9)
      dp[i + 1][0] = min(dp[i][0] + c, dp[i][1] + c);
    else
      dp[i + 1][0] = dp[i][0] + c;
    dp[i + 1][1] = min(dp[i][0] + 10 - c + 1, dp[i][1] + 9 - c);
    // cout << i+1 << ": " << dp[i+1][0] << " " << dp[i+1][1] << endl;
  }

  cout << min(dp[n][0], dp[n][1]) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i, x) for (long long i = 0; i < x; i++)
#define rrep(i, x) for (long long i = x - 1; i >= 0; i--)
#define repn(i, x) for (long long i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
vector<pair<string, P>> vec;
// vector<vector<int>> data(3, vector<int>(4));

ll dp[1000005][2];

int main() {
  string N;
  cin >> N;
  ll res = 0;
  dp[0][1] = INF;
  int n = N.size();
  rep(i, n) {
    ll c = N[n - 1 - i] - '0';
    if (c != 9)
      dp[i + 1][0] = min(dp[i][0] + c, dp[i][1] + c);
    else
      dp[i + 1][0] = dp[i][0] + c;
    dp[i + 1][1] = min(dp[i][0] + 10 - c + 1, dp[i][1] + 9 - c);
    // cout << i+1 << ": " << dp[i+1][0] << " " << dp[i+1][1] << endl;
  }

  cout << min(dp[n][0], dp[n][1]) << endl;
  return 0;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
typedef long long ll;
char s[N];
ll dp[N][2];
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  dp[0][1] = 1000;
  for (int i = 1; i <= n; i++) {
    int x = s[i] - '0';
    dp[i][0] = dp[i - 1][0] + min(x, 10 - x + 1);
    dp[i][0] = min(dp[i][0], dp[i - 1][1] + 10 - x);
    dp[i][1] = min(dp[i - 1][0] + 10 - x, dp[i - 1][1] + 10 - x - 1);
  }
  printf("%lld\n", min(dp[n][0], dp[n][1] + 1));
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long ll;
char s[N];
ll dp[N][2];
int main() {
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  dp[0][1] = 1000;
  for (int i = 1; i <= n; i++) {
    int x = s[i] - '0';
    dp[i][0] = dp[i - 1][0] + min(x, 10 - x + 1);
    dp[i][0] = min(dp[i][0], dp[i - 1][1] + 10 - x);
    dp[i][1] = min(dp[i - 1][0] + 10 - x, dp[i - 1][1] + 10 - x - 1);
  }
  printf("%lld\n", min(dp[n][0], dp[n][1] + 1));
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 105;
int num[maxn];
int main() {
  string ss;
  cin >> ss;
  reverse(ss.begin(), ss.end());
  for (int i = 1; i <= ss.length(); i++)
    num[i] = (ss[i - 1] - '0');
  int ans = 0;
  for (int i = 1; i <= ss.length() + 1; i++) {
    if (num[i] <= 4)
      ans += num[i];
    else if (num[i] == 5) {
      if (num[i + 1] >= 5) {
        ans += (10 - num[i]);
        num[i + 1]++;
      } else
        ans += num[i];
    } else {
      ans += (10 - num[i]);
      num[i + 1]++;
    }
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 1e6 + 5;
int num[maxn];
int main() {
  string ss;
  cin >> ss;
  reverse(ss.begin(), ss.end());
  for (int i = 1; i <= ss.length(); i++)
    num[i] = (ss[i - 1] - '0');
  int ans = 0;
  for (int i = 1; i <= ss.length() + 1; i++) {
    if (num[i] <= 4)
      ans += num[i];
    else if (num[i] == 5) {
      if (num[i + 1] >= 5) {
        ans += (10 - num[i]);
        num[i + 1]++;
      } else
        ans += num[i];
    } else {
      ans += (10 - num[i]);
      num[i + 1]++;
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#define _LIBCPP_DEBUG 0
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll MOD = 1e9 + 7;

int main(void) {

  string N;
  cin >> N;
  vector<int> vec(N.length());
  for (ll i = 0; i < N.length(); i++) {
    vec[i + 1] = N[i] - '0';
  }
  ll ans = 0;

  for (ll i = N.length(); i >= 0; --i) {

    if (vec[i] >= 10) { // 繰り上げ処理
      vec[i - 1]++;
      vec[i] %= 10;
    }

    if (vec[i] < 5) {
      ans += vec[i];
    } else if (vec[i] == 5) {
      if (vec[i - 1] >= 5) {
        ans += 10 - vec[i];
        vec[i - 1]++;
      } else {
        ans += vec[i]++;
      }
    } else {
      ans += 10 - vec[i];
      vec[i - 1]++;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#define _LIBCPP_DEBUG 0
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll MOD = 1e9 + 7;

int main(void) {

  string N;
  cin >> N;
  vector<int> vec(N.length() + 1);
  for (int i = 0; i < N.length(); i++) {
    vec[i + 1] = N[i] - '0';
  }
  ll ans = 0;

  for (ll i = N.length(); i >= 0; --i) {

    if (vec[i] >= 10) { // 繰り上げ処理
      vec[i - 1]++;
      vec[i] %= 10;
    }

    if (vec[i] < 5) {
      ans += vec[i];
    } else if (vec[i] == 5) {
      if (vec[i - 1] >= 5) {
        ans += 10 - vec[i];
        vec[i - 1]++;
      } else {
        ans += vec[i]++;
      }
    } else {
      ans += 10 - vec[i];
      vec[i - 1]++;
    }
  }
  cout << ans << endl;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 2e5 + 10;
int d[N][2];
int main() {
  string s;
  cin >> s;
  d[0][0] = s[0] - '0';
  d[0][1] = 11 - (s[0] - '0');
  for (int i = 1; i < s.size(); i++) {
    d[i][0] = min(d[i - 1][0], d[i - 1][1]) + s[i] - '0';
    d[i][1] = min(d[i - 1][0] + 1, d[i - 1][1] - 1) + 10 - (s[i] - '0');
  }
  cout << min(d[s.size() - 1][0], d[s.size() - 1][1]) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 2e6 + 10;
int d[N][2];
int main() {
  string s;
  cin >> s;
  d[0][0] = s[0] - '0';
  d[0][1] = 11 - (s[0] - '0');
  for (int i = 1; i < s.size(); i++) {
    d[i][0] = min(d[i - 1][0], d[i - 1][1]) + s[i] - '0';
    d[i][1] = min(d[i - 1][0] + 1, d[i - 1][1] - 1) + 10 - (s[i] - '0');
  }
  cout << min(d[s.size() - 1][0], d[s.size() - 1][1]) << endl;
}
Problem p02775 (C++)

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

int main() {
  string s;
  cin >> s;
  int n = s.length();
  long long dp[MAX_N][2];
  for (int i = 0; i < MAX_N; i++)
    dp[i][0] = dp[i][1] = 0;

  // dp[i][0] : ぴったり払う
  // dp[i][1] : 1多く払う
  dp[1][0] = s[0] - '0';
  dp[1][1] = 10 - (s[0] - '0');
  for (int i = 1; i < n; i++) {
    const int c = s[i] - '0';
    dp[i + 1][0] = min(dp[i][0] + c, dp[i][1] + c + 1);
    dp[i + 1][1] = min(dp[i][0] + 10 - c, dp[i][1] + 10 - (c + 1));
  }

  cout << min(dp[n][0], dp[n][1] + 1) << endl;
  return 0;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <class T, class U> inline bool chmax(T &a, U b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &a, U b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int MAX_N = 1001000;

int main() {
  string s;
  cin >> s;
  int n = s.length();
  long long dp[MAX_N][2];
  for (int i = 0; i < MAX_N; i++)
    dp[i][0] = dp[i][1] = 0;

  // dp[i][0] : ぴったり払う
  // dp[i][1] : 1多く払う
  dp[1][0] = s[0] - '0';
  dp[1][1] = 10 - (s[0] - '0');
  for (int i = 1; i < n; i++) {
    const int c = s[i] - '0';
    dp[i + 1][0] = min(dp[i][0] + c, dp[i][1] + c + 1);
    dp[i + 1][1] = min(dp[i][0] + 10 - c, dp[i][1] + 10 - (c + 1));
  }

  cout << min(dp[n][0], dp[n][1] + 1) << 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 sz(x) int(x.size())
#define show(x)                                                                \
  {                                                                            \
    for (auto i : x) {                                                         \
      cout << i << " ";                                                        \
    }                                                                          \
    cout << endl;                                                              \
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;

int dp[105][2];
const ll INF = 1e5;

int main() {
  // 一桁目から桁dp、dp[i][j]：i桁目まで決めた時の最小値、繰り上がりが起きているならj=1
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  S += '0';
  int N = sz(S);
  rep(i, N + 1) rep(j, 2) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, N) rep(j, 2) {
    rep(a, 10) {
      // aはその桁で払うお金、bはお釣り
      int cur = S[i] - '0';
      int ni = i + 1, nj = 0;
      cur += j; // 繰り上がり
      int b = a - cur;
      if (b < 0) {
        nj = 1;
        b += 10;
      }
      dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b);
    }
  }
  cout << dp[N][0] << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x)                                                                \
  {                                                                            \
    for (auto i : x) {                                                         \
      cout << i << " ";                                                        \
    }                                                                          \
    cout << endl;                                                              \
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;

int dp[1000005][2];
const ll INF = 1e9;

int main() {
  // 一桁目から桁dp、dp[i][j]：i桁目まで決めた時の最小値、繰り上がりが起きているならj=1
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  S += '0';
  int N = sz(S);
  rep(i, N + 1) rep(j, 2) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, N) rep(j, 2) {
    rep(a, 10) {
      // aはその桁で払うお金、bはお釣り
      int cur = S[i] - '0';
      int ni = i + 1, nj = 0;
      cur += j; // 繰り上がり
      int b = a - cur;
      if (b < 0) {
        nj = 1;
        b += 10;
      }
      dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b);
    }
  }
  cout << dp[N][0] << '\n';
  return 0;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define FI first.first
#define SE first.second
#define TH second
#define fi first
#define se second
#define th second

using namespace std;

class DebugStream {
} LOG;
template <typename T> DebugStream &operator<<(DebugStream &s, const T &) {
  return s;
}
#ifdef DEBUG
#define LOG clog
#endif

typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20,
          bs = 29;

int n, eq[maxN], bg[maxN];
string st;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> st;
  n = st.size();
  reverse(st.begin(), st.end());
  bg[n] = 1;

  for (int i = n - 1; i >= 0; i--) {
    int elm = st[i] - '0';
    eq[i] = min(eq[i + 1] + elm, bg[i + 1] + 10 - elm);
    if (elm == 9)
      bg[i] = bg[i + 1];
    else
      bg[i] = min(eq[i + 1] + elm + 1, bg[i + 1] + 10 - (elm + 1));
  }

  cout << eq[0] << '\n';
}


Fixed submission:
#include "bits/stdc++.h"
#define FI first.first
#define SE first.second
#define TH second
#define fi first
#define se second
#define th second

using namespace std;

class DebugStream {
} LOG;
template <typename T> DebugStream &operator<<(DebugStream &s, const T &) {
  return s;
}
#ifdef DEBUG
#define LOG clog
#endif

typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const int maxN = 1e6 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20,
          bs = 29;

int n, eq[maxN], bg[maxN];
string st;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> st;
  n = st.size();
  reverse(st.begin(), st.end());
  bg[n] = 1;

  for (int i = n - 1; i >= 0; i--) {
    int elm = st[i] - '0';
    eq[i] = min(eq[i + 1] + elm, bg[i + 1] + 10 - elm);
    if (elm == 9)
      bg[i] = bg[i + 1];
    else
      bg[i] = min(eq[i + 1] + elm + 1, bg[i + 1] + 10 - (elm + 1));
  }

  cout << eq[0] << '\n';
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <random>
using namespace std;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \
  for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first - r.first, l.second - r.second);
}
const lint MOD = 1e9 + 7, INF = 1e18;

string s;
lint arr[1000000];
int main() {
  cin >> s;
  IREP(i, SZ(s)) { arr[SZ(s) - i - 1] = (s[i] - '0'); }
  lint cnt = 0;
  REP(i, SZ(s) + 1) {
    if (arr[i] < 5 || (arr[i] == 5 && arr[i + 1] < 5)) {
      cnt += arr[i];
    } else {
      cnt += (10 - arr[i]);
      arr[i + 1]++;
    }
  }
  cout << cnt << endl;
}


Fixed submission:
#include "bits/stdc++.h"
#include <random>
using namespace std;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \
  for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \
  for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first - r.first, l.second - r.second);
}
const lint MOD = 1e9 + 7, INF = 1e18;

string s;
lint arr[1000001];
int main() {
  cin >> s;
  IREP(i, SZ(s)) { arr[SZ(s) - i - 1] = (s[i] - '0'); }
  lint cnt = 0;
  REP(i, SZ(s) + 1) {
    if (arr[i] < 5 || (arr[i] == 5 && arr[i + 1] < 5)) {
      cnt += arr[i];
    } else {
      cnt += (10 - arr[i]);
      arr[i + 1]++;
    }
  }
  cout << cnt << endl;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
string nd;
int dp[110][2];

int main() {
  cin >> nd;
  string n = "0" + nd;

  int nlen = n.length();

  for (int i = 0; i <= nlen + 2; ++i) {
    for (int j = 0; j < 2; ++j) {
      dp[i][j] = 0;
    }
  }

  int now = 0;
  for (int i = nlen - 1; i >= 0; --i) {
    int check = n.at(i) - '0';
    if (now == 0) {
      dp[now + 1][0] = dp[now][0] + check;
      dp[now + 1][1] = dp[now][1] + (10 - check);
    } else {
      dp[now + 1][0] = min(dp[now][0] + check, dp[now][1] + (check + 1));
      dp[now + 1][1] =
          min(dp[now][0] + (10 - check), dp[now][1] + (10 - (check + 1)));
    }
    now++;
  }

  cout << min(dp[nlen][0], dp[nlen][1]) << endl;

  /*for(int i = 0; i <= nlen; ++i) {
      for(int j = 0; j < 2; ++j) {
          cout << dp[i][j] << " ";
      }
      cout << endl;
  }*/
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
string nd;
int dp[10000100][2];

int main() {
  cin >> nd;
  string n = "0" + nd;

  int nlen = n.length();

  for (int i = 0; i <= nlen + 2; ++i) {
    for (int j = 0; j < 2; ++j) {
      dp[i][j] = 0;
    }
  }

  int now = 0;
  for (int i = nlen - 1; i >= 0; --i) {
    int check = n.at(i) - '0';
    if (now == 0) {
      dp[now + 1][0] = dp[now][0] + check;
      dp[now + 1][1] = dp[now][1] + (10 - check);
    } else {
      dp[now + 1][0] = min(dp[now][0] + check, dp[now][1] + (check + 1));
      dp[now + 1][1] =
          min(dp[now][0] + (10 - check), dp[now][1] + (10 - (check + 1)));
    }
    now++;
  }

  cout << min(dp[nlen][0], dp[nlen][1]) << endl;

  /*for(int i = 0; i <= nlen; ++i) {
      for(int j = 0; j < 2; ++j) {
          cout << dp[i][j] << " ";
      }
      cout << endl;
  }*/
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[200];
string s;
int main(void) {
  cin >> s;
  for (ll i = 0; i < s.size(); i++) {
    a[i + 1] = s[i] - '0';
  }
  ll cnt = 0, f = 0;
  a[0] = 0;
  for (ll i = s.size(); i > 0; i--) {
    a[i] = a[i] + f;
    cnt += min(a[i], 10 - a[i]);
    if (a[i] > 5 || a[i] == 5 && a[i - 1] >= 5) {
      f = 1;
    } else {
      f = 0;
    }
  }
  printf("%lld", cnt + f);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[1000005];
string s;
int main(void) {
  cin >> s;
  for (ll i = 0; i < s.size(); i++) {
    a[i + 1] = s[i] - '0';
  }
  ll cnt = 0, f = 0;
  a[0] = 0;
  for (ll i = s.size(); i > 0; i--) {
    a[i] = a[i] + f;
    cnt += min(a[i], 10 - a[i]);
    if (a[i] > 5 || a[i] == 5 && a[i - 1] >= 5) {
      f = 1;
    } else {
      f = 0;
    }
  }
  printf("%lld", cnt + f);
}
Problem p02775 (C++)

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

string s;
int n;
ll dp[1000001][2];
int main() {
  cin >> s;
  n = s.size();
  reverse(s.begin(), s.end());
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  rep(i, n) rep(j, 2) {
    dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + (s[i] - '0') + j);
    dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + 10 - (s[i] - '0') - j);
  }
  cout << min(dp[n][0], dp[n][1] + 1) << endl;
  return 0;
}

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

string s;
int n;
ll dp[1000005][2];
int main() {
  cin >> s;
  n = s.size();
  reverse(s.begin(), s.end());
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  rep(i, n) rep(j, 2) {
    dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + (s[i] - '0') + j);
    dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + 10 - (s[i] - '0') - j);
  }
  cout << min(dp[n][0], dp[n][1] + 1) << endl;
  return 0;
}
Problem p02775 (C++)

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

int main() {
  string N;
  int counter = 0;
  int count, counter1;
  cin >> N;
  int a[N.size()];
  for (int i = 0; i < N.size(); i++) {
    a[i] = N.at(i) - '0';
  }
  int dp[100010][2]{};
  dp[0][0] = a[0];
  dp[0][1] = 11 - a[0];
  for (int i = 0; i < N.size() - 1; i++) {
    dp[i + 1][0] = min(dp[i][0] + a[i + 1], dp[i][1] + a[i + 1]);
    dp[i + 1][1] = min(dp[i][0] + 11 - a[i + 1], dp[i][1] + 9 - a[i + 1]);
  }
  cout << min(dp[N.size() - 1][1], dp[N.size() - 1][0]) << endl;
}


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

int main() {
  string N;
  int counter = 0;
  int count, counter1;
  cin >> N;
  int a[N.size()];
  for (int i = 0; i < N.size(); i++) {
    a[i] = N.at(i) - '0';
  }
  int dp[1000010][2]{};
  dp[0][0] = a[0];
  dp[0][1] = 11 - a[0];
  for (int i = 0; i < N.size() - 1; i++) {
    dp[i + 1][0] = min(dp[i][0] + a[i + 1], dp[i][1] + a[i + 1]);
    dp[i + 1][1] = min(dp[i][0] + 11 - a[i + 1], dp[i][1] + 9 - a[i + 1]);
  }
  cout << min(dp[N.size() - 1][1], dp[N.size() - 1][0]) << endl;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  cin >> S;
  int ans = 0;
  int temp = 0;
  for (int i = S.length() - 1; i >= 0; i--) {
    if ((S.at(i) - '0') + temp > 5) {
      ans += 10 - (S.at(i) - '0' + temp);
      temp = 1;
    } else if ((S.at(i) - '0') + temp == 5) {
      if (S.at(i - 1) - '0' > 4 && i > 0) {
        ans += 10 - (S.at(i) - '0' + temp);
        temp = 1;
      } else {
        ans += (S.at(i) - '0') + temp;
        temp = 0;
      }
    } else {
      ans += (S.at(i) - '0') + temp;
      temp = 0;
    }
    // cout<<i<<" "<<ans<<endl;
  }
  cout << ans + temp << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  cin >> S;
  int ans = 0;
  int temp = 0;
  for (int i = S.length() - 1; i >= 0; i--) {
    if ((S.at(i) - '0') + temp > 5) {
      ans += 10 - (S.at(i) - '0' + temp);
      temp = 1;
    } else if ((S.at(i) - '0') + temp == 5 && i > 0) {
      if (S.at(i - 1) - '0' > 4) {
        ans += 10 - (S.at(i) - '0' + temp);
        temp = 1;
      } else {
        ans += (S.at(i) - '0') + temp;
        temp = 0;
      }
    } else {
      ans += (S.at(i) - '0') + temp;
      temp = 0;
    }
    // cout<<i<<" "<<ans<<endl;
  }
  cout << ans + temp << 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)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int INF = 1001001001;
int dp[100005][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)
      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)
using namespace std;
using ll = long long;
using P = pair<int, int>;

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)
      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;
#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for (int i = (int)(s - 1); i >= (int)(e); i--)
#define pb push_back
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-8;

template <typename T> T chmax(T &a, const T &b) { return a = (a > b ? a : b); }
template <typename T> T chmin(T &a, const T &b) { return a = (a < b ? a : b); }

const int MAX_N = 1e5 + 10;
ll dp[MAX_N][2];

int main() {
  string s;
  cin >> s;
  reverse(all(s));
  int n = s.size();
  REP(i, 1, n + 2) rep(j, 2) dp[i][j] = INF;
  dp[0][1] = INF;
  rep(i, n) {
    int x = s[i] - '0';
    {
      // ちょうど払う
      chmin(dp[i + 1][0], dp[i][0] + x);
      chmin(dp[i + 1][0], dp[i][1] + x + 1);
      // 繰り上げる
      chmin(dp[i + 1][1], dp[i][0] + 10 - x);
      chmin(dp[i + 1][1], dp[i][1] + 10 - (x + 1));

      // cout << i << " " << dp[i+1][0] << " " << dp[i+1][1] << endl;
    }
  }
  cout << min(dp[n][0], dp[n][1] + 1) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for (int i = (int)(s - 1); i >= (int)(e); i--)
#define pb push_back
#define all(r) r.begin(), r.end()
#define rall(r) r.rbegin(), r.rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const double EPS = 1e-8;

template <typename T> T chmax(T &a, const T &b) { return a = (a > b ? a : b); }
template <typename T> T chmin(T &a, const T &b) { return a = (a < b ? a : b); }

const int MAX_N = 1e6 + 10;
ll dp[MAX_N][2];

int main() {
  string s;
  cin >> s;
  reverse(all(s));
  int n = s.size();
  REP(i, 1, n + 2) rep(j, 2) dp[i][j] = INF;
  dp[0][1] = INF;
  rep(i, n) {
    int x = s[i] - '0';
    {
      // ちょうど払う
      chmin(dp[i + 1][0], dp[i][0] + x);
      chmin(dp[i + 1][0], dp[i][1] + x + 1);
      // 繰り上げる
      chmin(dp[i + 1][1], dp[i][0] + 10 - x);
      chmin(dp[i + 1][1], dp[i][1] + 10 - (x + 1));

      // cout << i << " " << dp[i+1][0] << " " << dp[i+1][1] << endl;
    }
  }
  cout << min(dp[n][0], dp[n][1] + 1) << endl;
  return 0;
}
Problem p02775 (C++)

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string N;
  cin >> N;
  ll ans = 0, l = N.length();
  lvector v(l + 1, 0);
  rep(i, l + 1) v[i + 1] = N[i] - '0';
  rep(i, l + 1) {
    ll pos = l - i;
    if (v[pos] < 5)
      ans += v[pos];
    else if (pos - 1 >= 0) {
      ll pattern1 =
          10 - v[pos] + min(v[pos - 1] + 1ll, 10ll - (v[pos - 1] + 1ll));
      ll pattern2 = v[pos] + min(v[pos - 1], 10ll - v[pos - 1]);
      if (pattern1 <= pattern2) {
        ans += 10 - v[pos];
        v[pos - 1] += 1;
      } else {
        ans += v[pos];
      }
    }
  }
  print(ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define P pair<ll, ll>
#define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define print(a) cout << (a) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string N;
  cin >> N;
  ll ans = 0, l = N.length();
  lvector v(l + 1, 0);
  rep(i, l) v[i + 1] = N[i] - '0';
  rep(i, l + 1) {
    ll pos = l - i;
    if (v[pos] < 5)
      ans += v[pos];
    else if (pos - 1 >= 0) {
      ll pattern1 =
          10 - v[pos] + min(v[pos - 1] + 1ll, 10ll - (v[pos - 1] + 1ll));
      ll pattern2 = v[pos] + min(v[pos - 1], 10ll - v[pos - 1]);
      if (pattern1 <= pattern2) {
        ans += 10 - v[pos];
        v[pos - 1] += 1;
      } else {
        ans += v[pos];
      }
    }
  }
  print(ans);
  return 0;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int f[105][2], n, num;
char s[105];
bool check(int num) {
  if (num >= 0 && num <= 9)
    return 1;
  return 0;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  memset(f, 0x3f3f3f3f, sizeof(f));
  f[0][0] = 0;
  f[0][1] = 1;
  for (register int i = 1; i <= n; ++i) {
    num = s[i] - '0';
    for (register int j = 0; j <= 9; ++j) {
      if (check(j - num))
        f[i][0] = min(f[i][0], f[i - 1][0] + j + j - num);
      if (check(j - num + 10))
        f[i][0] = min(f[i][0], f[i - 1][1] + j + j - num + 10);
      if (check(j - 1 - num))
        f[i][1] = min(f[i][1], f[i - 1][0] + j + j - 1 - num);
      if (check(j - 1 - num + 10))
        f[i][1] = min(f[i][1], f[i - 1][1] + j + j - 1 - num + 10);
    }
  }
  printf("%d\n", f[n][0]);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int f[1000005][2], n, num;
char s[1000005];
bool check(int num) {
  if (num >= 0 && num <= 9)
    return 1;
  return 0;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  memset(f, 0x3f3f3f3f, sizeof(f));
  f[0][0] = 0;
  f[0][1] = 1;
  for (register int i = 1; i <= n; ++i) {
    num = s[i] - '0';
    for (register int j = 0; j <= 9; ++j) {
      if (check(j - num))
        f[i][0] = min(f[i][0], f[i - 1][0] + j + j - num);
      if (check(j - num + 10))
        f[i][0] = min(f[i][0], f[i - 1][1] + j + j - num + 10);
      if (check(j - 1 - num))
        f[i][1] = min(f[i][1], f[i - 1][0] + j + j - 1 - num);
      if (check(j - 1 - num + 10))
        f[i][1] = min(f[i][1], f[i - 1][1] + j + j - 1 - num + 10);
    }
  }
  printf("%d\n", f[n][0]);
  return 0;
}
Problem p02775 (C++)

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

int n, dp[1000001][2];
int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  s += "0";
  n = s.length();

  for (int i = 0; i <= n; i++) {
    dp[i][0] = dp[i][1] = 1000000000;
  }
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2; j++) {
      int x = s[i] - '0';
      x += j;
      for (int a = 0; a < 10; a++) {
        int ni = i + 1, nj = 0;
        int b = a - x;
        if (b < 0) {
          nj = 1;
          b += 10;
        }
        dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b);
      }
    }
  }
  cout << min(dp[n][0], dp[n][1] + 1) << endl;
}

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

int n, dp[1000005][2];
int main() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  s += "0";
  n = s.length();

  for (int i = 0; i <= n; i++) {
    dp[i][0] = dp[i][1] = 1000000000;
  }
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2; j++) {
      int x = s[i] - '0';
      x += j;
      for (int a = 0; a < 10; a++) {
        int ni = i + 1, nj = 0;
        int b = a - x;
        if (b < 0) {
          nj = 1;
          b += 10;
        }
        dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b);
      }
    }
  }
  cout << min(dp[n][0], dp[n][1] + 1) << endl;
}
Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dbg(x) cerr << #x << ": " << x << endl
#define all(x) (x).begin(), (x).end()

int dp[100000][2];
const int INF = 1000000005;
int main() {
  string s;
  cin >> s;
  reverse(all(s));
  s += '0';
  int m = s.size();
  rep(i, m + 1) rep(j, 2) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, m) rep(j, 2) {
    int n = s[i] - '0';
    n += j;
    rep(a, 10) {
      int ni = i + 1, nj = (a - n < 0);
      int b = a - n >= 0 ? a - n : 10 + a - n;
      dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b);
    }
  }
  cout << dp[m][0] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define dbg(x) cerr << #x << ": " << x << endl
#define all(x) (x).begin(), (x).end()

int dp[1000100][2];
const int INF = 1000000005;
int main() {
  string s;
  cin >> s;
  reverse(all(s));
  s += '0';
  int m = s.size();
  rep(i, m + 1) rep(j, 2) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, m) rep(j, 2) {
    int n = s[i] - '0';
    n += j;
    rep(a, 10) {
      int ni = i + 1, nj = (a - n < 0);
      int b = a - n >= 0 ? a - n : 10 + a - n;
      dp[ni][nj] = min(dp[ni][nj], dp[i][j] + a + b);
    }
  }
  cout << dp[m][0] << endl;
  return 0;
}