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

/**
 *    created: 13.06.2020 21:27:52
 **/

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int inf = 1000000000;
/*
int main() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    rep(i, n){
        cin >> a[i];
    }
    //k = min(k, 2 + (int)log2(n));
    for (int l = 1; l <= k;l++)
    {
        vector<int> b(n);
        rep(i,n){
        b[min(i + a[i]+1, n)] --;
        b[max(0, i - a[i])] ++;
        }
        bool ok = 1;
        for (int i = 1; i < n; i++)
        {
            b[i] += b[i - 1];
            ok &= b[i] == n;
            a[i] = b[i];
            }
            a[0] = b[0];
            if (ok)    break;
            /*
        rep(i,n)cout <<"b "<< b[i] << endl;
        k = min(k, 2 + (int)log2(n));
        for (int i = 1; i < k; i++)
        {
            vector<int> e(n);
            rep(i,n){
                e[i] = b[i];
            }
            rep(i, n)
            {
                //cout << "i  "<<i << endl;
                int x = c[i];
                int y = d[i];
                c[i] = min(i + e[i], n);
                d[i] = max(0, i - e[i]);
                for (int j = d[i]; j < y; j++){
                    b[j]++;
                // cout << "j,b[j] "<<j<<" "<<b[j] << endl;
                }
                for (int j = x+1; j <= c[i]; j++){
                    b[j]++;
                }
            }
        }
    }
    rep(i,n)cout << a[i] <<" ";
    cout << endl;
}
*/
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) cin >> A[i];
  rep(t, K) {
    vector<int> imos(N + 1);
    rep(i, N) {
      imos[max(i - A[i], 0)]++;
      imos[min(i + A[i] + 1, N)]--;
    }
    rep(i, N) imos[i + 1] += imos[i];
    imos.pop_back();
    if (A == imos)
      break;
    A = imos;
  }
  rep(i, N) cout << A[i] << ' ';
  cout << '\\n';
}

Error: WA

Fixed submission:

/**
 *    created: 13.06.2020 21:27:52
 **/

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int inf = 1000000000;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  // k = min(k, 2 + (int)log2(n));
  for (int l = 1; l <= k; l++) {
    vector<int> b(n);
    rep(i, n) {
      b[min(i + a[i] + 1, n)]--;
      b[max(0, i - a[i])]++;
    }
    bool ok = 1;
    for (int i = 1; i < n; i++) {
      b[i] += b[i - 1];
      ok &= b[i] == n;
      a[i] = b[i];
    }
    a[0] = b[0];
    if (ok)
      break;
    /*
rep(i,n)cout <<"b "<< b[i] << endl;
k = min(k, 2 + (int)log2(n));
for (int i = 1; i < k; i++)
{
    vector<int> e(n);
    rep(i,n){
        e[i] = b[i];
    }
    rep(i, n)
    {
        //cout << "i  "<<i << endl;
        int x = c[i];
        int y = d[i];
        c[i] = min(i + e[i], n);
        d[i] = max(0, i - e[i]);
        for (int j = d[i]; j < y; j++){
            b[j]++;
        // cout << "j,b[j] "<<j<<" "<<b[j] << endl;
        }
        for (int j = x+1; j <= c[i]; j++){
            b[j]++;
        }
    }
}*/
  }
  rep(i, n) cout << a[i] << " ";
  cout << endl;
}