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

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define fw(p) for (int w = 0; w < (p); w++)
#define fx(p) for (int x = 0; x < (p); x++)
#define fy(p) for (int y = 0; y < (p); y++)
#define fz(p) for (int z = 0; z < (p); z++)
#define fyg(p, g) for (int y = (g); y < (p); y++)
#define fzg(p, g) for (int z = (g); z < (p); z++)
#define ce(d) cout << d << endl;
#define vecp(p)                                                                \\
  int aa;                                                                      \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecpl(p)                                                               \\
  long long aa;                                                                \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecps(p)                                                               \\
  string aa;                                                                   \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecp2(p)                                                               \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecpl2(p)                                                              \\
  long long a b;                                                               \\
  cin >> ab;                                                                   \\
  (p).push_back(ab);
#define vecps2(p)                                                              \\
  string ab;                                                                   \\
  cin >> ab;                                                                   \\
  (p).push_back(ab);
#define sorts(c) sort((c).begin(), (c).end());
#define reverses(c) reverse((c).begin(), (c).end());
#define vec(b) vector<int>(b);
#define vecl(b) vector<long long>(b);
#define vecs(b) vector<string>(b);
#define pb(b, a) (b).push_back((a));
#define doublece(a, b) cout << (a) << ' ' << (b) << endl;
#define pairs(s) vector<pair<int, int>>(s);
#define pairsp(s)                                                              \\
  int aa, bb;                                                                  \\
  cin >> aa >> bb;                                                             \\
  (s).push_back(make_pair(aa, bb));
#define MOD 1000000007
#define cey ce("Yes")
#define cen ce("No")
#define ceY ce("YES")
#define ceN ce("NO")
int table[365][26];
int main() {
  int D;
  cin >> D;
  vec(C);
  vec(Las);
  fx(26) {
    vecp(C);
    Las.push_back(0);
  }

  fx(D) {
    fy(26) { cin >> table[x][y]; }
  }

  vec(T);
  srand((time(NULL)));
  fx(D) { pb(T, rand() % 26 + 1); }
  ll sum = 0;
  vector<int> S(D, 0);

  int preDay = 0;
  fx(500) {
    int d;

    int dif = INT_MAX, ind = 0;
    fw(D - 1) {
      if (S[w + 1] - S[w] < dif) {
        ind = w + 1;
        dif = S[w + 1] - S[w];
      }
    }
    d = ind;
    if (d == preDay) {
      d = (d + 1) % D + 1;
    }
    preDay = d;

    for (int q = 1; q <= 26; q++) {
      int pre = T[d - 1];
      if (q == pre) {
        continue;
      }
      T[d - 1] = q;
      ll ans = 0;

      fy(26) { Las[y] = 0; }
      fw(D) {

        ans += table[w][T[w] - 1];
        Las[T[w] - 1] = w + 1;
        fz(26) { ans -= C[z] * (w + 1 - Las[z]); }
        S[w] = ans;
      }
      if (ans > sum) {
        sum = ans;
      } else {
        T[d - 1] = pre;
      }
    }
  }

  fx(D) { ce(T[x]) }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define fw(p) for (int w = 0; w < (p); w++)
#define fx(p) for (int x = 0; x < (p); x++)
#define fy(p) for (int y = 0; y < (p); y++)
#define fz(p) for (int z = 0; z < (p); z++)
#define fyg(p, g) for (int y = (g); y < (p); y++)
#define fzg(p, g) for (int z = (g); z < (p); z++)
#define ce(d) cout << d << endl;
#define vecp(p)                                                                \\
  int aa;                                                                      \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecpl(p)                                                               \\
  long long aa;                                                                \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecps(p)                                                               \\
  string aa;                                                                   \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecp2(p)                                                               \\
  cin >> aa;                                                                   \\
  (p).push_back(aa);
#define vecpl2(p)                                                              \\
  long long a b;                                                               \\
  cin >> ab;                                                                   \\
  (p).push_back(ab);
#define vecps2(p)                                                              \\
  string ab;                                                                   \\
  cin >> ab;                                                                   \\
  (p).push_back(ab);
#define sorts(c) sort((c).begin(), (c).end());
#define reverses(c) reverse((c).begin(), (c).end());
#define vec(b) vector<int>(b);
#define vecl(b) vector<long long>(b);
#define vecs(b) vector<string>(b);
#define pb(b, a) (b).push_back((a));
#define doublece(a, b) cout << (a) << ' ' << (b) << endl;
#define pairs(s) vector<pair<int, int>>(s);
#define pairsp(s)                                                              \\
  int aa, bb;                                                                  \\
  cin >> aa >> bb;                                                             \\
  (s).push_back(make_pair(aa, bb));
#define MOD 1000000007
#define cey ce("Yes")
#define cen ce("No")
#define ceY ce("YES")
#define ceN ce("NO")
int table[365][26];
int main() {
  int D;
  cin >> D;
  vec(C);
  vec(Las);
  fx(26) {
    vecp(C);
    Las.push_back(0);
  }

  fx(D) {
    fy(26) { cin >> table[x][y]; }
  }

  vec(T);
  srand((time(NULL)));
  fx(D) { pb(T, rand() % 26 + 1); }
  ll sum = 0;
  vector<int> S(D);

  int preDay = 0;
  fx(10000) {
    int d;

    int dif = INT_MAX, ind = 0;
    fw(D - 1) {
      if (S[w + 1] - S[w] < dif) {
        ind = w + 1;
        dif = S[w + 1] - S[w];
      }
    }
    d = ind;
    if (d == preDay) {
      d = (d + 1) % D + 1;
    }
    preDay = d;

    for (int q = 1; q <= 26; q++) {
      int pre = T[d - 1];
      if (q == pre) {
        continue;
      }
      T[d - 1] = q;
      ll ans = 0;

      fy(26) { Las[y] = 0; }
      fw(D) {

        ans += table[w][T[w] - 1];
        Las[T[w] - 1] = w + 1;
        fz(26) { ans -= C[z] * (w + 1 - Las[z]); }
        S[w] = ans;
      }
      if (ans > sum) {
        sum = ans;
      } else {
        T[d - 1] = pre;
      }
    }
  }

  fx(D) { ce(T[x]) }
  return 0;
}