Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#pragma region cp -helper
#include <bits/stdc++.h>
using namespace std;
#define AC                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define int long long
#define ll long long
#define ull unsigned long long
#define ii pair<int, int>
#define lll pair<ll, ll>
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vll vector<lll>
#define vvl vector<vl>
#define vii vector<ii>
#define all(a) a.begin(), a.end()
#define qsort(a) sort(all(a))
#define qsortd(a) sort(all(a), greater<>())
#define qsortf(a, f) sort(all(a), f)
#define pb(n) push_back(n)
#define eb(n) emplace_back(n)
#define pp(a, b) emplace_back(a, b)
#define umap unordered_map
#define uset unordered_set
#define nl '\n'
#define fileio(in, out)                                                        \
  freopen(in, "r", stdin);                                                     \
  freopen(out, "w", stdout)
#define qmod % mod
#define pls signed
#define give main()
const int mod = 1000000007;
#pragma endregion

const int N = 200005;
string a;
int ans, n, dp[N][2];

int f(int i, int carry) {
  int digit = ((a[i] - '0')) + carry;
  if (i == 0)
    return digit;
  if (dp[i][carry])
    return dp[i][carry];
  return dp[i][carry] = (min(f(i - 1, 1) + (10 - digit), f(i - 1, 0) + digit));
}

pls give {
  AC;
  cin >> a;
  n = a.length();
  a = '0' + a;
  cout << f(n, 0) << nl;
}

Fixed submission:
#pragma region cp -helper
#include <bits/stdc++.h>
using namespace std;
#define AC                                                                     \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define int long long
#define ll long long
#define ull unsigned long long
#define ii pair<int, int>
#define lll pair<ll, ll>
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vll vector<lll>
#define vvl vector<vl>
#define vii vector<ii>
#define all(a) a.begin(), a.end()
#define qsort(a) sort(all(a))
#define qsortd(a) sort(all(a), greater<>())
#define qsortf(a, f) sort(all(a), f)
#define pb(n) push_back(n)
#define eb(n) emplace_back(n)
#define pp(a, b) emplace_back(a, b)
#define umap unordered_map
#define uset unordered_set
#define nl '\n'
#define fileio(in, out)                                                        \
  freopen(in, "r", stdin);                                                     \
  freopen(out, "w", stdout)
#define qmod % mod
#define pls signed
#define give main()
const int mod = 1000000007;
#pragma endregion

const int N = 1000005;
string a;
int ans, n, dp[N][2];

int f(int i, int carry) {
  int digit = ((a[i] - '0')) + carry;
  if (i == 0)
    return digit;
  if (dp[i][carry])
    return dp[i][carry];
  return dp[i][carry] = (min(f(i - 1, 1) + (10 - digit), f(i - 1, 0) + digit));
}

pls give {
  AC;
  cin >> a;
  n = a.length();
  a = '0' + a;
  cout << f(n, 0) << nl;
}