Problem p03561 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

int main() {
  int n, k;
  scanf("%d %d", &k, &n);
  if (k % 2 == 0) {
    printf("%d ", k / 2);
    for (int i = 1; i < n; i++) {
      printf("%d ", k);
    }
    printf("\n");
  } else if (n % 2 == 0) {
    if (k - n >= 1) {
      for (int i = 0; i < n - 1; i++) {
        printf("%d ", (k + 1) / 2);
      }
      printf("%d ", (k - n + 1) / 2);
      printf("\n");
    } else {
      int a[300001];
      for (int i = 0; i < n - 1; i++) {
        a[i] = (k + 1) / 2;
      }
      a[n - 1] = 0;
      for (int j = 1; j <= (n - k - 1) / 2; j++) {
        int t = 0;
        while (a[n - 1 - t] == 0) {
          t++;
        }
        if (a[n - 1 - t] == 1) {
          a[n - 1 - t] = 0;
        } else {
          a[n - 1 - t]--;
          for (int i = n - t; i < n; i++) {
            a[i] = k;
          }
        }
      }
      for (int i = 0; i < n; i++) {
        if (a[i] > 0)
          printf("%d ", a[i]);
      }
      printf("\n");
    }
  } else {
    if (k - n >= 0) {
      for (int i = 0; i < n - 1; i++) {
        printf("%d ", (k + 1) / 2);
      }
      printf("%d ", (k - n + 2) / 2);
      printf("\n");
    } else {
      int a[300001];
      for (int i = 0; i < n - 1; i++) {
        a[i] = (k + 1) / 2;
      }
      a[n - 1] = 0;
      for (int j = 1; j <= (n - k - 2) / 2; j++) {
        int t = 0;
        while (a[n - 1 - t] == 0) {
          t++;
        }
        if (a[n - 1 - t] == 1) {
          a[n - 1 - t] = 0;
        } else {
          a[n - 1 - t]--;
          for (int i = n - t; i < n; i++) {
            a[i] = k;
          }
        }
      }
      for (int i = 0; i < n; i++) {
        if (a[i] > 0)
          printf("%d ", a[i]);
      }
      printf("\n");
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

int main() {
  int n, k;
  scanf("%d %d", &k, &n);
  if (k % 2 == 0) {
    printf("%d ", k / 2);
    for (int i = 1; i < n; i++) {
      printf("%d ", k);
    }
    printf("\n");
  } else if (k == 1) {
    if (n % 2 == 0) {
      for (int i = 0; i < n / 2; i++) {
        printf("%d ", 1);
      }
    } else {
      for (int i = 0; i < (n + 1) / 2; i++) {
        printf("%d ", 1);
      }
    }
  } else if (n % 2 == 0) {
    if (k - n >= 1) {
      for (int i = 0; i < n - 1; i++) {
        printf("%d ", (k + 1) / 2);
      }
      printf("%d ", (k - n + 1) / 2);
      printf("\n");
    } else {
      int a[300001];
      for (int i = 0; i < n - 1; i++) {
        a[i] = (k + 1) / 2;
      }
      a[n - 1] = 0;
      for (int j = 1; j <= (n - k - 1) / 2; j++) {
        int t = 0;
        while (a[n - 1 - t] == 0) {
          t++;
        }
        if (a[n - 1 - t] == 1) {
          a[n - 1 - t] = 0;
        } else {
          a[n - 1 - t]--;
          for (int i = n - t; i < n; i++) {
            a[i] = k;
          }
        }
      }
      for (int i = 0; i < n; i++) {
        if (a[i] > 0)
          printf("%d ", a[i]);
      }
      printf("\n");
    }
  } else {
    if (k - n >= 0) {
      for (int i = 0; i < n - 1; i++) {
        printf("%d ", (k + 1) / 2);
      }
      printf("%d ", (k - n + 2) / 2);
      printf("\n");
    } else {
      int a[300001];
      for (int i = 0; i < n - 1; i++) {
        a[i] = (k + 1) / 2;
      }
      a[n - 1] = 0;
      for (int j = 1; j <= (n - k - 2) / 2; j++) {
        int t = 0;
        while (a[n - 1 - t] == 0) {
          t++;
        }
        if (a[n - 1 - t] == 1) {
          a[n - 1 - t] = 0;
        } else {
          a[n - 1 - t]--;
          for (int i = n - t; i < n; i++) {
            a[i] = k;
          }
        }
      }
      for (int i = 0; i < n; i++) {
        if (a[i] > 0)
          printf("%d ", a[i]);
      }
      printf("\n");
    }
  }
  return 0;
}