Problem p02618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define MOD 1000000007
#define f(i, n) for (int i = 0; i < int(n); i++)
#define N 10500
int a[N][27];
int a2[N][27];
int e[N][365];
int v[N][365];
int sz, sz2;

int main() {
  int n;
  int x, y;
  vector<pair<int, pair<int, int>>> b;
  int c[26];
  int d[365][26];
  bool dd[365][26];
  vector<int> ds;
  int ans[365];
  scanf("%d", &n);
  f(i, 26) scanf("%d", &c[i]);
  f(i, n) {
    f(j, 26) scanf("%d", &d[i][j]);
    ds.clear();
    f(j, 26) ds.push_back(-d[i][j]);
    sort(ds.begin(), ds.end());
    f(j, 26) {
      if ((d[i][j] + ds[5]) > 0)
        dd[i][j] = true;
      else
        dd[i][j] = false;
    }
  }
  sz = 1;
  f(i, 26) a[0][i] = -1;
  a[0][26] = 0;
  f(t, n) {
    b.clear();
    f(i, sz) {
      x = a[i][26];
      f(j, 26) { x -= ((t - a[i][j]) * c[j]); }
      f(j, 26) {
        if (dd[t][j]) {
          y = x + d[t][j];
          y += ((t - a[i][j]) * c[j]);
          b.push_back(make_pair(y, make_pair(i, j)));
        }
      }
    }
    sort(b.begin(), b.end(), greater<pair<int, pair<int, int>>>());
    sz2 = b.size();
    if (sz2 > N)
      sz2 = N;
    f(i, sz2) {
      x = b[i].second.first;
      y = b[i].second.second;
      a2[i][26] = b[i].first;
      f(j, 26) a2[i][j] = a[x][j];
      a2[i][y] = t;
      e[i][t] = x;
      v[i][t] = y;
    }
    f(i, sz2) { f(j, 27) a[i][j] = a2[i][j]; }
    sz = sz2;
  }
  x = 0;
  for (int i = n - 1; i >= 0; i--) {
    ans[i] = v[x][i];
    x = e[x][i];
  }
  f(i, n) printf("%d\\n", ans[i] + 1);

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define MOD 1000000007
#define f(i, n) for (int i = 0; i < int(n); i++)
#define N 11500
int a[N][27];
int a2[N][27];
int e[N][365];
int v[N][365];
int sz, sz2;

int main() {
  int n;
  int x, y;
  vector<pair<int, pair<int, int>>> b;
  int c[26];
  int d[365][26];
  bool dd[365][26];
  vector<int> ds;
  int ans[365];
  scanf("%d", &n);
  f(i, 26) scanf("%d", &c[i]);
  f(i, n) {
    f(j, 26) scanf("%d", &d[i][j]);
    ds.clear();
    f(j, 26) ds.push_back(-d[i][j]);
    sort(ds.begin(), ds.end());
    f(j, 26) {
      if ((d[i][j] + ds[5]) > 0)
        dd[i][j] = true;
      else
        dd[i][j] = false;
    }
  }
  sz = 1;
  f(i, 26) a[0][i] = -1;
  a[0][26] = 0;
  f(t, n) {
    b.clear();
    f(i, sz) {
      x = a[i][26];
      f(j, 26) { x -= ((t - a[i][j]) * c[j]); }
      f(j, 26) {
        if (dd[t][j]) {
          y = x + d[t][j];
          y += ((t - a[i][j]) * c[j]);
          b.push_back(make_pair(y, make_pair(i, j)));
        }
      }
    }
    sort(b.begin(), b.end(), greater<pair<int, pair<int, int>>>());
    sz2 = b.size();
    if (sz2 > N)
      sz2 = N;
    f(i, sz2) {
      x = b[i].second.first;
      y = b[i].second.second;
      a2[i][26] = b[i].first;
      f(j, 26) a2[i][j] = a[x][j];
      a2[i][y] = t;
      e[i][t] = x;
      v[i][t] = y;
    }
    f(i, sz2) { f(j, 27) a[i][j] = a2[i][j]; }
    sz = sz2;
  }
  x = 0;
  for (int i = n - 1; i >= 0; i--) {
    ans[i] = v[x][i];
    x = e[x][i];
  }
  f(i, n) printf("%d\\n", ans[i] + 1);

  return 0;
}