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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vin = vector<int>;
using vll = vector<long long>;
using vvin = vector<vector<int>>;
using vvll = vector<vector<long long>>;
using vstr = vector<string>;
using vvstr = vector<vector<string>>;
using vch = vector<char>;
using vvch = vector<vector<char>>;
using vbo = vector<bool>;
using vvbo = vector<vector<bool>>;
using vpii = vector<pair<int, int>>;
using pqsin = priority_queue<int, vector<int>, greater<int>>;
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define decp(n) cout << fixed << setprecision((int)n)
const int inf = 1e9 + 7;
const ll INF = 1e18;

int main() {
  int d;
  cin >> d;
  vin c(26);
  rep(i, 26) cin >> c[i];
  vvin s(d, vin(26));
  rep(i, d) rep(j, 26) cin >> s[i][j];
  vvin last(d, vin(26, 0));
  int sum, tmp;
  ll m;
  vin ans(d);
  rep(i, d) {
    m = -INF;
    tmp = 0;
    if (i > 0)
      rep(j, 26) last[i][j] = last[i - 1][j];
    rep(j, 26) {
      sum = 0;
      sum += s[i][j];
      rep(k, 26) {
        if (k == j)
          continue;
        sum -= c[k] * (i + 1 - last[i][k]);
      }
      if ((ll)sum > m) {
        tmp = j;
        m = (ll)sum;
      }
    }
    ans[i] = tmp;
    last[i][tmp] = i + 1;
  }
  int res, l;
  rep(i, 30) {
    rep(j, d) {
      m = 0;
      rep(k, 26) {
        sum = 0;
        res = ans[j];
        rep2(l, j, d) {
          if (ans[l] == k) {
            sum += c[k] * (j + 1 - last[j][k]) * (l - j);
            break;
          }
        }
        rep2(l, j, d) {
          if (ans[l] == res) {
            sum -= c[res] * (j + 1 - last[j][res]) * (l - j);
          }
        }
        if (m < sum) {
          m = sum;
          tmp = k;
        }
      }
      if (m == 0)
        continue;
      ans[j] = tmp;
      l = last[j][tmp];
      rep2(k, j, d) {
        if (last[k][tmp] != l)
          break;
        last[k][tmp] = j + 1;
      }
    }
  }
  rep(i, d) cout << ans[i] + 1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vin = vector<int>;
using vll = vector<long long>;
using vvin = vector<vector<int>>;
using vvll = vector<vector<long long>>;
using vstr = vector<string>;
using vvstr = vector<vector<string>>;
using vch = vector<char>;
using vvch = vector<vector<char>>;
using vbo = vector<bool>;
using vvbo = vector<vector<bool>>;
using vpii = vector<pair<int, int>>;
using pqsin = priority_queue<int, vector<int>, greater<int>>;
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define decp(n) cout << fixed << setprecision((int)n)
const int inf = 1e9 + 7;
const ll INF = 1e18;

int main() {
  int d;
  cin >> d;
  vin c(26);
  rep(i, 26) cin >> c[i];
  vvin s(d, vin(26));
  rep(i, d) rep(j, 26) cin >> s[i][j];
  vvin last(d, vin(26, 0));
  int sum, tmp;
  ll m;
  vin ans(d);
  rep(i, d) {
    m = -INF;
    tmp = 0;
    if (i > 0)
      rep(j, 26) last[i][j] = last[i - 1][j];
    rep(j, 26) {
      sum = 0;
      sum += s[i][j];
      rep(k, 26) {
        if (k == j)
          continue;
        sum -= c[k] * (i + 1 - last[i][k]);
      }
      if ((ll)sum > m) {
        tmp = j;
        m = (ll)sum;
      }
    }
    ans[i] = tmp;
    last[i][tmp] = i + 1;
  }
  int res, l;
  rep(i, 900) {
    rep(j, d) {
      m = 0;
      rep(k, 26) {
        sum = 0;
        res = ans[j];
        rep2(l, j, d) {
          if (ans[l] == k) {
            sum += c[k] * (j + 1 - last[j][k]) * (l - j);
            break;
          }
        }
        rep2(l, j, d) {
          if (ans[l] == res) {
            sum -= c[res] * (j + 1 - last[j][res]) * (l - j);
          }
        }
        if (m < sum) {
          m = sum;
          tmp = k;
        }
      }
      if (m == 0)
        continue;
      ans[j] = tmp;
      l = last[j][tmp];
      rep2(k, j, d) {
        if (last[k][tmp] != l)
          break;
        last[k][tmp] = j + 1;
      }
    }
  }
  rep(i, d) cout << ans[i] + 1 << endl;
}