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

#include <bits/stdc++.h>
#define REP(i, a, n) for (long long i = (a); i < (long long)(n); ++i)
#define REPC(i, a, n) for (long long i = (a); i <= (long long)(n); ++i)
#define ALL(t) t.begin(), t.end()
#define RALL(t) t.rbegin(), t.rend()
#define MATINIT(type, row, col, init)                                          \\
  vector<vector<type>>(row, vector<type>(col, init));
#define Yes(cond) cout << (cond ? "Yes" : "No") << endl;
#define YES(cond) cout << (cond ? "YES" : "NO") << endl;
using namespace std;
using LL = long long;
using ULL = unsigned long long;
template <class T> using VEC = std::vector<T>;
template <class T> using MAT = std::vector<std::vector<T>>;
void DUMP() { cerr << endl; }
template <class Head, class... Tail> void DUMP(Head &&head, Tail &&...tail) {
  cerr << head << ", ";
  DUMP(std::move(tail)...);
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  os << "{";
  for (auto v : vec)
    os << v << ",";
  os << "}";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &s) {
  os << "{";
  for (auto p : s)
    os << p << ",";
  os << "}";
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, map<T1, T2> &m) {
  os << "{";
  for (auto p : m)
    os << p << ",";
  os << "}";
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
  os << "[" << p.first << " " << p.second << "]";
  return os;
}
VEC<int> ope(VEC<int> const &A) {
  int N = A.size();
  VEC<int> B(N + 1, 0);
  REP(i, 0, N) {
    B[max(0, (int)i - A[i])]++;
    B[min(N, (int)i + A[i] + 1)]--;
    // DUMP(B);
  }

  VEC<int> sum(N, 0);
  sum[0] = B[0];
  REP(i, 1, N) { sum[i] = sum[i - 1] + B[i]; }
  // DUMP("Sum", sum);

  return sum;
}

int main() {
  int N, K;
  cin >> N >> K;
  VEC<int> A(N);
  REP(i, 0, N) cin >> A[i];

  REP(i, 0, K) {
    auto res = ope(A);
    if (A[0] == N) {
      bool all = 1;
      REP(i, 1, N) {
        if (A[i] != N)
          all = 0;
      }
      if (all) {
        REP(i, 0, N) { printf("%d ", A[i]); }
        puts("");
        return 0;
      }
    }

    A = res;
  }

  REP(i, 0, N) { printf("%d ", A[i]); }
  puts("");

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, a, n) for (long long i = (a); i < (long long)(n); ++i)
#define REPC(i, a, n) for (long long i = (a); i <= (long long)(n); ++i)
#define ALL(t) t.begin(), t.end()
#define RALL(t) t.rbegin(), t.rend()
#define MATINIT(type, row, col, init)                                          \\
  vector<vector<type>>(row, vector<type>(col, init));
#define Yes(cond) cout << (cond ? "Yes" : "No") << endl;
#define YES(cond) cout << (cond ? "YES" : "NO") << endl;
using namespace std;
using LL = long long;
using ULL = unsigned long long;
template <class T> using VEC = std::vector<T>;
template <class T> using MAT = std::vector<std::vector<T>>;
void DUMP() { cerr << endl; }
template <class Head, class... Tail> void DUMP(Head &&head, Tail &&...tail) {
  cerr << head << ", ";
  DUMP(std::move(tail)...);
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  os << "{";
  for (auto v : vec)
    os << v << ",";
  os << "}";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &s) {
  os << "{";
  for (auto p : s)
    os << p << ",";
  os << "}";
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, map<T1, T2> &m) {
  os << "{";
  for (auto p : m)
    os << p << ",";
  os << "}";
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
  os << "[" << p.first << " " << p.second << "]";
  return os;
}
VEC<int> ope(VEC<int> &A) {
  int N = A.size();
  VEC<int> B(N + 1, 0);
  REP(i, 0, N) {
    B[max(0, (int)i - A[i])]++;
    B[min(N, (int)i + A[i] + 1)]--;
    // DUMP(B);
  }

  VEC<int> sum(N, 0);
  sum[0] = B[0];
  REP(i, 1, N) { sum[i] = sum[i - 1] + B[i]; }
  // DUMP("Sum", sum);
  return sum;
}

int main() {
  int N, K;
  cin >> N >> K;
  VEC<int> A(N);
  REP(i, 0, N) cin >> A[i];

  REP(i, 0, K) {
    auto res = ope(A);
    A = res;
  }

  REP(i, 0, N) { printf("%d ", A[i]); }
  puts("");

  return 0;
}