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

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <cmath>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <tuple>
#include <unordered_map>

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define FOR(i, a, done) for (int i = (a); i < (done); ++i)
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define debug(a) cout << (a) << endl
namespace mp = boost::multiprecision;
using cpp_int = mp::cpp_int;
using ll = long long;
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll M = 1000000007;

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<vector<int>> imos(50, vector<int>(n + 2, 0));
  rep(i, n) {
    cin >> a[i];
    int mi = max(i - a[i], 0);
    int ma = min(n - 1, i + a[i]);
    imos[0][mi]++;
    imos[0][ma + 1]--;
  }
  int cur = 0;
  rep(j, min(k, 50)) {
    cur = 0;
    bool fin = true;
    rep(i, n) {
      cur += imos[j][i];
      a[i] = cur;
      int mi = max(i - a[i], 0);
      int ma = min(n - 1, i + a[i]);
      imos[j + 1][mi]++;
      imos[j + 1][ma + 1]--;
      if (a[i] != n) {
        fin = false;
      }
    }
    if (fin) {
      // cout<<j<<endl;
      rep(i, n) {
        cout << n;
        if (i != n - 1) {
          cout << " ";
        } else {
          cout << endl;
        }
      }
      return;
    }
  }
  rep(i, n) {
    cout << a[i];
    if (i != n - 1) {
      cout << " ";
    } else {
      cout << endl;
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <cmath>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <tuple>
#include <unordered_map>

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define FOR(i, a, done) for (int i = (a); i < (done); ++i)
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define debug(a) cout << (a) << endl
namespace mp = boost::multiprecision;
using cpp_int = mp::cpp_int;
using ll = long long;
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

ll M = 1000000007;

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<vector<int>> imos(k + 2, vector<int>(n + 2, 0));
  rep(i, n) {
    cin >> a[i];
    int mi = max(i - a[i], 0);
    int ma = min(n - 1, i + a[i]);
    imos[0][mi]++;
    imos[0][ma + 1]--;
  }
  if ((ll)n * k >= (ll)1e6 or n <= k) {
    rep(i, n) {
      cout << n;
      if (i != n - 1) {
        cout << " ";
      } else {
        cout << endl;
      }
    }
    return;
  }
  int cur = 0;
  rep(j, min(k, 50)) {
    cur = 0;
    bool fin = true;
    rep(i, n) {
      cur += imos[j][i];
      a[i] = cur;
      int mi = max(i - a[i], 0);
      int ma = min(n - 1, i + a[i]);
      imos[j + 1][mi]++;
      imos[j + 1][ma + 1]--;
      if (a[i] != n) {
        fin = false;
      }
    }
    if (fin) {
      // cout<<j<<endl;
      rep(i, n) {
        cout << n;
        if (i != n - 1) {
          cout << " ";
        } else {
          cout << endl;
        }
      }
      return;
    }
  }
  rep(i, n) {
    cout << a[i];
    if (i != n - 1) {
      cout << " ";
    } else {
      cout << endl;
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
  return 0;
}