Problem p02647 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#pragma GCC optimize "Ofast,omit-frame-pointer,inline"
using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VP vector<pair<int, int>>
#define VPP vector<pair<int, pair<int, int>>>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i, a) for (auto &i : a)
typedef pair<int, int> P;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1 << 29;
const ll INFL = 1LL << 60;
const ll mod = 998244353;

int main() {

  int n, k;
  cin >> n >> k;

  VI v(n);
  REP(i, n) cin >> v[i];

  REP(i, k) {
    VI cs(n + 1, 0);
    REP(i, n) {
      cs[max(0, i - v[i])]++;
      cs[min(n, i + v[i] + 1)]--;
    }
    VI s(n);
    bool flag = false;
    s[0] = cs[0];
    if (s[0] != n)
      flag = true;

    FOR(i, 1, n) {
      s[i] = s[i - 1] + cs[i];
      if (s[i] != n)
        flag = true;
    }
    if (!flag) {
      REP(i, n) cout << s[i] << ' ';
      return 0;
    }
    v = s;
  }

  REP(i, n) cout << v[i] << ' ';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize "Ofast,omit-frame-pointer,inline"
using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VP vector<pair<int, int>>
#define VPP vector<pair<int, pair<int, int>>>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i, a) for (auto &i : a)
typedef pair<int, int> P;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1 << 29;
const ll INFL = 1LL << 60;
const ll mod = 998244353;

int main() {

  int n, k;
  cin >> n >> k;

  VI v(n);
  REP(i, n) cin >> v[i];

  REP(i, k) {
    VI cs(n + 1, 0);
    REP(i, n) {
      cs[max(0, i - v[i])]++;
      cs[min(n, i + v[i] + 1)]--;
    }
    VI s(n);
    bool flag = true;
    s[0] = cs[0];
    if (s[0] != n)
      flag = true;

    FOR(i, 1, n) {
      s[i] = s[i - 1] + cs[i];
      if (s[i] != n)
        flag = true;
    }
    if (!flag) {
      REP(i, n) cout << v[i] << ' ';
      return 0;
    }
    v = s;
  }

  REP(i, n) cout << v[i] << ' ';
}