Problem p04042 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
#define clr(x, i) memset(x, i, sizeof(x))
using namespace std;
const int N = 2011;
#define pii pair<int, int>
#define x first
#define y second
int n, K, len[N];
bitset<N * 50> ok[N];
string s[N], ans;
pii f[2][N * 5];
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> K;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    len[i] = s[i].size();
  }
  ok[n + 1][0] = 1;
  for (int i = n; i > 0; i--) {
    ok[i] = ok[i + 1] | (ok[i + 1] << len[i]);
    //		cout<<i<<' ';
    //		for(int j=0;j<=K;j++){
    //			ok[i][j]|=ok[i+1][j];
    //			if(j+len[i]<=K)ok[i][j+len[i]]|=ok[i+1][j];
    //			printf("%d ",ok[i][j]);
    //		}
    //		cout<<endl;
  }
  int qcnt = 0, tcnt = 0;
  for (int i = 1; i <= n; i++)
    if (ok[i + 1][K - len[i]])
      f[1][++qcnt] = pii(i, 0);
  for (int t = 1; t <= K; t++) {
    int i = t & 1;
    //		cout<<' '<<i<<endl;
    qcnt = min(qcnt, K);
    //		cout<<qcnt<<"  ";
    char ch = 'z';
    for (int j = 1; j <= qcnt; j++)
      ch = min(
          ch, s[f[i][j].x][f[i][j].y]); // cout<<f[i][j].x<<' '<<f[i][j].y<<" ";
    //		cout<<endl;
    ans.push_back(ch);
    int head = n + 1;
    for (int j = 1; j <= qcnt; j++) {
      if (s[f[i][j].x][f[i][j].y] != ch)
        continue;
      if (f[i][j].y == len[f[i][j].x] - 1)
        head = min(head, f[i][j].x);
      else
        f[i ^ 1][++tcnt] = pii(f[i][j].x, f[i][j].y + 1);
    }
    //		cout<<head<<endl;
    for (int j = head + 1; j <= n; j++)
      if (K - t - len[j] >= 0 && ok[j + 1][K - t - len[j]])
        f[i ^ 1][++tcnt] = pii(j, 0);
    qcnt = tcnt;
    tcnt = 0;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define clr(x, i) memset(x, i, sizeof(x))
using namespace std;
const int N = 2011;
#define pii pair<int, int>
#define x first
#define y second
int n, K, len[N];
bitset<N * 50> ok[N];
string s[N], ans;
pii f[2][N * 50];
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> K;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    len[i] = s[i].size();
  }
  ok[n + 1][0] = 1;
  for (int i = n; i > 0; i--) {
    ok[i] = ok[i + 1] | (ok[i + 1] << len[i]);
    //		cout<<i<<' ';
    //		for(int j=0;j<=K;j++){
    //			ok[i][j]|=ok[i+1][j];
    //			if(j+len[i]<=K)ok[i][j+len[i]]|=ok[i+1][j];
    //			printf("%d ",ok[i][j]);
    //		}
    //		cout<<endl;
  }
  int qcnt = 0, tcnt = 0;
  for (int i = 1; i <= n; i++)
    if (ok[i + 1][K - len[i]])
      f[1][++qcnt] = pii(i, 0);
  for (int t = 1; t <= K; t++) {
    int i = t & 1;
    //		cout<<' '<<i<<endl;
    qcnt = min(qcnt, K);
    //		cout<<qcnt<<"  ";
    char ch = 'z';
    for (int j = 1; j <= qcnt; j++)
      ch = min(
          ch, s[f[i][j].x][f[i][j].y]); // cout<<f[i][j].x<<' '<<f[i][j].y<<" ";
    //		cout<<endl;
    ans.push_back(ch);
    int head = n + 1;
    for (int j = 1; j <= qcnt; j++) {
      if (s[f[i][j].x][f[i][j].y] != ch)
        continue;
      if (f[i][j].y == len[f[i][j].x] - 1)
        head = min(head, f[i][j].x);
      else
        f[i ^ 1][++tcnt] = pii(f[i][j].x, f[i][j].y + 1);
    }
    //		cout<<head<<endl;
    for (int j = head + 1; j <= n; j++)
      if (K - t - len[j] >= 0 && ok[j + 1][K - t - len[j]])
        f[i ^ 1][++tcnt] = pii(j, 0);
    qcnt = tcnt;
    tcnt = 0;
  }
  cout << ans << endl;
  return 0;
}