Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> mp;
ll inf = 1e9;
#define N 26
#define iteration 100000

ll d, ansv, nowv;
vector<ll> c(N), ans, now;
vector<vector<ll>> s;
vector<ll> last(N, 0);
ll cost(vector<ll> &tmp) {
  ll sum = 0;
  for (ll i = 0; i < N; i++)
    last[i] = 0;
  for (ll i = 0; i < d; i++) {
    sum += s[i][tmp[i]];
    last[tmp[i]] = i + 1;
    for (ll j = 0; j < N; j++)
      sum -= (i + 1 - last[j]) * c[j];
  }
  return sum;
}

/*
ll cost(){
 return cost(now);
}
*/

int main() {
  cin >> d;
  // ll c[N];
  for (ll i = 0; i < N; i++)
    cin >> c[i];
  // ll s[d][N];
  s = vector<vector<ll>>(d, vector<ll>(N));
  for (ll i = 0; i < d; i++)
    for (ll j = 0; j < N; j++)
      cin >> s[i][j];

  srand(time(NULL));
  ansv = 0, nowv = 0;
  ans = now = vector<ll>(d, 0);

  for (ll i = 0; i < N; i++) {
    vector<ll> tmp(d);
    for (ll j = 0; j < N; j++)
      tmp[j] = rand() % N;

    ll tv = cost(tmp);
    if (tv > nowv) {
      ansv = nowv = tv;
      ans = now = tmp;
    }
  }

  ll Iteration = iteration;
  while (Iteration) {
    Iteration--;
    if (rand() % 100 != 0) {
      // 1-opt
      ll day = rand() % d;
      // vector<ll> tmp = now;
      ll first = now[day];
      ll fv = nowv;
      ll T = 10;
      for (ll t = 0; t < T; t++) {
        ll i = rand() % N;
        now[day] = i;
        nowv = cost(now);
        if (nowv < fv) {
          if (nowv < ansv) {
            ansv = nowv;
            ans = now;
          }
          first = i;
          fv = nowv;
        }
      }
      nowv = fv;
      now[day] = first;

    } else {

      ll t = rand() % 10;
      for (ll i = 0; i < t; i++) {
        now[rand() % d] = rand() % N;
      }
      nowv = cost(now);
      if (nowv < ansv) {
        ansv = nowv;
        ans = now;
      }
    }
  }

  for (ll i = 0; i < d; i++)
    cout << ans[i] + 1 << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> mp;
ll inf = 1e9;
#define N 26
#define iteration 5000

ll d, ansv, nowv;
vector<ll> c(N), ans, now;
vector<vector<ll>> s;
vector<ll> last(N, 0);
ll cost(vector<ll> &tmp) {
  ll sum = 0;
  for (ll i = 0; i < N; i++)
    last[i] = 0;
  for (ll i = 0; i < d; i++) {
    sum += s[i][tmp[i]];
    last[tmp[i]] = i + 1;
    for (ll j = 0; j < N; j++)
      sum -= (i + 1 - last[j]) * c[j];
  }
  return sum;
}

/*
ll cost(){
 return cost(now);
}
*/

int main() {
  cin >> d;
  // ll c[N];
  for (ll i = 0; i < N; i++)
    cin >> c[i];
  // ll s[d][N];
  s = vector<vector<ll>>(d, vector<ll>(N));
  for (ll i = 0; i < d; i++)
    for (ll j = 0; j < N; j++)
      cin >> s[i][j];

  srand(time(NULL));
  ansv = 0, nowv = 0;
  ans = now = vector<ll>(d, 0);

  for (ll i = 0; i < N; i++) {
    vector<ll> tmp(d);
    for (ll j = 0; j < N; j++)
      tmp[j] = rand() % N;

    ll tv = cost(tmp);
    if (tv > nowv) {
      ansv = nowv = tv;
      ans = now = tmp;
    }
  }

  ll Iteration = iteration;
  while (Iteration) {
    Iteration--;
    if (rand() % 100 != 0) {
      // 1-opt
      ll day = rand() % d;
      // vector<ll> tmp = now;
      ll first = now[day];
      ll fv = nowv;
      ll T = 10;
      for (ll t = 0; t < T; t++) {
        ll i = rand() % N;
        now[day] = i;
        nowv = cost(now);
        if (nowv < fv) {
          if (nowv < ansv) {
            ansv = nowv;
            ans = now;
          }
          first = i;
          fv = nowv;
        }
      }
      nowv = fv;
      now[day] = first;

    } else {

      ll t = rand() % 10;
      for (ll i = 0; i < t; i++) {
        now[rand() % d] = rand() % N;
      }
      nowv = cost(now);
      if (nowv < ansv) {
        ansv = nowv;
        ans = now;
      }
    }
  }

  for (ll i = 0; i < d; i++)
    cout << ans[i] + 1 << endl;

  return 0;
}