Problem p02647 (C++)

Buggy submission (status: Runtime Error):
// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007
#define NMAX 100000

int N, K, A[NMAX];
int g[NMAX], a[NMAX];

void inpmake() {
  N = NMAX;
  K = NMAX;
  for (int i = 0; i < N; i++) {
    A[i] = 0;
  }
  A[N - 3] = 1;
}
void inp() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
}
void gutyoku() {
  for (int i = 0; i < N; i++) {
    g[i] = A[i];
  }
  for (int i = 0; i < K; i++) {
    int n[NMAX] = {0};
    for (int j = 0; j < N; j++) {
      for (int k = -g[j]; k <= g[j]; k++) {
        if (j + k >= 0 && j + k < N) {
          n[j + k]++;
        }
      }
    }
    for (int j = 0; j < N; j++) {
      g[j] = n[j];
      cout << n[j] << " ";
    }
    cout << endl;
  }
}

void ans() {
  for (int i = 0; i < N; i++) {
    a[i] = A[i];
  }
  for (int i = 0; i < K; i++) {
    int n[NMAX] = {0};
    int sp = 0;
    for (int j = 0; j < N; j++) {
      if (j - a[j] < 0)
        sp++;
      else
        n[j - a[j]]++;
      if (j + a[j] + 1 < N)
        n[j + a[j] + 1]--;
    }
    for (int j = 0; j < N; j++) {
      sp += n[j];
      n[j] = sp;
    }
    bool zf = true;
    bool mf = true;
    for (int j = 0; j < N; j++) {
      a[j] = n[j];
      // cout<<n[j]<<" ";
      if (a[j] != 0)
        zf = false;
      if (a[j] != N)
        mf = false;
    }
    // cout<<endl;
    if (zf || mf)
      break;
    // cout<<i<<endl;
  }
  for (int j = 0; j < N; j++) {
    cout << a[j] << " ";
  }
  cout << endl;
}
signed main() {
  inp();
  ans();
}
/*
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 3 3 2
4 5 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7 7 7 7 8 7 7 7 6 6 5 4 8 9 10 11 12 12 13 13 14 14 14 15 15 15
15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15
15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15
15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 16 15 15 15 15 14 14 14 13 13 12
12 11 10 9 8 16 17 18 19 20 21 22 23 24 24 25 25 26 26 27 27 28 28 28 29 29 29
30 30 30 30 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31
31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 32 31 31 31 31 31 30 30
30 30 29 29 29 28 28 28 27 27 26 26 25 25 24 24 23 22 21 20 19 18 17 16 32 33 34
35 36 37 38 39 40 41 42 43 44 45 46 47 48 48 49 49 50 50 51 51 52 52 53 53 54 54
55 55 56 56 56 57 58 57 58 58 58 59 59 58 59 59 59 59 59 59 59 59 59 59 59 59 58
59 59 58 58 58 57 57 57 56 56 56 55 55 54 54 53 53 52 52 51 51 50 50 49 49 48 48
47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 59 60 61 62 62 63 64 65 65 66 67
68 68 69 70 70 71 72 72 73 74 74 75 76 76 77 78 78 79 80 80 81 82 81 82 82 82 82
82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82
82 81 82 81 80 80 79 78 78 77 76 76 75 74 74 73 72 72 71 70 70 69 68 68 67 66 65
65 64 64 62 62 61 60 59 77 77 78 78 79 80 80 81 81 82 83 83 84 84 85 86 86 87 87
88 89 89 90 90 91 91 92 93 93 94 95 95 95 96 97 97 98 98 99 99 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 99 99 98 98 97
97 96 95 95 94 94 93 93 92 91 91 90 90 89 89 88 87 87 86 86 85 84 84 83 83 82 81
81 80 80 79 78 78 77 77 86 86 87 88 88 89 90 90 91 91 92 93 93 94 95 95 96 96 97
98 98 99 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 99 98 98
97 96 96 95 95 94 93 93 92 91 91 90 90 89 88 88 87 86 86 92 92 93 94 94 95 95 96
97 97 98 98 99 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 99 98 98
97 97 96 95 95 94 94 93 92 92 95 96 97 97 98 98 99 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 99
98 98 97 97 96 95 98 98 99 99 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
99 99 98 98 99 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 99 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100
*/


Fixed submission:
// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007
#define NMAX 200010

int N, K, A[NMAX];
int g[NMAX], a[NMAX];

void inpmake() {
  N = NMAX;
  K = NMAX;
  for (int i = 0; i < N; i++) {
    A[i] = 0;
  }
  A[N - 3] = 1;
}
void inp() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
}
void gutyoku() {
  for (int i = 0; i < N; i++) {
    g[i] = A[i];
  }
  for (int i = 0; i < K; i++) {
    int n[NMAX] = {0};
    for (int j = 0; j < N; j++) {
      for (int k = -g[j]; k <= g[j]; k++) {
        if (j + k >= 0 && j + k < N) {
          n[j + k]++;
        }
      }
    }
    for (int j = 0; j < N; j++) {
      g[j] = n[j];
      cout << n[j] << " ";
    }
    cout << endl;
  }
}

void ans() {
  for (int i = 0; i < N; i++) {
    a[i] = A[i];
  }
  for (int i = 0; i < K; i++) {
    int n[NMAX] = {0};
    int sp = 0;
    for (int j = 0; j < N; j++) {
      if (j - a[j] < 0)
        sp++;
      else
        n[j - a[j]]++;
      if (j + a[j] + 1 < N)
        n[j + a[j] + 1]--;
    }
    for (int j = 0; j < N; j++) {
      sp += n[j];
      n[j] = sp;
    }
    bool zf = true;
    bool mf = true;
    for (int j = 0; j < N; j++) {
      a[j] = n[j];
      // cout<<n[j]<<" ";
      if (a[j] != 0)
        zf = false;
      if (a[j] != N)
        mf = false;
    }
    // cout<<endl;
    if (zf || mf)
      break;
    // cout<<i<<endl;
  }
  for (int j = 0; j < N; j++) {
    cout << a[j] << " ";
  }
  cout << endl;
}
signed main() {
  inp();
  ans();
}
/*
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 3 3 2
4 5 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7 7 7 7 8 7 7 7 6 6 5 4 8 9 10 11 12 12 13 13 14 14 14 15 15 15
15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15
15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15
15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 16 15 15 15 15 14 14 14 13 13 12
12 11 10 9 8 16 17 18 19 20 21 22 23 24 24 25 25 26 26 27 27 28 28 28 29 29 29
30 30 30 30 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31
31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 31 32 31 31 31 31 31 30 30
30 30 29 29 29 28 28 28 27 27 26 26 25 25 24 24 23 22 21 20 19 18 17 16 32 33 34
35 36 37 38 39 40 41 42 43 44 45 46 47 48 48 49 49 50 50 51 51 52 52 53 53 54 54
55 55 56 56 56 57 58 57 58 58 58 59 59 58 59 59 59 59 59 59 59 59 59 59 59 59 58
59 59 58 58 58 57 57 57 56 56 56 55 55 54 54 53 53 52 52 51 51 50 50 49 49 48 48
47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 59 60 61 62 62 63 64 65 65 66 67
68 68 69 70 70 71 72 72 73 74 74 75 76 76 77 78 78 79 80 80 81 82 81 82 82 82 82
82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82
82 81 82 81 80 80 79 78 78 77 76 76 75 74 74 73 72 72 71 70 70 69 68 68 67 66 65
65 64 64 62 62 61 60 59 77 77 78 78 79 80 80 81 81 82 83 83 84 84 85 86 86 87 87
88 89 89 90 90 91 91 92 93 93 94 95 95 95 96 97 97 98 98 99 99 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 99 99 98 98 97
97 96 95 95 94 94 93 93 92 91 91 90 90 89 89 88 87 87 86 86 85 84 84 83 83 82 81
81 80 80 79 78 78 77 77 86 86 87 88 88 89 90 90 91 91 92 93 93 94 95 95 96 96 97
98 98 99 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 99 98 98
97 96 96 95 95 94 93 93 92 91 91 90 90 89 88 88 87 86 86 92 92 93 94 94 95 95 96
97 97 98 98 99 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 99 98 98
97 97 96 95 95 94 94 93 92 92 95 96 97 97 98 98 99 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 99
98 98 97 97 96 95 98 98 99 99 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
99 99 98 98 99 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 99 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
100 100 100
*/