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

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC optimize("Os")
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);
#define FO cout.tie(NULL);
#define FI cin.tie(NULL);
#define IN cin >>
#define OUT cout <<
#define loop(i, a, n) for (int i = a; i < n; i++)
#define rloop(i, a, n) for (int i = a; i >= n; i--)
#define endl "\\n";
#define pb push_back
#define mp make_pair
#define set_bits(a) __builtin_popcountll(a)
#define ll long long int
#define ld long double
#define vll vector<long long int>
#define pll pair<long long int, long long int>
#define mod 1000000007
#define M 998244353
using namespace std;
ll gcd(ll a, ll b) { return (b ? gcd(b, a % b) : a); }
ll P(ll B, ll power, ll modulo) {
  ll ans = 1LL;
  while (power > 0LL) {
    if (power % 2LL == 1LL) {
      ans = (ans * B) % modulo;
    }
    B = (B * B) % modulo;
    power /= 2LL;
  }
  return ans;
}
bool isPrime(ll n) {
  if (n <= 1LL) {
    return false;
  }
  if (n <= 3LL) {
    return true;
  }
  if (n % 2 == 0LL || n % 3 == 0LL) {
    return false;
  }
  for (ll i = 5LL; (i * i) <= n; i += 6LL) {
    if (n % i == 0LL || n % (i + 2LL) == 0LL) {
      return false;
    }
  }
  return true;
}
void vok() {
  FAST FO FI
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
int main() {
  vok();
  int n, k;
  IN n >> k;
  vll a(n);
  loop(i, 0, n) { IN a[i]; }
  loop(i, 0, k) {
    vll temp(n, 0);
    loop(j, 0, n) {
      int low = j - a[j];
      int high = j + a[j];
      low = max(low, 0);
      high = min(high, n - 1);
      // OUT low<<" "<<high<<endl
      temp[low]++;
      if (high != n - 1) {
        temp[high + 1]--;
      }
    }
    bool flag = true;
    // loop(j,0,n)
    // {
    //     OUT temp[j]<<" ";
    // }
    loop(j, 1, n) {
      temp[j] += temp[j - 1];
      if (temp[j] != temp[j - 1]) {
        flag = false;
      }
    }
    // loop(j,0,n)
    // {
    //     OUT temp[j]<<" ";
    // }
    // OUT endl
    a = temp;
    if (flag) {
      loop(j, 0, n) { a[j] += (k - 1 - i); }
      break;
    }
  }
  loop(i, 0, n) { OUT min(a[i], (ll)n) << " "; }
  OUT endl return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC optimize("Os")
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);
#define FO cout.tie(NULL);
#define FI cin.tie(NULL);
#define IN cin >>
#define OUT cout <<
#define loop(i, a, n) for (int i = a; i < n; i++)
#define rloop(i, a, n) for (int i = a; i >= n; i--)
#define endl "\\n";
#define pb push_back
#define mp make_pair
#define set_bits(a) __builtin_popcountll(a)
#define ll long long int
#define ld long double
#define vll vector<long long int>
#define pll pair<long long int, long long int>
#define mod 1000000007
#define M 998244353
using namespace std;
ll gcd(ll a, ll b) { return (b ? gcd(b, a % b) : a); }
ll P(ll B, ll power, ll modulo) {
  ll ans = 1LL;
  while (power > 0LL) {
    if (power % 2LL == 1LL) {
      ans = (ans * B) % modulo;
    }
    B = (B * B) % modulo;
    power /= 2LL;
  }
  return ans;
}
bool isPrime(ll n) {
  if (n <= 1LL) {
    return false;
  }
  if (n <= 3LL) {
    return true;
  }
  if (n % 2 == 0LL || n % 3 == 0LL) {
    return false;
  }
  for (ll i = 5LL; (i * i) <= n; i += 6LL) {
    if (n % i == 0LL || n % (i + 2LL) == 0LL) {
      return false;
    }
  }
  return true;
}
void vok() {
  FAST FO FI
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}
int main() {
  vok();
  int n, k;
  IN n >> k;
  vll a(n);
  loop(i, 0, n) { IN a[i]; }
  loop(i, 0, k) {
    vll temp(n, 0);
    loop(j, 0, n) {
      int low = j - a[j];
      int high = j + a[j];
      low = max(low, 0);
      high = min(high, n - 1);
      // OUT low<<" "<<high<<endl
      temp[low]++;
      if (high != n - 1) {
        temp[high + 1]--;
      }
    }
    bool flag = true;
    // loop(j,0,n)
    // {
    //     OUT temp[j]<<" ";
    // }
    loop(j, 1, n) {
      temp[j] += temp[j - 1];
      if (temp[j] != temp[j - 1]) {
        flag = false;
      }
    }
    // loop(j,0,n)
    // {
    //     OUT temp[j]<<" ";
    // }
    // OUT endl
    a = temp;
    // if(flag)
    // {
    //     loop(j,0,n)
    //     {
    //         a[j]+=(k-1-i);
    //     }
    //     break;
    // }
  }
  loop(i, 0, n) { OUT a[i] << " "; }
  OUT endl return 0;
}