Problem p02775 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE)                                                   \
  if (TRUE_OR_FALSE) {                                                         \
    cout << "YES" << endl;                                                     \
  } else {                                                                     \
    cout << "NO" << endl;                                                      \
  }
#define IFYes(TRUE_OR_FALSE)                                                   \
  if (TRUE_OR_FALSE) {                                                         \
    cout << "Yes" << endl;                                                     \
  } else {                                                                     \
    cout << "No" << endl;                                                      \
  }
#define IFyes(TRUE_OR_FALSE)                                                   \
  if (TRUE_OR_FALSE) {                                                         \
    cout << "yes" << endl;                                                     \
  } else {                                                                     \
    cout << "no" << endl;                                                      \
  }

const long long int mod = 1000000007;
const long long int INF = 99999999999999999;

long long int N, M, K, A[200050] = {}, pl = 0, zero = 0, mi = 0, tmp = 0,
                       res = 0;
long long int dp[1000020] = {}, dp2[1000020] = {};
vector<long long int> minus_num, plus_num;
string str;
map<string, long long int> mp;
int main() {
  cout << fixed << setprecision(18);
  cin >> str;
  for (int i = 0; i < str.length(); i++) {
    A[str.length() - i - 1] = str[i] - '0';
    dp[i] = INF;
    dp2[i] = INF;
  }
  // dpはより大きなお金を払う状態
  dp[0] = 10 - A[0];
  dp2[0] = A[0];

  for (int i = 1; i < str.length(); i++) {

    dp[i] = min(dp[i - 1] + 9 - A[i], dp2[i - 1] + 10 - A[i]);
    if (A[i] != 9) {
      dp2[i] = min(dp[i - 1] + A[i] + 1, dp2[i - 1] + A[i]);
    } else {
      dp2[i] = dp2[i - 1] + A[i];
    }
    tmp = i;

    // cout<<dp[i]<<" "<<dp2[i]<<endl;
  }

  cout << min(dp[str.length() - 1] + 1, dp2[str.length() - 1]) << endl;
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE)                                                   \
  if (TRUE_OR_FALSE) {                                                         \
    cout << "YES" << endl;                                                     \
  } else {                                                                     \
    cout << "NO" << endl;                                                      \
  }
#define IFYes(TRUE_OR_FALSE)                                                   \
  if (TRUE_OR_FALSE) {                                                         \
    cout << "Yes" << endl;                                                     \
  } else {                                                                     \
    cout << "No" << endl;                                                      \
  }
#define IFyes(TRUE_OR_FALSE)                                                   \
  if (TRUE_OR_FALSE) {                                                         \
    cout << "yes" << endl;                                                     \
  } else {                                                                     \
    cout << "no" << endl;                                                      \
  }

const long long int mod = 1000000007;
const long long int INF = 99999999999999999;

long long int N, M, K, A[1100050] = {}, pl = 0, zero = 0, mi = 0, tmp = 0,
                       res = 0;
long long int dp[1000020] = {}, dp2[1000020] = {};
vector<long long int> minus_num, plus_num;
string str;
map<string, long long int> mp;
int main() {
  cout << fixed << setprecision(18);
  cin >> str;
  for (int i = 0; i < str.length(); i++) {
    A[str.length() - i - 1] = str[i] - '0';
    dp[i] = INF;
    dp2[i] = INF;
  }
  // dpはより大きなお金を払う状態
  dp[0] = 10 - A[0];
  dp2[0] = A[0];

  for (int i = 1; i < str.length(); i++) {

    dp[i] = min(dp[i - 1] + 9 - A[i], dp2[i - 1] + 10 - A[i]);
    if (A[i] != 9) {
      dp2[i] = min(dp[i - 1] + A[i] + 1, dp2[i - 1] + A[i]);
    } else {
      dp2[i] = dp2[i - 1] + A[i];
    }
    tmp = i;

    // cout<<dp[i]<<" "<<dp2[i]<<endl;
  }

  cout << min(dp[str.length() - 1] + 1, dp2[str.length() - 1]) << endl;
}