Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

/*
using PAIR = pair<string, int>;
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long ll;

#define fi first
#define se second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define debugA() cerr << "AAAAA" << endl
#define debug_() cerr << "-------------" << endl
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                                                           \
  for (int i = 0; i < v.size(); i++) {                                         \
    cout << v[i] << " ";                                                       \
  }                                                                            \
  cout << endl;
#define debug_vec2(v)                                                          \
  for (int i = 0; i < v.size(); i++) {                                         \
    for (int j = 0; j < v[i].size(); j++) {                                    \
      cout << v[i][j] << " ";                                                  \
    }                                                                          \
    cout << endl;                                                              \
  }

using Graph = vector<vector<int>>;
using P = pair<int, int>;
using P1 = pair<pair<int, int>,
                int>; // クラスカル法とかで、{cost, {from, to}}的に使う。
const int INF = 1001001001;
const double pi = acos(-1);

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  vector<ll> SUM(n + 10, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    SUM[max(0ll, i - a[i])]++;
    SUM[min(n, i + a[i] + 1)]--;
  }
  for (int i = 0; i < n; i++) {
    SUM[i + 1] += SUM[i];
  }
  int rest = 0;
  for (ll i = 0; i < k - 1; i++) {
    ll MIN = 1001001001;
    vector<ll> SUM1(n + 10, 0);
    for (ll j = 0; j < n; j++) {
      SUM1[max(0ll, j - SUM[j])]++;
      SUM1[min(n, j + SUM[j] + 1)]--;
    }
    for (int j = 0; j < n; j++) {
      SUM1[j + 1] += SUM1[j];
      MIN = min(MIN, SUM[j + 1]);
    }
    if (MIN >= n) {
      rest = k - 1 - i;
      break;
    }
    swap(SUM, SUM1);
  }
  for (int i = 0; i < n; i++) {
    SUM[i] += n * rest;
  }
  for (int i = 0; i < n; i++) {
    cout << SUM[i] << " ";
  }
  cout << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

/*
using PAIR = pair<string, int>;
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long ll;

#define fi first
#define se second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define debugA() cerr << "AAAAA" << endl
#define debug_() cerr << "-------------" << endl
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                                                           \
  for (int i = 0; i < v.size(); i++) {                                         \
    cout << v[i] << " ";                                                       \
  }                                                                            \
  cout << endl;
#define debug_vec2(v)                                                          \
  for (int i = 0; i < v.size(); i++) {                                         \
    for (int j = 0; j < v[i].size(); j++) {                                    \
      cout << v[i][j] << " ";                                                  \
    }                                                                          \
    cout << endl;                                                              \
  }

using Graph = vector<vector<int>>;
using P = pair<int, int>;
using P1 = pair<pair<int, int>,
                int>; // クラスカル法とかで、{cost, {from, to}}的に使う。
const int INF = 1001001001;
const double pi = acos(-1);

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  vector<ll> SUM(n + 10, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    SUM[max(0ll, i - a[i])]++;
    SUM[min(n, i + a[i] + 1)]--;
  }
  for (int i = 0; i < n; i++) {
    SUM[i + 1] += SUM[i];
  }
  int rest = 0;
  for (ll i = 0; i < min(100ll, k - 1); i++) {
    ll MIN = 1001001001;
    vector<ll> SUM1(n + 10, 0);
    for (ll j = 0; j < n; j++) {
      SUM1[max(0ll, j - SUM[j])]++;
      SUM1[min(n, j + SUM[j] + 1)]--;
    }
    for (int j = 0; j < n; j++) {
      SUM1[j + 1] += SUM1[j];
      MIN = min(MIN, SUM[j + 1]);
    }
    if (MIN >= n) {
      rest = k - 1 - i;
      break;
    }
    swap(SUM, SUM1);
  }
  for (int i = 0; i < n; i++) {
    SUM[i] += n * rest;
  }
  for (int i = 0; i < n; i++) {
    cout << SUM[i] << " ";
  }
  cout << endl;

  return 0;
}