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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define pal pair<long, long>
#define pad pair<double, double>
#define PB push_back
#define MP make_pair
#define F first
#define S second

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double EPS = 1e-8;

long long pow(long long x, long long n) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return ret;
}

template <typename T, typename U> bool chmax(T &max, const U &x) {
  if (max < x) {
    max = x;
    return true;
  }
  return false;
}

template <typename T, typename U> bool chmin(T &min, const U &x) {
  if (min > x) {
    min = x;
    return true;
  }
  return false;
}
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  bool zf = false;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] != 0) {
      zf = true;
    }
  }

  for (ll j = 0; j < k; j++) {
    vector<ll> sim(n, 0);
    for (ll i = 0; i < n; i++) {
      if (i - a[i] < 0) {
        sim[0]++;
      } else {
        sim[i - a[i]]++;
      }
      if (i + a[i] > n - 2) {
      } else {
        sim[i + a[i] + 1]--;
      }
    }
    bool flg = true;
    a[0] = sim[0];
    if (a[0] != n)
      flg = false;
    for (ll i = 1; i < n; i++) {
      a[i] = a[i - 1] + sim[i];
      if (a[i] != n)
        flg = false;
    }
    if (flg) {
      for (ll i = 0; i < n; i++) {
        cout << n << " ";
      }
      cout << endl;
      return 0;
    }
  }
  for (ll i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define pal pair<long, long>
#define pad pair<double, double>
#define PB push_back
#define MP make_pair
#define F first
#define S second

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double EPS = 1e-8;

long long pow(long long x, long long n) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return ret;
}

template <typename T, typename U> bool chmax(T &max, const U &x) {
  if (max < x) {
    max = x;
    return true;
  }
  return false;
}

template <typename T, typename U> bool chmin(T &min, const U &x) {
  if (min > x) {
    min = x;
    return true;
  }
  return false;
}
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (ll j = 0; j < k; j++) {
    vector<ll> sim(n, 0);
    for (ll i = 0; i < n; i++) {
      if (i - a[i] < 0) {
        sim[0]++;
      } else {
        sim[i - a[i]]++;
      }
      if (i + a[i] > n - 1) {
      } else {
        sim[i + a[i] + 1]--;
      }
    }
    bool flg = true;
    a[0] = sim[0];
    if (a[0] != n)
      flg = false;
    for (ll i = 1; i < n; i++) {
      a[i] = a[i - 1] + sim[i];
      if (a[i] != n)
        flg = false;
    }
    if (flg) {
      for (ll i = 0; i < n; i++) {
        cout << n << " ";
      }
      cout << endl;
      return 0;
    }
  }
  for (ll i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}