Problem p03561 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define forn(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define ford(i, a, n) for (int i = (int)(n)-1; i >= (int)(a); --i)
#define fore(i, a, n) for (int i = (int)(a); i <= (int)(n); ++i)
#define all(a) (a).begin(), (a).end()
#define fs first
#define sn second
#define trace(a)                                                               \
  for (auto i : a)                                                             \
    cerr << i << ' ';                                                          \
  cerr << '\n'
#define eb emplace_back

#ifndef M_PI
const ld M_PI = acos(-1.0);
#endif

template <typename T> inline void setmax(T &a, T b) {
  if (a < b)
    a = b;
}

template <typename T> inline void setmin(T &a, T b) {
  if (a > b)
    a = b;
}

const ld eps = 1e-9;
const int INF = 2000000000;
const ll LINF = 1ll * INF * INF;
const ll MOD = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  srand((unsigned)chrono::high_resolution_clock::now()
            .time_since_epoch()
            .count());
  int n, k;
  cin >> k >> n;
  if (k % 2 == 0) {
    cout << k / 2 << ' ';
    forn(i, 1, n) cout << k << ' ';
  } else {
    if (n % 2) {
      cout << (k + 1) / 2 << ' ';
      --n;
    }
    int cnt = n / 2;
    vector<int> ans(n, (k + 1) / 2);
    while (cnt--) {
      int last = n - 1;
      while (ans[last] == 0)
        --last;
      if (ans[last] >= 2) {
        --ans[last];
        forn(j, last + 1, n) ans[j] = k;
      } else {
        ans[last] = 0;
      }
    }
    for (int x : ans) {
      if (x == 0)
        break;
      cout << x << ' ';
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define forn(i, a, n) for (int i = (int)(a); i < (int)(n); ++i)
#define ford(i, a, n) for (int i = (int)(n)-1; i >= (int)(a); --i)
#define fore(i, a, n) for (int i = (int)(a); i <= (int)(n); ++i)
#define all(a) (a).begin(), (a).end()
#define fs first
#define sn second
#define trace(a)                                                               \
  for (auto i : a)                                                             \
    cerr << i << ' ';                                                          \
  cerr << '\n'
#define eb emplace_back

#ifndef M_PI
const ld M_PI = acos(-1.0);
#endif

template <typename T> inline void setmax(T &a, T b) {
  if (a < b)
    a = b;
}

template <typename T> inline void setmin(T &a, T b) {
  if (a > b)
    a = b;
}

const ld eps = 1e-9;
const int INF = 2000000000;
const ll LINF = 1ll * INF * INF;
const ll MOD = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  srand((unsigned)chrono::high_resolution_clock::now()
            .time_since_epoch()
            .count());
  int n, k;
  cin >> k >> n;
  if (k == 1) {
    forn(i, 0, (n + 1) / 2) cout << 1 << ' ';
    return 0;
  }
  if (k % 2 == 0) {
    cout << k / 2 << ' ';
    forn(i, 1, n) cout << k << ' ';
  } else {
    if (n % 2) {
      cout << (k + 1) / 2 << ' ';
      --n;
    }
    int cnt = n / 2;
    vector<int> ans(n, (k + 1) / 2);
    while (cnt--) {
      int last = n - 1;
      while (ans[last] == 0)
        --last;
      if (ans[last] >= 2) {
        --ans[last];
        forn(j, last + 1, n) ans[j] = k;
      } else {
        ans[last] = 0;
      }
    }
    for (int x : ans) {
      if (x == 0)
        break;
      cout << x << ' ';
    }
  }
}