Problem p03561 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

#ifdef BTK
#define DEBUG if (1)
#else
#define DEBUG if (0)
#define CIN_ONLY if (1)
struct cww {
  cww() {
    CIN_ONLY {
      ios::sync_with_stdio(false);
      cin.tie(0);
    }
  }
} star;
#endif

#define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second
#define pb push_back
#define REC(ret, ...) std::function<ret(__VA_ARGS__)>
template <typename T> inline bool chmin(T &l, T r) {
  bool a = l > r;
  if (a)
    l = r;
  return a;
}
template <typename T> inline bool chmax(T &l, T r) {
  bool a = l < r;
  if (a)
    l = r;
  return a;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}

constexpr char ln = '\n';
constexpr int mod = 1e9 + 7;
const string ok = "Yes";
const string ng = "No";
void f(int K, vector<int> &v) {
  int id = 0;
  while (v[id] == 0)
    id++;
  if (v[id] == 1) {
    v[id] = 0;
  } else {
    v[id]--;
    REP(i, id) v[i] = K;
  }
}

void solve(int K, int N) {
  if (K % 2 == 0) {
    cout << K / 2;
    REP(i, N - 1) cout << " " << K;
    cout << endl;
  } else {
    int x = N / 2;
    int p = (K + 1) / 2;
    vector<int> ret(N, p);
    REP(i, x) { f(K, ret); }
    reverse(ALL(ret));
    cout << ret[0];
    FOR(i, 1, N) if (ret[i]) cout << " " << ret[i];
    cout << endl;
  }
}
int main() {
  int k, n;
  cin >> k >> n;
  solve(k, n);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

#ifdef BTK
#define DEBUG if (1)
#else
#define DEBUG if (0)
#define CIN_ONLY if (1)
struct cww {
  cww() {
    CIN_ONLY {
      ios::sync_with_stdio(false);
      cin.tie(0);
    }
  }
} star;
#endif

#define FOR(i, bg, ed) for (int i = (bg); i < (ed); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define fi first
#define se second
#define pb push_back
#define REC(ret, ...) std::function<ret(__VA_ARGS__)>
template <typename T> inline bool chmin(T &l, T r) {
  bool a = l > r;
  if (a)
    l = r;
  return a;
}
template <typename T> inline bool chmax(T &l, T r) {
  bool a = l < r;
  if (a)
    l = r;
  return a;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &it : v)
    is >> it;
  return is;
}

constexpr char ln = '\n';
constexpr int mod = 1e9 + 7;
const string ok = "Yes";
const string ng = "No";
void f(int K, vector<int> &v) {
  int id = 0;
  while (v[id] == 0)
    id++;
  if (v[id] == 1) {
    v[id] = 0;
  } else {
    v[id]--;
    REP(i, id) v[i] = K;
  }
}

void solve(int K, int N) {
  if (K % 2 == 0) {
    cout << K / 2;
    REP(i, N - 1) cout << " " << K;
    cout << endl;
  } else {
    int x = N / 2;

    if (K != 1) {
      int p = (K + 1) / 2;
      vector<int> ret(N, p);
      REP(i, x) { f(K, ret); }
      reverse(ALL(ret));
      cout << ret[0];
      FOR(i, 1, N) if (ret[i]) cout << " " << ret[i];
      cout << endl;
    } else {
      int len = N - x;
      REP(i, len - 1) cout << 1 << " ";
      cout << 1 << endl;
    }
  }
}
int main() {
  int k, n;
  cin >> k >> n;
  solve(k, n);
  return 0;
}