Problem p03561 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define int long long
#define endl '\n'
#define INF 1000000000000000000
#define EPS 1e-10
#define all(a) a.begin(), a.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define fi first
#define se second
#define pb push_back
#define double long double
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
template <class T, class S> bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class S> bool chmin(T &a, const S &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll n, ll m) { return (m ? gcd(m, n % m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  // modcalc();

  int k, n;
  cin >> k >> n;
  if (k % 2 == 0) {
    cout << k / 2 << ' ';
    rep(i, n - 1) { cout << k << ' '; }
    cout << endl;
    return 0;
  } else {
    int m = n / 2;
    vector<int> ans(n, k / 2 + 1);
    rep(i, m) {
      if (ans[n - 1] != 0)
        ans[n - 1]--;
      else {
        int p;
        for (int j = n - 2; j >= 0; j--) {
          if (ans[j] != 0) {
            p = j;
            break;
          }
        }
        int q;
        for (int j = p; j >= 0; j--) {
          if (ans[j] != 1) {
            q = j;
            break;
          }
        }
        bool ok = true;
        if (ans[p] != 1)
          ok = false;
        for (int j = p + 1; j < n; j++) {
          if (ans[j])
            ok = false;
        }
        if (ok)
          ans[p]--;
        else {
          ans[q]--;
          for (int j = q + 1; j < n; j++) {
            ans[j] = k;
          }
        }
      }
    }
    rep(i, n) {
      if (ans[i])
        cout << ans[i] << ' ';
    }
    cout << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define int long long
#define endl '\n'
#define INF 1000000000000000000
#define EPS 1e-10
#define all(a) a.begin(), a.end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define fi first
#define se second
#define pb push_back
#define double long double
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
template <class T, class S> bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class S> bool chmin(T &a, const S &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll n, ll m) { return (m ? gcd(m, n % m) : n); }
ll lcm(ll n, ll m) { return n / gcd(n, m) * m; }

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  // modcalc();

  int k, n;
  cin >> k >> n;
  if (k == 1) {
    n = (n + 1) / 2;
    rep(i, n) { cout << 1 << ' '; }
    cout << endl;
    return 0;
  }
  if (k % 2 == 0) {
    cout << k / 2 << ' ';
    rep(i, n - 1) { cout << k << ' '; }
    cout << endl;
    return 0;
  } else {
    int m = n / 2;
    vector<int> ans(n, k / 2 + 1);
    rep(i, m) {
      if (ans[n - 1] != 0)
        ans[n - 1]--;
      else {
        int p;
        for (int j = n - 2; j >= 0; j--) {
          if (ans[j] != 0) {
            p = j;
            break;
          }
        }
        int q;
        for (int j = p; j >= 0; j--) {
          if (ans[j] != 1) {
            q = j;
            break;
          }
        }
        bool ok = true;
        if (ans[p] != 1)
          ok = false;
        for (int j = p + 1; j < n; j++) {
          if (ans[j])
            ok = false;
        }
        if (ok)
          ans[p]--;
        else {
          ans[q]--;
          for (int j = q + 1; j < n; j++) {
            ans[j] = k;
          }
        }
      }
    }
    rep(i, n) {
      if (ans[i])
        cout << ans[i] << ' ';
    }
    cout << endl;
  }
}