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

// 問題の URL を書いておく
//

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>
using namespace std;

// #define ENABLE_PRINT

template <typename T> T Input() {
  T v;
  cin >> v;
  return v;
}

template <typename T> vector<T> InputVector(int n) {
  vector<T> v(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  return v;
}

template <typename T> void InputArray(T *begin, int n) {
  for (int i = 0; i < n; ++i) {
    cin >> begin[i];
  }
}

#if defined(ENABLE_PRINT)

#define Print(v)                                                               \\
  do {                                                                         \\
    cout << #v << ": " << v << endl;                                           \\
  } while (0)

#define PrintVec(v)                                                            \\
  do {                                                                         \\
    for (int __i = 0; __i < v.size(); ++__i) {                                 \\
      cout << #v << "[" << __i << "]: " << v[__i] << endl;                     \\
    }                                                                          \\
  } while (0)

#else

#define Print(v) ((void)0)
#define PrintVec(v) ((void)0)

#endif

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

#if 0
int main(int, const char**)
{
    int n, k;
    cin >> n >> k;
    vector<vector<int>> as(k + 1, vector<int>(n, 0));
    rep(i, n)
    {
        cin >> as[0][i];
    }
    rep(i, k)
    {
        rep(j, n)
        {
            auto a = as[i][j];
            rep(l, a + 1)
            {
                if(l == 0)
                {
                    as[i + 1][j]++;
                    continue;
                }
                auto i0 = j - l;
                auto i1 = j + l;
                if(i0 >= 0)
                {
                    as[i + 1][i0]++;
                }
                if(i1 <= n - 1)
                {
                    as[i + 1][i1]++;
                }
            }
        }
    }
    rep(i, n)
    {
        cout << as[k][i] << " ";
    }
    cout << endl;
    return 0;
}
#endif

int main(int, const char **) {
  int n, k;
  cin >> n >> k;
  k = min(k, 50);
  vector<vector<int>> as(k + 1, vector<int>(n, 0));
  rep(i, n) { cin >> as[0][i]; }
  int ei = 0;
  rep(i, k) {
    rep(j, n) {
      auto a = as[i][j];
      auto pi = max(0, j - a);
      as[i + 1][pi]++;
      auto mi = j + a + 1;
      if (mi <= n - 1) {
        as[i + 1][mi]--;
      }
    }
#if 0        
        rep(j, n)
        {
            cout << as[i + 1][j] << " ";
        }
        cout << endl;
#endif
    for (int j = 1; j < n; ++j) {
      as[i + 1][j] += as[i + 1][j - 1];
    }
    ei = i + 1;
  }
  rep(i, n) { cout << as[ei][i] << " "; }
  cout << endl;
  return 0;
}


Error: WA

Fixed submission:

// 問題の URL を書いておく
//

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>
using namespace std;

// #define ENABLE_PRINT

template <typename T> T Input() {
  T v;
  cin >> v;
  return v;
}

template <typename T> vector<T> InputVector(int n) {
  vector<T> v(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  return v;
}

template <typename T> void InputArray(T *begin, int n) {
  for (int i = 0; i < n; ++i) {
    cin >> begin[i];
  }
}

#if defined(ENABLE_PRINT)

#define Print(v)                                                               \\
  do {                                                                         \\
    cout << #v << ": " << v << endl;                                           \\
  } while (0)

#define PrintVec(v)                                                            \\
  do {                                                                         \\
    for (int __i = 0; __i < v.size(); ++__i) {                                 \\
      cout << #v << "[" << __i << "]: " << v[__i] << endl;                     \\
    }                                                                          \\
  } while (0)

#else

#define Print(v) ((void)0)
#define PrintVec(v) ((void)0)

#endif

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

#if 0
int main(int, const char**)
{
    int n, k;
    cin >> n >> k;
    vector<vector<int>> as(k + 1, vector<int>(n, 0));
    rep(i, n)
    {
        cin >> as[0][i];
    }
    rep(i, k)
    {
        rep(j, n)
        {
            auto a = as[i][j];
            rep(l, a + 1)
            {
                if(l == 0)
                {
                    as[i + 1][j]++;
                    continue;
                }
                auto i0 = j - l;
                auto i1 = j + l;
                if(i0 >= 0)
                {
                    as[i + 1][i0]++;
                }
                if(i1 <= n - 1)
                {
                    as[i + 1][i1]++;
                }
            }
        }
    }
    rep(i, n)
    {
        cout << as[k][i] << " ";
    }
    cout << endl;
    return 0;
}
#endif

int main(int, const char **) {
  int n, k;
  cin >> n >> k;
  vector<vector<int>> as(k + 1, vector<int>(n, 0));
  rep(i, n) { cin >> as[0][i]; }
  int ei = 0;
  rep(i, min(k, 50)) {
    rep(j, n) {
      auto a = as[i][j];
      auto pi = max(0, j - a);
      as[i + 1][pi]++;
      auto mi = j + a + 1;
      if (mi <= n - 1) {
        as[i + 1][mi]--;
      }
    }
#if 0        
        rep(j, n)
        {
            cout << as[i + 1][j] << " ";
        }
        cout << endl;
#endif
    for (int j = 1; j < n; ++j) {
      as[i + 1][j] += as[i + 1][j - 1];
    }
    ei = i + 1;
  }
  rep(i, n) { cout << as[ei][i] << " "; }
  cout << endl;
  return 0;
}