Problem p03561 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

using std::bitset;
using std::deque;
using std::iterator;
using std::map;
using std::multimap;
using std::multiset;
using std::pair;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::vector;

using std::fill;
using std::ios_base;
using std::max_element;
using std::min_element;
using std::reverse;
using std::sort;
using std::stable_sort;
using std::swap;
using std::unique;

using std::fixed;
using std::setprecision;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
// typedef string S;

ll min(ll a, ll b) { return a < b ? a : b; }
ll min(int a, ll b) { return a < b ? a : b; }
ll min(ll a, int b) { return a < b ? a : b; }
ll min(int a, int b) { return a < b ? a : b; }

ll max(ll a, ll b) { return a > b ? a : b; }
ll max(int a, ll b) { return a > b ? a : b; }
ll max(ll a, int b) { return a > b ? a : b; }
ll max(int a, int b) { return a > b ? a : b; }

namespace MySpace {};

#define F(i, n) for (int(i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()

const int N = 120010;
const long long MOD = 998244353;
const int INF = 1e9;

int n, k;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> k >> n;
  if (k % 2 == 0) {
    cout << k / 2 << " ";
    for (int i = 0; i < n - 1; i++)
      cout << k << " ";
    return 0;
  }
  if (n % 2 == 0) {
    vector<int> kek;
    for (int i = 0; i < n; i++)
      kek.push_back(k / 2 + 1);
    int it = n / 2;
    while (it--) {
      if (kek.back() == 1)
        kek.pop_back();
      else {
        kek[kek.size() - 1]--;
        while (kek.size() < n)
          kek.push_back(k);
      }
    }
    for (auto it : kek)
      cout << it << " ";
    return 0;
  }
  return 1;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

using std::bitset;
using std::deque;
using std::iterator;
using std::map;
using std::multimap;
using std::multiset;
using std::pair;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::vector;

using std::fill;
using std::ios_base;
using std::max_element;
using std::min_element;
using std::reverse;
using std::sort;
using std::stable_sort;
using std::swap;
using std::unique;

using std::fixed;
using std::setprecision;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
// typedef string S;

ll min(ll a, ll b) { return a < b ? a : b; }
ll min(int a, ll b) { return a < b ? a : b; }
ll min(ll a, int b) { return a < b ? a : b; }
ll min(int a, int b) { return a < b ? a : b; }

ll max(ll a, ll b) { return a > b ? a : b; }
ll max(int a, ll b) { return a > b ? a : b; }
ll max(ll a, int b) { return a > b ? a : b; }
ll max(int a, int b) { return a > b ? a : b; }

namespace MySpace {};

#define F(i, n) for (int(i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()

const int N = 120010;
const long long MOD = 998244353;
const int INF = 1e9;

int n, k;

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> k >> n;
  if (k % 2 == 0) {
    cout << k / 2 << " ";
    for (int i = 0; i < n - 1; i++)
      cout << k << " ";
    return 0;
  }

  {
    vector<int> kek;
    for (int i = 0; i < n; i++)
      kek.push_back(k / 2 + 1);
    int it = n / 2;
    while (it--) {
      if (kek.back() == 1)
        kek.pop_back();
      else {
        kek[kek.size() - 1]--;
        while (kek.size() < n)
          kek.push_back(k);
      }
    }
    for (auto it : kek)
      cout << it << " ";
    return 0;
  }
  return 1;
}