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

#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, n, m) for (int i = m; i < (n); ++i)
#define repl(i, n) for (long long i = 0; i < (n); ++i)
#define reppl(i, n, m) for (long long i = m; i < (n); ++i)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PL = pair<long long, long long>;
using Pxy = pair<double, double>;
using T = tuple<int, int, bool>;
const int INF = 1001001007;
const int mod = 1000000007;
const int MOD = 998244353;
const ll inf = 1e18;
template <typename AT> void printvec(vector<AT> &ar) {
  rep(i, ar.size() - 1) cout << ar[i] << " ";
  cout << ar[ar.size() - 1] << endl;
}
template <typename Q> void printvvec(vector<vector<Q>> &ar) {
  rep(i, ar.size()) {
    rep(j, ar[0].size() - 1) cout << ar[i][j] << " ";
    cout << ar[i][ar[0].size() - 1] << endl;
  }
}
template <typename S> bool range(S a, S b, S x) { return (a <= x && x < b); }

int main() {
  int dd;
  cin >> dd;
  vector<int> ar(26);
  rep(i, 26) cin >> ar[i];
  vector<vector<int>> sc(dd, vector<int>(26));
  rep(i, dd) rep(j, 26) cin >> sc[i][j];
  vector<int> la(26, 0);
  rep(i, (dd - 2) / 3) {
    int ma = -10000000;
    vector<int> ko(3);
    rep(a, 26) rep(b, 26) rep(c, 26) {
      vector<int> nla = la;
      vector<int> im = {a, b, c};
      int ima = 0;
      rep(j, 3) {
        ima += sc[3 * i + j][im[j]];
        nla[im[j]] = 3 * i + j + 1;
        rep(k, 26) { ima -= ar[k] * (3 * i + j + 1 - nla[k]); }
      }
      if (ma < ima) {
        ma = ima;
        ko = im;
      }
    }
    rep(j, 3) {
      la[ko[j]] = 3 * i + j + 1;
      cout << ko[j] + 1 << endl;
    }
  }
  rep(i, 2) {
    int ma = -10000000;
    int as = 0;
    int ima = 0;
    rep(j, 26) { ima += ar[j] * (dd - 1 + i - la[j]); }
    rep(j, 26) {
      int sp = sc[dd - 2 + i][j];
      sp -= ima;
      sp += ar[j] * (dd - 1 + i - la[j]);
      if (ma < sp) {
        ma = sp;
        as = j + 1;
      }
    }
    la[as - 1] = dd - 1 + i;
    cout << as << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, n, m) for (int i = m; i < (n); ++i)
#define repl(i, n) for (long long i = 0; i < (n); ++i)
#define reppl(i, n, m) for (long long i = m; i < (n); ++i)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PL = pair<long long, long long>;
using Pxy = pair<double, double>;
using T = tuple<int, int, bool>;
const int INF = 1001001007;
const int mod = 1000000007;
const int MOD = 998244353;
const ll inf = 1e18;
template <typename AT> void printvec(vector<AT> &ar) {
  rep(i, ar.size() - 1) cout << ar[i] << " ";
  cout << ar[ar.size() - 1] << endl;
}
template <typename Q> void printvvec(vector<vector<Q>> &ar) {
  rep(i, ar.size()) {
    rep(j, ar[0].size() - 1) cout << ar[i][j] << " ";
    cout << ar[i][ar[0].size() - 1] << endl;
  }
}
template <typename S> bool range(S a, S b, S x) { return (a <= x && x < b); }

int main() {
  int dd;
  cin >> dd;
  vector<int> ar(26);
  rep(i, 26) cin >> ar[i];
  vector<vector<int>> sc(dd, vector<int>(26));
  rep(i, dd) rep(j, 26) cin >> sc[i][j];
  vector<int> la(26, 0);
  int ap = -10000;
  int aim = 0;
  rep(i, 26) {
    if (ap < sc[0][i]) {
      ap = sc[0][i];
      aim = i + 1;
    }
  }
  cout << aim << endl;
  la[aim - 1] = 1;
  rep(i, (dd - 1) / 4) {
    int ma = -10000000;
    vector<int> ko(4);
    rep(a, 26) rep(b, 26) rep(c, 26) rep(d, 26) {
      vector<int> nla = la;
      vector<int> im = {a, b, c, d};
      int ima = 0;
      rep(j, 4) {
        ima += sc[4 * i + j + 1][im[j]];
        nla[im[j]] = 4 * i + j + 2;
        rep(k, 26) { ima -= ar[k] * (4 * i + j + 2 - nla[k]); }
      }
      if (ma < ima) {
        ma = ima;
        ko = im;
      }
    }
    rep(j, 4) {
      la[ko[j]] = 4 * i + j + 2;
      cout << ko[j] + 1 << endl;
    }
  }
}