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

#include <algorithm>
#include <cstdio>

int w(int);
int w10(int);
int y(int, int);
int main(void) {
  int c50[8192], c10[8192], c5[8192], c1[8192];
  int i, j, k = 0;
  int s, a50, a10, a5, a1;
  while (true) {
    //??\\???
    scanf("%d", &s);
    if (s == 0)
      break;

    scanf("%d%d%d%d", &a1, &a5, &a10, &a50);

    //  if(s%10!=0){
    //      s = s + 10 - s%10;
    //  }
    //  printf("%d\\n",s);
    s = s / 10;

    //?????????
    if (a1 < w(s)) {
      c1[k] = 0;
    } else {
      c1[k] = w(s) + y((a1 - w(s)), 5) * 5;
    }

    s = s - c1[k];
    if (w(s) != 0) {
      s = s + 5 - w(s);
    }

    //?????????
    if (a5 == 0) {
      c5[k] = 0;
    } else if (w10(s) == 5) {
      c5[k] = 1 + (a5 - 1) / 2 * 2;
    } else {
      c5[k] = a5 / 2 * 2;
    }

    s = s - c5[k] * 5;
    if (w10(s) == 5) {
      s = s + 5;
    }

    s = s / 10;
    // printf("s=%d\\n",s);

    //?????????
    if (a10 < w(s)) {
      c10[k] = 0;
    } else {
      c10[k] = w(s) + y(a10 - w(s), 5) * 5;
    }

    s = s - c10[k];
    if (w(s) != 0) {
      s = s + 5 - w(s);
    }

    // printf("s=%d\\n",s);
    // 50??????
    if (s > 0) {
      c50[k] = y(s, 5);
    }
    k++;
  }

  //??????
  for (i = 0; i < k - 1; i++) {
    if (c1[i] != 0) {
      printf("10 %d\\n", c1[i]);
    }
    if (c5[i] != 0) {
      printf("50 %d\\n", c5[i]);
    }
    if (c10[i] != 0) {
      printf("100 %d\\n", c10[i]);
    }
    if (c50[i] != 0) {
      printf("500 %d\\n", c50[i]);
    }
    printf("\\n");
  }
  if (c1[k - 1] != 0) {
    printf("10 %d\\n", c1[k - 1]);
  }
  if (c5[k - 1] != 0) {
    printf("50 %d\\n", c5[k - 1]);
  }
  if (c10[k - 1] != 0) {
    printf("100 %d\\n", c10[k - 1]);
  }
  if (c50[k - 1] != 0) {
    printf("500 %d\\n", c50[k - 1]);
  }
  return 0;
}

int w(int n) {
  int m;
  if (n > 0) {
    return n % 5;
  } else if (n == 0) {
    return 0;
  } else {
    m = -n;
    m = m % 5;
    if (m == 0) {
      return 0;
    } else {
      return 5 - m;
    }
  }
}
int w10(int n) {
  if (n >= 0) {
    return n % 10;

  } else {
    return (-n) % 10;
  }
}

int y(int a, int b) {
  if (a >= 0) {
    return a / b;
  } else {
    if ((-a) % b == 0) {
      return -((-a) / b);
    } else {
      return -((-a) / b + 1);
    }
  }
}


Error: 0

Fixed submission:

#include <cstdio>

int main(void) {
  int c50[1024], c10[1024], c5[1024], c1[1024];
  int i, j, k = 0;
  int s, a50, a10, a5, a1;
  while (true) {

    //??\\???
    scanf("%d", &s);
    if (s == 0)
      break;

    scanf("%d%d%d%d", &a1, &a5, &a10, &a50);
    s = s / 10;

    //?????????
    if (s % 5 <= a1) {
      c1[k] = s % 5 + (a1 - s % 5) / 5 * 5;
    } else {
      c1[k] = 0;
    }

    s = s - c1[k];

    //?????????
    if (a5 == 0) {
      c5[k] = 0;
    } else if (s % 10 <= 5) {
      c5[k] = 1 + a5 / 2 * 2;
    } else {
      c5[k] = a5 / 2 * 2;
    }

    s = s - c5[k] * 5;

    if (s % 10 == 0) {
      s = s / 10;
    } else {
      s = s / 10 + 1;
    }

    //?????????
    if (s % 5 <= a10) {
      c10[k] = s % 5 + (a10 - s % 5) / 5 * 5;
    } else {
      c10[k] = 0;
    }

    s = s - c10[k];

    // 50??????
    if (s % 5 == 0) {
      c50[k] = s / 5;
    } else {
      c50[k] = s / 5 + 1;
    }

    k++;
  }

  //??????
  for (i = 0; i < k - 1; i++) {
    if (c1[i] != 0) {
      printf("10 %d\\n", c1[i]);
    }
    if (c5[i] != 0) {
      printf("50 %d\\n", c5[i]);
    }
    if (c10[i] != 0) {
      printf("100 %d\\n", c10[i]);
    }
    if (c50[i] != 0) {
      printf("500 %d\\n", c50[i]);
    }
    printf("\\n");
  }
  if (c1[k - 1] != 0) {
    printf("10 %d\\n", c1[k - 1]);
  }
  if (c5[k - 1] != 0) {
    printf("50 %d\\n", c5[k - 1]);
  }
  if (c10[k - 1] != 0) {
    printf("100 %d\\n", c10[k - 1]);
  }
  if (c50[k - 1] != 0) {
    printf("500 %d\\n", c50[k - 1]);
  }
  return 0;
}