Problem p03128 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int INF = 1e9 + 7;
const int maxn = 2e5 + 5;
int a[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

struct note {
  int i, cost;

  bool operator<(const note &x) const { return i > x.i; }
} t[100];

int ans[100], tot, flag;
int temp[100], at;
int n, m, mi = 99, di;
int l1;

void bi() {
  int f = 0;
  if (tot > at) {
    f = 1;
  } else
    for (int q = 0; q < tot; q++) {
      if (ans[q] > temp[q]) {
        f = 1;
        break;
      } else if (ans[q] < temp[q])
        break;
    }

  if (f) {
    for (int q = 0; q < tot; q++)
      temp[q] = ans[q];
    at = tot;
  }
}
void dfs(int yu) {
  if (yu == 0) {
    if (flag) {
      bi();
    } else {
      flag = 1;
      at = tot;
      for (int q = 0; q < tot; q++)
        temp[q] = ans[q];
    }
    return;
  }

  for (int q = 0; q < m; q++) {
    if (yu >= t[q].cost) {
      yu -= t[q].cost;
      ans[tot++] = t[q].i;
      dfs(yu);
      tot--;
      yu += t[q].cost;
    }
  }
}

int main() {
  scanf("%d%d", &n, &m);

  for (int q = 0; q < m; q++) {
    scanf("%d", &t[q].i);
    t[q].cost = a[t[q].i];
    if (mi > t[q].cost) {
      mi = t[q].cost;
      di = t[q].i;
    } else if (mi == t[q].cost) {
      di = max(di, t[q].i);
    }
  }

  sort(t, t + m);
  int len = n / mi;
  int yu = n % mi;
  tot = 0;
  flag = 0;

  if (len >= 30) {
    len -= 30;
    yu += mi * 30;
  } else {
    yu += len * mi;
    len = 0;
  }

  dfs(yu);

  tot = at;
  if (flag) {
    int l = 0;
    for (; l < tot; l++) {
      if (temp[l] > di) {
        printf("%d", temp[l]);
      } else
        break;
    }

    for (int q = 0; q < len; q++)
      printf("%d", di);
    for (; l < tot; l++)
      printf("%d", temp[l]);
  }

  printf("\n");
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int INF = 1e9 + 7;
const int maxn = 2e5 + 5;
int a[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

struct note {
  int i, cost;

  bool operator<(const note &x) const { return i > x.i; }
} t[100];

int ans[100], tot, flag;
int temp[100], at;
int n, m, mi = 99, di;
int l1;

void bi() {
  int f = 0;
  if (tot > at) {
    f = 1;
  } else
    for (int q = 0; q < tot; q++) {
      if (ans[q] > temp[q]) {
        f = 1;
        break;
      } else if (ans[q] < temp[q])
        break;
    }

  if (f) {
    for (int q = 0; q < tot; q++)
      temp[q] = ans[q];
    at = tot;
  }
}
void dfs(int yu) {
  if (yu == 0) {
    if (flag) {
      bi();
    } else {
      flag = 1;
      at = tot;
      for (int q = 0; q < tot; q++)
        temp[q] = ans[q];
    }
    return;
  }

  for (int q = 0; q < m; q++) {
    if (yu >= t[q].cost) {
      yu -= t[q].cost;
      ans[tot++] = t[q].i;
      dfs(yu);
      tot--;
      yu += t[q].cost;
    }
  }
}

int main() {
  scanf("%d%d", &n, &m);

  for (int q = 0; q < m; q++) {
    scanf("%d", &t[q].i);
    t[q].cost = a[t[q].i];
    if (mi > t[q].cost) {
      mi = t[q].cost;
      di = t[q].i;
    } else if (mi == t[q].cost) {
      di = max(di, t[q].i);
    }
  }

  sort(t, t + m);
  int len = n / mi;
  int yu = n % mi;
  tot = 0;
  flag = 0;

  if (len >= 5) {
    len -= 5;
    yu += mi * 5;
  } else {
    yu += len * mi;
    len = 0;
  }

  dfs(yu);

  tot = at;
  if (flag) {
    int l = 0;
    for (; l < tot; l++) {
      if (temp[l] > di) {
        printf("%d", temp[l]);
      } else
        break;
    }

    for (int q = 0; q < len; q++)
      printf("%d", di);
    for (; l < tot; l++)
      printf("%d", temp[l]);
  }

  printf("\n");
}