Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
////////////////////////////////////////////////
#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 REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define REPe(e, mp) for (auto &e : mp)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
using ll = long long int;
using vecint = vector<int>;
using vecll = vector<ll>;
using vec2int = vector<vector<int>>;
using P = pair<int, int>;
////////////////////////////////////////////////
////////////////////////////////////////////////
int main() {
  ////////////////////////////////////////////////
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ////////////////////////////////////////////////
  int n, k;
  cin >> n >> k;
  vecint a(n, 0);
  REPe(e, a) cin >> e;
  if (n <= k) {
    REP(i, n) cout << n << " ";
    cout << endl;
    return 0;
  }
  vecint b(n, 0);
  REP(j, k) {
    REP(i, n) b[i] = 0;
    REP(i, n) {
      int l = max(0, i - a[i]);
      int r = i + a[i] + 1;
      b[l]++;
      if (r < n)
        b[r]--;
    }
    a[0] = b[0];
    for (int i = 1; i < n; i++)
      a[i] = a[i - 1] + b[i];
  }
  REPe(e, a) cout << e << " ";
  cout << endl;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
////////////////////////////////////////////////
#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 REPvec(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define REPe(e, mp) for (auto &e : mp)
#define all(x) x.begin(), x.end()
#define MOD 1000000007 // 1e9+7
using namespace std;
using ll = long long int;
using vecint = vector<int>;
using vecll = vector<ll>;
using vec2int = vector<vector<int>>;
using P = pair<int, int>;
////////////////////////////////////////////////
////////////////////////////////////////////////
int main() {
  ////////////////////////////////////////////////
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ////////////////////////////////////////////////
  int n, k;
  cin >> n >> k;
  vecint a(n, 0);
  REPe(e, a) cin >> e;
  if (n < 2 * k) {
    REP(i, n) cout << n << " ";
    cout << endl;
    return 0;
  }
  vecint b(n, 0);
  REP(j, k) {
    REP(i, n) b[i] = 0;
    REP(i, n) {
      int l = max(0, i - a[i]);
      int r = i + a[i] + 1;
      b[l]++;
      if (r < n)
        b[r]--;
    }
    a[0] = b[0];
    for (int i = 1; i < n; i++)
      a[i] = a[i - 1] + b[i];
  }
  REPe(e, a) cout << e << " ";
  cout << endl;
}