Problem p01467 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int len;
char A[40];
char B[40];

ll calc(string A, string B, int borrow, int depth, int rest);
ll NextState(string A, string B, int borrow, int depth, int rest) {
  int nborrow = 0;
  int c = A[depth] - B[depth] + '0' - borrow;
  if (c < '0') {
    c += 10;
    nborrow = 1;
  }
  A[depth] = c;
  return calc(A, B, nborrow, depth - 1, rest);
}
ll calc(string A, string B, int borrow, int depth, int rest) {
  if (depth == -1) {
    return atoi(A.c_str());
  }
  ll ret = 0;
  if (rest != 0 && borrow) {
    ret = max(ret, NextState(A, B, 0, depth, rest - 1));
  }
  ret = max(ret, NextState(A, B, borrow, depth, rest));
  return ret;
}

int main() {
  MEMSET(A, '0');
  MEMSET(B, '0');
  int k;
  while (scanf("%s %s %d", A + 20, B + 20, &k) > 0) {
    len = strlen(A + 20);
    printf("%lld\\n",
           calc(A + 20, B + 20 - (len - strlen(B + 20)), 0, len - 1, k));
    MEMSET(A, '0');
    MEMSET(B, '0');
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int len;
char A[30];
char B[30];

ll calc(string A, string B, int borrow, int depth, int rest);
ll NextState(string A, string B, int borrow, int depth, int rest) {
  int nborrow = 0;
  int c = A[depth] - B[depth] + '0' - borrow;
  if (c < '0') {
    c += 10;
    nborrow = 1;
  }
  A[depth] = c;
  return calc(A, B, nborrow, depth - 1, rest);
}
ll calc(string A, string B, int borrow, int depth, int rest) {
  if (depth == -1) {
    return atoi(A.c_str());
  }
  ll ret = 0;
  if (rest != 0 && borrow) {
    ret = max(ret, NextState(A, B, 0, depth, rest - 1));
  }
  ret = max(ret, NextState(A, B, borrow, depth, rest));
  return ret;
}

int main() {
  MEMSET(A, '0');
  MEMSET(B, '0');
  int k;
  while (scanf("%s %s %d", A + 10, B + 10, &k) > 0) {
    len = strlen(A);
    printf("%lld\\n", calc(A + 10, B + 10 - (len - strlen(B)), 0, len - 1, k));
    MEMSET(A, '0');
    MEMSET(B, '0');
  }
}