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

#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
int main() {
  const int D = 365, A = 26;
  int z;
  scanf("%d", &z);
  vector<int> C(A);
  for (int i = 0; i < A; i++) {
    scanf("%d", &C[i]);
  }
  vector<vector<int>> S(D, vector<int>(A));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < A; j++) {
      scanf("%d", &S[i][j]);
    }
  }
  vector<int> X(A, 0);
  vector<int> XX(A, 0);
  vector<vector<pair<int, int>>> Q(D, vector<pair<int, int>>(A));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < A; j++) {
      X[j] += C[j];
      XX[j] += X[j];
      Q[i][j] = make_pair(-S[i][j] - X[j], j);
    }
    sort(Q[i].begin(), Q[i].end());
    XX[Q[i][0].second] -= X[Q[i][0].second];
    X[Q[i][0].second] = 0;
  }
  vector<vector<int>> P(D, vector<int>(A));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < A; j++) {
      P[i][j] = Q[i][j].second;
    }
  }
  int SC = 0, SC2;
  for (int i = 0; i < D; i++) {
    SC += S[i][P[i][0]];
  }
  vector<int> Y;
  vector<int> YY;
  int Z;
  int SX, SY;
  z = -1;
  for (int l = 0; l < 169; l++) {
    for (int k = 0; k < 25; k++) {
      while (k == z) {
        k = rand() % 25;
      }
      z = k;
      for (int i = 0; i < D; i++) {
        SC2 = SC;
        YY = XX;
        Y = X;
        SC2 -= S[i][P[i][0]];
        SC2 += S[i][P[i][k + 1]];
        for (int j = 0; j < A; j++) {
          Y[j] = 0;
        }
        YY[P[i][0]] = 0;
        YY[P[i][k + 1]] = 0;
        for (int j = 0; j < D; j++) {
          if (i == j) {
            Z = P[j][k + 1];
          } else {
            Z = P[j][0];
          }
          Y[P[i][0]] += C[P[i][0]];
          Y[P[i][k + 1]] += C[P[i][k + 1]];
          Y[Z] = 0;
          YY[P[i][0]] += Y[P[i][0]];
          YY[P[i][k + 1]] += Y[P[i][k + 1]];
        }
        SX = 0;
        SY = 0;
        for (int j = 0; j < A; j++) {
          SX += XX[j];
          SY += YY[j];
        }
        if ((SC2 - SY + rand() % (int(3500 - 10.0302 * l))) > (SC - SX)) {
          P[i].insert(P[i].begin(), P[i][k + 1]);
          P[i].erase(P[i].begin() + k + 2, P[i].begin() + k + 3);
          XX = YY;
          SC = SC2;
        }
      }
    }
  }
  /*
  int uku=0;
  for(int i=0;i<A;i++){
    X[i]=0;
  }
  for(int i=0;i<D;i++){
    uku+=S[i][P[i][0]];
    for(int j=0;j<A;j++){
      X[j]+=C[j];
    }
    X[P[i][0]]=0;
    SX=0;
    for(int i=0;i<A;i++){
      SX+=X[i];
    }
    uku-=SX;
  }
  cout<<uku<<endl;
  */
  for (int i = 0; i < D; i++) {
    cout << P[i][0] + 1 << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int D, A = 26;
  scanf("%d", &D);
  vector<int> C(A);
  for (int i = 0; i < A; i++) {
    scanf("%d", &C[i]);
  }
  vector<vector<int>> S(D, vector<int>(A));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < A; j++) {
      scanf("%d", &S[i][j]);
    }
  }
  vector<int> X(A, 0);
  vector<int> XX(A, 0);
  vector<vector<pair<int, int>>> Q(D, vector<pair<int, int>>(A));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < A; j++) {
      X[j] += C[j];
      XX[j] += X[j];
      Q[i][j] = make_pair(-S[i][j] - X[j], j);
    }
    sort(Q[i].begin(), Q[i].end());
    XX[Q[i][0].second] -= X[Q[i][0].second];
    X[Q[i][0].second] = 0;
  }
  vector<vector<int>> P(D, vector<int>(A));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < A; j++) {
      P[i][j] = Q[i][j].second;
    }
  }
  int SC = 0, SC2;
  for (int i = 0; i < D; i++) {
    SC += S[i][P[i][0]];
  }
  vector<int> Y;
  vector<int> YY;
  int Z;
  int SX, SY;
  int z = -1;
  for (int l = 0; l < 160; l++) {
    for (int k = 0; k < 25; k++) {
      while (k == z) {
        k = rand() % 25;
      }
      z = k;
      for (int i = 0; i < D; i++) {
        SC2 = SC;
        YY = XX;
        Y = X;
        SC2 -= S[i][P[i][0]];
        SC2 += S[i][P[i][k + 1]];
        for (int j = 0; j < A; j++) {
          Y[j] = 0;
        }
        YY[P[i][0]] = 0;
        YY[P[i][k + 1]] = 0;
        for (int j = 0; j < D; j++) {
          if (i == j) {
            Z = P[j][k + 1];
          } else {
            Z = P[j][0];
          }
          Y[P[i][0]] += C[P[i][0]];
          Y[P[i][k + 1]] += C[P[i][k + 1]];
          Y[Z] = 0;
          YY[P[i][0]] += Y[P[i][0]];
          YY[P[i][k + 1]] += Y[P[i][k + 1]];
        }
        SX = 0;
        SY = 0;
        for (int j = 0; j < A; j++) {
          SX += XX[j];
          SY += YY[j];
        }
        if ((SC2 - SY + rand() % (int(3500 - 10.0302 * l))) > (SC - SX)) {
          P[i].insert(P[i].begin(), P[i][k + 1]);
          P[i].erase(P[i].begin() + k + 2, P[i].begin() + k + 3);
          XX = YY;
          SC = SC2;
        }
      }
    }
  }
  /*
  int uku=0;
  for(int i=0;i<A;i++){
    X[i]=0;
  }
  for(int i=0;i<D;i++){
    uku+=S[i][P[i][0]];
    for(int j=0;j<A;j++){
      X[j]+=C[j];
    }
    X[P[i][0]]=0;
    SX=0;
    for(int i=0;i<A;i++){
      SX+=X[i];
    }
    uku-=SX;
  }
  cout<<uku<<endl;
  */
  for (int i = 0; i < D; i++) {
    cout << P[i][0] + 1 << "\\n";
  }
}